From 7f3852c37615d472f416195b4b73191dd9c1050a Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 23 Feb 2024 03:00:42 +0100 Subject: [PATCH] test: ensure delay in recursive fs watch tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The recursive fs watch tests that mutate the watched folder immediately after fs.watch() returns are all flaking in the CI while the others that mutate the folder with a bit of delay aren't flaking. So this patch adds a bit of delay for the rest of the tests to deflake them. PR-URL: https://github.com/nodejs/node/pull/51842 Refs: https://github.com/nodejs/reliability/issues/790 Reviewed-By: Michaƫl Zasso Reviewed-By: Antoine du Hamel Reviewed-By: Luigi Pinca --- ...st-fs-watch-recursive-add-file-to-existing-subfolder.js | 5 ++++- .../test-fs-watch-recursive-add-file-to-new-folder.js | 7 +++++-- test/parallel/test-fs-watch-recursive-add-file.js | 5 ++++- test/parallel/test-fs-watch-recursive-assert-leaks.js | 7 ++++++- test/parallel/test-fs-watch-recursive-sync-write.js | 5 ++++- test/parallel/test-fs-watch-recursive-update-file.js | 5 ++++- 6 files changed, 27 insertions(+), 7 deletions(-) diff --git a/test/parallel/test-fs-watch-recursive-add-file-to-existing-subfolder.js b/test/parallel/test-fs-watch-recursive-add-file-to-existing-subfolder.js index 995c82743e4..628ca4b2fdf 100644 --- a/test/parallel/test-fs-watch-recursive-add-file-to-existing-subfolder.js +++ b/test/parallel/test-fs-watch-recursive-add-file-to-existing-subfolder.js @@ -48,7 +48,10 @@ watcher.on('change', function(event, filename) { } }); -fs.writeFileSync(childrenAbsolutePath, 'world'); +// Do the write with a delay to ensure that the OS is ready to notify us. +setTimeout(() => { + fs.writeFileSync(childrenAbsolutePath, 'world'); +}, common.platformTimeout(200)); process.once('exit', function() { assert(watcherClosed, 'watcher Object was not closed'); diff --git a/test/parallel/test-fs-watch-recursive-add-file-to-new-folder.js b/test/parallel/test-fs-watch-recursive-add-file-to-new-folder.js index 1d5f0098428..2f91c968f78 100644 --- a/test/parallel/test-fs-watch-recursive-add-file-to-new-folder.js +++ b/test/parallel/test-fs-watch-recursive-add-file-to-new-folder.js @@ -44,8 +44,11 @@ watcher.on('change', function(event, filename) { } }); -fs.mkdirSync(filePath); -fs.writeFileSync(childrenAbsolutePath, 'world'); +// Do the write with a delay to ensure that the OS is ready to notify us. +setTimeout(() => { + fs.mkdirSync(filePath); + fs.writeFileSync(childrenAbsolutePath, 'world'); +}, common.platformTimeout(200)); process.once('exit', function() { assert(watcherClosed, 'watcher Object was not closed'); diff --git a/test/parallel/test-fs-watch-recursive-add-file.js b/test/parallel/test-fs-watch-recursive-add-file.js index d03a4144ac8..27b933871cb 100644 --- a/test/parallel/test-fs-watch-recursive-add-file.js +++ b/test/parallel/test-fs-watch-recursive-add-file.js @@ -39,7 +39,10 @@ watcher.on('change', function(event, filename) { } }); -fs.writeFileSync(testFile, 'world'); +// Do the write with a delay to ensure that the OS is ready to notify us. +setTimeout(() => { + fs.writeFileSync(testFile, 'world'); +}, common.platformTimeout(200)); process.once('exit', function() { assert(watcherClosed, 'watcher Object was not closed'); diff --git a/test/parallel/test-fs-watch-recursive-assert-leaks.js b/test/parallel/test-fs-watch-recursive-assert-leaks.js index 9d178fcfe82..f5950e38ce2 100644 --- a/test/parallel/test-fs-watch-recursive-assert-leaks.js +++ b/test/parallel/test-fs-watch-recursive-assert-leaks.js @@ -42,4 +42,9 @@ watcher.on('change', common.mustCallAtLeast(async (event, filename) => { process.on('exit', function() { assert(watcherClosed, 'watcher Object was not closed'); }); -fs.writeFileSync(filePath, 'content'); + +// Do the write with a delay to ensure that the OS is ready to notify us. +(async () => { + await setTimeout(200); + fs.writeFileSync(filePath, 'content'); +})().then(common.mustCall()); diff --git a/test/parallel/test-fs-watch-recursive-sync-write.js b/test/parallel/test-fs-watch-recursive-sync-write.js index 38dce82fb11..ecc380d190e 100644 --- a/test/parallel/test-fs-watch-recursive-sync-write.js +++ b/test/parallel/test-fs-watch-recursive-sync-write.js @@ -32,4 +32,7 @@ const watcher = watch(tmpDir, { recursive: true }, common.mustCall((eventType, _ assert.strictEqual(join(tmpDir, _filename), filename); })); -writeFileSync(filename, 'foobar2'); +// Do the write with a delay to ensure that the OS is ready to notify us. +setTimeout(() => { + writeFileSync(filename, 'foobar2'); +}, common.platformTimeout(200)); diff --git a/test/parallel/test-fs-watch-recursive-update-file.js b/test/parallel/test-fs-watch-recursive-update-file.js index ee8e8fe52b4..7100b015ab2 100644 --- a/test/parallel/test-fs-watch-recursive-update-file.js +++ b/test/parallel/test-fs-watch-recursive-update-file.js @@ -39,4 +39,7 @@ watcher.on('change', common.mustCallAtLeast(function(event, filename) { } })); -fs.writeFileSync(testFile, 'hello'); +// Do the write with a delay to ensure that the OS is ready to notify us. +setTimeout(() => { + fs.writeFileSync(testFile, 'hello'); +}, common.platformTimeout(200));