From e98bcfa2cb3ea3442c785c9b95d4be0e4e02dcba Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sun, 10 Jan 2016 21:18:02 -0800 Subject: [PATCH] test: remove 1 second delay from test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In test-cluster-worker-wait-server-close, remove unneeded 1-second delay and refactor to eliminate flakiness on FreeBSD. PR-URL: https://github.com/nodejs/node/pull/4616 Reviewed-By: Johan Bergström Reviewed-By: James M Snell --- .../test-cluster-worker-wait-server-close.js | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/test/parallel/test-cluster-worker-wait-server-close.js b/test/parallel/test-cluster-worker-wait-server-close.js index 0488343cae..dfcc4a0de5 100644 --- a/test/parallel/test-cluster-worker-wait-server-close.js +++ b/test/parallel/test-cluster-worker-wait-server-close.js @@ -5,44 +5,44 @@ var assert = require('assert'); var cluster = require('cluster'); var net = require('net'); +var serverClosed = false; + if (cluster.isWorker) { - net.createServer(function(socket) { + var server = net.createServer(function(socket) { // Wait for any data, then close connection socket.write('.'); - socket.on('data', function discard() { - }); + socket.on('data', function discard() {}); }).listen(common.PORT, common.localhostIPv4); -} else if (cluster.isMaster) { - var connectionDone; - var ok; + server.once('close', function() { + serverClosed = true; + }); + // Although not typical, the worker process can exit before the disconnect + // event fires. Use this to keep the process open until the event has fired. + var keepOpen = setInterval(function() {}, 9999); + + // Check worker events and properties + process.once('disconnect', function() { + // disconnect should occur after socket close + assert(serverClosed); + clearInterval(keepOpen); + }); +} else if (cluster.isMaster) { // start worker var worker = cluster.fork(); + var socket; // Disconnect worker when it is ready worker.once('listening', function() { net.createConnection(common.PORT, common.localhostIPv4, function() { - var socket = this; + socket = this; this.on('data', function() { console.log('got data from client'); // socket definitely connected to worker if we got data worker.disconnect(); - setTimeout(function() { - socket.end(); - connectionDone = true; - }, 1000); + socket.end(); }); }); }); - - // Check worker events and properties - worker.once('disconnect', function() { - assert.ok(connectionDone, 'disconnect should occur after socket close'); - ok = true; - }); - - process.once('exit', function() { - assert.ok(ok); - }); }