From 45c30fe5003eddfd0380b1e3007c7618d3f10a29 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 23 Feb 2024 03:00:42 +0100 Subject: [PATCH 1/2] test: ensure delay in recursive fs watch tests 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. --- ...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 995c82743e49ea..628ca4b2fdf805 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 1d5f0098428c03..2f91c968f78a1a 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 d03a4144ac81bb..27b933871cb403 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 9d178fcfe8212b..f5950e38ce2201 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 38dce82fb115aa..7c7776a5a2beff 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'); +}); diff --git a/test/parallel/test-fs-watch-recursive-update-file.js b/test/parallel/test-fs-watch-recursive-update-file.js index ee8e8fe52b4374..7100b015ab2567 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)); From 535729ff593184acebf5b19799415875babbf4eb Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 23 Feb 2024 18:41:20 +0100 Subject: [PATCH 2/2] fixup! test: ensure delay in recursive fs watch tests Co-authored-by: Antoine du Hamel --- test/parallel/test-fs-watch-recursive-sync-write.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-fs-watch-recursive-sync-write.js b/test/parallel/test-fs-watch-recursive-sync-write.js index 7c7776a5a2beff..ecc380d190eee7 100644 --- a/test/parallel/test-fs-watch-recursive-sync-write.js +++ b/test/parallel/test-fs-watch-recursive-sync-write.js @@ -35,4 +35,4 @@ const watcher = watch(tmpDir, { recursive: true }, common.mustCall((eventType, _ // Do the write with a delay to ensure that the OS is ready to notify us. setTimeout(() => { writeFileSync(filename, 'foobar2'); -}); +}, common.platformTimeout(200));