@@ -15,68 +15,81 @@ tmpdir.refresh({ spawn: false });
15
15
16
16
const file1 = path . join ( tmpdir . path , 'file1' ) ;
17
17
const file2 = path . join ( tmpdir . path , 'file2' ) ;
18
- fs . writeFileSync ( file1 , 'foo' ) ;
19
- fs . writeFileSync ( file2 , 'bar' ) ;
20
18
21
- const hooks = initHooks ( ) ;
22
- hooks . enable ( ) ;
23
-
24
- function onchange1 ( curr , prev ) {
25
- console . log ( 'Watcher: w1' ) ;
19
+ const onchangex = ( x ) => ( curr , prev ) => {
20
+ console . log ( `Watcher: ${ x } ` ) ;
26
21
console . log ( 'current stat data:' , curr ) ;
27
22
console . log ( 'previous stat data:' , prev ) ;
28
- }
29
- // Install first file watcher
30
- const w1 = fs . watchFile ( file1 , { interval : 10 } , onchange1 ) ;
23
+ } ;
24
+
25
+ const checkWatcherStart = ( name , watcher ) => {
26
+ assert . strictEqual ( watcher . type , 'STATWATCHER' ) ;
27
+ assert . strictEqual ( typeof watcher . uid , 'number' ) ;
28
+ assert . strictEqual ( watcher . triggerAsyncId , 1 ) ;
29
+ checkInvocations ( watcher , { init : 1 } ,
30
+ `${ name } : when started to watch file` ) ;
31
+ } ;
32
+
33
+ const hooks = initHooks ( ) ;
34
+ hooks . enable ( ) ;
31
35
36
+ // Install first file watcher.
37
+ const w1 = fs . watchFile ( file1 , { interval : 10 } , onchangex ( 'w1' ) ) ;
32
38
let as = hooks . activitiesOfTypes ( 'STATWATCHER' ) ;
33
39
assert . strictEqual ( as . length , 1 ) ;
40
+ // Count all change events to account for all of them in checkInvocations.
41
+ let w1HookCount = 0 ;
42
+ w1 . on ( 'change' , ( ) => w1HookCount ++ ) ;
34
43
35
44
const statwatcher1 = as [ 0 ] ;
36
- assert . strictEqual ( statwatcher1 . type , 'STATWATCHER' ) ;
37
- assert . strictEqual ( typeof statwatcher1 . uid , 'number' ) ;
38
- assert . strictEqual ( statwatcher1 . triggerAsyncId , 1 ) ;
39
- checkInvocations ( statwatcher1 , { init : 1 } ,
40
- 'watcher1: when started to watch file' ) ;
41
-
42
- function onchange2 ( curr , prev ) {
43
- console . log ( 'Watcher: w2' ) ;
44
- console . log ( 'current stat data:' , curr ) ;
45
- console . log ( 'previous stat data:' , prev ) ;
46
- }
45
+ checkWatcherStart ( 'watcher1' , statwatcher1 ) ;
47
46
48
47
// Install second file watcher
49
- const w2 = fs . watchFile ( file2 , { interval : 10 } , onchange2 ) ;
48
+ const w2 = fs . watchFile ( file2 , { interval : 10 } , onchangex ( 'w2' ) ) ;
50
49
as = hooks . activitiesOfTypes ( 'STATWATCHER' ) ;
51
50
assert . strictEqual ( as . length , 2 ) ;
51
+ // Count all change events to account for all of them in checkInvocations.
52
+ let w2HookCount = 0 ;
53
+ w2 . on ( 'change' , ( ) => w2HookCount ++ ) ;
52
54
53
55
const statwatcher2 = as [ 1 ] ;
54
- assert . strictEqual ( statwatcher2 . type , 'STATWATCHER' ) ;
55
- assert . strictEqual ( typeof statwatcher2 . uid , 'number' ) ;
56
- assert . strictEqual ( statwatcher2 . triggerAsyncId , 1 ) ;
57
56
checkInvocations ( statwatcher1 , { init : 1 } ,
58
57
'watcher1: when started to watch second file' ) ;
59
- checkInvocations ( statwatcher2 , { init : 1 } ,
60
- 'watcher2: when started to watch second file' ) ;
58
+ checkWatcherStart ( 'watcher2' , statwatcher2 ) ;
61
59
62
60
setTimeout ( ( ) => fs . writeFileSync ( file1 , 'foo++' ) ,
63
61
common . platformTimeout ( 100 ) ) ;
64
- w1 . once ( 'change' , common . mustCall ( ( curr , prev ) => {
65
- console . log ( 'w1 change' , curr , prev ) ;
62
+ w1 . on ( 'change' , common . mustCallAtLeast ( ( curr , prev ) => {
63
+ console . log ( 'w1 change to' , curr , 'from' , prev ) ;
64
+ // Wait until we get the write above.
65
+ if ( prev . size !== 0 || curr . size !== 5 )
66
+ return ;
67
+ // Remove listeners to make w1HookCount final
68
+ w1 . removeAllListeners ( 'change' ) ;
69
+
66
70
setImmediate ( ( ) => {
67
- checkInvocations ( statwatcher1 , { init : 1 , before : 1 , after : 1 } ,
71
+ checkInvocations ( statwatcher1 ,
72
+ { init : 1 , before : w1HookCount , after : w1HookCount } ,
68
73
'watcher1: when unwatched first file' ) ;
69
74
checkInvocations ( statwatcher2 , { init : 1 } ,
70
75
'watcher2: when unwatched first file' ) ;
71
76
72
77
setTimeout ( ( ) => fs . writeFileSync ( file2 , 'bar++' ) ,
73
78
common . platformTimeout ( 100 ) ) ;
74
- w2 . once ( 'change' , common . mustCall ( ( curr , prev ) => {
75
- console . log ( 'w2 change' , curr , prev ) ;
79
+ w2 . on ( 'change' , common . mustCallAtLeast ( ( curr , prev ) => {
80
+ console . log ( 'w2 change to' , curr , 'from' , prev ) ;
81
+ // Wait until we get the write above.
82
+ if ( prev . size !== 0 || curr . size !== 5 )
83
+ return ;
84
+ // Remove listeners to make w2HookCount final
85
+ w2 . removeAllListeners ( 'change' ) ;
86
+
76
87
setImmediate ( ( ) => {
77
- checkInvocations ( statwatcher1 , { init : 1 , before : 1 , after : 1 } ,
88
+ checkInvocations ( statwatcher1 ,
89
+ { init : 1 , before : w1HookCount , after : w1HookCount } ,
78
90
'watcher1: when unwatched second file' ) ;
79
- checkInvocations ( statwatcher2 , { init : 1 , before : 1 , after : 1 } ,
91
+ checkInvocations ( statwatcher2 ,
92
+ { init : 1 , before : w2HookCount , after : w2HookCount } ,
80
93
'watcher2: when unwatched second file' ) ;
81
94
fs . unwatchFile ( file1 ) ;
82
95
fs . unwatchFile ( file2 ) ;
@@ -85,13 +98,13 @@ w1.once('change', common.mustCall((curr, prev) => {
85
98
} ) ;
86
99
} ) ) ;
87
100
88
- process . on ( 'exit' , onexit ) ;
89
-
90
- function onexit ( ) {
101
+ process . once ( 'exit' , ( ) => {
91
102
hooks . disable ( ) ;
92
103
hooks . sanityCheck ( 'STATWATCHER' ) ;
93
- checkInvocations ( statwatcher1 , { init : 1 , before : 1 , after : 1 } ,
104
+ checkInvocations ( statwatcher1 ,
105
+ { init : 1 , before : w1HookCount , after : w1HookCount } ,
94
106
'watcher1: when process exits' ) ;
95
- checkInvocations ( statwatcher2 , { init : 1 , before : 1 , after : 1 } ,
107
+ checkInvocations ( statwatcher2 ,
108
+ { init : 1 , before : w2HookCount , after : w2HookCount } ,
96
109
'watcher2: when process exits' ) ;
97
- }
110
+ } ) ;
0 commit comments