2016-11-22 17:13:44 +01:00
|
|
|
'use strict';
|
|
|
|
|
2018-05-18 01:20:25 +02:00
|
|
|
const common = require('../common');
|
2018-06-10 15:36:55 +02:00
|
|
|
const tmpdir = require('../common/tmpdir');
|
2016-11-22 17:13:44 +01:00
|
|
|
const assert = require('assert');
|
|
|
|
const initHooks = require('./init-hooks');
|
|
|
|
const { checkInvocations } = require('./hook-checks');
|
|
|
|
const fs = require('fs');
|
2018-06-10 15:36:55 +02:00
|
|
|
const path = require('path');
|
2016-11-22 17:13:44 +01:00
|
|
|
|
2018-05-18 01:20:25 +02:00
|
|
|
if (!common.isMainThread)
|
|
|
|
common.skip('Worker bootstrapping works differently -> different async IDs');
|
|
|
|
|
2019-12-12 10:05:38 +01:00
|
|
|
tmpdir.refresh();
|
2018-06-10 15:36:55 +02:00
|
|
|
|
|
|
|
const file1 = path.join(tmpdir.path, 'file1');
|
|
|
|
const file2 = path.join(tmpdir.path, 'file2');
|
|
|
|
|
2019-11-10 12:41:41 +01:00
|
|
|
const onchangex = (x) => (curr, prev) => {
|
|
|
|
console.log(`Watcher: ${x}`);
|
2019-06-18 00:01:02 +02:00
|
|
|
console.log('current stat data:', curr);
|
|
|
|
console.log('previous stat data:', prev);
|
2019-11-10 12:41:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const checkWatcherStart = (name, watcher) => {
|
|
|
|
assert.strictEqual(watcher.type, 'STATWATCHER');
|
|
|
|
assert.strictEqual(typeof watcher.uid, 'number');
|
|
|
|
assert.strictEqual(watcher.triggerAsyncId, 1);
|
|
|
|
checkInvocations(watcher, { init: 1 },
|
|
|
|
`${name}: when started to watch file`);
|
|
|
|
};
|
|
|
|
|
|
|
|
const hooks = initHooks();
|
|
|
|
hooks.enable();
|
2016-11-22 17:13:44 +01:00
|
|
|
|
2019-11-10 12:41:41 +01:00
|
|
|
// Install first file watcher.
|
|
|
|
const w1 = fs.watchFile(file1, { interval: 10 }, onchangex('w1'));
|
2016-11-22 17:13:44 +01:00
|
|
|
let as = hooks.activitiesOfTypes('STATWATCHER');
|
2017-05-26 17:53:06 +02:00
|
|
|
assert.strictEqual(as.length, 1);
|
2019-11-10 12:41:41 +01:00
|
|
|
// Count all change events to account for all of them in checkInvocations.
|
|
|
|
let w1HookCount = 0;
|
|
|
|
w1.on('change', () => w1HookCount++);
|
2016-11-22 17:13:44 +01:00
|
|
|
|
|
|
|
const statwatcher1 = as[0];
|
2019-11-10 12:41:41 +01:00
|
|
|
checkWatcherStart('watcher1', statwatcher1);
|
2019-06-18 00:01:02 +02:00
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
// Install second file watcher
|
2019-11-10 12:41:41 +01:00
|
|
|
const w2 = fs.watchFile(file2, { interval: 10 }, onchangex('w2'));
|
2016-11-22 17:13:44 +01:00
|
|
|
as = hooks.activitiesOfTypes('STATWATCHER');
|
2017-05-26 17:53:06 +02:00
|
|
|
assert.strictEqual(as.length, 2);
|
2019-11-10 12:41:41 +01:00
|
|
|
// Count all change events to account for all of them in checkInvocations.
|
|
|
|
let w2HookCount = 0;
|
|
|
|
w2.on('change', () => w2HookCount++);
|
2016-11-22 17:13:44 +01:00
|
|
|
|
|
|
|
const statwatcher2 = as[1];
|
|
|
|
checkInvocations(statwatcher1, { init: 1 },
|
|
|
|
'watcher1: when started to watch second file');
|
2019-11-10 12:41:41 +01:00
|
|
|
checkWatcherStart('watcher2', statwatcher2);
|
2016-11-22 17:13:44 +01:00
|
|
|
|
2018-06-10 15:36:55 +02:00
|
|
|
setTimeout(() => fs.writeFileSync(file1, 'foo++'),
|
|
|
|
common.platformTimeout(100));
|
2019-11-10 12:41:41 +01:00
|
|
|
w1.on('change', common.mustCallAtLeast((curr, prev) => {
|
|
|
|
console.log('w1 change to', curr, 'from', prev);
|
|
|
|
// Wait until we get the write above.
|
|
|
|
if (prev.size !== 0 || curr.size !== 5)
|
|
|
|
return;
|
|
|
|
|
2018-06-10 15:36:55 +02:00
|
|
|
setImmediate(() => {
|
2019-11-10 12:41:41 +01:00
|
|
|
checkInvocations(statwatcher1,
|
|
|
|
{ init: 1, before: w1HookCount, after: w1HookCount },
|
2018-06-10 15:36:55 +02:00
|
|
|
'watcher1: when unwatched first file');
|
|
|
|
checkInvocations(statwatcher2, { init: 1 },
|
|
|
|
'watcher2: when unwatched first file');
|
2016-11-22 17:13:44 +01:00
|
|
|
|
2018-06-10 15:36:55 +02:00
|
|
|
setTimeout(() => fs.writeFileSync(file2, 'bar++'),
|
|
|
|
common.platformTimeout(100));
|
2019-11-10 12:41:41 +01:00
|
|
|
w2.on('change', common.mustCallAtLeast((curr, prev) => {
|
|
|
|
console.log('w2 change to', curr, 'from', prev);
|
|
|
|
// Wait until we get the write above.
|
|
|
|
if (prev.size !== 0 || curr.size !== 5)
|
|
|
|
return;
|
|
|
|
|
2018-06-10 15:36:55 +02:00
|
|
|
setImmediate(() => {
|
2019-11-10 12:41:41 +01:00
|
|
|
checkInvocations(statwatcher1,
|
|
|
|
{ init: 1, before: w1HookCount, after: w1HookCount },
|
2018-06-10 15:36:55 +02:00
|
|
|
'watcher1: when unwatched second file');
|
2019-11-10 12:41:41 +01:00
|
|
|
checkInvocations(statwatcher2,
|
|
|
|
{ init: 1, before: w2HookCount, after: w2HookCount },
|
2018-06-10 15:36:55 +02:00
|
|
|
'watcher2: when unwatched second file');
|
|
|
|
fs.unwatchFile(file1);
|
|
|
|
fs.unwatchFile(file2);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}));
|
2016-11-22 17:13:44 +01:00
|
|
|
|
2019-11-10 12:41:41 +01:00
|
|
|
process.once('exit', () => {
|
2016-11-22 17:13:44 +01:00
|
|
|
hooks.disable();
|
|
|
|
hooks.sanityCheck('STATWATCHER');
|
2019-11-10 12:41:41 +01:00
|
|
|
checkInvocations(statwatcher1,
|
|
|
|
{ init: 1, before: w1HookCount, after: w1HookCount },
|
2017-05-26 17:53:06 +02:00
|
|
|
'watcher1: when process exits');
|
2019-11-10 12:41:41 +01:00
|
|
|
checkInvocations(statwatcher2,
|
|
|
|
{ init: 1, before: w2HookCount, after: w2HookCount },
|
2016-11-22 17:13:44 +01:00
|
|
|
'watcher2: when process exits');
|
2019-11-10 12:41:41 +01:00
|
|
|
});
|