forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-statwatcher.js
More file actions
133 lines (112 loc) · 4.35 KB
/
Copy pathtest-statwatcher.js
File metadata and controls
133 lines (112 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict';
const common = require('../common');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const initHooks = require('./init-hooks');
const { checkInvocations } = require('./hook-checks');
const fs = require('fs');
const { isMainThread } = require('worker_threads');
if (!isMainThread) {
common.skip('Worker bootstrapping works differently -> different async IDs');
}
tmpdir.refresh();
const file1 = tmpdir.resolve('file1');
const file2 = tmpdir.resolve('file2');
const onchangex = (x) => (curr, prev) => {
console.log(`Watcher: ${x}`);
console.log('current stat data:', curr);
console.log('previous stat data:', prev);
};
const checkWatcherStart = common.mustCall((name, watcher) => {
assert.strictEqual(watcher.type, 'STATWATCHER');
assert.strictEqual(typeof watcher.uid, 'number');
assert.strictEqual(watcher.triggerAsyncId, 1);
checkInvocations(watcher, { init: 1 },
`${name}: when started to watch file`);
}, 2);
const hooks = initHooks();
hooks.enable();
// Install first file watcher.
const w1 = fs.watchFile(file1, { interval: 10 }, onchangex('w1'));
let as = hooks.activitiesOfTypes('STATWATCHER');
assert.strictEqual(as.length, 1);
// Count all change events to account for all of them in checkInvocations.
let w1HookCount = 0;
w1.on('change', () => w1HookCount++);
const statwatcher1 = as[0];
checkWatcherStart('watcher1', statwatcher1);
// Install second file watcher
const w2 = fs.watchFile(file2, { interval: 10 }, onchangex('w2'));
as = hooks.activitiesOfTypes('STATWATCHER');
assert.strictEqual(as.length, 2);
// Count all change events to account for all of them in checkInvocations.
let w2HookCount = 0;
w2.on('change', () => w2HookCount++);
const statwatcher2 = as[1];
checkInvocations(statwatcher1, { init: 1 },
'watcher1: when started to watch second file');
checkWatcherStart('watcher2', statwatcher2);
let w2Initialized = false;
let writeFile2AfterW1 = false;
const onW2Initialized = (curr, prev) => {
if (curr.nlink !== 0 || prev.nlink !== 0)
return;
w2.removeListener('change', onW2Initialized);
w2Initialized = true;
if (writeFile2AfterW1) {
setTimeout(() => fs.writeFileSync(file2, 'bar++'),
common.platformTimeout(100));
}
};
w2.on('change', onW2Initialized);
w1.on('change', common.mustCallAtLeast((curr, prev) => {
console.log('w1 change to', curr, 'from', prev);
// Wait for the initial ENOENT poll before creating the file. Otherwise the
// first stat can race with the write and use the created file as the baseline.
if (curr.nlink === 0 && prev.nlink === 0) {
setTimeout(() => fs.writeFileSync(file1, 'foo++'),
common.platformTimeout(100));
return;
}
// Wait until we get the write above.
if (prev.size !== 0 || curr.size !== 5)
return;
setImmediate(common.mustCall(() => {
checkInvocations(statwatcher1,
{ init: 1, before: w1HookCount, after: w1HookCount },
'watcher1: when unwatched first file');
checkInvocations(statwatcher2, { init: 1 },
'watcher2: when unwatched first file');
writeFile2AfterW1 = true;
if (w2Initialized) {
setTimeout(() => fs.writeFileSync(file2, 'bar++'),
common.platformTimeout(100));
}
w2.on('change', common.mustCallAtLeast((curr, prev) => {
console.log('w2 change to', curr, 'from', prev);
// Wait until we get the write above.
if (prev.size !== 0 || curr.size !== 5)
return;
setImmediate(() => {
checkInvocations(statwatcher1,
{ init: 1, before: w1HookCount, after: w1HookCount },
'watcher1: when unwatched second file');
checkInvocations(statwatcher2,
{ init: 1, before: w2HookCount, after: w2HookCount },
'watcher2: when unwatched second file');
fs.unwatchFile(file1);
fs.unwatchFile(file2);
});
}));
}));
}));
process.once('exit', () => {
hooks.disable();
hooks.sanityCheck('STATWATCHER');
checkInvocations(statwatcher1,
{ init: 1, before: w1HookCount, after: w1HookCount },
'watcher1: when process exits');
checkInvocations(statwatcher2,
{ init: 1, before: w2HookCount, after: w2HookCount },
'watcher2: when process exits');
});