Skip to content

Commit 8cb306e

Browse files
Trottaddaleax
authored andcommitted
test: fix flaky test-watch-file
PR-URL: #34420 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Myles Borins <[email protected]>
1 parent ae60f50 commit 8cb306e

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

test/pummel/test-watch-file.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ fs.closeSync(fs.openSync(f, 'w'));
3434
let changes = 0;
3535
function watchFile() {
3636
fs.watchFile(f, (curr, prev) => {
37+
// Make sure there is at least one watch event that shows a changed mtime.
38+
if (curr.mtime <= prev.mtime) {
39+
return;
40+
}
3741
changes++;
38-
assert.notDeepStrictEqual(curr.mtime, prev.mtime);
3942
fs.unwatchFile(f);
4043
watchFile();
4144
fs.unwatchFile(f);
@@ -44,10 +47,17 @@ function watchFile() {
4447

4548
watchFile();
4649

50+
function changeFile() {
51+
const fd = fs.openSync(f, 'w+');
52+
fs.writeSync(fd, 'xyz\n');
53+
fs.closeSync(fd);
54+
}
4755

48-
const fd = fs.openSync(f, 'w+');
49-
fs.writeSync(fd, 'xyz\n');
50-
fs.closeSync(fd);
56+
changeFile();
57+
const interval = setInterval(changeFile, 1000);
58+
// Use unref() here so fs.watchFile() watcher is the only thing keeping the
59+
// event loop open.
60+
interval.unref();
5161

5262
process.on('exit', function() {
5363
assert.ok(changes > 0);

0 commit comments

Comments
 (0)