Skip to content

Commit 098b478

Browse files
inoway46aduh95
authored andcommitted
test: deflake test-watch-mode-files_watcher
Some test cases made a single change right after creating a watcher. On macOS, libuv starts the FSEvents stream asynchronously, so that change could be missed and the test hung until it timed out. Keep making the change until the watcher reports it, as the other cases in the file already do. Refs: nodejs/reliability#1715 Refs: nodejs/reliability#1644 Signed-off-by: inoway46 <inoueyuya416@gmail.com> PR-URL: #67298 Refs: https://github.com/nodejs/node/actions/runs/42177414954 Reviewed-By: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
1 parent 136ae6f commit 098b478

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

‎test/parallel/test-watch-mode-files_watcher.mjs‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ describe('watch mode file watcher', () => {
3737
afterEach(() => watcher.clear());
3838

3939
let counter = 0;
40+
// A newly created watcher might not report changes right away (e.g. on
41+
// macOS the FSEvents stream is started asynchronously), so a single write
42+
// can be missed. Keep writing until a change is reported.
4043
function writeAndWaitForChanges(watcher, file) {
4144
return new Promise((resolve) => {
4245
const interval = setInterval(() => writeFileSync(file, `write ${counter++}`), 100);
@@ -93,19 +96,29 @@ describe('watch mode file watcher', () => {
9396
['--watch', file1],
9497
{ stdio: ['ignore', 'pipe', 'ignore'] });
9598
let completeCount = 0;
99+
let appendInterval;
96100
for await (const line of createInterface(child.stdout)) {
97101
if (!line.startsWith('Completed running')) {
98102
continue;
99103
}
100104
completeCount++;
101105
if (completeCount === 1) {
106+
// The watcher of the child might not report changes yet, so keep
107+
// changing the file until it is reloaded. The interval is longer than
108+
// the default debounce of watch mode so the restart is not deferred.
102109
appendFileSync(file1, '\n // append 1');
110+
appendInterval = setInterval(() => appendFileSync(file1, '\n // append'),
111+
common.platformTimeout(1000));
103112
}
104113
// The file is reloaded due to file watching
105114
if (completeCount === 2) {
115+
clearInterval(appendInterval);
106116
child.kill();
107117
}
108118
}
119+
// The child might have exited before the reload.
120+
clearInterval(appendInterval);
121+
assert.strictEqual(completeCount, 2);
109122
});
110123

111124
it('should debounce changes', async () => {
@@ -135,6 +148,7 @@ describe('watch mode file watcher', () => {
135148
watcher.filterFile(file);
136149
files.push(file);
137150
}
151+
await writeAndWaitForChanges(watcher, files[0]);
138152

139153
files.forEach((file) => writeFileSync(file, '1'));
140154
files.forEach((file) => writeFileSync(file, '2'));
@@ -203,10 +217,7 @@ describe('watch mode file watcher', () => {
203217
const file = tmpdir.resolve('file5');
204218
watcher.watchPath(tmpdir.path);
205219

206-
const changed = once(watcher, 'changed');
207-
await setTimeout(common.platformTimeout(100)); // avoid throttling
208-
writeFileSync(file, 'changed');
209-
await changed;
220+
await writeAndWaitForChanges(watcher, file);
210221
assert.strictEqual(changesCount, 1);
211222
});
212223

0 commit comments

Comments
 (0)