From 151d2acc0d987f49a6a4aa3768d25304ef08e5e5 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Fri, 9 Dec 2016 22:44:58 -0800 Subject: [PATCH 1/3] test: fix flaky test-dgram-exclusive-implicit-bind test-dgram-exclusive-implicit-bind is written assuming that dgram messages are received with 100% reliability. While missing a dgram message sent to localhost is rare, we do see it as evidenced by CI failures from time to time. The test has been rewritten to send dgram messages over and over until the test requirements have been met. Additional incidental refactoring includes: * var -> const * use of common.mustCall() instead of exit listener + boolean --- .../test-dgram-exclusive-implicit-bind.js | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/test/parallel/test-dgram-exclusive-implicit-bind.js b/test/parallel/test-dgram-exclusive-implicit-bind.js index c8799cf058b99e..e9fbf54e06e673 100644 --- a/test/parallel/test-dgram-exclusive-implicit-bind.js +++ b/test/parallel/test-dgram-exclusive-implicit-bind.js @@ -20,10 +20,10 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. -var common = require('../common'); -var assert = require('assert'); -var cluster = require('cluster'); -var dgram = require('dgram'); +const common = require('../common'); +const assert = require('assert'); +const cluster = require('cluster'); +const dgram = require('dgram'); // Without an explicit bind, send() causes an implicit bind, which always // generate a unique per-socket ephemeral port. An explicit bind to a port @@ -40,17 +40,21 @@ var dgram = require('dgram'); // with ENOTSUP. if (cluster.isMaster) { - var pass; var messages = 0; - var ports = {}; - - process.on('exit', function() { - assert.strictEqual(pass, true); - }); + const ports = {}; + const pids = []; var target = dgram.createSocket('udp4'); + const done = common.mustCall(function() { + cluster.disconnect(); + target.close(); + }); + target.on('message', function(buf, rinfo) { + if (pids.includes(buf.toString())) + return; + pids.push(buf.toString()); messages++; ports[rinfo.port] = true; @@ -63,12 +67,6 @@ if (cluster.isMaster) { assert.strictEqual(Object.keys(ports).length, 3); done(); } - - function done() { - pass = true; - cluster.disconnect(); - target.close(); - } }); target.on('listening', function() { @@ -85,7 +83,12 @@ if (cluster.isMaster) { return; } -var source = dgram.createSocket('udp4'); +const source = dgram.createSocket('udp4'); +var timer; + +source.on('close', function() { + clearTimeout(timer); +}); if (process.env.BOUND === 'y') { source.bind(0); @@ -96,4 +99,12 @@ if (process.env.BOUND === 'y') { source.unref(); } -source.send(Buffer.from('abc'), 0, 3, common.PORT, '127.0.0.1'); +function send() { + const buf = Buffer.from(process.pid.toString()); + timer = setTimeout(function() { + source.send(buf, common.PORT, '127.0.0.1', send); + }, 1); + timer.unref(); +} + +send(); From 909e78e18e9335180f83552743fbede0e17492c4 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 10 Dec 2016 14:50:00 -0800 Subject: [PATCH 2/3] squash: use setInterval instead of chaining setTimeouts --- .../test-dgram-exclusive-implicit-bind.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/test/parallel/test-dgram-exclusive-implicit-bind.js b/test/parallel/test-dgram-exclusive-implicit-bind.js index e9fbf54e06e673..9bb08b2052f0ce 100644 --- a/test/parallel/test-dgram-exclusive-implicit-bind.js +++ b/test/parallel/test-dgram-exclusive-implicit-bind.js @@ -84,10 +84,10 @@ if (cluster.isMaster) { } const source = dgram.createSocket('udp4'); -var timer; +var interval; source.on('close', function() { - clearTimeout(timer); + clearTimeout(interval); }); if (process.env.BOUND === 'y') { @@ -99,12 +99,7 @@ if (process.env.BOUND === 'y') { source.unref(); } -function send() { - const buf = Buffer.from(process.pid.toString()); - timer = setTimeout(function() { - source.send(buf, common.PORT, '127.0.0.1', send); - }, 1); - timer.unref(); -} - -send(); +const buf = Buffer.from(process.pid.toString()); +interval = setInterval(() => { + source.send(buf, common.PORT, '127.0.0.1'); +}, 1).unref(); From e346f8489bbc3dd16ab6b1d57457f3e750d110fc Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sun, 11 Dec 2016 10:11:30 -0800 Subject: [PATCH 3/3] squash: clearTimeout() -> clearInterval() --- test/parallel/test-dgram-exclusive-implicit-bind.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-dgram-exclusive-implicit-bind.js b/test/parallel/test-dgram-exclusive-implicit-bind.js index 9bb08b2052f0ce..97fd6bed5d6e33 100644 --- a/test/parallel/test-dgram-exclusive-implicit-bind.js +++ b/test/parallel/test-dgram-exclusive-implicit-bind.js @@ -87,7 +87,7 @@ const source = dgram.createSocket('udp4'); var interval; source.on('close', function() { - clearTimeout(interval); + clearInterval(interval); }); if (process.env.BOUND === 'y') {