Browse Source

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 <vieuxtech@gmail.com>
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
Reviewed-By: Rod Vagg <rod@vagg.org>
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
v2.3.1-release
Sam Roberts 10 years ago
parent
commit
03ce84dfa1
  1. 18
      test/parallel/test-cluster-worker-wait-server-close.js

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

@ -8,7 +8,9 @@ var net = require('net');
if (cluster.isWorker) { if (cluster.isWorker) {
net.createServer(function(socket) { net.createServer(function(socket) {
// Wait for any data, then close connection // 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); }).listen(common.PORT, common.localhostIPv4);
} else if (cluster.isMaster) { } else if (cluster.isMaster) {
@ -22,11 +24,15 @@ if (cluster.isWorker) {
worker.once('listening', function() { worker.once('listening', function() {
net.createConnection(common.PORT, common.localhostIPv4, function() { net.createConnection(common.PORT, common.localhostIPv4, function() {
var socket = this; var socket = this;
worker.disconnect(); this.on('data', function() {
setTimeout(function() { console.log('got data from client');
socket.write('.'); // socket definitely connected to worker if we got data
connectionDone = true; worker.disconnect();
}, 1000); setTimeout(function() {
socket.end();
connectionDone = true;
}, 1000);
});
}); });
}); });

Loading…
Cancel
Save