From 9e2e4437b91c62bfecce8970a04d362d7ecaa723 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Sun, 21 Nov 2021 16:54:38 +0100 Subject: [PATCH] test: deflake fs-promises-watch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change the contents of the file every 100 milliseconds until the watcher notices the change. This is already done in the callback based version of the test (`test-fs-watch.js`). Fixes: https://github.com/nodejs/node/issues/37637 PR-URL: https://github.com/nodejs/node/pull/40863 Reviewed-By: Michaƫl Zasso Reviewed-By: Rich Trott Reviewed-By: Nitzan Uziely Reviewed-By: James M Snell Reviewed-By: Yongsheng Zhang --- test/parallel/test-fs-promises-watch.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-fs-promises-watch.js b/test/parallel/test-fs-promises-watch.js index c8c2d9250bad03..31ab1aafd43e63 100644 --- a/test/parallel/test-fs-promises-watch.js +++ b/test/parallel/test-fs-promises-watch.js @@ -47,9 +47,11 @@ for (const testCase of kCases) { const content1 = Date.now() + testCase.fileName.toLowerCase().repeat(1e4); fs.writeFileSync(testCase.filePath, content1); + let interval; async function test() { const watcher = watch(testCase[testCase.field]); for await (const { eventType, filename } of watcher) { + clearInterval(interval); assert.strictEqual(['rename', 'change'].includes(eventType), true); assert.strictEqual(filename, testCase.fileName); break; @@ -64,10 +66,10 @@ for (const testCase of kCases) { // Long content so it's actually flushed. toUpperCase so there's real change. const content2 = Date.now() + testCase.fileName.toUpperCase().repeat(1e4); - setImmediate(() => { + interval = setInterval(() => { fs.writeFileSync(testCase.filePath, ''); fs.writeFileSync(testCase.filePath, content2); - }); + }, 100); test().then(common.mustCall()); }