process.exit() tends to hide bugs, both in tests and node.js.
Rewrite the tests so that the event loop exits naturally.
if (cluster.isMaster) {
cluster.on('online', function() {
- workersOnline++;
- if (workersOnline == NUMBER_OF_WORKERS)
- console.error('all workers are running');
- });
+ if (++workersOnline === NUMBER_OF_WORKERS) {
+ console.error('all workers are running');
+ for (var key in cluster.workers) {
+ cluster.workers[key].disconnect();
+ }
+ }
+ });
for (var i = 0; i < NUMBER_OF_WORKERS; i++) {
cluster.fork();
var child = spawn(process.execPath, args);
var outputLines = [];
-
child.stderr.on('data', function(data) {
var lines = data.toString().replace(/\r/g, '').trim().split('\n');
var line = lines[0];
if (line === 'all workers are running') {
assertOutputLines();
- process.exit();
} else {
outputLines = outputLines.concat(lines);
}
});
-setTimeout(function testTimedOut() {
- assert(false, 'test timed out.');
-}, 3000);
-
-process.on('exit', function() {
- child.kill();
-});
-
-function assertOutputLines() {
+var assertOutputLines = common.mustCall(function() {
var expectedLines = [
'debugger listening on port ' + 5858,
'debugger listening on port ' + 5859,
assert.equal(outputLines.length, expectedLines.length)
for (var i = 0; i < expectedLines.length; i++)
assert.equal(outputLines[i], expectedLines[i]);
-}
+});
if (line === 'all workers are running') {
assertOutputLines();
- process.exit();
} else {
outputLines = outputLines.concat(lines);
}
});
-setTimeout(function testTimedOut() {
- assert(false, 'test timed out.');
-}, 3000);
-
-process.on('exit', function() {
- child.kill();
-});
-
-function assertOutputLines() {
+var assertOutputLines = common.mustCall(function() {
var expectedLines = [
'debugger listening on port ' + port,
'debugger listening on port ' + (port+1),
assert.equal(outputLines.length, expectedLines.length)
for (var i = 0; i < expectedLines.length; i++)
assert.equal(outputLines[i], expectedLines[i]);
-}
+});