From 3b12a8d6e7a053ba873de5a9b3c1d580ada294ac Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Fri, 26 May 2017 20:48:19 -0700 Subject: [PATCH] test: fix flaky test-fs-watchfile on macOS On macOS, a watcher created with fs.watch() does not necessarily start receiving events immediately. So it can miss a change by fs.writefile() if it comes very soon after the watcher is created. Fix test flakiness caused by this by using `setInterval()` to repeat the write action. PR-URL: https://github.com/nodejs/node/pull/13252 Fixes: https://github.com/nodejs/node/issues/13248 Reviewed-By: Refael Ackermann Reviewed-By: Luigi Pinca Reviewed-By: Benjamin Gruenbaum Reviewed-By: Colin Ihrig Reviewed-By: Michael Dawson Reviewed-By: James M Snell --- test/parallel/test-fs-watchfile.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-fs-watchfile.js b/test/parallel/test-fs-watchfile.js index fe22f93a10e..270a281ce31 100644 --- a/test/parallel/test-fs-watchfile.js +++ b/test/parallel/test-fs-watchfile.js @@ -72,12 +72,15 @@ if (common.isLinux || common.isOSX || common.isWindows || common.isAix) { if (err) assert.fail(err); fs.watch(dir, common.mustCall(function(eventType, filename) { + clearInterval(interval); this._handle.close(); assert.strictEqual(filename, 'foo.txt'); })); - fs.writeFile(`${dir}/foo.txt`, 'foo', common.mustCall(function(err) { - if (err) assert.fail(err); - })); + const interval = setInterval(() => { + fs.writeFile(`${dir}/foo.txt`, 'foo', common.mustCall(function(err) { + if (err) assert.fail(err); + })); + }, 1); })); }