11'use strict' ;
22
33const common = require ( '../common' ) ;
4- const commonPath = require . resolve ( '../common' ) ;
4+ const tmpdir = require ( '../common/tmpdir ' ) ;
55const assert = require ( 'assert' ) ;
66const initHooks = require ( './init-hooks' ) ;
77const { checkInvocations } = require ( './hook-checks' ) ;
88const fs = require ( 'fs' ) ;
9+ const path = require ( 'path' ) ;
910
1011if ( ! common . isMainThread )
1112 common . skip ( 'Worker bootstrapping works differently -> different async IDs' ) ;
1213
14+ tmpdir . refresh ( ) ;
15+
16+ const file1 = path . join ( tmpdir . path , 'file1' ) ;
17+ const file2 = path . join ( tmpdir . path , 'file2' ) ;
18+ fs . writeFileSync ( file1 , 'foo' ) ;
19+ fs . writeFileSync ( file2 , 'bar' ) ;
20+
1321const hooks = initHooks ( ) ;
1422hooks . enable ( ) ;
1523
1624function onchange ( ) { }
1725// install first file watcher
18- fs . watchFile ( __filename , onchange ) ;
26+ const w1 = fs . watchFile ( file1 , { interval : 10 } , onchange ) ;
1927
2028let as = hooks . activitiesOfTypes ( 'STATWATCHER' ) ;
2129assert . strictEqual ( as . length , 1 ) ;
@@ -28,7 +36,7 @@ checkInvocations(statwatcher1, { init: 1 },
2836 'watcher1: when started to watch file' ) ;
2937
3038// install second file watcher
31- fs . watchFile ( commonPath , onchange ) ;
39+ const w2 = fs . watchFile ( file2 , { interval : 10 } , onchange ) ;
3240as = hooks . activitiesOfTypes ( 'STATWATCHER' ) ;
3341assert . strictEqual ( as . length , 2 ) ;
3442
@@ -41,19 +49,29 @@ checkInvocations(statwatcher1, { init: 1 },
4149checkInvocations ( statwatcher2 , { init : 1 } ,
4250 'watcher2: when started to watch second file' ) ;
4351
44- // remove first file watcher
45- fs . unwatchFile ( __filename ) ;
46- checkInvocations ( statwatcher1 , { init : 1 , before : 1 , after : 1 } ,
47- 'watcher1: when unwatched first file' ) ;
48- checkInvocations ( statwatcher2 , { init : 1 } ,
49- 'watcher2: when unwatched first file' ) ;
52+ setTimeout ( ( ) => fs . writeFileSync ( file1 , 'foo++' ) ,
53+ common . platformTimeout ( 100 ) ) ;
54+ w1 . once ( 'change' , common . mustCall ( ( ) => {
55+ setImmediate ( ( ) => {
56+ checkInvocations ( statwatcher1 , { init : 1 , before : 1 , after : 1 } ,
57+ 'watcher1: when unwatched first file' ) ;
58+ checkInvocations ( statwatcher2 , { init : 1 } ,
59+ 'watcher2: when unwatched first file' ) ;
5060
51- // remove second file watcher
52- fs . unwatchFile ( commonPath ) ;
53- checkInvocations ( statwatcher1 , { init : 1 , before : 1 , after : 1 } ,
54- 'watcher1: when unwatched second file' ) ;
55- checkInvocations ( statwatcher2 , { init : 1 , before : 1 , after : 1 } ,
56- 'watcher2: when unwatched second file' ) ;
61+ setTimeout ( ( ) => fs . writeFileSync ( file2 , 'bar++' ) ,
62+ common . platformTimeout ( 100 ) ) ;
63+ w2 . once ( 'change' , common . mustCall ( ( ) => {
64+ setImmediate ( ( ) => {
65+ checkInvocations ( statwatcher1 , { init : 1 , before : 1 , after : 1 } ,
66+ 'watcher1: when unwatched second file' ) ;
67+ checkInvocations ( statwatcher2 , { init : 1 , before : 1 , after : 1 } ,
68+ 'watcher2: when unwatched second file' ) ;
69+ fs . unwatchFile ( file1 ) ;
70+ fs . unwatchFile ( file2 ) ;
71+ } ) ;
72+ } ) ) ;
73+ } ) ;
74+ } ) ) ;
5775
5876process . on ( 'exit' , onexit ) ;
5977
0 commit comments