Skip to content

Commit

Permalink
test: fix flaky test-watch-file
Browse files Browse the repository at this point in the history
PR-URL: #34420
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
  • Loading branch information
Trott authored and MylesBorins committed Jul 27, 2020
1 parent d6ee1fd commit eecb92c
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions test/pummel/test-watch-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ fs.closeSync(fs.openSync(f, 'w'));
let changes = 0;
function watchFile() {
fs.watchFile(f, (curr, prev) => {
// Make sure there is at least one watch event that shows a changed mtime.
if (curr.mtime <= prev.mtime) {
return;
}
changes++;
assert.notDeepStrictEqual(curr.mtime, prev.mtime);
fs.unwatchFile(f);
watchFile();
fs.unwatchFile(f);
Expand All @@ -44,10 +47,17 @@ function watchFile() {

watchFile();

function changeFile() {
const fd = fs.openSync(f, 'w+');
fs.writeSync(fd, 'xyz\n');
fs.closeSync(fd);
}

const fd = fs.openSync(f, 'w+');
fs.writeSync(fd, 'xyz\n');
fs.closeSync(fd);
changeFile();
const interval = setInterval(changeFile, 1000);
// Use unref() here so fs.watchFile() watcher is the only thing keeping the
// event loop open.
interval.unref();

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

0 comments on commit eecb92c

Please sign in to comment.