1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

test a few extra paths in several nodes

trigger, switch and file
(and linted delay just because)
This commit is contained in:
Dave Conway-Jones 2016-04-23 21:06:48 +01:00
parent 595ff63b72
commit d0f57efe0b
4 changed files with 164 additions and 56 deletions

View File

@ -28,7 +28,6 @@ var secondsToMinutes = 60;
var secondsToHours = 3600; var secondsToHours = 3600;
var secondsToDays = 86400; var secondsToDays = 86400;
describe('delay Node', function() { describe('delay Node', function() {
beforeEach(function(done) { beforeEach(function(done) {
@ -71,11 +70,11 @@ describe('delay Node', function() {
}); });
var TimeUnitEnum = { var TimeUnitEnum = {
MILLIS : "milliseconds", MILLIS : "milliseconds",
SECONDS : "seconds", SECONDS : "seconds",
MINUTES : "minutes", MINUTES : "minutes",
HOURS : "hours", HOURS : "hours",
DAYS : "days" DAYS : "days"
} }
/** /**
@ -90,7 +89,7 @@ describe('delay Node', function() {
var minExpected = expectedValue - toleranceFraction; var minExpected = expectedValue - toleranceFraction;
var maxExpected = expectedValue + toleranceFraction; var maxExpected = expectedValue + toleranceFraction;
if(actualValue >= minExpected && actualValue <= maxExpected) { if (actualValue >= minExpected && actualValue <= maxExpected) {
toReturn = true; toReturn = true;
} else { } else {
toReturn = false; toReturn = false;
@ -117,23 +116,23 @@ describe('delay Node', function() {
var aTimeoutUnifiedToSeconds; var aTimeoutUnifiedToSeconds;
// calculating the timeout in seconds // calculating the timeout in seconds
if(aTimeoutUnit == TimeUnitEnum.MILLIS) { if (aTimeoutUnit == TimeUnitEnum.MILLIS) {
aTimeoutUnifiedToSeconds = aTimeout / millisToSeconds; aTimeoutUnifiedToSeconds = aTimeout / millisToSeconds;
} else if(aTimeoutUnit == TimeUnitEnum.SECONDS) { } else if (aTimeoutUnit == TimeUnitEnum.SECONDS) {
aTimeoutUnifiedToSeconds = aTimeout; aTimeoutUnifiedToSeconds = aTimeout;
} else if(aTimeoutUnit == TimeUnitEnum.MINUTES) { } else if (aTimeoutUnit == TimeUnitEnum.MINUTES) {
aTimeoutUnifiedToSeconds = aTimeout * secondsToMinutes; aTimeoutUnifiedToSeconds = aTimeout * secondsToMinutes;
} else if(aTimeoutUnit == TimeUnitEnum.HOURS) { } else if (aTimeoutUnit == TimeUnitEnum.HOURS) {
aTimeoutUnifiedToSeconds = aTimeout * secondsToHours; aTimeoutUnifiedToSeconds = aTimeout * secondsToHours;
} else if(aTimeoutUnit == TimeUnitEnum.DAYS) { } else if (aTimeoutUnit == TimeUnitEnum.DAYS) {
aTimeoutUnifiedToSeconds = aTimeout * secondsToDays; aTimeoutUnifiedToSeconds = aTimeout * secondsToDays;
} }
if(closeEnough(runtimeSeconds, aTimeoutUnifiedToSeconds, GRACE_PERCENTAGE)) { if (closeEnough(runtimeSeconds, aTimeoutUnifiedToSeconds, GRACE_PERCENTAGE)) {
done(); done();
} else { } else {
try { try {
should.fail(null, null, "Delayed runtime seconds " + runtimeSeconds + " was not close enough to exlected timeout seconds: " + aTimeoutUnifiedToSeconds); should.fail(null, null, "Delayed runtime seconds " + runtimeSeconds + " was not close enough to exlected timeout seconds: " + aTimeoutUnifiedToSeconds);
} catch (err) { } catch (err) {
done(err); done(err);
} }
@ -188,7 +187,7 @@ describe('delay Node', function() {
var receiveTimestamp; var receiveTimestamp;
helperNode1.on("input", function(msg) { helperNode1.on("input", function(msg) {
if(receiveTimestamp) { if (receiveTimestamp) {
var elapse = process.hrtime(receiveTimestamp); var elapse = process.hrtime(receiveTimestamp);
var receiveInterval = (elapse[0] * 1000) + ((elapse[1] / nanosToSeconds) * 1000); var receiveInterval = (elapse[0] * 1000) + ((elapse[1] / nanosToSeconds) * 1000);
receiveInterval.should.be.above(rate * 0.9); receiveInterval.should.be.above(rate * 0.9);
@ -200,16 +199,16 @@ describe('delay Node', function() {
var possibleMaxMessageCount = Math.ceil(aLimit * (runtimeInMillis / 1000) + aLimit); // +aLimit as at the start of the 2nd period, we're allowing the 3rd burst var possibleMaxMessageCount = Math.ceil(aLimit * (runtimeInMillis / 1000) + aLimit); // +aLimit as at the start of the 2nd period, we're allowing the 3rd burst
var i = 0; var i = 0;
for(; i < possibleMaxMessageCount + 1; i++) { for (; i < possibleMaxMessageCount + 1; i++) {
delayNode1.receive({payload:i}); delayNode1.receive({payload:i});
} }
setTimeout(function() { setTimeout(function() {
try { try {
receivedMessagesStack.length.should.be.lessThan(possibleMaxMessageCount); receivedMessagesStack.length.should.be.lessThan(possibleMaxMessageCount);
for(var j = 0; j < receivedMessagesStack.length; j++) { for (var j = 0; j < receivedMessagesStack.length; j++) {
if(receivedMessagesStack[j].payload === j) { if (receivedMessagesStack[j].payload === j) {
if(j === (receivedMessagesStack.length -1)) { // last message, all matched so far if (j === (receivedMessagesStack.length -1)) { // last message, all matched so far
done(); done();
} }
} else { } else {
@ -250,7 +249,7 @@ describe('delay Node', function() {
var receiveTimestamp; var receiveTimestamp;
helperNode1.on("input", function(msg) { helperNode1.on("input", function(msg) {
if(receiveTimestamp) { if (receiveTimestamp) {
var elapse = process.hrtime(receiveTimestamp); var elapse = process.hrtime(receiveTimestamp);
var receiveInterval = (elapse[0] * 1000) + ((elapse[1] / nanosToSeconds) * 1000); var receiveInterval = (elapse[0] * 1000) + ((elapse[1] / nanosToSeconds) * 1000);
receiveInterval.should.be.above(rate * 0.9); receiveInterval.should.be.above(rate * 0.9);
@ -264,9 +263,9 @@ describe('delay Node', function() {
var i = 0; var i = 0;
delayNode1.receive({payload:i}); delayNode1.receive({payload:i});
i++; i++;
for(; i < possibleMaxMessageCount + 1; i++) { for (; i < possibleMaxMessageCount + 1; i++) {
setTimeout(function() { setTimeout(function() {
delayNode1.receive({payload:i}); delayNode1.receive({payload:i});
}, 2 * ((rate * i) / possibleMaxMessageCount) ); }, 2 * ((rate * i) / possibleMaxMessageCount) );
} }
@ -281,9 +280,9 @@ describe('delay Node', function() {
receivedMessagesStack.length.should.be.greaterThan(2); // ensure that we receive more than 1st and last message receivedMessagesStack.length.should.be.greaterThan(2); // ensure that we receive more than 1st and last message
receivedMessagesStack[0].payload.should.be.exactly(0); // means we received the last message injected just before test termination receivedMessagesStack[0].payload.should.be.exactly(0); // means we received the last message injected just before test termination
var foundAtLeastOneDrop = false; var foundAtLeastOneDrop = false;
for(var i = 0; i < receivedMessagesStack.length; i++) { for (var i = 0; i < receivedMessagesStack.length; i++) {
if(i > 0) { if (i > 0) {
if(receivedMessagesStack[i].payload - receivedMessagesStack[i - 1].payload > 1) { if (receivedMessagesStack[i].payload - receivedMessagesStack[i - 1].payload > 1) {
foundAtLeastOneDrop = true; foundAtLeastOneDrop = true;
} }
} }
@ -317,11 +316,11 @@ describe('delay Node', function() {
* @param allowedGracePercent - The percentage of grace allowed * @param allowedGracePercent - The percentage of grace allowed
*/ */
function inBetweenDelays(timeoutFrom, timeoutTo, actualTimeout, allowedGracePercent) { function inBetweenDelays(timeoutFrom, timeoutTo, actualTimeout, allowedGracePercent) {
if(closeEnough(actualTimeout, timeoutFrom, allowedGracePercent)) { if (closeEnough(actualTimeout, timeoutFrom, allowedGracePercent)) {
return true; return true;
} else if(closeEnough(actualTimeout, timeoutTo, allowedGracePercent)) { } else if (closeEnough(actualTimeout, timeoutTo, allowedGracePercent)) {
return true; return true;
} else if(timeoutFrom < actualTimeout && timeoutTo > actualTimeout) { } else if (timeoutFrom < actualTimeout && timeoutTo > actualTimeout) {
return true; return true;
} else { } else {
return false; return false;
@ -349,28 +348,28 @@ describe('delay Node', function() {
var aTimeoutToUnifiedToSeconds; var aTimeoutToUnifiedToSeconds;
// calculating the timeout in seconds // calculating the timeout in seconds
if(aTimeoutUnit == TimeUnitEnum.MILLIS) { if (aTimeoutUnit == TimeUnitEnum.MILLIS) {
aTimeoutFromUnifiedToSeconds = aTimeoutFrom / millisToSeconds; aTimeoutFromUnifiedToSeconds = aTimeoutFrom / millisToSeconds;
aTimeoutToUnifiedToSeconds = aTimeoutTo / millisToSeconds; aTimeoutToUnifiedToSeconds = aTimeoutTo / millisToSeconds;
} else if(aTimeoutUnit == TimeUnitEnum.SECONDS) { } else if (aTimeoutUnit == TimeUnitEnum.SECONDS) {
aTimeoutFromUnifiedToSeconds = aTimeoutFrom; aTimeoutFromUnifiedToSeconds = aTimeoutFrom;
aTimeoutToUnifiedToSeconds = aTimeoutTo; aTimeoutToUnifiedToSeconds = aTimeoutTo;
} else if(aTimeoutUnit == TimeUnitEnum.MINUTES) { } else if (aTimeoutUnit == TimeUnitEnum.MINUTES) {
aTimeoutFromUnifiedToSeconds = aTimeoutFrom * secondsToMinutes; aTimeoutFromUnifiedToSeconds = aTimeoutFrom * secondsToMinutes;
aTimeoutToUnifiedToSeconds = aTimeoutTo * secondsToMinutes; aTimeoutToUnifiedToSeconds = aTimeoutTo * secondsToMinutes;
} else if(aTimeoutUnit == TimeUnitEnum.HOURS) { } else if (aTimeoutUnit == TimeUnitEnum.HOURS) {
aTimeoutFromUnifiedToSeconds = aTimeoutFrom * secondsToHours; aTimeoutFromUnifiedToSeconds = aTimeoutFrom * secondsToHours;
aTimeoutToUnifiedToSeconds = aTimeoutTo * secondsToHours; aTimeoutToUnifiedToSeconds = aTimeoutTo * secondsToHours;
} else if(aTimeoutUnit == TimeUnitEnum.DAYS) { } else if (aTimeoutUnit == TimeUnitEnum.DAYS) {
aTimeoutFromUnifiedToSeconds = aTimeoutFrom * secondsToDays; aTimeoutFromUnifiedToSeconds = aTimeoutFrom * secondsToDays;
aTimeoutToUnifiedToSeconds = aTimeoutTo * secondsToDays; aTimeoutToUnifiedToSeconds = aTimeoutTo * secondsToDays;
} }
if(inBetweenDelays(aTimeoutFromUnifiedToSeconds, aTimeoutToUnifiedToSeconds, runtimeSeconds, GRACE_PERCENTAGE)) { if (inBetweenDelays(aTimeoutFromUnifiedToSeconds, aTimeoutToUnifiedToSeconds, runtimeSeconds, GRACE_PERCENTAGE)) {
done(); done();
} else { } else {
try { try {
should.fail(null, null, "Delayed runtime seconds " + runtimeSeconds + " was not \"in between enough\" enough to expected values of: " + aTimeoutFromUnifiedToSeconds + " and " + aTimeoutToUnifiedToSeconds); should.fail(null, null, "Delayed runtime seconds " + runtimeSeconds + " was not \"in between enough\" enough to expected values of: " + aTimeoutFromUnifiedToSeconds + " and " + aTimeoutToUnifiedToSeconds);
} catch (err) { } catch (err) {
done(err); done(err);
} }
@ -420,20 +419,20 @@ describe('delay Node', function() {
var messageBurstSize = 1500; var messageBurstSize = 1500;
// we ensure that we note that a warning is received for buffer growth // we ensure that we note that a warning is received for buffer growth
sinon.stub(delayNode1, 'warn', function(warning){ sinon.stub(delayNode1, 'warn', function(warning) {
if(warning.indexOf("buffer exceeded 1000 messages" > -1)) { if (warning.indexOf("buffer exceeded 1000 messages" > -1)) {
receivedWarning = true; receivedWarning = true;
} }
}); });
// we ensure that the warning is received for buffer size and that we get the last message // we ensure that the warning is received for buffer size and that we get the last message
helperNode1.on("input", function(msg) { helperNode1.on("input", function(msg) {
if(msg.payload === (messageBurstSize - 1) && receivedWarning === true) { if (msg.payload === (messageBurstSize - 1) && receivedWarning === true) {
done(); // it will timeout if we don't receive the last message done(); // it will timeout if we don't receive the last message
} }
}); });
// send 1500 messages as quickly as possible // send 1500 messages as quickly as possible
for(var i = 0; i < messageBurstSize; i++) { for (var i = 0; i < messageBurstSize; i++) {
delayNode1.receive({payload:i}); delayNode1.receive({payload:i});
} }
}); });

View File

@ -285,13 +285,39 @@ describe('trigger Node', function() {
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
var c = 0; var c = 0;
n2.on("input", function(msg) { n2.on("input", function(msg) {
c += 1;
msg.should.have.a.property("payload", 1); msg.should.have.a.property("payload", 1);
}); });
setTimeout( function() { setTimeout( function() {
n1.emit("input", {reset:true}); if (c === 2) { done(); }
done(); },100);
},500); n1.emit("input", {payload:null}); // trigger
n1.emit("input", {payload:null}); n1.emit("input", {payload:null}); // blocked
n1.emit("input", {payload:null}); // blocked
n1.emit("input", {reset:true}); // clear the blockage
n1.emit("input", {payload:null}); // trigger
});
});
it('should be able to set infinite timeout, and clear timeout by message', function(done) {
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", reset:"boo", duration:-5, 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) {
c += 1;
msg.should.have.a.property("payload", 1);
});
setTimeout( function() {
if (c === 2) { done(); }
},100);
n1.emit("input", {payload:null}); // trigger
n1.emit("input", {payload:null}); // blocked
n1.emit("input", {payload:null}); // blocked
n1.emit("input", {payload:"boo"}); // clear the blockage
n1.emit("input", {payload:null}); // trigger
}); });
}); });

View File

@ -100,7 +100,7 @@ describe('switch Node', function() {
var helperNode1 = helper.getNode("helperNode1"); var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) { helperNode1.on("input", function(msg) {
try { try {
if(shouldReceive === true) { if (shouldReceive === true) {
msg.payload.should.equal(sendPayload); msg.payload.should.equal(sendPayload);
done(); done();
} else { } else {
@ -111,7 +111,7 @@ describe('switch Node', function() {
} }
}); });
switchNode1.receive({payload:sendPayload}); switchNode1.receive({payload:sendPayload});
if(shouldReceive === false) { if (shouldReceive === false) {
setTimeout(function() { setTimeout(function() {
done(); done();
}, 200); }, 200);
@ -184,7 +184,7 @@ describe('switch Node', function() {
}); });
it('should check if payload is between given values', function(done) { it('should check if payload is between given values', function(done) {
twoFieldSwitchTest("btwn", 3, 5, true, true, 4, done); twoFieldSwitchTest("btwn", "3", "5", true, true, 4, done);
}); });
it('should check if payload is not between given values', function(done) { it('should check if payload is not between given values', function(done) {
@ -243,18 +243,25 @@ describe('switch Node', function() {
singularSwitchTest(false, true, false, true, done); singularSwitchTest(false, true, false, true, done);
}); });
it('should check if input is indeed null', function(done) { it('should check input against a previous value', function(done) {
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"null"}],checkall:true,outputs:1,wires:[["helperNode1"]]}, var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{ "t": "gt", "v": "", "vt": "prev" }],checkall:true,outputs:1,wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}]; {id:"helperNode1", type:"helper", wires:[]}];
helper.load(switchNode, flow, function() { helper.load(switchNode, flow, function() {
var switchNode1 = helper.getNode("switchNode1"); var switchNode1 = helper.getNode("switchNode1");
var helperNode1 = helper.getNode("helperNode1"); var helperNode1 = helper.getNode("helperNode1");
var c = 0;
helperNode1.on("input", function(msg) { helperNode1.on("input", function(msg) {
if(msg.payload) { if (msg.payload) {
try { try {
should.fail(null, null, "msg.payload should be undefined!"); if (c === 0) {
msg.payload.should.equal(1);
}
if (c === 1) {
msg.payload.should.equal(2);
done();
}
c += 1;
} catch (err) { } catch (err) {
done(err); done(err);
} }
@ -262,6 +269,81 @@ describe('switch Node', function() {
done(); done();
} }
}); });
switchNode1.receive({payload:1});
switchNode1.receive({payload:0});
switchNode1.receive({payload:-2});
switchNode1.receive({payload:2});
});
});
it('should check input against a previous value (2nd option)', function(done) {
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t": "btwn", "v": "10", "vt": "num", "v2": "", "v2t": "prev" }],checkall:true,outputs:1,wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(switchNode, flow, function() {
var switchNode1 = helper.getNode("switchNode1");
var helperNode1 = helper.getNode("helperNode1");
var c = 0;
helperNode1.on("input", function(msg) {
if (msg.payload) {
try {
if (c === 0) {
msg.payload.should.equal(20);
}
if (c === 1) {
msg.payload.should.equal(25);
done();
}
c += 1;
} catch (err) {
done(err);
}
} else {
done();
}
});
switchNode1.receive({payload:0});
switchNode1.receive({payload:20}); // between 10 and 0
switchNode1.receive({payload:30}); // between 10 and 20
switchNode1.receive({payload:20}); // between 10 and 30 yes
switchNode1.receive({payload:30}); // between 10 and 20 no
switchNode1.receive({payload:25}); // between 10 and 30 yes
});
});
it('should check if input is indeed null', function(done) {
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"null"}],checkall:true,outputs:1,wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(switchNode, flow, function() {
var switchNode1 = helper.getNode("switchNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
if (msg.payload === null) {
done();
} else {
console.log("msg is ",msg);
}
});
switchNode1.receive({payload:null});
});
});
it('should check if input is indeed undefined', function(done) {
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"null"}],checkall:true,outputs:1,wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(switchNode, flow, function() {
var switchNode1 = helper.getNode("switchNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
if (msg.payload === undefined) {
done();
}
else {
console.log("msg is ",msg);
}
});
switchNode1.receive({payload:undefined}); switchNode1.receive({payload:undefined});
}); });
}); });
@ -275,7 +357,7 @@ describe('switch Node', function() {
var switchNode1 = helper.getNode("switchNode1"); var switchNode1 = helper.getNode("switchNode1");
var helperNode1 = helper.getNode("helperNode1"); var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) { helperNode1.on("input", function(msg) {
if(msg.payload) { if (msg.payload) {
done(); done();
} else { } else {
try { try {
@ -319,7 +401,7 @@ describe('switch Node', function() {
try { try {
msg.payload.should.equal("Hello"); msg.payload.should.equal("Hello");
nodeHitCount++; nodeHitCount++;
if(nodeHitCount == 2) { if (nodeHitCount == 2) {
done(); done();
} else { } else {
try { try {
@ -349,7 +431,6 @@ describe('switch Node', function() {
{id:"helperNode2", type:"helper", wires:[]}, {id:"helperNode2", type:"helper", wires:[]},
{id:"helperNode3", type:"helper", wires:[]}]; {id:"helperNode3", type:"helper", wires:[]}];
helper.load(switchNode, flow, function() { helper.load(switchNode, flow, function() {
var switchNode1 = helper.getNode("switchNode1"); var switchNode1 = helper.getNode("switchNode1");
var helperNode1 = helper.getNode("helperNode1"); var helperNode1 = helper.getNode("helperNode1");

View File

@ -71,11 +71,13 @@ describe('file Nodes', function() {
n1.emit("input", {payload:"test2"}); // string n1.emit("input", {payload:"test2"}); // string
setTimeout(function() { setTimeout(function() {
n1.emit("input", {payload:true}); // boolean n1.emit("input", {payload:true}); // boolean
n1.emit("input", {payload:1});
n1.emit("input", {payload:[2]});
},50); },50);
setTimeout(function() { setTimeout(function() {
var f = fs.readFileSync(fileToTest).toString(); var f = fs.readFileSync(fileToTest).toString();
f.should.have.length(11); f.should.have.length(17);
f.should.equal("test2\ntrue\n"); f.should.equal("test2\ntrue\n1\n[2]\n");
done(); done();
},wait); },wait);
}); });