|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | const common = require('../common'); |
4 | | -const commonPath = require.resolve('../common'); |
| 4 | +const tmpdir = require('../common/tmpdir'); |
5 | 5 | const assert = require('assert'); |
6 | 6 | const initHooks = require('./init-hooks'); |
7 | 7 | const { checkInvocations } = require('./hook-checks'); |
8 | 8 | const fs = require('fs'); |
| 9 | +const path = require('path'); |
9 | 10 |
|
10 | 11 | if (!common.isMainThread) |
11 | 12 | common.skip('Worker bootstrapping works differently -> different async IDs'); |
12 | 13 |
|
| 14 | +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 | + |
13 | 21 | const hooks = initHooks(); |
14 | 22 | hooks.enable(); |
15 | 23 |
|
16 | 24 | function onchange() {} |
17 | 25 | // install first file watcher |
18 | | -fs.watchFile(__filename, onchange); |
| 26 | +const w1 = fs.watchFile(file1, { interval: 10 }, onchange); |
19 | 27 |
|
20 | 28 | let as = hooks.activitiesOfTypes('STATWATCHER'); |
21 | 29 | assert.strictEqual(as.length, 1); |
@@ -28,7 +36,7 @@ checkInvocations(statwatcher1, { init: 1 }, |
28 | 36 | 'watcher1: when started to watch file'); |
29 | 37 |
|
30 | 38 | // install second file watcher |
31 | | -fs.watchFile(commonPath, onchange); |
| 39 | +const w2 = fs.watchFile(file2, { interval: 10 }, onchange); |
32 | 40 | as = hooks.activitiesOfTypes('STATWATCHER'); |
33 | 41 | assert.strictEqual(as.length, 2); |
34 | 42 |
|
@@ -41,19 +49,29 @@ checkInvocations(statwatcher1, { init: 1 }, |
41 | 49 | checkInvocations(statwatcher2, { init: 1 }, |
42 | 50 | 'watcher2: when started to watch second file'); |
43 | 51 |
|
44 | | -// 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'); |
50 | 60 |
|
51 | | -// 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 | +})); |
57 | 75 |
|
58 | 76 | process.on('exit', onexit); |
59 | 77 |
|
|