node/test/async-hooks/test-statwatcher.js
Anna Henningsen 229dca3dee
test,tools: enable running tests under workers
Enable running tests inside workers by passing `--worker`
to `tools/test.py`. A number of tests are marked as skipped,
or have been slightly altered to fit the different environment.

PR-URL: https://github.com/nodejs/node/pull/20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
2018-06-06 19:44:11 +02:00

68 lines
2.3 KiB
JavaScript

'use strict';
const common = require('../common');
const commonPath = require.resolve('../common');
const assert = require('assert');
const initHooks = require('./init-hooks');
const { checkInvocations } = require('./hook-checks');
const fs = require('fs');
if (!common.isMainThread)
common.skip('Worker bootstrapping works differently -> different async IDs');
const hooks = initHooks();
hooks.enable();
function onchange() {}
// install first file watcher
fs.watchFile(__filename, onchange);
let as = hooks.activitiesOfTypes('STATWATCHER');
assert.strictEqual(as.length, 1);
const statwatcher1 = as[0];
assert.strictEqual(statwatcher1.type, 'STATWATCHER');
assert.strictEqual(typeof statwatcher1.uid, 'number');
assert.strictEqual(statwatcher1.triggerAsyncId, 1);
checkInvocations(statwatcher1, { init: 1 },
'watcher1: when started to watch file');
// install second file watcher
fs.watchFile(commonPath, onchange);
as = hooks.activitiesOfTypes('STATWATCHER');
assert.strictEqual(as.length, 2);
const statwatcher2 = as[1];
assert.strictEqual(statwatcher2.type, 'STATWATCHER');
assert.strictEqual(typeof statwatcher2.uid, 'number');
assert.strictEqual(statwatcher2.triggerAsyncId, 1);
checkInvocations(statwatcher1, { init: 1 },
'watcher1: when started to watch second file');
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');
// 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');
process.on('exit', onexit);
function onexit() {
hooks.disable();
hooks.sanityCheck('STATWATCHER');
checkInvocations(statwatcher1, { init: 1, before: 1, after: 1 },
'watcher1: when process exits');
checkInvocations(statwatcher2, { init: 1, before: 1, after: 1 },
'watcher2: when process exits');
}