Skip to content

Commit d1c2643

Browse files
committed
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>
1 parent be6f8f8 commit d1c2643

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

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

Lines changed: 12 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,16 +96,23 @@ 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
}
@@ -135,6 +145,7 @@ describe('watch mode file watcher', () => {
135145
watcher.filterFile(file);
136146
files.push(file);
137147
}
148+
await writeAndWaitForChanges(watcher, files[0]);
138149

139150
files.forEach((file) => writeFileSync(file, '1'));
140151
files.forEach((file) => writeFileSync(file, '2'));
@@ -183,10 +194,7 @@ describe('watch mode file watcher', () => {
183194
const file = tmpdir.resolve('file5');
184195
watcher.watchPath(tmpdir.path);
185196

186-
const changed = once(watcher, 'changed');
187-
await setTimeout(common.platformTimeout(100)); // avoid throttling
188-
writeFileSync(file, 'changed');
189-
await changed;
197+
await writeAndWaitForChanges(watcher, file);
190198
assert.strictEqual(changesCount, 1);
191199
});
192200

0 commit comments

Comments
 (0)