Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: avoid test-cluster-master-kill flakiness #6531

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 21 additions & 24 deletions test/parallel/test-cluster-master-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
cluster.on('listening', function listeningEvent() {

Expand All @@ -39,10 +39,10 @@ if (cluster.isWorker) {
// Stop listening
cluster.removeListener('listening', listeningEvent);

// throw accidently error
// Throw accidental error
process.nextTick(function() {
console.error('about to throw');
throw new Error('accidently error');
throw new Error('accidental error');
});
}

Expand All @@ -68,8 +68,8 @@ if (cluster.isWorker) {
}
};

var existMaster = false;
var existWorker = false;
var masterExited = false;
var workersExited = false;

// List all workers
var workers = [];
Expand All @@ -89,36 +89,33 @@ if (cluster.isWorker) {
// When cluster is dead
master.on('exit', function(code) {

// Check that the cluster died accidently
existMaster = !!code;
// Check that the cluster died accidentally (non-zero exit code)
masterExited = !!code;

// Give the workers time to shut down
var timeout = 200;
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 pollWorkers = function() {
// When master is dead all workers should be dead too
var alive = false;
workers.forEach(function(pid) {
if (isAlive(pid)) {
alive = true;
}
});

// If a worker was alive this did not act as expected
existWorker = !alive;
}
if (alive) {
setTimeout(pollWorkers, 50);
} else {
workersExited = true;
}
};

// Loop indefinitely until worker exit
pollWorkers();
});

process.once('exit', function() {
var m = 'The master did not die after an error was throwed';
assert.ok(existMaster, m);
var m = 'The master did not die after an error was thrown';
assert.ok(masterExited, m);
m = 'The workers did not die after an error in the master';
assert.ok(existWorker, m);
assert.ok(workersExited, m);
});

}
21 changes: 8 additions & 13 deletions test/parallel/test-cluster-master-kill.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,21 @@ if (cluster.isWorker) {
var alive = true;
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);

// check worker process status
var timeout = 200;
if (common.isAix) {
// AIX needs more time due to default exit performance
timeout = 1000;
}
setTimeout(function() {
var pollWorker = function() {
alive = isAlive(pid);
}, timeout);
if (alive) {
setTimeout(pollWorker, 50);
}
};
// Loop indefinitely until worker exit.
pollWorker();
});

process.once('exit', function() {
// cleanup: kill the worker if alive
if (alive) {
process.kill(pid);
}

assert.equal(typeof pid, 'number', 'did not get worker pid info');
assert.equal(alive, false, 'worker was alive after master died');
});
Expand Down