mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Merge branch '0.18' into projects
This commit is contained in:
@@ -609,11 +609,70 @@ describe('trigger node', function() {
|
||||
n1.emit("input", {payload:null}); // trigger
|
||||
n1.emit("input", {payload:null}); // blocked
|
||||
n1.emit("input", {payload:null}); // blocked
|
||||
n1.emit("input", {payload:"foo"}); // don't clear the blockage
|
||||
n1.emit("input", {payload:"boo"}); // clear the blockage
|
||||
n1.emit("input", {payload:null}); // trigger
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to set infinite timeout, and clear timeout by boolean true', function(done) {
|
||||
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", reset:"true", duration:"0", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(triggerNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var c = 0;
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
c += 1;
|
||||
msg.should.have.a.property("payload", "1");
|
||||
}
|
||||
catch(err) { done(err); }
|
||||
});
|
||||
setTimeout( function() {
|
||||
if (c === 2) { done(); }
|
||||
else {
|
||||
done(new Error("Too many messages received"));
|
||||
}
|
||||
},20);
|
||||
n1.emit("input", {payload:null}); // trigger
|
||||
n1.emit("input", {payload:null}); // blocked
|
||||
n1.emit("input", {payload:null}); // blocked
|
||||
n1.emit("input", {payload:false}); // don't clear the blockage
|
||||
n1.emit("input", {payload:true}); // clear the blockage
|
||||
n1.emit("input", {payload:null}); // trigger
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to set infinite timeout, and clear timeout by boolean false', function(done) {
|
||||
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", reset:"false", duration:"0", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(triggerNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var c = 0;
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
c += 1;
|
||||
msg.should.have.a.property("payload", "1");
|
||||
}
|
||||
catch(err) { done(err); }
|
||||
});
|
||||
setTimeout( function() {
|
||||
if (c === 2) { done(); }
|
||||
else {
|
||||
done(new Error("Too many messages received"));
|
||||
}
|
||||
},20);
|
||||
n1.emit("input", {payload:null}); // trigger
|
||||
n1.emit("input", {payload:null}); // blocked
|
||||
n1.emit("input", {payload:null}); // blocked
|
||||
n1.emit("input", {payload:"foo"}); // don't clear the blockage
|
||||
n1.emit("input", {payload:false}); // clear the blockage
|
||||
n1.emit("input", {payload:null}); // trigger
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to set a repeat, and clear loop by reset', function(done) {
|
||||
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", reset:"boo", op1:"", op1type:"pay", duration:-25, wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
|
@@ -18,6 +18,7 @@ var should = require("should");
|
||||
|
||||
var switchNode = require("../../../../nodes/core/logic/10-switch.js");
|
||||
var helper = require("../../helper.js");
|
||||
var RED = require("../../../../red/red.js");
|
||||
|
||||
describe('switch Node', function() {
|
||||
|
||||
@@ -28,6 +29,7 @@ describe('switch Node', function() {
|
||||
afterEach(function(done) {
|
||||
helper.unload();
|
||||
helper.stopServer(done);
|
||||
RED.settings.switchMaxKeptMsgsCount = 0;
|
||||
});
|
||||
|
||||
it('should be loaded with some defaults', function(done) {
|
||||
@@ -119,6 +121,44 @@ describe('switch Node', function() {
|
||||
});
|
||||
}
|
||||
|
||||
function customFlowSequenceSwitchTest(flow, seq_in, seq_out, done) {
|
||||
helper.load(switchNode, flow, function() {
|
||||
var switchNode1 = helper.getNode("switchNode1");
|
||||
var helperNode1 = helper.getNode("helperNode1");
|
||||
var sid = undefined;
|
||||
var count = 0;
|
||||
helperNode1.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("payload", seq_out[count]);
|
||||
msg.should.have.property("parts");
|
||||
var parts = msg.parts;
|
||||
parts.should.have.property("id");
|
||||
var id = parts.id;
|
||||
if (sid === undefined) {
|
||||
sid = id;
|
||||
}
|
||||
else {
|
||||
id.should.equal(sid);
|
||||
}
|
||||
parts.should.have.property("index", count);
|
||||
parts.should.have.property("count", seq_out.length);
|
||||
count++;
|
||||
if (count === seq_out.length) {
|
||||
done();
|
||||
}
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
var len = seq_in.length;
|
||||
for (var i = 0; i < len; i++) {
|
||||
var parts = {index:i, count:len, id:222};
|
||||
var msg = {payload:seq_in[i], parts:parts};
|
||||
switchNode1.receive(msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
it('should check if payload equals given value', function(done) {
|
||||
genericSwitchTest("eq", "Hello", true, true, "Hello", done);
|
||||
});
|
||||
@@ -498,4 +538,176 @@ describe('switch Node', function() {
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
customFlowSwitchTest(flow, true, -5, done);
|
||||
});
|
||||
|
||||
it('should take head of message sequence', function(done) {
|
||||
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"head","v":3}],checkall:true,outputs:1,wires:[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
customFlowSequenceSwitchTest(flow, [0, 1, 2, 3, 4], [0, 1, 2], done);
|
||||
});
|
||||
|
||||
it('should take tail of message sequence', function(done) {
|
||||
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"tail","v":3}],checkall:true,outputs:1,wires:[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
customFlowSequenceSwitchTest(flow, [0, 1, 2, 3, 4], [2, 3, 4], done);
|
||||
});
|
||||
|
||||
it('should take slice of message sequence', function(done) {
|
||||
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"index","v":1,"v2":3}],checkall:true,outputs:1,wires:[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
customFlowSequenceSwitchTest(flow, [0, 1, 2, 3, 4], [1,2, 3], done);
|
||||
});
|
||||
|
||||
it('should check JSONata expression is true', function(done) {
|
||||
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",
|
||||
rules:[{"t":"jsonata_exp","v":"payload%2 = 1","vt":"jsonata"}],
|
||||
checkall:true,outputs:1,wires:[["helperNode1"]]},
|
||||
{id:"helperNode1", type:"helper", wires:[]}];
|
||||
customFlowSwitchTest(flow, true, 9, done);
|
||||
});
|
||||
|
||||
it('should repair message sequence', function(done) {
|
||||
var flow = [{id:"n1",type:"switch",name:"switchNode",property:"payload",
|
||||
rules:[{"t":"gt","v":0},{"t":"lt","v":0},{"t":"else"}],
|
||||
checkall:true,repair:true,
|
||||
outputs:3,wires:[["n2"],["n3"],["n4"]]},
|
||||
{id:"n2", type:"helper", wires:[]},
|
||||
{id:"n3", type:"helper", wires:[]},
|
||||
{id:"n4", type:"helper", wires:[]}
|
||||
];
|
||||
helper.load(switchNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
var data = { "0":1, "1":-2, "2":2, "3":0, "4":-1 };
|
||||
var count = 0;
|
||||
function check_msg(msg, vf) {
|
||||
try {
|
||||
msg.should.have.property("payload");
|
||||
var payload = msg.payload;
|
||||
msg.should.have.property("parts");
|
||||
vf(payload).should.be.ok;
|
||||
var parts = msg.parts;
|
||||
parts.should.have.property("id", 222);
|
||||
parts.should.have.property("count", 5);
|
||||
parts.should.have.property("index");
|
||||
var index = parts.index;
|
||||
payload.should.equal(data[index]);
|
||||
count++;
|
||||
if (count == 5) {
|
||||
done();
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
n2.on("input", function(msg) {
|
||||
check_msg(msg, function(x) { return(x < 0); });
|
||||
});
|
||||
n3.on("input", function(msg) {
|
||||
check_msg(msg, function(x) { return(x === 0); });
|
||||
});
|
||||
n4.on("input", function(msg) {
|
||||
check_msg(msg, function(x) { return(x > 0); });
|
||||
});
|
||||
for(var i in data) {
|
||||
n1.receive({payload: data[i], parts:{index:i,count:5,id:222}});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should create message sequence for each port', function(done) {
|
||||
var flow = [{id:"n1",type:"switch",name:"switchNode",property:"payload",
|
||||
rules:[{"t":"gt","v":0},{"t":"lt","v":0},{"t":"else"}],
|
||||
checkall:true,repair:false,
|
||||
outputs:3,wires:[["n2"],["n3"],["n4"]]},
|
||||
{id:"n2", type:"helper", wires:[]}, // >0
|
||||
{id:"n3", type:"helper", wires:[]}, // <0
|
||||
{id:"n4", type:"helper", wires:[]} // ==0
|
||||
];
|
||||
helper.load(switchNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
var data = [ 1, -2, 2, 0, -1 ];
|
||||
var vals = [[1, 2], [-2, -1], [0]];
|
||||
var ids = [undefined, undefined, undefined];
|
||||
var counts = [0, 0, 0];
|
||||
var count = 0;
|
||||
function check_msg(msg, ix, vf) {
|
||||
try {
|
||||
msg.should.have.property("payload");
|
||||
var payload = msg.payload;
|
||||
msg.should.have.property("parts");
|
||||
vf(payload).should.be.ok;
|
||||
var parts = msg.parts;
|
||||
var evals = vals[ix];
|
||||
parts.should.have.property("count", evals.length);
|
||||
parts.should.have.property("id");
|
||||
var id = parts.id;
|
||||
if (ids[ix] === undefined) {
|
||||
ids[ix] = id;
|
||||
}
|
||||
else {
|
||||
ids[ix].should.equal(id);
|
||||
}
|
||||
parts.should.have.property("index");
|
||||
var index = parts.index;
|
||||
var eindex = counts[ix];
|
||||
var eval = evals[eindex];
|
||||
payload.should.equal(eval);
|
||||
counts[ix]++;
|
||||
count++;
|
||||
if (count == 5) {
|
||||
done();
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
n2.on("input", function(msg) {
|
||||
check_msg(msg, 0, function(x) { return(x > 0); });
|
||||
});
|
||||
n3.on("input", function(msg) {
|
||||
check_msg(msg, 1, function(x) { return(x < 0); });
|
||||
});
|
||||
n4.on("input", function(msg) {
|
||||
check_msg(msg, 2, function(x) { return(x === 0); });
|
||||
});
|
||||
for(var i in data) {
|
||||
n1.receive({payload: data[i], parts:{index:i,count:5,id:222}});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle too many pending messages', function(done) {
|
||||
var flow = [{id:"n1",type:"switch",name:"switchNode",property:"payload",
|
||||
rules:[{"t":"tail","v":2}],
|
||||
checkall:true,repair:false,
|
||||
outputs:3,wires:[["n2"]]},
|
||||
{id:"n2", type:"helper", wires:[]}
|
||||
];
|
||||
helper.load(switchNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
RED.settings.switchMaxKeptMsgsCount = 2;
|
||||
setTimeout(function() {
|
||||
var logEvents = helper.log().args.filter(function (evt) {
|
||||
return evt[0].type == "switch";
|
||||
});
|
||||
var evt = logEvents[0][0];
|
||||
evt.should.have.property('id', "n1");
|
||||
evt.should.have.property('type', "switch");
|
||||
evt.should.have.property('msg', "switch.errors.too-many");
|
||||
done();
|
||||
}, 150);
|
||||
n1.receive({payload:3, parts:{index:2, count:4, id:222}});
|
||||
n1.receive({payload:2, parts:{index:1, count:4, id:222}});
|
||||
n1.receive({payload:4, parts:{index:3, count:4, id:222}});
|
||||
n1.receive({payload:1, parts:{index:0, count:4, id:222}});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -18,6 +18,7 @@ var should = require("should");
|
||||
var splitNode = require("../../../../nodes/core/logic/17-split.js");
|
||||
var joinNode = require("../../../../nodes/core/logic/17-split.js");
|
||||
var helper = require("../../helper.js");
|
||||
var RED = require("../../../../red/red.js");
|
||||
|
||||
describe('SPLIT node', function() {
|
||||
|
||||
@@ -269,6 +270,7 @@ describe('JOIN node', function() {
|
||||
|
||||
afterEach(function() {
|
||||
helper.unload();
|
||||
RED.settings.joinMaxKeptMsgsCount = 0;
|
||||
});
|
||||
|
||||
it('should be loaded', function(done) {
|
||||
@@ -727,6 +729,394 @@ describe('JOIN node', function() {
|
||||
});
|
||||
s1.receive({payload:[[1,2,3],"a\nb\nc",[7,8,9]]});
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
it('should merge messages with topics (single)', function(done) {
|
||||
var flow = [{id:"n1", type:"join", mode:"merge",
|
||||
topics:[{topic:"TA"}, {topic:"TB"}],
|
||||
wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var count = 0;
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("TA");
|
||||
msg.should.have.property("TB");
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.an.Array();
|
||||
msg.payload.length.should.equal(2);
|
||||
count++;
|
||||
if (count === 1) {
|
||||
msg.TA.should.equal("a");
|
||||
msg.TB.should.equal("b");
|
||||
msg.payload[0].should.equal("a");
|
||||
msg.payload[1].should.equal("b");
|
||||
}
|
||||
if (count === 2) {
|
||||
msg.TA.should.equal("d");
|
||||
msg.TB.should.equal("c");
|
||||
msg.payload[0].should.equal("d");
|
||||
msg.payload[1].should.equal("c");
|
||||
done();
|
||||
}
|
||||
}
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
n1.receive({payload:"a", topic:"TA"});
|
||||
n1.receive({payload:"b", topic:"TB"});
|
||||
n1.receive({payload:"c", topic:"TB"});
|
||||
n1.receive({payload:"d", topic:"TA"});
|
||||
});
|
||||
});
|
||||
|
||||
it('should merge messages with topics (multiple)', function(done) {
|
||||
var flow = [{id:"n1", type:"join", mode:"merge",
|
||||
topics:[{topic:"TA"}, {topic:"TB"}, {topic:"TA"}],
|
||||
wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var count = 0;
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("TA");
|
||||
msg.TA.should.be.an.Array();
|
||||
msg.TA.length.should.equal(2);
|
||||
msg.should.have.property("TB");
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.an.Array();
|
||||
msg.payload.length.should.equal(3);
|
||||
count++;
|
||||
if (count === 1) {
|
||||
msg.TA[0].should.equal("a");
|
||||
msg.TA[1].should.equal("d");
|
||||
msg.TB.should.equal("b");
|
||||
msg.payload[0].should.equal("a");
|
||||
msg.payload[1].should.equal("b");
|
||||
msg.payload[2].should.equal("d");
|
||||
}
|
||||
if (count === 2) {
|
||||
msg.TA[0].should.equal("e");
|
||||
msg.TA[1].should.equal("f");
|
||||
msg.TB.should.equal("c");
|
||||
msg.payload[0].should.equal("e");
|
||||
msg.payload[1].should.equal("c");
|
||||
msg.payload[2].should.equal("f");
|
||||
done();
|
||||
}
|
||||
}
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
n1.receive({payload:"a", topic:"TA"});
|
||||
n1.receive({payload:"b", topic:"TB"});
|
||||
n1.receive({payload:"c", topic:"TB"});
|
||||
n1.receive({payload:"d", topic:"TA"});
|
||||
n1.receive({payload:"e", topic:"TA"});
|
||||
n1.receive({payload:"f", topic:"TA"});
|
||||
});
|
||||
});
|
||||
|
||||
it('should merge messages with topics (single, send on new topic)', function(done) {
|
||||
var flow = [{id:"n1", type:"join", mode:"merge",
|
||||
topics:[{topic:"TA"}, {topic:"TB"}],
|
||||
mergeOnChange:true,
|
||||
wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var count = 0;
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("TA");
|
||||
msg.should.have.property("TB");
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.an.Array();
|
||||
msg.payload.length.should.equal(2);
|
||||
count++;
|
||||
if (count === 1) {
|
||||
msg.TA.should.equal("a");
|
||||
msg.TB.should.equal("b");
|
||||
msg.payload[0].should.equal("a");
|
||||
msg.payload[1].should.equal("b");
|
||||
}
|
||||
if (count === 2) {
|
||||
msg.TA.should.equal("a");
|
||||
msg.TB.should.equal("c");
|
||||
msg.payload[0].should.equal("a");
|
||||
msg.payload[1].should.equal("c");
|
||||
}
|
||||
if (count === 3) {
|
||||
msg.TA.should.equal("d");
|
||||
msg.TB.should.equal("c");
|
||||
msg.payload[0].should.equal("d");
|
||||
msg.payload[1].should.equal("c");
|
||||
done();
|
||||
}
|
||||
}
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
n1.receive({payload:"a", topic:"TA"});
|
||||
n1.receive({payload:"b", topic:"TB"});
|
||||
n1.receive({payload:"c", topic:"TB"});
|
||||
n1.receive({payload:"d", topic:"TA"});
|
||||
});
|
||||
});
|
||||
|
||||
it('should merge messages with topics (multiple, send on new topic)', function(done) {
|
||||
var flow = [{id:"n1", type:"join", mode:"merge",
|
||||
topics:[{topic:"TA"}, {topic:"TB"}, {topic:"TA"}],
|
||||
mergeOnChange:true,
|
||||
wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var count = 0;
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("TA");
|
||||
msg.TA.should.be.an.Array();
|
||||
msg.TA.length.should.equal(2);
|
||||
msg.should.have.property("TB");
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.an.Array();
|
||||
msg.payload.length.should.equal(3);
|
||||
count++;
|
||||
if (count === 1) {
|
||||
msg.TA[0].should.equal("a");
|
||||
msg.TA[1].should.equal("c");
|
||||
msg.TB.should.equal("b");
|
||||
msg.payload[0].should.equal("a");
|
||||
msg.payload[1].should.equal("b");
|
||||
msg.payload[2].should.equal("c");
|
||||
}
|
||||
if (count === 2) {
|
||||
msg.TA[0].should.equal("c");
|
||||
msg.TA[1].should.equal("d");
|
||||
msg.TB.should.equal("b");
|
||||
msg.payload[0].should.equal("c");
|
||||
msg.payload[1].should.equal("b");
|
||||
msg.payload[2].should.equal("d");
|
||||
}
|
||||
if (count === 3) {
|
||||
msg.TA[0].should.equal("c");
|
||||
msg.TA[1].should.equal("d");
|
||||
msg.TB.should.equal("e");
|
||||
msg.payload[0].should.equal("c");
|
||||
msg.payload[1].should.equal("e");
|
||||
msg.payload[2].should.equal("d");
|
||||
done();
|
||||
}
|
||||
}
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
n1.receive({payload:"a", topic:"TA"});
|
||||
n1.receive({payload:"b", topic:"TB"});
|
||||
n1.receive({payload:"c", topic:"TA"});
|
||||
n1.receive({payload:"d", topic:"TA"});
|
||||
n1.receive({payload:"e", topic:"TB"});
|
||||
});
|
||||
});
|
||||
|
||||
it('should redece messages', function(done) {
|
||||
var flow = [{id:"n1", type:"join", mode:"reduce",
|
||||
reduceRight:false,
|
||||
reduceExp:"$A+payload",
|
||||
reduceInit:"0",
|
||||
reduceInitType:"num",
|
||||
reduceFixup:undefined,
|
||||
wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var count = 0;
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.equal(10);
|
||||
done();
|
||||
}
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
n1.receive({payload:3, parts:{index:2, count:4, id:222}});
|
||||
n1.receive({payload:2, parts:{index:1, count:4, id:222}});
|
||||
n1.receive({payload:4, parts:{index:3, count:4, id:222}});
|
||||
n1.receive({payload:1, parts:{index:0, count:4, id:222}});
|
||||
});
|
||||
});
|
||||
|
||||
it('should redece messages using $I', function(done) {
|
||||
var flow = [{id:"n1", type:"join", mode:"reduce",
|
||||
reduceRight:false,
|
||||
reduceExp:"$A+$I",
|
||||
reduceInit:"0",
|
||||
reduceInitType:"num",
|
||||
reduceFixup:undefined,
|
||||
wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var count = 0;
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.equal(6);
|
||||
done();
|
||||
}
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
n1.receive({payload:3, parts:{index:2, count:4, id:222}});
|
||||
n1.receive({payload:2, parts:{index:1, count:4, id:222}});
|
||||
n1.receive({payload:4, parts:{index:3, count:4, id:222}});
|
||||
n1.receive({payload:1, parts:{index:0, count:4, id:222}});
|
||||
});
|
||||
});
|
||||
|
||||
it('should redece messages with fixup', function(done) {
|
||||
var flow = [{id:"n1", type:"join", mode:"reduce",
|
||||
reduceRight:false,
|
||||
reduceExp:"$A+payload",
|
||||
reduceInit:"0",
|
||||
reduceInitType:"num",
|
||||
reduceFixup:"$A/$N",
|
||||
wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var count = 0;
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.equal(2);
|
||||
done();
|
||||
}
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
n1.receive({payload:3, parts:{index:2, count:5, id:222}});
|
||||
n1.receive({payload:2, parts:{index:1, count:5, id:222}});
|
||||
n1.receive({payload:4, parts:{index:3, count:5, id:222}});
|
||||
n1.receive({payload:1, parts:{index:0, count:5, id:222}});
|
||||
n1.receive({payload:0, parts:{index:4, count:5, id:222}});
|
||||
});
|
||||
});
|
||||
|
||||
it('should redece messages (left)', function(done) {
|
||||
var flow = [{id:"n1", type:"join", mode:"reduce",
|
||||
reduceRight:false,
|
||||
reduceExp:"'(' & $A & '+' & payload & ')'",
|
||||
reduceInit:"0",
|
||||
reduceInitType:"str",
|
||||
reduceFixup:undefined,
|
||||
wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var count = 0;
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.an.String();
|
||||
msg.payload.should.equal("((((0+1)+2)+3)+4)");
|
||||
done();
|
||||
}
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
n1.receive({payload:'3', parts:{index:2, count:4, id:222}});
|
||||
n1.receive({payload:'2', parts:{index:1, count:4, id:222}});
|
||||
n1.receive({payload:'4', parts:{index:3, count:4, id:222}});
|
||||
n1.receive({payload:'1', parts:{index:0, count:4, id:222}});
|
||||
});
|
||||
});
|
||||
|
||||
it('should redece messages (right)', function(done) {
|
||||
var flow = [{id:"n1", type:"join", mode:"reduce",
|
||||
reduceRight:true,
|
||||
reduceExp:"'(' & $A & '+' & payload & ')'",
|
||||
reduceInit:"0",
|
||||
reduceInitType:"str",
|
||||
reduceFixup:undefined,
|
||||
wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var count = 0;
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.an.String();
|
||||
msg.payload.should.equal("((((0+4)+3)+2)+1)");
|
||||
done();
|
||||
}
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
n1.receive({payload:'3', parts:{index:2, count:4, id:222}});
|
||||
n1.receive({payload:'2', parts:{index:1, count:4, id:222}});
|
||||
n1.receive({payload:'4', parts:{index:3, count:4, id:222}});
|
||||
n1.receive({payload:'1', parts:{index:0, count:4, id:222}});
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle too many pending messages for merge mode', function(done) {
|
||||
var flow = [{id:"n1", type:"join", mode:"merge",
|
||||
topics:[{topic:"TA"}, {topic:"TA"}, {topic:"TB"}],
|
||||
wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
RED.settings.joinMaxKeptMsgsCount = 2;
|
||||
setTimeout(function() {
|
||||
var logEvents = helper.log().args.filter(function (evt) {
|
||||
return evt[0].type == "join";
|
||||
});
|
||||
var evt = logEvents[0][0];
|
||||
evt.should.have.property('id', "n1");
|
||||
evt.should.have.property('type', "join");
|
||||
evt.should.have.property('msg', "join.too-many");
|
||||
done();
|
||||
}, 150);
|
||||
n1.receive({payload:"a", topic:"TA"});
|
||||
n1.receive({payload:"b", topic:"TB"});
|
||||
n1.receive({payload:"c", topic:"TB"});
|
||||
n1.receive({payload:"d", topic:"TA"});
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle too many pending messages for reduce mode', function(done) {
|
||||
var flow = [{id:"n1", type:"join", mode:"reduce",
|
||||
reduceRight:false,
|
||||
reduceExp:"$A+payload",
|
||||
reduceInit:"0",
|
||||
reduceInitType:"num",
|
||||
reduceFixup:undefined,
|
||||
wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(joinNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
RED.settings.joinMaxKeptMsgsCount = 2;
|
||||
setTimeout(function() {
|
||||
var logEvents = helper.log().args.filter(function (evt) {
|
||||
return evt[0].type == "join";
|
||||
});
|
||||
var evt = logEvents[0][0];
|
||||
evt.should.have.property('id', "n1");
|
||||
evt.should.have.property('type', "join");
|
||||
evt.should.have.property('msg', "join.too-many");
|
||||
done();
|
||||
}, 150);
|
||||
n1.receive({payload:3, parts:{index:2, count:4, id:222}});
|
||||
n1.receive({payload:2, parts:{index:1, count:4, id:222}});
|
||||
n1.receive({payload:4, parts:{index:3, count:4, id:222}});
|
||||
n1.receive({payload:1, parts:{index:0, count:4, id:222}});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -27,10 +27,11 @@ describe('SORT node', function() {
|
||||
|
||||
afterEach(function() {
|
||||
helper.unload();
|
||||
RED.settings.maxKeptMsgsCount = 0;
|
||||
});
|
||||
|
||||
it('should be loaded', function(done) {
|
||||
var flow = [{id:"n1", type:"sort", order:"ascending", as_num:false, keyType:"payload", name: "SortNode", wires:[["n2"]]},
|
||||
var flow = [{id:"n1", type:"sort", order:"ascending", as_num:false, name: "SortNode", wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(sortNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
@@ -39,16 +40,58 @@ describe('SORT node', function() {
|
||||
});
|
||||
});
|
||||
|
||||
function check_sort0(flow, data_in, data_out, done) {
|
||||
function check_sort0(flow, target, key, key_type, data_in, data_out, done) {
|
||||
var sort = flow[0];
|
||||
sort.target = target;
|
||||
sort.targetType = "msg";
|
||||
sort.msgKey = key;
|
||||
sort.msgKeyType = key_type;
|
||||
helper.load(sortNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.property(target);
|
||||
var data = msg[target];
|
||||
data.length.should.equal(data_out.length);
|
||||
for(var i = 0; i < data_out.length; i++) {
|
||||
data[i].should.equal(data_out[i]);
|
||||
}
|
||||
done();
|
||||
});
|
||||
var msg = {};
|
||||
msg[target] = data_in;
|
||||
n1.receive(msg);
|
||||
});
|
||||
}
|
||||
|
||||
function check_sort0A(flow, data_in, data_out, done) {
|
||||
check_sort0(flow, "payload", "", "elem", data_in, data_out, done);
|
||||
}
|
||||
|
||||
function check_sort0B(flow, data_in, data_out, done) {
|
||||
check_sort0(flow, "data", "", "elem", data_in, data_out, done);
|
||||
}
|
||||
|
||||
function check_sort0C(flow, exp, data_in, data_out, done) {
|
||||
check_sort0(flow, "data", exp, "jsonata", data_in, data_out, done);
|
||||
}
|
||||
|
||||
function check_sort1(flow, key, key_type, data_in, data_out, done) {
|
||||
var sort = flow[0];
|
||||
var prop = (key_type === "msg") ? key : "payload";
|
||||
sort.targetType = "seq";
|
||||
sort.seqKey = key;
|
||||
sort.seqKeyType = key_type;
|
||||
helper.load(sortNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var count = 0;
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.property("payload");
|
||||
msg.should.have.property(prop);
|
||||
msg.should.have.property("parts");
|
||||
msg.parts.should.have.property("count", data_out.length);
|
||||
var index = data_out.indexOf(msg.payload);
|
||||
var data = msg[prop];
|
||||
var index = data_out.indexOf(data);
|
||||
msg.parts.should.have.property("index", index);
|
||||
count++;
|
||||
if (count === data_out.length) {
|
||||
@@ -58,111 +101,132 @@ describe('SORT node', function() {
|
||||
var len = data_in.length;
|
||||
for(var i = 0; i < len; i++) {
|
||||
var parts = { id: "X", index: i, count: len };
|
||||
n1.receive({payload:data_in[i], parts: parts});
|
||||
var msg = {parts: parts};
|
||||
msg[prop] = data_in[i];
|
||||
n1.receive(msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function check_sort1(flow, data_in, data_out, done) {
|
||||
helper.load(sortNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.length.should.equal(data_out.length);
|
||||
for(var i = 0; i < data_out.length; i++) {
|
||||
msg.payload[i].should.equal(data_out[i]);
|
||||
}
|
||||
done();
|
||||
});
|
||||
n1.receive({payload:data_in});
|
||||
});
|
||||
function check_sort1A(flow, data_in, data_out, done) {
|
||||
check_sort1(flow, "payload", "msg", data_in, data_out, done);
|
||||
}
|
||||
|
||||
function check_sort1B(flow, data_in, data_out, done) {
|
||||
check_sort1(flow, "data", "msg", data_in, data_out, done);
|
||||
}
|
||||
|
||||
function check_sort1C(flow, exp, data_in, data_out, done) {
|
||||
check_sort1(flow, exp, "jsonata", data_in, data_out, done);
|
||||
}
|
||||
|
||||
|
||||
(function() {
|
||||
var flow = [{id:"n1", type:"sort", order:"ascending", as_num:false, keyType:"payload", wires:[["n2"]]},
|
||||
var flow = [{id:"n1", type:"sort", order:"ascending", as_num:false, wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var data_in = [ "200", "4", "30", "1000" ];
|
||||
var data_out = [ "1000", "200", "30", "4" ];
|
||||
it('should sort message group (payload, not number, ascending)', function(done) {
|
||||
check_sort0(flow, data_in, data_out, done);
|
||||
it('should sort payload (elem, not number, ascending)', function(done) {
|
||||
check_sort0A(flow, data_in, data_out, done);
|
||||
});
|
||||
it('should sort payload (payload, not number, ascending)', function(done) {
|
||||
check_sort1(flow, data_in, data_out, done);
|
||||
it('should sort msg prop (elem, not number, ascending)', function(done) {
|
||||
check_sort0B(flow, data_in, data_out, done);
|
||||
});
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var flow = [{id:"n1", type:"sort", order:"descending", as_num:false, keyType:"payload", wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var data_in = [ "200", "4", "30", "1000" ];
|
||||
var data_out = [ "4", "30", "200", "1000" ];
|
||||
it('should sort message group (payload, not number, descending)', function(done) {
|
||||
check_sort0(flow, data_in, data_out, done);
|
||||
it('should sort message group/payload (not number, ascending)', function(done) {
|
||||
check_sort1A(flow, data_in, data_out, done);
|
||||
});
|
||||
it('should sort payload (payload, not number, descending)', function(done) {
|
||||
check_sort1(flow, data_in, data_out, done);
|
||||
});
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var flow = [{id:"n1", type:"sort", order:"ascending", as_num:true, keyType:"payload", wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var data_in = [ "200", "4", "30", "1000" ];
|
||||
var data_out = [ "4", "30", "200", "1000" ];
|
||||
it('should sort message group (payload, number, ascending)', function(done) {
|
||||
check_sort0(flow, data_in, data_out, done);
|
||||
});
|
||||
it('should sort payload (payload, number, ascending)', function(done) {
|
||||
check_sort1(flow, data_in, data_out, done);
|
||||
});
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var flow = [{id:"n1", type:"sort", order:"descending", as_num:true, keyType:"payload", wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var data_in = [ "200", "4", "30", "1000" ];
|
||||
var data_out = [ "1000", "200", "30", "4" ];
|
||||
it('should sort message group (payload, number, descending)', function(done) {
|
||||
check_sort0(flow, data_in, data_out, done);
|
||||
});
|
||||
it('should sort payload (payload, number, descending)', function(done) {
|
||||
check_sort1(flow, data_in, data_out, done);
|
||||
it('should sort message group/prop (not number, ascending)', function(done) {
|
||||
check_sort1B(flow, data_in, data_out, done);
|
||||
});
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var flow = [{id:"n1", type:"sort", order:"descending", as_num:false, wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var data_in = [ "200", "4", "30", "1000" ];
|
||||
var data_out = [ "4", "30", "200", "1000" ];
|
||||
it('should sort payload (elem, not number, descending)', function(done) {
|
||||
check_sort0A(flow, data_in, data_out, done);
|
||||
});
|
||||
it('should sort msg prop (elem, not number, descending)', function(done) {
|
||||
check_sort0B(flow, data_in, data_out, done);
|
||||
});
|
||||
it('should sort message group/payload (not number, descending)', function(done) {
|
||||
check_sort1A(flow, data_in, data_out, done);
|
||||
});
|
||||
it('should sort message group/prop (not number, descending)', function(done) {
|
||||
check_sort1B(flow, data_in, data_out, done);
|
||||
});
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var flow = [{id:"n1", type:"sort", order:"ascending", as_num:true, wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var data_in = [ "200", "4", "30", "1000" ];
|
||||
var data_out = [ "4", "30", "200", "1000" ];
|
||||
it('should sort payload (elem, number, ascending)', function(done) {
|
||||
check_sort0A(flow, data_in, data_out, done);
|
||||
});
|
||||
it('should sort msg prop (elem, number, ascending)', function(done) {
|
||||
check_sort0B(flow, data_in, data_out, done);
|
||||
});
|
||||
it('should sort message group/payload (number, ascending)', function(done) {
|
||||
check_sort1A(flow, data_in, data_out, done);
|
||||
});
|
||||
it('should sort message group/prop (number, ascending)', function(done) {
|
||||
check_sort1B(flow, data_in, data_out, done);
|
||||
});
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var flow = [{id:"n1", type:"sort", order:"descending", as_num:true, wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var data_in = [ "200", "4", "30", "1000" ];
|
||||
var data_out = [ "1000", "200", "30", "4" ];
|
||||
it('should sort payload (elem, number, descending)', function(done) {
|
||||
check_sort0A(flow, data_in, data_out, done);
|
||||
});
|
||||
it('should sort msg prop (elem, number, descending)', function(done) {
|
||||
check_sort0B(flow, data_in, data_out, done);
|
||||
});
|
||||
it('should sort message group/payload (number, descending)', function(done) {
|
||||
check_sort1A(flow, data_in, data_out, done);
|
||||
});
|
||||
it('should sort message group/prop (number, descending)', function(done) {
|
||||
check_sort1B(flow, data_in, data_out, done);
|
||||
});
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var flow = [{id:"n1", type:"sort", order:"ascending", as_num:false, wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var data_in = [ "C200", "A4", "B30", "D1000" ];
|
||||
var data_out = [ "D1000", "C200", "B30", "A4" ];
|
||||
it('should sort message group (exp, not number, ascending)', function(done) {
|
||||
var flow = [{id:"n1", type:"sort", order:"ascending", as_num:false, keyType:"exp", key:"$substring(payload, 1)", wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
check_sort0(flow, data_in, data_out, done);
|
||||
});
|
||||
it('should sort payload (exp, not number, ascending)', function(done) {
|
||||
var flow = [{id:"n1", type:"sort", order:"ascending", as_num:false, keyType:"exp", key:"$substring($, 1)", wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
check_sort1(flow, data_in, data_out, done);
|
||||
check_sort0C(flow, "$substring($,1)", data_in, data_out, done);
|
||||
});
|
||||
it('should sort message group (exp, not number, ascending)', function(done) {
|
||||
check_sort1C(flow, "$substring(payload,1)", data_in, data_out, done);
|
||||
});
|
||||
})();
|
||||
|
||||
return;
|
||||
|
||||
(function() {
|
||||
var flow = [{id:"n1", type:"sort", order:"descending", as_num:false, wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var data_in = [ "C200", "A4", "B30", "D1000" ];
|
||||
var data_out = [ "A4", "B30", "C200", "D1000" ];
|
||||
it('should sort message group (exp, not number, descending)', function(done) {
|
||||
var flow = [{id:"n1", type:"sort", order:"descending", as_num:false, keyType:"exp", key:"$substring(payload, 1)", wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
check_sort0(flow, data_in, data_out, done);
|
||||
check_sort0C(flow, "$substring($,1)", data_in, data_out, done);
|
||||
});
|
||||
it('should sort payload (exp, not number, descending)', function(done) {
|
||||
var flow = [{id:"n1", type:"sort", order:"descending", as_num:false, keyType:"exp", key:"$substring($, 1)", wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
check_sort1(flow, data_in, data_out, done);
|
||||
check_sort1C(flow, "$substring(payload,1)", data_in, data_out, done);
|
||||
});
|
||||
})();
|
||||
|
||||
it('should handle JSONata script error', function(done) {
|
||||
var flow = [{id:"n1", type:"sort", order:"ascending", as_num:false, keyType:"exp", key:"$unknown()", wires:[["n2"]]},
|
||||
var flow = [{id:"n1", type:"sort", order:"ascending", as_num:false, target:"payload", targetType:"seq", seqKey:"$unknown()", seqKeyType:"jsonata", wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(sortNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
@@ -183,12 +247,14 @@ describe('SORT node', function() {
|
||||
});
|
||||
});
|
||||
|
||||
return;
|
||||
|
||||
it('should handle too many pending messages', function(done) {
|
||||
var flow = [{id:"n1", type:"sort", order:"ascending", as_num:false, keyType:"payload", wires:[["n2"]]},
|
||||
var flow = [{id:"n1", type:"sort", order:"ascending", as_num:false, target:"payload", targetType:"seq", seqKey:"payload", seqKeyType:"msg", wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(sortNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
RED.settings.sortMaxKeptMsgsCount = 2;
|
||||
RED.settings.maxKeptMsgsCount = 2;
|
||||
setTimeout(function() {
|
||||
var logEvents = helper.log().args.filter(function (evt) {
|
||||
return evt[0].type == "sort";
|
||||
@@ -208,7 +274,7 @@ describe('SORT node', function() {
|
||||
});
|
||||
|
||||
it('should clear pending messages on close', function(done) {
|
||||
var flow = [{id:"n1", type:"sort", order:"ascending", as_num:false, keyType:"payload", wires:[["n2"]]},
|
||||
var flow = [{id:"n1", type:"sort", order:"ascending", as_num:false, target:"payload", targetType:"seq", seqKey:"payload", seqKeyType:"msg", wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(sortNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
|
338
test/nodes/core/logic/19-batch_spec.js
Normal file
338
test/nodes/core/logic/19-batch_spec.js
Normal file
@@ -0,0 +1,338 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var batchNode = require("../../../../nodes/core/logic/19-batch.js");
|
||||
var helper = require("../../helper.js");
|
||||
var RED = require("../../../../red/red.js");
|
||||
|
||||
describe('BATCH node', function() {
|
||||
this.timeout(8000);
|
||||
|
||||
before(function(done) {
|
||||
helper.startServer(done);
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
helper.unload();
|
||||
RED.settings.batchMaxKeptMsgsCount = 0;
|
||||
});
|
||||
|
||||
it('should be loaded with defaults', function(done) {
|
||||
var flow = [{id:"n1", type:"batch", name: "BatchNode", wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(batchNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
n1.should.have.property('name', 'BatchNode');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
function check_parts(msg, id, idx, count) {
|
||||
msg.should.have.property("parts");
|
||||
var parts = msg.parts;
|
||||
parts.should.have.property("id", id);
|
||||
parts.should.have.property("index", idx);
|
||||
parts.should.have.property("count", count);
|
||||
}
|
||||
|
||||
function check_data(n1, n2, results, done) {
|
||||
var id = undefined;
|
||||
var ix0 = 0; // seq no
|
||||
var ix1 = 0; // loc. in seq
|
||||
var seq = undefined;
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
if (seq === undefined) {
|
||||
seq = results[ix0];
|
||||
}
|
||||
var val = seq[ix1];
|
||||
msg.should.have.property("payload", val);
|
||||
if (id === undefined) {
|
||||
id = msg.parts.id;
|
||||
}
|
||||
check_parts(msg, id, ix1, seq.length);
|
||||
ix1++;
|
||||
if (ix1 === seq.length) {
|
||||
ix0++;
|
||||
ix1 = 0;
|
||||
seq = undefined;
|
||||
id = undefined;
|
||||
if (ix0 === results.length) {
|
||||
done();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function check_count(flow, results, done) {
|
||||
try {
|
||||
helper.load(batchNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
check_data(n1, n2, results, done);
|
||||
for(var i = 0; i < 6; i++) {
|
||||
n1.receive({payload: i});
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (e) {
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
|
||||
function delayed_send(receiver, index, count, delay) {
|
||||
if (index < count) {
|
||||
setTimeout(function() {
|
||||
receiver.receive({payload: index});
|
||||
delayed_send(receiver, index+1, count, delay);
|
||||
}, delay);
|
||||
}
|
||||
}
|
||||
|
||||
function check_interval(flow, results, delay, done) {
|
||||
helper.load(batchNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
check_data(n1, n2, results, done);
|
||||
delayed_send(n1, 0, 4, delay);
|
||||
});
|
||||
}
|
||||
|
||||
function check_concat(flow, results, inputs, done) {
|
||||
try {
|
||||
helper.load(batchNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
check_data(n1, n2, results, done);
|
||||
for(var data of inputs) {
|
||||
var msg = {
|
||||
topic: data[0],
|
||||
payload: data[1],
|
||||
parts: {
|
||||
id: data[0],
|
||||
index: data[2],
|
||||
count: data[3]
|
||||
}
|
||||
};
|
||||
n1.receive(msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (e) {
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
|
||||
describe('mode: count', function() {
|
||||
|
||||
it('should create seq. with count', function(done) {
|
||||
var flow = [{id:"n1", type:"batch", name: "BatchNode", mode: "count", count: 2, overwrap: 0, interval: 10, allowEmptySequence: false, topics: [], wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var results = [
|
||||
[0, 1],
|
||||
[2, 3],
|
||||
[4, 5]
|
||||
];
|
||||
check_count(flow, results, done);
|
||||
});
|
||||
|
||||
it('should create seq. with count and overwrap', function(done) {
|
||||
var flow = [{id:"n1", type:"batch", name: "BatchNode", mode: "count", count: 3, overwrap: 2, interval: 10, allowEmptySequence: false, topics: [], wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var results = [
|
||||
[0, 1, 2],
|
||||
[1, 2, 3],
|
||||
[2, 3, 4],
|
||||
[3, 4, 5]
|
||||
];
|
||||
check_count(flow, results, done);
|
||||
});
|
||||
|
||||
it('should handle too many pending messages', function(done) {
|
||||
var flow = [{id:"n1", type:"batch", name: "BatchNode", mode: "count", count: 5, overwrap: 0, interval: 10, allowEmptySequence: false, topics: [], wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(batchNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
RED.settings.batchMaxKeptMsgsCount = 2;
|
||||
setTimeout(function() {
|
||||
var logEvents = helper.log().args.filter(function (evt) {
|
||||
return evt[0].type == "batch";
|
||||
});
|
||||
var evt = logEvents[0][0];
|
||||
evt.should.have.property('id', "n1");
|
||||
evt.should.have.property('type', "batch");
|
||||
evt.should.have.property('msg', "batch.too-many");
|
||||
done();
|
||||
}, 150);
|
||||
for(var i = 0; i < 3; i++) {
|
||||
n1.receive({payload: i});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('mode: interval', function() {
|
||||
|
||||
it('should create seq. with interval', function(done) {
|
||||
var flow = [{id:"n1", type:"batch", name: "BatchNode", mode: "interval", count: 0, overwrap: 0, interval: 1, allowEmptySequence: false, topics: [], wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var results = [
|
||||
[0, 1],
|
||||
[2, 3]
|
||||
];
|
||||
check_interval(flow, results, 450, done);
|
||||
});
|
||||
|
||||
it('should create seq. with interval (in float)', function(done) {
|
||||
var flow = [{id:"n1", type:"batch", name: "BatchNode", mode: "interval", count: 0, overwrap: 0, interval: 0.5, allowEmptySequence: false, topics: [], wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var results = [
|
||||
[0, 1],
|
||||
[2, 3]
|
||||
];
|
||||
check_interval(flow, results, 225, done);
|
||||
});
|
||||
|
||||
it('should create seq. with interval & not send empty seq', function(done) {
|
||||
var flow = [{id:"n1", type:"batch", name: "BatchNode", mode: "interval", count: 0, overwrap: 0, interval: 1, allowEmptySequence: false, topics: [], wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var results = [
|
||||
// 1300, 2600, 3900, 5200,
|
||||
[0], [1], [2], [3]
|
||||
];
|
||||
check_interval(flow, results, 1300, done);
|
||||
});
|
||||
|
||||
it('should create seq. with interval & send empty seq', function(done) {
|
||||
var flow = [{id:"n1", type:"batch", name: "BatchNode", mode: "interval", count: 0, overwrap: 0, interval: 1, allowEmptySequence: true, topics: [], wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var results = [
|
||||
// 1300, 2600, 3900, 5200,
|
||||
[null], [0], [1], [2], [null], [3]
|
||||
];
|
||||
check_interval(flow, results, 1300, done);
|
||||
});
|
||||
|
||||
it('should handle too many pending messages', function(done) {
|
||||
var flow = [{id:"n1", type:"batch", name: "BatchNode", mode: "interval", count: 0, overwrap: 0, interval: 1, allowEmptySequence: false, topics: [], wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(batchNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
RED.settings.batchMaxKeptMsgsCount = 2;
|
||||
setTimeout(function() {
|
||||
var logEvents = helper.log().args.filter(function (evt) {
|
||||
return evt[0].type == "batch";
|
||||
});
|
||||
var evt = logEvents[0][0];
|
||||
evt.should.have.property('id', "n1");
|
||||
evt.should.have.property('type', "batch");
|
||||
evt.should.have.property('msg', "batch.too-many");
|
||||
done();
|
||||
}, 150);
|
||||
for(var i = 0; i < 3; i++) {
|
||||
n1.receive({payload: i});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('mode: concat', function() {
|
||||
|
||||
it('should concat two seq. (series)', function(done) {
|
||||
var flow = [{id:"n1", type:"batch", name: "BatchNode", mode: "concat", count: 0, overwrap: 0, interval: 1, allowEmptySequence: false, topics: [{topic: "TA"}, {topic: "TB"}], wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var results = [
|
||||
[2, 3, 0, 1]
|
||||
];
|
||||
var inputs = [
|
||||
["TB", 0, 0, 2],
|
||||
["TB", 1, 1, 2],
|
||||
["TA", 2, 0, 2],
|
||||
["TA", 3, 1, 2]
|
||||
];
|
||||
check_concat(flow, results, inputs, done);
|
||||
});
|
||||
|
||||
it('should concat two seq. (mixed)', function(done) {
|
||||
var flow = [{id:"n1", type:"batch", name: "BatchNode", mode: "concat", count: 0, overwrap: 0, interval: 1, allowEmptySequence: false, topics: [{topic: "TA"}, {topic: "TB"}], wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var results = [
|
||||
[2, 3, 0, 1]
|
||||
];
|
||||
var inputs = [
|
||||
["TA", 2, 0, 2],
|
||||
["TB", 0, 0, 2],
|
||||
["TA", 3, 1, 2],
|
||||
["TB", 1, 1, 2]
|
||||
];
|
||||
check_concat(flow, results, inputs, done);
|
||||
});
|
||||
|
||||
it('should concat three seq.', function(done) {
|
||||
var flow = [{id:"n1", type:"batch", name: "BatchNode", mode: "concat", count: 0, overwrap: 0, interval: 1, allowEmptySequence: false, topics: [{topic: "TA"}, {topic: "TB"}, {topic: "TC"}], wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
var results = [
|
||||
[2, 3, 0, 1, 4]
|
||||
];
|
||||
var inputs = [
|
||||
["TC", 4, 0, 1],
|
||||
["TB", 0, 0, 2],
|
||||
["TB", 1, 1, 2],
|
||||
["TA", 2, 0, 2],
|
||||
["TA", 3, 1, 2]
|
||||
];
|
||||
check_concat(flow, results, inputs, done);
|
||||
});
|
||||
|
||||
it('should handle too many pending messages', function(done) {
|
||||
var flow = [{id:"n1", type:"batch", name: "BatchNode", mode: "concat", count: 0, overwrap: 0, interval: 1, allowEmptySequence: false, topics: [{topic: "TA"}, {topic: "TB"}], wires:[["n2"]]},
|
||||
{id:"n2", type:"helper"}];
|
||||
helper.load(batchNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
RED.settings.batchMaxKeptMsgsCount = 2;
|
||||
setTimeout(function() {
|
||||
var logEvents = helper.log().args.filter(function (evt) {
|
||||
return evt[0].type == "batch";
|
||||
});
|
||||
var evt = logEvents[0][0];
|
||||
evt.should.have.property('id', "n1");
|
||||
evt.should.have.property('type', "batch");
|
||||
evt.should.have.property('msg', "batch.too-many");
|
||||
done();
|
||||
}, 150);
|
||||
var C = 3;
|
||||
for(var i = 0; i < C; i++) {
|
||||
var parts_a = {index:i, count:C, id:"A"};
|
||||
var parts_b = {index:i, count:C, id:"B"};
|
||||
n1.receive({payload: i, topic: "TA", parts:parts_a});
|
||||
n1.receive({payload: i, topic: "TB", parts:parts_b});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
Reference in New Issue
Block a user