Skip to content

Commit 94f9424

Browse files
dario-piotrowiczRafaelGSS
authored andcommitted
test: deflake test-runner-watch-mode-kill-signal
PR-URL: #58952 Refs: nodejs/reliability#1250 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent 31252b9 commit 94f9424

File tree

1 file changed

+63
-54
lines changed

1 file changed

+63
-54
lines changed
Lines changed: 63 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as common from '../common/index.mjs';
22
import { describe, it, beforeEach } from 'node:test';
3-
import { once } from 'node:events';
43
import assert from 'node:assert';
54
import { spawn } from 'node:child_process';
65
import { writeFileSync } from 'node:fs';
@@ -19,27 +18,10 @@ if (common.isAIX) {
1918
}
2019

2120
const indexContents = `
22-
const { setTimeout } = require("timers/promises");
23-
(async () => {
24-
// Wait a few milliseconds to make sure that the
25-
// parent process has time to attach its listeners
26-
await setTimeout(200);
27-
28-
process.on('SIGTERM', () => {
29-
console.log('__SIGTERM received__');
30-
process.exit(123);
31-
});
32-
33-
process.on('SIGINT', () => {
34-
console.log('__SIGINT received__');
35-
process.exit(124);
36-
});
37-
38-
console.log('ready!');
39-
40-
// Wait for a long time (just to keep the process alive)
41-
await setTimeout(100_000_000);
42-
})();
21+
process.on('SIGTERM', () => { console.log('__SIGTERM received__'); process.exit(); });
22+
process.on('SIGINT', () => { console.log('__SIGINT received__'); process.exit(); });
23+
process.send('script ready');
24+
setTimeout(() => {}, 100_000);
4325
`;
4426

4527
let indexPath = '';
@@ -54,60 +36,82 @@ describe('test runner watch mode with --watch-kill-signal', () => {
5436
beforeEach(refresh);
5537

5638
it('defaults to SIGTERM', async () => {
57-
let currentRun = Promise.withResolvers();
58-
const child = spawn(process.execPath, ['--watch', indexPath], {
59-
cwd: tmpdir.path,
60-
});
39+
const child = spawn(
40+
process.execPath,
41+
['--watch', indexPath],
42+
{
43+
cwd: tmpdir.path,
44+
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
45+
}
46+
);
6147

6248
let stdout = '';
6349
child.stdout.on('data', (data) => {
64-
stdout += data.toString();
65-
currentRun.resolve();
50+
stdout += `${data}`;
51+
if (/__(SIGINT|SIGTERM) received__/.test(stdout)) {
52+
child.kill();
53+
}
6654
});
6755

68-
await currentRun.promise;
56+
child.on('message', (msg) => {
57+
if (msg === 'script ready') {
58+
writeFileSync(indexPath, indexContents);
59+
}
60+
});
6961

70-
currentRun = Promise.withResolvers();
71-
writeFileSync(indexPath, indexContents);
62+
await new Promise((resolve) =>
63+
child.on('exit', () => {
64+
resolve();
65+
})
66+
);
7267

73-
await currentRun.promise;
74-
child.kill();
75-
const [exitCode] = await once(child, 'exit');
7668
assert.match(stdout, /__SIGTERM received__/);
77-
assert.strictEqual(exitCode, 123);
69+
assert.doesNotMatch(stdout, /__SIGINT received__/);
7870
});
7971

8072
it('can be overridden (to SIGINT)', async () => {
81-
let currentRun = Promise.withResolvers();
82-
const child = spawn(process.execPath, ['--watch', '--watch-kill-signal', 'SIGINT', indexPath], {
83-
cwd: tmpdir.path,
84-
});
85-
let stdout = '';
73+
const child = spawn(
74+
process.execPath,
75+
['--watch', '--watch-kill-signal', 'SIGINT', indexPath],
76+
{
77+
cwd: tmpdir.path,
78+
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
79+
}
80+
);
8681

82+
let stdout = '';
8783
child.stdout.on('data', (data) => {
88-
stdout += data.toString();
89-
if (stdout.includes('ready!')) {
90-
currentRun.resolve();
84+
stdout += `${data}`;
85+
if (/__(SIGINT|SIGTERM) received__/.test(stdout)) {
86+
child.kill();
9187
}
9288
});
9389

94-
await currentRun.promise;
90+
child.on('message', (msg) => {
91+
if (msg === 'script ready') {
92+
writeFileSync(indexPath, indexContents);
93+
}
94+
});
9595

96-
currentRun = Promise.withResolvers();
97-
writeFileSync(indexPath, indexContents);
96+
await new Promise((resolve) =>
97+
child.on('exit', () => {
98+
resolve();
99+
})
100+
);
98101

99-
await currentRun.promise;
100-
child.kill();
101-
const [exitCode] = await once(child, 'exit');
102102
assert.match(stdout, /__SIGINT received__/);
103-
assert.strictEqual(exitCode, 124);
103+
assert.doesNotMatch(stdout, /__SIGTERM received__/);
104104
});
105105

106106
it('errors if an invalid signal is provided', async () => {
107107
const currentRun = Promise.withResolvers();
108-
const child = spawn(process.execPath, ['--watch', '--watch-kill-signal', 'invalid_signal', indexPath], {
109-
cwd: tmpdir.path,
110-
});
108+
const child = spawn(
109+
process.execPath,
110+
['--watch', '--watch-kill-signal', 'invalid_signal', indexPath],
111+
{
112+
cwd: tmpdir.path,
113+
}
114+
);
111115
let stdout = '';
112116

113117
child.stderr.on('data', (data) => {
@@ -117,6 +121,11 @@ describe('test runner watch mode with --watch-kill-signal', () => {
117121

118122
await currentRun.promise;
119123

120-
assert.match(stdout, new RegExp(/TypeError \[ERR_UNKNOWN_SIGNAL\]: Unknown signal: invalid_signal/));
124+
assert.match(
125+
stdout,
126+
new RegExp(
127+
/TypeError \[ERR_UNKNOWN_SIGNAL\]: Unknown signal: invalid_signal/
128+
)
129+
);
121130
});
122131
});

0 commit comments

Comments
 (0)