Skip to content

Commit

Permalink
Make the assersion in the test and not in the end
Browse files Browse the repository at this point in the history
as suggested by @bnoordhuis
  • Loading branch information
Bar Admoni committed Jan 22, 2021
1 parent ba01ed1 commit 0bb6dea
Showing 1 changed file with 12 additions and 59 deletions.
71 changes: 12 additions & 59 deletions test/parallel/test-cluster-worker-kill-signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,6 @@ if (cluster.isWorker) {

} else if (cluster.isMaster) {
const KILL_SIGNAL = 'SIGKILL';
const expectedResults = {
emitDisconnect: {
value: 1,
message: "the worker did not emit 'disconnect'"
},
emitExit: {
value: 1,
message: "the worker did not emit 'exit'"
},
state: {
value: 'disconnected',
message: 'the worker state is incorrect'
},
exitedAfter: {
value: false,
message: 'the .exitedAfterDisconnect flag is incorrect'
},
died: {
value: true,
message: 'the worker is still running'
},
exitCode: {
value: null,
message: 'the worker exited w/ incorrect exitCode'
},
signalCode: {
value: KILL_SIGNAL,
message: 'the worker exited w/ incorrect signalCode'
},
numberOfWorkers: {
value: 0,
message: 'the number of workers running is wrong'
},
};
const results = {
emitDisconnect: 0,
emitExit: 0
};

// Start worker
const worker = cluster.fork();
Expand All @@ -67,30 +29,21 @@ if (cluster.isWorker) {

// Check worker events and properties
worker.on('disconnect', common.mustCall(() => {
results.emitDisconnect += 1;
results.exitedAfter = worker.exitedAfterDisconnect;
results.state = worker.state;
}));
assert.strictEqual(worker.exitedAfterDisconnect, false);
assert.strictEqual(worker.state, 'disconnected');
}, 1));

// Check that the worker died
worker.once('exit', common.mustCall((exitCode, signalCode) => {
// Setting the results
results.exitCode = exitCode;
results.signalCode = signalCode;
results.emitExit += 1;
results.died = !common.isAlive(worker.process.pid);
results.numberOfWorkers = Object.keys(cluster.workers).length;
}));
const isWorkerProcessStillAlive = common.isAlive(worker.process.pid);
const numOfRunningWorkers = Object.keys(cluster.workers).length;

cluster.on('exit', common.mustCall(() => {
// Checking if the results are as expected
for (const [key, expected] of Object.entries(expectedResults)) {
const actual = results[key];
assert.strictEqual(exitCode, null);
assert.strictEqual(signalCode, KILL_SIGNAL);
assert.strictEqual(isWorkerProcessStillAlive, false);
assert.strictEqual(numOfRunningWorkers, 0);
}, 1));

assert.strictEqual(
actual, expected.value,
`${expected.message} [expected: ${expected.value} / actual: ${actual}]`
);
}
}));
// Check if the cluster was killed as well
cluster.on('exit', common.mustCall(() => {}, 1));
}

0 comments on commit 0bb6dea

Please sign in to comment.