Skip to content

Commit 2c7358e

Browse files
Trottdanielleadams
authored andcommitted
test: fix flaky sequential/test-fs-watch
Fixes: #36247 PR-URL: #36249 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent a2da7ba commit 2c7358e

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

test/sequential/test-fs-watch.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ const testDir = tmpdir.path;
4242

4343
tmpdir.refresh();
4444

45+
// Because macOS (and possibly other operating systems) can return a watcher
46+
// before it is actually watching, we need to repeat the operation to avoid
47+
// a race condition.
48+
function repeat(fn) {
49+
setImmediate(fn);
50+
const interval = setInterval(fn, 5000);
51+
return interval;
52+
}
53+
4554
{
4655
const filepath = path.join(testDir, 'watch.txt');
4756

@@ -54,12 +63,11 @@ tmpdir.refresh();
5463
if (expectFilePath) {
5564
assert.strictEqual(filename, 'watch.txt');
5665
}
66+
clearInterval(interval);
5767
watcher.close();
5868
}));
5969

60-
setImmediate(function() {
61-
fs.writeFileSync(filepath, 'world');
62-
});
70+
const interval = repeat(() => { fs.writeFileSync(filepath, 'world'); });
6371
}
6472

6573
{
@@ -76,12 +84,11 @@ tmpdir.refresh();
7684
if (expectFilePath) {
7785
assert.strictEqual(filename, 'hasOwnProperty');
7886
}
87+
clearInterval(interval);
7988
watcher.close();
8089
}));
8190

82-
setImmediate(function() {
83-
fs.writeFileSync(filepathAbs, 'pardner');
84-
});
91+
const interval = repeat(() => { fs.writeFileSync(filepathAbs, 'pardner'); });
8592
}
8693

8794
{
@@ -97,14 +104,15 @@ tmpdir.refresh();
97104
} else {
98105
assert.strictEqual(filename, null);
99106
}
107+
clearInterval(interval);
100108
watcher.close();
101109
}));
102110

103-
setImmediate(function() {
111+
const interval = repeat(() => {
112+
fs.rmSync(filepath, { force: true });
104113
const fd = fs.openSync(filepath, 'w');
105114
fs.closeSync(fd);
106115
});
107-
108116
}
109117

110118
// https://github.com/joyent/node/issues/2293 - non-persistent watcher should

0 commit comments

Comments
 (0)