Skip to content

Commit 03eecd9

Browse files
committed
test: fix flaky test-watch-mode-kill-signal-*
After the write triggers a restart of the grandchild, the newly spawned second grandchild can post another 'script ready' message before the stdout from the first grandchild is relayed by the watcher and processed by this parent process to kill the watcher. If we write again and trigger another restart, we can end up in an infinite loop and never receive the stdout of the grandchildren in time. Only write once to verify the first grandchild process receives the expected signal. We don't care about the subsequent grandchild processes.
1 parent 3c8c1ef commit 03eecd9

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,28 @@ const child = spawn(
2727

2828
let stdout = '';
2929
child.stdout.on('data', (data) => {
30-
stdout += `${data}`;
30+
let dataStr = data.toString();
31+
console.log(`[STDOUT] ${dataStr}`);
32+
stdout += `${dataStr}`;
3133
if (/__(SIGINT|SIGTERM) received__/.test(stdout)) {
34+
console.log(`[PARENT] Sending kill signal to child process: ${child.pid}`);
3235
child.kill();
3336
}
3437
});
3538

39+
// After the write triggers a restart of the grandchild, the newly spawned second
40+
// grandchild can post another 'script ready' message before the stdout from the first
41+
// grandchild is relayed by the watcher and processed by this parent process to kill
42+
// the watcher. If we write again and trigger another restart, we can
43+
// end up in an infinite loop and never receive the stdout of the grandchildren in time.
44+
// Only write once to verify the first grandchild process receives the expected signal.
45+
// We don't care about the subsequent grandchild processes.
46+
let written = false;
3647
child.on('message', (msg) => {
37-
if (msg === 'script ready') {
48+
console.log(`[MESSAGE]`, msg);
49+
if (msg === 'script ready' && !written) {
3850
writeFileSync(indexPath, indexContents);
51+
written = true;
3952
}
4053
});
4154

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,28 @@ const child = spawn(
2828

2929
let stdout = '';
3030
child.stdout.on('data', (data) => {
31-
stdout += `${data}`;
31+
let dataStr = data.toString();
32+
console.log(`[STDOUT] ${dataStr}`);
33+
stdout += `${dataStr}`;
3234
if (/__(SIGINT|SIGTERM) received__/.test(stdout)) {
35+
console.log(`[PARENT] Sending kill signal to child process: ${child.pid}`);
3336
child.kill();
3437
}
3538
});
3639

40+
// After the write triggers a restart of the grandchild, the newly spawned second
41+
// grandchild can post another 'script ready' message before the stdout from the first
42+
// grandchild is relayed by the watcher and processed by this parent process to kill
43+
// the watcher. If we write again and trigger another restart, we can
44+
// end up in an infinite loop and never receive the stdout of the grandchildren in time.
45+
// Only write once to verify the first grandchild process receives the expected signal.
46+
// We don't care about the subsequent grandchild processes.
47+
let written = false;
3748
child.on('message', (msg) => {
38-
if (msg === 'script ready') {
49+
console.log(`[MESSAGE]`, msg);
50+
if (msg === 'script ready' && !written) {
3951
writeFileSync(indexPath, indexContents);
52+
written = true;
4053
}
4154
});
4255

0 commit comments

Comments
 (0)