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 secondsToDays = 86400;
describe('delay Node', function() {
beforeEach(function(done) {
@ -71,11 +70,11 @@ describe('delay Node', function() {
});
var TimeUnitEnum = {
MILLIS : "milliseconds",
SECONDS : "seconds",
MINUTES : "minutes",
HOURS : "hours",
DAYS : "days"
MILLIS : "milliseconds",
SECONDS : "seconds",
MINUTES : "minutes",
HOURS : "hours",
DAYS : "days"
}
/**
@ -90,7 +89,7 @@ describe('delay Node', function() {
var minExpected = expectedValue - toleranceFraction;
var maxExpected = expectedValue + toleranceFraction;
if(actualValue >= minExpected && actualValue <= maxExpected) {
if (actualValue >= minExpected && actualValue <= maxExpected) {
toReturn = true;
} else {
toReturn = false;
@ -117,23 +116,23 @@ describe('delay Node', function() {
var aTimeoutUnifiedToSeconds;
// calculating the timeout in seconds
if(aTimeoutUnit == TimeUnitEnum.MILLIS) {
if (aTimeoutUnit == TimeUnitEnum.MILLIS) {
aTimeoutUnifiedToSeconds = aTimeout / millisToSeconds;
} else if(aTimeoutUnit == TimeUnitEnum.SECONDS) {
} else if (aTimeoutUnit == TimeUnitEnum.SECONDS) {
aTimeoutUnifiedToSeconds = aTimeout;
} else if(aTimeoutUnit == TimeUnitEnum.MINUTES) {
} else if (aTimeoutUnit == TimeUnitEnum.MINUTES) {
aTimeoutUnifiedToSeconds = aTimeout * secondsToMinutes;
} else if(aTimeoutUnit == TimeUnitEnum.HOURS) {
} else if (aTimeoutUnit == TimeUnitEnum.HOURS) {
aTimeoutUnifiedToSeconds = aTimeout * secondsToHours;
} else if(aTimeoutUnit == TimeUnitEnum.DAYS) {
} else if (aTimeoutUnit == TimeUnitEnum.DAYS) {
aTimeoutUnifiedToSeconds = aTimeout * secondsToDays;
}
if(closeEnough(runtimeSeconds, aTimeoutUnifiedToSeconds, GRACE_PERCENTAGE)) {
if (closeEnough(runtimeSeconds, aTimeoutUnifiedToSeconds, GRACE_PERCENTAGE)) {
done();
} else {
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) {
done(err);
}
@ -188,7 +187,7 @@ describe('delay Node', function() {
var receiveTimestamp;
helperNode1.on("input", function(msg) {
if(receiveTimestamp) {
if (receiveTimestamp) {
var elapse = process.hrtime(receiveTimestamp);
var receiveInterval = (elapse[0] * 1000) + ((elapse[1] / nanosToSeconds) * 1000);
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 i = 0;
for(; i < possibleMaxMessageCount + 1; i++) {
for (; i < possibleMaxMessageCount + 1; i++) {
delayNode1.receive({payload:i});
}
setTimeout(function() {
try {
receivedMessagesStack.length.should.be.lessThan(possibleMaxMessageCount);
for(var j = 0; j < receivedMessagesStack.length; j++) {
if(receivedMessagesStack[j].payload === j) {
if(j === (receivedMessagesStack.length -1)) { // last message, all matched so far
for (var j = 0; j < receivedMessagesStack.length; j++) {
if (receivedMessagesStack[j].payload === j) {
if (j === (receivedMessagesStack.length -1)) { // last message, all matched so far
done();
}
} else {
@ -250,7 +249,7 @@ describe('delay Node', function() {
var receiveTimestamp;
helperNode1.on("input", function(msg) {
if(receiveTimestamp) {
if (receiveTimestamp) {
var elapse = process.hrtime(receiveTimestamp);
var receiveInterval = (elapse[0] * 1000) + ((elapse[1] / nanosToSeconds) * 1000);
receiveInterval.should.be.above(rate * 0.9);
@ -264,9 +263,9 @@ describe('delay Node', function() {
var i = 0;
delayNode1.receive({payload:i});
i++;
for(; i < possibleMaxMessageCount + 1; i++) {
for (; i < possibleMaxMessageCount + 1; i++) {
setTimeout(function() {
delayNode1.receive({payload:i});
delayNode1.receive({payload:i});
}, 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[0].payload.should.be.exactly(0); // means we received the last message injected just before test termination
var foundAtLeastOneDrop = false;
for(var i = 0; i < receivedMessagesStack.length; i++) {
if(i > 0) {
if(receivedMessagesStack[i].payload - receivedMessagesStack[i - 1].payload > 1) {
for (var i = 0; i < receivedMessagesStack.length; i++) {
if (i > 0) {
if (receivedMessagesStack[i].payload - receivedMessagesStack[i - 1].payload > 1) {
foundAtLeastOneDrop = true;
}
}
@ -317,11 +316,11 @@ describe('delay Node', function() {
* @param allowedGracePercent - The percentage of grace allowed
*/
function inBetweenDelays(timeoutFrom, timeoutTo, actualTimeout, allowedGracePercent) {
if(closeEnough(actualTimeout, timeoutFrom, allowedGracePercent)) {
if (closeEnough(actualTimeout, timeoutFrom, allowedGracePercent)) {
return true;
} else if(closeEnough(actualTimeout, timeoutTo, allowedGracePercent)) {
} else if (closeEnough(actualTimeout, timeoutTo, allowedGracePercent)) {
return true;
} else if(timeoutFrom < actualTimeout && timeoutTo > actualTimeout) {
} else if (timeoutFrom < actualTimeout && timeoutTo > actualTimeout) {
return true;
} else {
return false;
@ -349,28 +348,28 @@ describe('delay Node', function() {
var aTimeoutToUnifiedToSeconds;
// calculating the timeout in seconds
if(aTimeoutUnit == TimeUnitEnum.MILLIS) {
if (aTimeoutUnit == TimeUnitEnum.MILLIS) {
aTimeoutFromUnifiedToSeconds = aTimeoutFrom / millisToSeconds;
aTimeoutToUnifiedToSeconds = aTimeoutTo / millisToSeconds;
} else if(aTimeoutUnit == TimeUnitEnum.SECONDS) {
} else if (aTimeoutUnit == TimeUnitEnum.SECONDS) {
aTimeoutFromUnifiedToSeconds = aTimeoutFrom;
aTimeoutToUnifiedToSeconds = aTimeoutTo;
} else if(aTimeoutUnit == TimeUnitEnum.MINUTES) {
} else if (aTimeoutUnit == TimeUnitEnum.MINUTES) {
aTimeoutFromUnifiedToSeconds = aTimeoutFrom * secondsToMinutes;
aTimeoutToUnifiedToSeconds = aTimeoutTo * secondsToMinutes;
} else if(aTimeoutUnit == TimeUnitEnum.HOURS) {
} else if (aTimeoutUnit == TimeUnitEnum.HOURS) {
aTimeoutFromUnifiedToSeconds = aTimeoutFrom * secondsToHours;
aTimeoutToUnifiedToSeconds = aTimeoutTo * secondsToHours;
} else if(aTimeoutUnit == TimeUnitEnum.DAYS) {
} else if (aTimeoutUnit == TimeUnitEnum.DAYS) {
aTimeoutFromUnifiedToSeconds = aTimeoutFrom * secondsToDays;
aTimeoutToUnifiedToSeconds = aTimeoutTo * secondsToDays;
}
if(inBetweenDelays(aTimeoutFromUnifiedToSeconds, aTimeoutToUnifiedToSeconds, runtimeSeconds, GRACE_PERCENTAGE)) {
if (inBetweenDelays(aTimeoutFromUnifiedToSeconds, aTimeoutToUnifiedToSeconds, runtimeSeconds, GRACE_PERCENTAGE)) {
done();
} else {
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) {
done(err);
}
@ -420,20 +419,20 @@ describe('delay Node', function() {
var messageBurstSize = 1500;
// we ensure that we note that a warning is received for buffer growth
sinon.stub(delayNode1, 'warn', function(warning){
if(warning.indexOf("buffer exceeded 1000 messages" > -1)) {
sinon.stub(delayNode1, 'warn', function(warning) {
if (warning.indexOf("buffer exceeded 1000 messages" > -1)) {
receivedWarning = true;
}
});
// we ensure that the warning is received for buffer size and that we get the last message
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
}
});
// 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});
}
});

View File

@ -285,13 +285,39 @@ describe('trigger Node', function() {
var n2 = helper.getNode("n2");
var c = 0;
n2.on("input", function(msg) {
c += 1;
msg.should.have.a.property("payload", 1);
});
setTimeout( function() {
n1.emit("input", {reset:true});
done();
},500);
n1.emit("input", {payload:null});
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", {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");
helperNode1.on("input", function(msg) {
try {
if(shouldReceive === true) {
if (shouldReceive === true) {
msg.payload.should.equal(sendPayload);
done();
} else {
@ -111,7 +111,7 @@ describe('switch Node', function() {
}
});
switchNode1.receive({payload:sendPayload});
if(shouldReceive === false) {
if (shouldReceive === false) {
setTimeout(function() {
done();
}, 200);
@ -184,7 +184,7 @@ describe('switch Node', function() {
});
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) {
@ -243,18 +243,25 @@ describe('switch Node', function() {
singularSwitchTest(false, true, false, true, done);
});
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"]]},
it('should check input against a previous value', function(done) {
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:[]}];
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) {
if (msg.payload) {
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) {
done(err);
}
@ -262,6 +269,81 @@ describe('switch Node', function() {
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});
});
});
@ -275,7 +357,7 @@ describe('switch Node', function() {
var switchNode1 = helper.getNode("switchNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
if(msg.payload) {
if (msg.payload) {
done();
} else {
try {
@ -319,7 +401,7 @@ describe('switch Node', function() {
try {
msg.payload.should.equal("Hello");
nodeHitCount++;
if(nodeHitCount == 2) {
if (nodeHitCount == 2) {
done();
} else {
try {
@ -349,7 +431,6 @@ describe('switch Node', function() {
{id:"helperNode2", type:"helper", wires:[]},
{id:"helperNode3", type:"helper", wires:[]}];
helper.load(switchNode, flow, function() {
var switchNode1 = helper.getNode("switchNode1");
var helperNode1 = helper.getNode("helperNode1");

View File

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