Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dea3ac7

Browse filesBrowse 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 <cjihrig@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 169bff3 commit dea3ac7
Copy full SHA for dea3ac7

File tree

Expand file treeCollapse file tree

1 file changed

+33
-15
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+33
-15
lines changed
Open diff view settings
Collapse file

‎test/async-hooks/test-statwatcher.js‎

Copy file name to clipboardExpand all lines: test/async-hooks/test-statwatcher.js
+33-15Lines changed: 33 additions & 15 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.