Browse Source

test: avoid test-cluster-master-* flakiness

Removed reliance on worker exit before arbitrary timeout. Instead of failing
the test after 200 or 1000 ms wait indefinitely for child process exit. If
the test hangs the test harness global timeout will kick in and fail the test.

Note that if the orphaned children are not reaped correctly (in the absence
of init, e.g. Docker) the test will hang and the harness will fail it.

PR-URL: https://github.com/nodejs/node/pull/6531
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
v6.x
Stefan Budeanu 9 years ago
committed by Evan Lucas
parent
commit
916e694b2b
  1. 43
      test/parallel/test-cluster-master-error.js
  2. 21
      test/parallel/test-cluster-master-kill.js

43
test/parallel/test-cluster-master-error.js

@ -30,7 +30,7 @@ if (cluster.isWorker) {
} }
}); });
// Throw accidently error when all workers are listening // Throw accidental error when all workers are listening
var listeningNum = 0; var listeningNum = 0;
cluster.on('listening', function listeningEvent() { cluster.on('listening', function listeningEvent() {
@ -39,10 +39,10 @@ if (cluster.isWorker) {
// Stop listening // Stop listening
cluster.removeListener('listening', listeningEvent); cluster.removeListener('listening', listeningEvent);
// throw accidently error // Throw accidental error
process.nextTick(function() { process.nextTick(function() {
console.error('about to throw'); console.error('about to throw');
throw new Error('accidently error'); throw new Error('accidental error');
}); });
} }
@ -68,8 +68,8 @@ if (cluster.isWorker) {
} }
}; };
var existMaster = false; var masterExited = false;
var existWorker = false; var workersExited = false;
// List all workers // List all workers
var workers = []; var workers = [];
@ -89,36 +89,33 @@ if (cluster.isWorker) {
// When cluster is dead // When cluster is dead
master.on('exit', function(code) { master.on('exit', function(code) {
// Check that the cluster died accidently // Check that the cluster died accidentally (non-zero exit code)
existMaster = !!code; masterExited = !!code;
// Give the workers time to shut down var pollWorkers = function() {
var timeout = 200; // When master is dead all workers should be dead too
if (common.isAix) {
// AIX needs more time due to default exit performance
timeout = 1000;
}
setTimeout(checkWorkers, timeout);
function checkWorkers() {
// When master is dead all workers should be dead to
var alive = false; var alive = false;
workers.forEach(function(pid) { workers.forEach(function(pid) {
if (isAlive(pid)) { if (isAlive(pid)) {
alive = true; alive = true;
} }
}); });
if (alive) {
// If a worker was alive this did not act as expected setTimeout(pollWorkers, 50);
existWorker = !alive; } else {
workersExited = true;
} }
};
// Loop indefinitely until worker exit
pollWorkers();
}); });
process.once('exit', function() { process.once('exit', function() {
var m = 'The master did not die after an error was throwed'; var m = 'The master did not die after an error was thrown';
assert.ok(existMaster, m); assert.ok(masterExited, m);
m = 'The workers did not die after an error in the master'; m = 'The workers did not die after an error in the master';
assert.ok(existWorker, m); assert.ok(workersExited, m);
}); });
} }

21
test/parallel/test-cluster-master-kill.js

@ -55,26 +55,21 @@ if (cluster.isWorker) {
var alive = true; var alive = true;
master.on('exit', function(code) { master.on('exit', function(code) {
// make sure that the master died by purpose // make sure that the master died on purpose
assert.equal(code, 0); assert.equal(code, 0);
// check worker process status // check worker process status
var timeout = 200; var pollWorker = function() {
if (common.isAix) {
// AIX needs more time due to default exit performance
timeout = 1000;
}
setTimeout(function() {
alive = isAlive(pid); alive = isAlive(pid);
}, timeout);
});
process.once('exit', function() {
// cleanup: kill the worker if alive
if (alive) { if (alive) {
process.kill(pid); setTimeout(pollWorker, 50);
} }
};
// Loop indefinitely until worker exit.
pollWorker();
});
process.once('exit', function() {
assert.equal(typeof pid, 'number', 'did not get worker pid info'); assert.equal(typeof pid, 'number', 'did not get worker pid info');
assert.equal(alive, false, 'worker was alive after master died'); assert.equal(alive, false, 'worker was alive after master died');
}); });

Loading…
Cancel
Save