◐ Shell
clean mode source ↗

test: improve statwatcher async_hooks test · nodejs/node@dea3ac7

11

'use strict';

2233

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');

9101011

if (!common.isMainThread)

1112

common.skip('Worker bootstrapping works differently -> different async IDs');

121314+

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();

15231624

function onchange() {}

1725

// install first file watcher

18-

fs.watchFile(__filename, onchange);

26+

const w1 = fs.watchFile(file1, { interval: 10 }, onchange);

19272028

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');

29373038

// 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');

435144-

// 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');

506051-

// 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+

}));

57755876

process.on('exit', onexit);

5977