-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
Changes from 5 commits
3602ffc
e017a83
66aa5ca
adfb579
c88680b
a78fda2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,29 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const commonPath = require.resolve('../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 path = require('path'); | ||
|
||
if (!common.isMainThread) | ||
common.skip('Worker bootstrapping works differently -> different async IDs'); | ||
|
||
tmpdir.refresh(); | ||
|
||
const file1 = path.join(tmpdir.path, 'file1'); | ||
const file2 = path.join(tmpdir.path, 'file2'); | ||
fs.writeFileSync(file1, 'foo'); | ||
fs.writeFileSync(file2, 'bar'); | ||
|
||
const hooks = initHooks(); | ||
hooks.enable(); | ||
|
||
function onchange() {} | ||
// install first file watcher | ||
fs.watchFile(__filename, onchange); | ||
const w1 = fs.watchFile(file1, { interval: 10 }, onchange); | ||
|
||
let as = hooks.activitiesOfTypes('STATWATCHER'); | ||
assert.strictEqual(as.length, 1); | ||
|
@@ -28,7 +36,7 @@ checkInvocations(statwatcher1, { init: 1 }, | |
'watcher1: when started to watch file'); | ||
|
||
// install second file watcher | ||
fs.watchFile(commonPath, onchange); | ||
const w2 = fs.watchFile(file2, { interval: 10 }, onchange); | ||
as = hooks.activitiesOfTypes('STATWATCHER'); | ||
assert.strictEqual(as.length, 2); | ||
|
||
|
@@ -41,19 +49,27 @@ 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'); | ||
setTimeout(() => fs.writeFileSync(file1, 'foo++'), 10); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we can't use that, shouldn't we be using the platform timeout modifier? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So… the reason for this is that the stat watcher itself internally itself uses file system requests + timeouts. In order to witness a change, the write needs to hit a spot after the initial internal I’ve updated to use |
||
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'); | ||
setTimeout(() => fs.writeFileSync(file2, 'bar++'), 10); | ||
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'); | ||
fs.unwatchFile(file1); | ||
fs.unwatchFile(file2); | ||
}); | ||
})); | ||
}); | ||
})); | ||
|
||
process.on('exit', onexit); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big deal, but don't we usually use a normal
owner
property on the handles elsewhere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use a symbol in (most/all) new code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I’d like to transition to using a single
kOwner
symbol for all native handlesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea (and probably what would have been done if symbols existed back in the day).