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: modernize test-cluster-master-error #34685

Closed
wants to merge 1 commit into from
Closed
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
38 changes: 8 additions & 30 deletions test/parallel/test-cluster-master-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,11 @@ const totalWorkers = 2;
// Cluster setup
if (cluster.isWorker) {
const http = require('http');
http.Server(() => {

}).listen(0, '127.0.0.1');

http.Server(() => {}).listen(0, '127.0.0.1');
} else if (process.argv[2] === 'cluster') {

// Send PID to testcase process
let forkNum = 0;
cluster.on('fork', common.mustCall(function forkEvent(worker) {

// Send PID
process.send({
cmd: 'worker',
Expand All @@ -49,12 +44,11 @@ if (cluster.isWorker) {
if (++forkNum === totalWorkers) {
cluster.removeListener('fork', forkEvent);
}
}));
}, totalWorkers));

// Throw accidental error when all workers are listening
let listeningNum = 0;
cluster.on('listening', common.mustCall(function listeningEvent() {

// When all workers are listening
if (++listeningNum === totalWorkers) {
// Stop listening
Expand All @@ -65,21 +59,16 @@ if (cluster.isWorker) {
throw new Error('accidental error');
});
}

}));
}, totalWorkers));

// Startup a basic cluster
cluster.fork();
cluster.fork();

} else {
// This is the testcase

const fork = require('child_process').fork;

let masterExited = false;
let workersExited = false;

// List all workers
const workers = [];

Expand All @@ -88,7 +77,6 @@ if (cluster.isWorker) {

// Handle messages from the cluster
master.on('message', common.mustCall((data) => {

// Add worker pid to list and progress tracker
if (data.cmd === 'worker') {
workers.push(data.workerPID);
Expand All @@ -97,30 +85,20 @@ if (cluster.isWorker) {

// When cluster is dead
master.on('exit', common.mustCall((code) => {
jasnell marked this conversation as resolved.
Show resolved Hide resolved

// Check that the cluster died accidentally (non-zero exit code)
masterExited = !!code;
assert.strictEqual(code, 1);

// XXX(addaleax): The fact that this uses raw PIDs makes the test inherently
// flaky – another process might end up being started right after the
// workers finished and receive the same PID.
const pollWorkers = () => {
// When master is dead all workers should be dead too
let alive = false;
workers.forEach((pid) => alive = common.isAlive(pid));
if (alive) {
if (workers.some((pid) => common.isAlive(pid))) {
setTimeout(pollWorkers, 50);
} else {
workersExited = true;
}
};

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

process.once('exit', () => {
assert(masterExited,
'The master did not die after an error was thrown');
assert(workersExited,
'The workers did not die after an error in the master');
});

}