diff options
author | Anna Henningsen <anna@addaleax.net> | 2018-06-10 15:36:55 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2018-06-14 13:19:07 +0200 |
commit | 65b2e4b6fa511c42f913c8814561b5090805ab7c (patch) | |
tree | c1c47f87e1ca1b3a5fc59a009a296bff28656456 /test | |
parent | 2a08925896872c345e2948778eab01e9853c22cb (diff) | |
download | android-node-v8-65b2e4b6fa511c42f913c8814561b5090805ab7c.tar.gz android-node-v8-65b2e4b6fa511c42f913c8814561b5090805ab7c.tar.bz2 android-node-v8-65b2e4b6fa511c42f913c8814561b5090805ab7c.zip |
test: improve statwatcher async_hooks test
Modify the `fs.watchFile()` async hooks test to be more
accurate; currently, it relies on undocumented methods
and the fact that they use `MakeCallback()` even though
there is always a JS stack below.
PR-URL: https://github.com/nodejs/node/pull/21244
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/async-hooks/test-statwatcher.js | 48 |
1 files changed, 33 insertions, 15 deletions
diff --git a/test/async-hooks/test-statwatcher.js b/test/async-hooks/test-statwatcher.js index 8085ebf51b..92832a4680 100644 --- a/test/async-hooks/test-statwatcher.js +++ b/test/async-hooks/test-statwatcher.js @@ -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,29 @@ 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++'), + common.platformTimeout(100)); +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++'), + common.platformTimeout(100)); + 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); |