From 7909ca24d38a11c42b790936a95fb423f473b557 Mon Sep 17 00:00:00 2001 From: Mark Hindess Date: Wed, 14 Jan 2015 10:18:47 +0000 Subject: [PATCH 1/2] Fix random delay in milliseconds case and change test to reproduce bug. Because there was no multiplier the node.randomFirst was a string so the later '+' was a concatentation. The test failed to catch this because it uses integers not strings to configure the node. --- nodes/core/core/89-delay.js | 4 ++-- test/nodes/core/core/89-delay_spec.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/nodes/core/core/89-delay.js b/nodes/core/core/89-delay.js index dfc5616ae..3a299caeb 100644 --- a/nodes/core/core/89-delay.js +++ b/nodes/core/core/89-delay.js @@ -62,8 +62,8 @@ module.exports = function(RED) { } if (n.randomUnits === "milliseconds") { - this.randomFirst = n.randomFirst; - this.randomLast = n.randomLast; + this.randomFirst = n.randomFirst * 1; + this.randomLast = n.randomLast * 1; } else if (n.randomUnits === "seconds") { this.randomFirst = n.randomFirst * 1000; this.randomLast = n.randomLast * 1000; diff --git a/test/nodes/core/core/89-delay_spec.js b/test/nodes/core/core/89-delay_spec.js index 22c3173a5..87ca6088f 100644 --- a/test/nodes/core/core/89-delay_spec.js +++ b/test/nodes/core/core/89-delay_spec.js @@ -368,7 +368,7 @@ describe('delayNode', function() { }); it(' randomly delays the message in milliseconds', function(done) { - randomDelayTest(400, 800, "milliseconds", done); + randomDelayTest("400", "800", "milliseconds", done); }); it('randomly delays the message in minutes', function(done) { From 5510dffe184d91203a1e0a72cad5d01b0726ab12 Mon Sep 17 00:00:00 2001 From: Mark Hindess Date: Wed, 14 Jan 2015 14:19:21 +0000 Subject: [PATCH 2/2] Fix random delay mode to be random delay not random rate-limited stack. Documentation says "Introduces a delay into a flow or rate limits messages." but this node was doing delay and rate limit in random mode which doesn't seem that useful. Worse it was a stack not a queue. I can't think of any sane use cases for that behaviour. --- nodes/core/core/89-delay.js | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/nodes/core/core/89-delay.js b/nodes/core/core/89-delay.js index 3a299caeb..02a1cc2e5 100644 --- a/nodes/core/core/89-delay.js +++ b/nodes/core/core/89-delay.js @@ -21,16 +21,6 @@ module.exports = function(RED) { var MILLIS_TO_NANOS = 1000000; var SECONDS_TO_NANOS = 1000000000; - function random(n) { - var wait = n.randomFirst + (n.diff * Math.random()); - if (n.buffer.length > 0) { - n.send(n.buffer.pop()); - n.randomID = setTimeout(function() {random(n);},wait); - } else { - n.randomID = -1; - } - } - function DelayNode(n) { RED.nodes.createNode(this,n); @@ -180,19 +170,22 @@ module.exports = function(RED) { }); } else if (this.pauseType === "random") { - this.on("input",function(msg){ - node.buffer.push(msg); - if (node.randomID === -1) { - var wait = node.randomFirst + (node.diff * Math.random()); - node.randomID = setTimeout(function() {random(node);},wait); - } + this.on("input", function(msg) { + var wait = node.randomFirst + (node.diff * Math.random()); + var id = setTimeout(function(){ + node.idList.splice(node.idList.indexOf(id),1); + node.send(msg); + }, wait); + this.idList.push(id); }); - this.on("close", function (){ - if (this.randomID !== -1) { - clearTimeout(this.randomID); + this.on("close", function() { + for (var i=0; i