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 <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Nitzan Uziely <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Yongsheng Zhang <[email protected]>
  • Loading branch information
lpinca authored and targos committed Nov 26, 2021
1 parent 878ba91 commit 009ab4d
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions test/parallel/test-fs-promises-watch.js
Original file line number Diff line number Diff line change
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 009ab4d

Please sign in to comment.