Skip to content

Commit 03ce84d

Browse files
committed
test: fix cluster-worker-wait-server-close races
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 <[email protected]> Reviewed-By: Johan Bergström <[email protected]> Reviewed-By: Rod Vagg <[email protected]> PR-URL: #1953 Fixes: #1933 Fixes: #1400
1 parent 2a7fd0a commit 03ce84d

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

test/parallel/test-cluster-worker-wait-server-close.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ var net = require('net');
88
if (cluster.isWorker) {
99
net.createServer(function(socket) {
1010
// Wait for any data, then close connection
11-
socket.on('data', socket.end.bind(socket));
11+
socket.write('.');
12+
socket.on('data', function discard() {
13+
});
1214
}).listen(common.PORT, common.localhostIPv4);
1315
} else if (cluster.isMaster) {
1416

@@ -22,11 +24,15 @@ if (cluster.isWorker) {
2224
worker.once('listening', function() {
2325
net.createConnection(common.PORT, common.localhostIPv4, function() {
2426
var socket = this;
25-
worker.disconnect();
26-
setTimeout(function() {
27-
socket.write('.');
28-
connectionDone = true;
29-
}, 1000);
27+
this.on('data', function() {
28+
console.log('got data from client');
29+
// socket definitely connected to worker if we got data
30+
worker.disconnect();
31+
setTimeout(function() {
32+
socket.end();
33+
connectionDone = true;
34+
}, 1000);
35+
});
3036
});
3137
});
3238

0 commit comments

Comments
 (0)