|
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 | constcommon=require('../common'); |
4 | 14 | constfs=require('fs'); |
5 | 15 | constpath=require('path'); |
6 | | -constassert=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 | constfn='新建文夹件.txt'; |
16 | 20 | consta=path.join(common.tmpDir,fn); |
17 | 21 |
|
| 22 | +constwatchers=newSet(); |
| 23 | + |
| 24 | +functionregisterWatcher(watcher){ |
| 25 | +watchers.add(watcher); |
| 26 | +} |
| 27 | + |
| 28 | +functionunregisterWatcher(watcher){ |
| 29 | +watcher.close(); |
| 30 | +watchers.delete(watcher); |
| 31 | +if(watchers.size===0){ |
| 32 | +clearInterval(interval); |
| 33 | +} |
| 34 | +} |
| 35 | + |
18 | 36 | constwatcher1=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 | constwatcher2=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 | constwatcher3=fs.watch( |
38 | 56 | common.tmpDir, |
39 | 57 | {encoding: 'buffer'}, |
40 | 58 | (event,filename)=>{ |
41 | | -if(filename){ |
42 | | -assert(filenameinstanceofBuffer); |
43 | | -assert.equal(filename.toString('utf8'),fn); |
44 | | -} |
45 | | -watcher3.close(); |
| 59 | +if(filenameinstanceofBuffer&&filename.toString('utf8')===fn) |
| 60 | +done(watcher3); |
| 61 | +elseif(filename===null) |
| 62 | +done(watcher3); |
46 | 63 | } |
47 | 64 | ); |
| 65 | +registerWatcher(watcher3); |
48 | 66 |
|
49 | | -constfd=fs.openSync(a,'w+'); |
50 | | -fs.closeSync(fd); |
| 67 | +constdone=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 | +constinterval=setInterval(()=>{ |
| 72 | +constfd=fs.openSync(a,'w+'); |
| 73 | +fs.closeSync(fd); |
| 74 | +fs.unlinkSync(a); |
| 75 | +},common.platformTimeout(100)); |
0 commit comments