From 03ce84dfa13a1cc075bb45772a8a566213148939 Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Thu, 11 Jun 2015 14:43:09 -0700 Subject: [PATCH] test: fix cluster-worker-wait-server-close races MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Wait for data to arrive from worker before doing a disconnect. Without this, whether the disconnect arrives at the worker before the master accepts and forwards the connection descriptor to the worker is a race. Reviewed-By: Sam Roberts Reviewed-By: Johan Bergström Reviewed-By: Rod Vagg PR-URL: https://github.com/nodejs/io.js/pull/1953 Fixes: https://github.com/nodejs/io.js/issues/1933 Fixes: https://github.com/nodejs/io.js/pull/1400 --- .../test-cluster-worker-wait-server-close.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/test/parallel/test-cluster-worker-wait-server-close.js b/test/parallel/test-cluster-worker-wait-server-close.js index 0f1836c7eccaa2..0488343cae3365 100644 --- a/test/parallel/test-cluster-worker-wait-server-close.js +++ b/test/parallel/test-cluster-worker-wait-server-close.js @@ -8,7 +8,9 @@ var net = require('net'); if (cluster.isWorker) { net.createServer(function(socket) { // Wait for any data, then close connection - socket.on('data', socket.end.bind(socket)); + socket.write('.'); + socket.on('data', function discard() { + }); }).listen(common.PORT, common.localhostIPv4); } else if (cluster.isMaster) { @@ -22,11 +24,15 @@ if (cluster.isWorker) { worker.once('listening', function() { net.createConnection(common.PORT, common.localhostIPv4, function() { var socket = this; - worker.disconnect(); - setTimeout(function() { - socket.write('.'); - connectionDone = true; - }, 1000); + 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); + }); }); });