mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
test: fix flaky test-watch-file
PR-URL: https://github.com/nodejs/node/pull/34420 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Myles Borins <myles.borins@gmail.com>
This commit is contained in:
@@ -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);
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user