Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib,src: make StatWatcher a HandleWrap #21244

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
addaleax committed Jun 10, 2018
commit 3602ffc3ce7365313effc68136d00b286c962df6
40 changes: 26 additions & 14 deletions test/async-hooks/test-statwatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ hooks.enable();

function onchange() {}
// install first file watcher
fs.watchFile(__filename, onchange);
const w1 = fs.watchFile(__filename, { interval: 10 }, onchange);

let as = hooks.activitiesOfTypes('STATWATCHER');
assert.strictEqual(as.length, 1);
Expand All @@ -28,7 +28,7 @@ checkInvocations(statwatcher1, { init: 1 },
'watcher1: when started to watch file');

// install second file watcher
fs.watchFile(commonPath, onchange);
const w2 = fs.watchFile(commonPath, { interval: 10 }, onchange);
as = hooks.activitiesOfTypes('STATWATCHER');
assert.strictEqual(as.length, 2);

Expand All @@ -41,19 +41,31 @@ checkInvocations(statwatcher1, { init: 1 },
checkInvocations(statwatcher2, { init: 1 },
'watcher2: when started to watch second file');

// remove first file watcher
fs.unwatchFile(__filename);
checkInvocations(statwatcher1, { init: 1, before: 1, after: 1 },
'watcher1: when unwatched first file');
checkInvocations(statwatcher2, { init: 1 },
'watcher2: when unwatched first file');
// Touch the first file by modifying its access time.
const origStat = fs.statSync(__filename);
fs.utimesSync(__filename, Date.now() + 10, origStat.mtime);
w1.once('change', common.mustCall(() => {
setImmediate(() => {
checkInvocations(statwatcher1, { init: 1, before: 1, after: 1 },
'watcher1: when unwatched first file');
checkInvocations(statwatcher2, { init: 1 },
'watcher2: when unwatched first file');

// remove second file watcher
fs.unwatchFile(commonPath);
checkInvocations(statwatcher1, { init: 1, before: 1, after: 1 },
'watcher1: when unwatched second file');
checkInvocations(statwatcher2, { init: 1, before: 1, after: 1 },
'watcher2: when unwatched second file');
// Touch the second file by modifying its access time.
const origStat = fs.statSync(commonPath);
fs.utimesSync(commonPath, Date.now() + 10, origStat.mtime);
w2.once('change', common.mustCall(() => {
setImmediate(() => {
checkInvocations(statwatcher1, { init: 1, before: 1, after: 1 },
'watcher1: when unwatched second file');
checkInvocations(statwatcher2, { init: 1, before: 1, after: 1 },
'watcher2: when unwatched second file');
w1.stop();
w2.stop();
});
}));
});
}));

process.on('exit', onexit);

Expand Down