Skip to content

Commit 2259e5d

Browse files
stefanmbMyles Borins
authored and
Myles Borins
committed
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: #6531 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Andreas Madsen <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]>
1 parent 5f444ed commit 2259e5d

File tree

2 files changed

+29
-37
lines changed

2 files changed

+29
-37
lines changed

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

+21-24
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ if (cluster.isWorker) {
3030
}
3131
});
3232

33-
// Throw accidently error when all workers are listening
33+
// Throw accidental error when all workers are listening
3434
var listeningNum = 0;
3535
cluster.on('listening', function listeningEvent() {
3636

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

42-
// throw accidently error
42+
// Throw accidental error
4343
process.nextTick(function() {
4444
console.error('about to throw');
45-
throw new Error('accidently error');
45+
throw new Error('accidental error');
4646
});
4747
}
4848

@@ -68,8 +68,8 @@ if (cluster.isWorker) {
6868
}
6969
};
7070

71-
var existMaster = false;
72-
var existWorker = false;
71+
var masterExited = false;
72+
var workersExited = false;
7373

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

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

95-
// Give the workers time to shut down
96-
var timeout = 200;
97-
if (common.isAix) {
98-
// AIX needs more time due to default exit performance
99-
timeout = 1000;
100-
}
101-
setTimeout(checkWorkers, timeout);
102-
103-
function checkWorkers() {
104-
// When master is dead all workers should be dead to
95+
var pollWorkers = function() {
96+
// When master is dead all workers should be dead too
10597
var alive = false;
10698
workers.forEach(function(pid) {
10799
if (isAlive(pid)) {
108100
alive = true;
109101
}
110102
});
111-
112-
// If a worker was alive this did not act as expected
113-
existWorker = !alive;
114-
}
103+
if (alive) {
104+
setTimeout(pollWorkers, 50);
105+
} else {
106+
workersExited = true;
107+
}
108+
};
109+
110+
// Loop indefinitely until worker exit
111+
pollWorkers();
115112
});
116113

117114
process.once('exit', function() {
118-
var m = 'The master did not die after an error was throwed';
119-
assert.ok(existMaster, m);
115+
var m = 'The master did not die after an error was thrown';
116+
assert.ok(masterExited, m);
120117
m = 'The workers did not die after an error in the master';
121-
assert.ok(existWorker, m);
118+
assert.ok(workersExited, m);
122119
});
123120

124121
}

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

+8-13
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,21 @@ if (cluster.isWorker) {
5555
var alive = true;
5656
master.on('exit', function(code) {
5757

58-
// make sure that the master died by purpose
58+
// make sure that the master died on purpose
5959
assert.equal(code, 0);
6060

6161
// check worker process status
62-
var timeout = 200;
63-
if (common.isAix) {
64-
// AIX needs more time due to default exit performance
65-
timeout = 1000;
66-
}
67-
setTimeout(function() {
62+
var pollWorker = function() {
6863
alive = isAlive(pid);
69-
}, timeout);
64+
if (alive) {
65+
setTimeout(pollWorker, 50);
66+
}
67+
};
68+
// Loop indefinitely until worker exit.
69+
pollWorker();
7070
});
7171

7272
process.once('exit', function() {
73-
// cleanup: kill the worker if alive
74-
if (alive) {
75-
process.kill(pid);
76-
}
77-
7873
assert.equal(typeof pid, 'number', 'did not get worker pid info');
7974
assert.equal(alive, false, 'worker was alive after master died');
8075
});

0 commit comments

Comments
 (0)