Skip to content

Commit 4000e0e

Browse files
TrottFishrock123
authored andcommitted
test: fix flaky test-fs-watch-encoding on OS X
PR-URL: #7356 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]>
1 parent 6d9549c commit 4000e0e

File tree

1 file changed

+43
-22
lines changed

1 file changed

+43
-22
lines changed

Diff for: test/parallel/test-fs-watch-encoding.js

+43-22
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,75 @@
11
'use strict';
22

3+
// This test is a bit more complicated than it ideally needs to be to work
4+
// around issues on OS X and SmartOS.
5+
//
6+
// On OS X, watch events are subject to peculiar timing oddities such that an
7+
// event might fire out of order. The synchronous refreshing of the tmp
8+
// directory might trigger an event on the watchers that are instantiated after
9+
// it!
10+
//
11+
// On SmartOS, the watch events fire but the filename is null.
12+
313
const common = require('../common');
414
const fs = require('fs');
515
const path = require('path');
6-
const assert = require('assert');
7-
8-
if (common.isFreeBSD) {
9-
common.skip('Test currently not working on FreeBSD');
10-
return;
11-
}
1216

1317
common.refreshTmpDir();
1418

1519
const fn = '新建文夹件.txt';
1620
const a = path.join(common.tmpDir, fn);
1721

22+
const watchers = new Set();
23+
24+
function registerWatcher(watcher) {
25+
watchers.add(watcher);
26+
}
27+
28+
function unregisterWatcher(watcher) {
29+
watcher.close();
30+
watchers.delete(watcher);
31+
if (watchers.size === 0) {
32+
clearInterval(interval);
33+
}
34+
}
35+
1836
const watcher1 = fs.watch(
1937
common.tmpDir,
2038
{encoding: 'hex'},
2139
(event, filename) => {
22-
if (filename)
23-
assert.equal(filename, 'e696b0e5bbbae69687e5a4b9e4bbb62e747874');
24-
watcher1.close();
40+
if (['e696b0e5bbbae69687e5a4b9e4bbb62e747874', null].includes(filename))
41+
done(watcher1);
2542
}
2643
);
44+
registerWatcher(watcher1);
2745

2846
const watcher2 = fs.watch(
2947
common.tmpDir,
3048
(event, filename) => {
31-
if (filename)
32-
assert.equal(filename, fn);
33-
watcher2.close();
49+
if ([fn, null].includes(filename))
50+
done(watcher2);
3451
}
3552
);
53+
registerWatcher(watcher2);
3654

3755
const watcher3 = fs.watch(
3856
common.tmpDir,
3957
{encoding: 'buffer'},
4058
(event, filename) => {
41-
if (filename) {
42-
assert(filename instanceof Buffer);
43-
assert.equal(filename.toString('utf8'), fn);
44-
}
45-
watcher3.close();
59+
if (filename instanceof Buffer && filename.toString('utf8') === fn)
60+
done(watcher3);
61+
else if (filename === null)
62+
done(watcher3);
4663
}
4764
);
65+
registerWatcher(watcher3);
4866

49-
const fd = fs.openSync(a, 'w+');
50-
fs.closeSync(fd);
67+
const done = common.mustCall(unregisterWatcher, watchers.size);
5168

52-
process.on('exit', () => {
53-
fs.unlink(a);
54-
});
69+
// OS X and perhaps other systems can have surprising race conditions with
70+
// file events. So repeat the operation in case it is missed the first time.
71+
const interval = setInterval(() => {
72+
const fd = fs.openSync(a, 'w+');
73+
fs.closeSync(fd);
74+
fs.unlinkSync(a);
75+
}, common.platformTimeout(100));

0 commit comments

Comments
 (0)