|
1 | 1 | 'use strict';
|
2 | 2 |
|
| 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 | + |
3 | 13 | const common = require('../common');
|
4 | 14 | const fs = require('fs');
|
5 | 15 | 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 |
| -} |
12 | 16 |
|
13 | 17 | common.refreshTmpDir();
|
14 | 18 |
|
15 | 19 | const fn = '新建文夹件.txt';
|
16 | 20 | const a = path.join(common.tmpDir, fn);
|
17 | 21 |
|
| 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 | + |
18 | 36 | const watcher1 = fs.watch(
|
19 | 37 | common.tmpDir,
|
20 | 38 | {encoding: 'hex'},
|
21 | 39 | (event, filename) => {
|
22 |
| - if (filename) |
23 |
| - assert.equal(filename, 'e696b0e5bbbae69687e5a4b9e4bbb62e747874'); |
24 |
| - watcher1.close(); |
| 40 | + if (['e696b0e5bbbae69687e5a4b9e4bbb62e747874', null].includes(filename)) |
| 41 | + done(watcher1); |
25 | 42 | }
|
26 | 43 | );
|
| 44 | +registerWatcher(watcher1); |
27 | 45 |
|
28 | 46 | const watcher2 = fs.watch(
|
29 | 47 | common.tmpDir,
|
30 | 48 | (event, filename) => {
|
31 |
| - if (filename) |
32 |
| - assert.equal(filename, fn); |
33 |
| - watcher2.close(); |
| 49 | + if ([fn, null].includes(filename)) |
| 50 | + done(watcher2); |
34 | 51 | }
|
35 | 52 | );
|
| 53 | +registerWatcher(watcher2); |
36 | 54 |
|
37 | 55 | const watcher3 = fs.watch(
|
38 | 56 | common.tmpDir,
|
39 | 57 | {encoding: 'buffer'},
|
40 | 58 | (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); |
46 | 63 | }
|
47 | 64 | );
|
| 65 | +registerWatcher(watcher3); |
48 | 66 |
|
49 |
| -const fd = fs.openSync(a, 'w+'); |
50 |
| -fs.closeSync(fd); |
| 67 | +const done = common.mustCall(unregisterWatcher, watchers.size); |
51 | 68 |
|
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