Skip to content

Commit 3dd124a

Browse files
committed
test: fix race condition in test-child-process-bad-stdio
test-child-process-bad-stdio.js contains a race condition between a timeout in the test process and a timeout in the spawned child process. This commit addresses the race condition by having the child process wait indefinitely to be killed.
1 parent ed52ab9 commit 3dd124a

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

test/parallel/test-child-process-bad-stdio.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
// Flags: --expose-internals
33
const common = require('../common');
44

5-
if (process.argv[2] === 'child') {
5+
if (process.argv[2] === 'timeout') {
66
setTimeout(() => {}, common.platformTimeout(100));
77
return;
88
}
99

10+
if (process.argv[2] === 'interval') {
11+
setInterval(() => {}, 10_000);
12+
return;
13+
}
14+
1015
const assert = require('node:assert');
1116
const cp = require('node:child_process');
1217
const { mock, test } = require('node:test');
@@ -29,7 +34,8 @@ mock.method(ChildProcess.prototype, 'spawn', function() {
2934
});
3035

3136
function createChild(options, callback) {
32-
const [cmd, opts] = common.escapePOSIXShell`"${process.execPath}" "${__filename}" child`;
37+
const childType = options.child ?? 'timeout';
38+
const [cmd, opts] = common.escapePOSIXShell`"${process.execPath}" "${__filename}" ${childType}`;
3339
options = { ...options, env: { ...opts?.env, ...options.env } };
3440

3541
return cp.exec(cmd, options, common.mustCall(callback));
@@ -57,7 +63,8 @@ test('execution with an error event is handled', (_, done) => {
5763
});
5864

5965
test('execution with a killed process is handled', (_, done) => {
60-
createChild({ timeout: 1 }, (err, stdout, stderr) => {
66+
const timeout = common.platformTimeout(100);
67+
createChild({ child: 'interval', timeout }, (err, stdout, stderr) => {
6168
assert.strictEqual(err.killed, true);
6269
assert.strictEqual(stdout, '');
6370
assert.strictEqual(stderr, '');

0 commit comments

Comments
 (0)