Skip to content

Commit

Permalink
test: don't call process.exit() in debugger tests
Browse files Browse the repository at this point in the history
process.exit() tends to hide bugs, both in tests and node.js.
Rewrite the tests so that the event loop exits naturally.
  • Loading branch information
bnoordhuis committed Sep 6, 2013
1 parent aaf4f8d commit 81655a2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 27 deletions.
11 changes: 7 additions & 4 deletions test/fixtures/clustered-server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ var workersOnline = 0;

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();
Expand Down
14 changes: 2 additions & 12 deletions test/simple/test-debug-cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ var args = ['--debug', common.fixturesDir + '/clustered-server/app.js' ];
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];
Expand All @@ -36,21 +35,12 @@ child.stderr.on('data', function(data) {

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,
Expand All @@ -65,4 +55,4 @@ function assertOutputLines() {
assert.equal(outputLines.length, expectedLines.length)
for (var i = 0; i < expectedLines.length; i++)
assert.equal(outputLines[i], expectedLines[i]);
}
});
13 changes: 2 additions & 11 deletions test/simple/test-debug-port-cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,12 @@ child.stderr.on('data', function(data) {

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),
Expand All @@ -70,4 +61,4 @@ function assertOutputLines() {
assert.equal(outputLines.length, expectedLines.length)
for (var i = 0; i < expectedLines.length; i++)
assert.equal(outputLines[i], expectedLines[i]);
}
});

0 comments on commit 81655a2

Please sign in to comment.