diff options
author | Rich Trott <rtrott@gmail.com> | 2020-11-24 06:09:45 -0800 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2020-11-27 05:00:33 -0800 |
commit | 8ba65b550f21adac9b3de9f595907baf0f213fdb (patch) | |
tree | a431294f8272c77930c075647c520faf15ee0864 /test/sequential | |
parent | 0a23d65a629596935d525531fc15811538c1664c (diff) | |
download | node-new-8ba65b550f21adac9b3de9f595907baf0f213fdb.tar.gz |
test: fix flaky sequential/test-fs-watch
Fixes: https://github.com/nodejs/node/issues/36247
PR-URL: https://github.com/nodejs/node/pull/36249
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Diffstat (limited to 'test/sequential')
-rw-r--r-- | test/sequential/test-fs-watch.js | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/test/sequential/test-fs-watch.js b/test/sequential/test-fs-watch.js index 8c543a2a17..c5db585f3f 100644 --- a/test/sequential/test-fs-watch.js +++ b/test/sequential/test-fs-watch.js @@ -42,6 +42,15 @@ const testDir = tmpdir.path; tmpdir.refresh(); +// Because macOS (and possibly other operating systems) can return a watcher +// before it is actually watching, we need to repeat the operation to avoid +// a race condition. +function repeat(fn) { + setImmediate(fn); + const interval = setInterval(fn, 5000); + return interval; +} + { const filepath = path.join(testDir, 'watch.txt'); @@ -54,12 +63,11 @@ tmpdir.refresh(); if (expectFilePath) { assert.strictEqual(filename, 'watch.txt'); } + clearInterval(interval); watcher.close(); })); - setImmediate(function() { - fs.writeFileSync(filepath, 'world'); - }); + const interval = repeat(() => { fs.writeFileSync(filepath, 'world'); }); } { @@ -76,12 +84,11 @@ tmpdir.refresh(); if (expectFilePath) { assert.strictEqual(filename, 'hasOwnProperty'); } + clearInterval(interval); watcher.close(); })); - setImmediate(function() { - fs.writeFileSync(filepathAbs, 'pardner'); - }); + const interval = repeat(() => { fs.writeFileSync(filepathAbs, 'pardner'); }); } { @@ -97,14 +104,15 @@ tmpdir.refresh(); } else { assert.strictEqual(filename, null); } + clearInterval(interval); watcher.close(); })); - setImmediate(function() { + const interval = repeat(() => { + fs.rmSync(filepath, { force: true }); const fd = fs.openSync(filepath, 'w'); fs.closeSync(fd); }); - } // https://github.com/joyent/node/issues/2293 - non-persistent watcher should |