Skip to content

Commit

Permalink
test: deflake fs-promises-watch
Browse files Browse the repository at this point in the history
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: #37637

PR-URL: #40863
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Nitzan Uziely <linkgoron@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
  • Loading branch information
lpinca authored and danielleadams committed Feb 1, 2022
1 parent b54d53b commit 9e2e443
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions test/parallel/test-fs-promises-watch.js
Expand Up @@ -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;
Expand All @@ -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());
}
Expand Down

0 comments on commit 9e2e443

Please sign in to comment.