Skip to content

Commit 4d88f9d

Browse files
committed
fixup! test: fix flaky test-watch-mode-kill-signal-*
1 parent 5e0933e commit 4d88f9d

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
process.on('SIGTERM', () => { console.log('__SIGTERM received__'); process.exit(); });
2-
process.on('SIGINT', () => { console.log('__SIGINT received__'); process.exit(); });
3-
process.send('script ready');
1+
process.on('SIGTERM', () => {
2+
console.log(`__SIGTERM received__ ${process.pid}`);
3+
process.exit();
4+
});
5+
process.on('SIGINT', () => {
6+
console.log(`__SIGINT received__ ${process.pid}`);
7+
process.exit();
8+
});
9+
process.send(`script ready ${process.pid}`);
410
setTimeout(() => {}, 100_000);

test/parallel/test-watch-mode-kill-signal-default.mjs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ const child = spawn(
2626
);
2727

2828
let stdout = '';
29+
let firstGrandchildPid;
2930
child.stdout.on('data', (data) => {
3031
const dataStr = data.toString();
3132
console.log(`[STDOUT] ${dataStr}`);
3233
stdout += `${dataStr}`;
33-
if (/__(SIGINT|SIGTERM) received__/.test(stdout)) {
34-
console.log(`[PARENT] Sending kill signal to child process: ${child.pid}`);
34+
const match = dataStr.match(/__(SIGINT|SIGTERM) received__ (\d+)/);
35+
if (match && match[2] === firstGrandchildPid) {
36+
console.log(`[PARENT] Sending kill signal to watcher process: ${child.pid}`);
3537
child.kill();
3638
}
3739
});
@@ -43,16 +45,18 @@ child.stdout.on('data', (data) => {
4345
// end up in an infinite loop and never receive the stdout of the grandchildren in time.
4446
// Only write once to verify the first grandchild process receives the expected signal.
4547
// We don't care about the subsequent grandchild processes.
46-
let written = false;
4748
child.on('message', (msg) => {
4849
console.log(`[MESSAGE]`, msg);
49-
if (msg === 'script ready' && !written) {
50-
writeFileSync(indexPath, indexContents);
51-
written = true;
50+
if (!firstGrandchildPid && typeof msg === 'string') {
51+
const match = msg.match(/script ready (\d+)/);
52+
if (match) {
53+
firstGrandchildPid = match[1]; // This is the first grandchild
54+
writeFileSync(indexPath, indexContents);
55+
}
5256
}
5357
});
5458

5559
await once(child, 'exit');
5660

57-
assert.match(stdout, /__SIGTERM received__/);
58-
assert.doesNotMatch(stdout, /__SIGINT received__/);
61+
assert.match(stdout, new RegExp(`__SIGTERM received__ ${firstGrandchildPid}`));
62+
assert.doesNotMatch(stdout, new RegExp(`__SIGINT received__ ${firstGrandchildPid}`));

test/parallel/test-watch-mode-kill-signal-override.mjs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ const child = spawn(
2727
);
2828

2929
let stdout = '';
30+
let firstGrandchildPid;
3031
child.stdout.on('data', (data) => {
3132
const dataStr = data.toString();
3233
console.log(`[STDOUT] ${dataStr}`);
3334
stdout += `${dataStr}`;
34-
if (/__(SIGINT|SIGTERM) received__/.test(stdout)) {
35-
console.log(`[PARENT] Sending kill signal to child process: ${child.pid}`);
35+
const match = dataStr.match(/__(SIGINT|SIGTERM) received__ (\d+)/);
36+
if (match && match[2] === firstGrandchildPid) {
37+
console.log(`[PARENT] Sending kill signal to watcher process: ${child.pid}`);
3638
child.kill();
3739
}
3840
});
@@ -44,16 +46,21 @@ child.stdout.on('data', (data) => {
4446
// end up in an infinite loop and never receive the stdout of the grandchildren in time.
4547
// Only write once to verify the first grandchild process receives the expected signal.
4648
// We don't care about the subsequent grandchild processes.
47-
let written = false;
4849
child.on('message', (msg) => {
4950
console.log(`[MESSAGE]`, msg);
50-
if (msg === 'script ready' && !written) {
51-
writeFileSync(indexPath, indexContents);
52-
written = true;
51+
if (!firstGrandchildPid && typeof msg === 'string') {
52+
const match = msg.match(/script ready (\d+)/);
53+
if (match) {
54+
firstGrandchildPid = match[1]; // This is the first grandchild
55+
writeFileSync(indexPath, indexContents);
56+
}
5357
}
5458
});
5559

5660
await once(child, 'exit');
5761

58-
assert.match(stdout, /__SIGINT received__/);
59-
assert.doesNotMatch(stdout, /__SIGTERM received__/);
62+
// The second grandchild, if there is one, could receive SIGTERM if it's killed as a
63+
// consequence of the parent being killed in this process instead of being killed by the
64+
// parent for file changes. Here we only care about the first grandchild.
65+
assert.match(stdout, new RegExp(`__SIGINT received__ ${firstGrandchildPid}`));
66+
assert.doesNotMatch(stdout, new RegExp(`__SIGTERM received__ ${firstGrandchildPid}`));

0 commit comments

Comments
 (0)