node/test/parallel/test-cluster-net-send.js
Brian White 3809126427 test: fix flaky cluster-net-send
Before this commit, it was possible on Windows for the server's
'connection' handler to be called *after* the client socket's
'connect' handler. This caused the 'message' event to be missed
and the test would never end (timing out in CI). This problem
was more easily reproducible on a low resource (slow CPU)
Windows (2012r2) installation.

This commit waits until both handlers have been called before
sending the handle to the master process.

Fixes: https://github.com/nodejs/node/issues/3957
PR-URL: https://github.com/nodejs/node/pull/4444
Reviewed-By: Rich Trott <rtrott@gmail.com>
2015-12-29 00:14:56 -05:00

57 lines
1.2 KiB
JavaScript

'use strict';
var common = require('../common');
var assert = require('assert');
var fork = require('child_process').fork;
var net = require('net');
if (process.argv[2] !== 'child') {
console.error('[%d] master', process.pid);
var worker = fork(__filename, ['child']);
var called = false;
worker.once('message', function(msg, handle) {
assert.equal(msg, 'handle');
assert.ok(handle);
worker.send('got');
handle.on('data', function(data) {
called = true;
assert.equal(data.toString(), 'hello');
});
handle.on('end', function() {
worker.kill();
});
});
process.once('exit', function() {
assert.ok(called);
});
} else {
console.error('[%d] worker', process.pid);
var socket;
var cbcalls = 0;
function socketConnected() {
if (++cbcalls === 2)
process.send('handle', socket);
}
var server = net.createServer(function(c) {
process.once('message', function(msg) {
assert.equal(msg, 'got');
c.end('hello');
});
socketConnected();
});
server.listen(common.PORT, function() {
socket = net.connect(common.PORT, '127.0.0.1', socketConnected);
});
process.on('disconnect', function() {
process.exit();
server.close();
});
}