Skip to content

Commit dea3ac7

Browse files
addaleaxtargos
authored andcommitted
test: improve statwatcher async_hooks test
Modify the `fs.watchFile()` async hooks test to be more accurate; currently, it relies on undocumented methods and the fact that they use `MakeCallback()` even though there is always a JS stack below. PR-URL: #21244 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 169bff3 commit dea3ac7

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

test/async-hooks/test-statwatcher.js

+33-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
'use strict';
22

33
const common = require('../common');
4-
const commonPath = require.resolve('../common');
4+
const tmpdir = require('../common/tmpdir');
55
const assert = require('assert');
66
const initHooks = require('./init-hooks');
77
const { checkInvocations } = require('./hook-checks');
88
const fs = require('fs');
9+
const path = require('path');
910

1011
if (!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+
1321
const hooks = initHooks();
1422
hooks.enable();
1523

1624
function onchange() {}
1725
// install first file watcher
18-
fs.watchFile(__filename, onchange);
26+
const w1 = fs.watchFile(file1, { interval: 10 }, onchange);
1927

2028
let as = hooks.activitiesOfTypes('STATWATCHER');
2129
assert.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);
3240
as = hooks.activitiesOfTypes('STATWATCHER');
3341
assert.strictEqual(as.length, 2);
3442

@@ -41,19 +49,29 @@ checkInvocations(statwatcher1, { init: 1 },
4149
checkInvocations(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

5876
process.on('exit', onexit);
5977

0 commit comments

Comments
 (0)