Skip to content

Commit 6b45bf3

Browse files
addaleaxBethGriggs
authored andcommitted
test: modernize test-cluster-master-error
Some stylistic changes to bring this more in line with what our tests currently look like, and add a note about general flakiness. PR-URL: #34685 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ricky Zhou <[email protected]>
1 parent 34430ab commit 6b45bf3

File tree

1 file changed

+8
-30
lines changed

1 file changed

+8
-30
lines changed

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

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,11 @@ const totalWorkers = 2;
2929
// Cluster setup
3030
if (cluster.isWorker) {
3131
const http = require('http');
32-
http.Server(() => {
33-
34-
}).listen(0, '127.0.0.1');
35-
32+
http.Server(() => {}).listen(0, '127.0.0.1');
3633
} else if (process.argv[2] === 'cluster') {
37-
3834
// Send PID to testcase process
3935
let forkNum = 0;
4036
cluster.on('fork', common.mustCall(function forkEvent(worker) {
41-
4237
// Send PID
4338
process.send({
4439
cmd: 'worker',
@@ -49,12 +44,11 @@ if (cluster.isWorker) {
4944
if (++forkNum === totalWorkers) {
5045
cluster.removeListener('fork', forkEvent);
5146
}
52-
}));
47+
}, totalWorkers));
5348

5449
// Throw accidental error when all workers are listening
5550
let listeningNum = 0;
5651
cluster.on('listening', common.mustCall(function listeningEvent() {
57-
5852
// When all workers are listening
5953
if (++listeningNum === totalWorkers) {
6054
// Stop listening
@@ -65,21 +59,16 @@ if (cluster.isWorker) {
6559
throw new Error('accidental error');
6660
});
6761
}
68-
69-
}));
62+
}, totalWorkers));
7063

7164
// Startup a basic cluster
7265
cluster.fork();
7366
cluster.fork();
74-
7567
} else {
7668
// This is the testcase
7769

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

80-
let masterExited = false;
81-
let workersExited = false;
82-
8372
// List all workers
8473
const workers = [];
8574

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

8978
// Handle messages from the cluster
9079
master.on('message', common.mustCall((data) => {
91-
9280
// Add worker pid to list and progress tracker
9381
if (data.cmd === 'worker') {
9482
workers.push(data.workerPID);
@@ -97,30 +85,20 @@ if (cluster.isWorker) {
9785

9886
// When cluster is dead
9987
master.on('exit', common.mustCall((code) => {
100-
10188
// Check that the cluster died accidentally (non-zero exit code)
102-
masterExited = !!code;
89+
assert.strictEqual(code, 1);
10390

91+
// XXX(addaleax): The fact that this uses raw PIDs makes the test inherently
92+
// flaky – another process might end up being started right after the
93+
// workers finished and receive the same PID.
10494
const pollWorkers = () => {
10595
// When master is dead all workers should be dead too
106-
let alive = false;
107-
workers.forEach((pid) => alive = common.isAlive(pid));
108-
if (alive) {
96+
if (workers.some((pid) => common.isAlive(pid))) {
10997
setTimeout(pollWorkers, 50);
110-
} else {
111-
workersExited = true;
11298
}
11399
};
114100

115101
// Loop indefinitely until worker exit
116102
pollWorkers();
117103
}));
118-
119-
process.once('exit', () => {
120-
assert(masterExited,
121-
'The master did not die after an error was thrown');
122-
assert(workersExited,
123-
'The workers did not die after an error in the master');
124-
});
125-
126104
}

0 commit comments

Comments
 (0)