From 4dd044b5dc60668e47a0f0a7714b9ed1d91dec9c Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Wed, 21 Sep 2016 14:30:09 -0400 Subject: [PATCH] test: improve test-child-process-fork-dgram Previous implementation was subject to timing out as there was no guarantee that the parent or child would ever receive a message. Modified test so that once parent or child receives a message it closes its connection to the socket so we can guarantee the next messsage will be recieved by the other node. The test also left lingering processes when it timed out so pulled the timeout into the test itself so that it could properly cleanup in case of failure. --- test/parallel/parallel.status | 3 -- .../parallel/test-child-process-fork-dgram.js | 32 +++++++++++++++---- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/test/parallel/parallel.status b/test/parallel/parallel.status index 8c3239909b255c..164de96ea058a1 100644 --- a/test/parallel/parallel.status +++ b/test/parallel/parallel.status @@ -32,6 +32,3 @@ test-fs-watch-encoding : FAIL, PASS #being worked under https://github.com/nodejs/node/issues/7973 test-stdio-closed : PASS, FLAKY - -#covered by https://github.com/nodejs/node/issues/8271 -test-child-process-fork-dgram : PASS, FLAKY diff --git a/test/parallel/test-child-process-fork-dgram.js b/test/parallel/test-child-process-fork-dgram.js index 5a00dca06697cf..57d8fb04548cb9 100644 --- a/test/parallel/test-child-process-fork-dgram.js +++ b/test/parallel/test-child-process-fork-dgram.js @@ -8,9 +8,9 @@ * Because it's not really possible to predict how the messages will be * distributed among the parent and the child processes, we keep sending * messages until both the parent and the child received at least one - * message. The worst case scenario is when either one never receives - * a message. In this case the test runner will timeout after 60 secs - * and the test will fail. + * message. When either the parent or child receives a message we close + * the server on that side so the next message is guaranteed to be + * received by the other. */ const common = require('../common'); @@ -26,22 +26,30 @@ if (common.isWindows) { var server; if (process.argv[2] === 'child') { + var serverClosed = false; process.on('message', function removeMe(msg, clusterServer) { if (msg === 'server') { server = clusterServer; server.on('message', function() { process.send('gotMessage'); + // got a message so close the server to make sure + // the parent also gets a message + serverClosed = true; + server.close(); }); } else if (msg === 'stop') { - server.close(); process.removeListener('message', removeMe); + if (!serverClosed) { + server.close(); + } } }); } else { server = dgram.createSocket('udp4'); + var serverPort = null; var client = dgram.createSocket('udp4'); var child = fork(__filename, ['child']); @@ -52,9 +60,13 @@ if (process.argv[2] === 'child') { server.on('message', function(msg, rinfo) { parentGotMessage = true; + // got a message so close the server to make sure + // the child also gets a message + server.close(); }); server.on('listening', function() { + serverPort = server.address().port; child.send('server', server); child.once('message', function(msg) { @@ -66,13 +78,16 @@ if (process.argv[2] === 'child') { sendMessages(); }); + var iterations = 0; var sendMessages = function() { var timer = setInterval(function() { + iterations++; + client.send( msg, 0, msg.length, - server.address().port, + serverPort, '127.0.0.1', function(err) { if (err) throw err; @@ -83,7 +98,8 @@ if (process.argv[2] === 'child') { * Both the parent and the child got at least one message, * test passed, clean up everyting. */ - if (parentGotMessage && childGotMessage) { + if ((parentGotMessage && childGotMessage) || + ((iterations / 1000) > 45)) { clearInterval(timer); shutdown(); } @@ -94,7 +110,9 @@ if (process.argv[2] === 'child') { var shutdown = function() { child.send('stop'); - server.close(); + if (!parentGotMessage) { + server.close(); + } client.close(); };