diff options
author | Maya Lekova <apokalyptra@gmail.com> | 2018-05-09 15:05:51 +0200 |
---|---|---|
committer | Matteo Collina <hello@matteocollina.com> | 2018-05-14 16:25:50 +0200 |
commit | e993e45dbf99a155583ed4c7e933ceae926f597c (patch) | |
tree | aa239e4a55d13008fa561116d9c816583d233547 /test/async-hooks/test-async-await.js | |
parent | 657723e5c0ed7192a7974b50b2e87c5e97187748 (diff) | |
download | android-node-v8-e993e45dbf99a155583ed4c7e933ceae926f597c.tar.gz android-node-v8-e993e45dbf99a155583ed4c7e933ceae926f597c.tar.bz2 android-node-v8-e993e45dbf99a155583ed4c7e933ceae926f597c.zip |
test: add test for async hooks parity for async/await
Add a basic test ensuring parity between before-after and
init-promiseResolve hooks when using async/await.
Add ability to initHooks and to checkInvocations utilities to transmit
promiseResolve hook as well.
See: https://github.com/nodejs/node/issues/20516
PR-URL: https://github.com/nodejs/node/pull/20626
Refs: https://github.com/nodejs/node/issues/20516
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Benedikt Meurer <benedikt.meurer@gmail.com>
Diffstat (limited to 'test/async-hooks/test-async-await.js')
-rw-r--r-- | test/async-hooks/test-async-await.js | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/test/async-hooks/test-async-await.js b/test/async-hooks/test-async-await.js new file mode 100644 index 0000000000..7f88cd9b18 --- /dev/null +++ b/test/async-hooks/test-async-await.js @@ -0,0 +1,91 @@ +'use strict'; +const common = require('../common'); + +// This test ensures async hooks are being properly called +// when using async-await mechanics. This involves: +// 1. Checking that all initialized promises are being resolved +// 2. Checking that for each 'before' corresponding hook 'after' hook is called + +const assert = require('assert'); +const initHooks = require('./init-hooks'); + +const util = require('util'); + +const sleep = util.promisify(setTimeout); +// either 'inited' or 'resolved' +const promisesInitState = new Map(); +// either 'before' or 'after' AND asyncId must be present in the other map +const promisesExecutionState = new Map(); + +const hooks = initHooks({ + oninit, + onbefore, + onafter, + ondestroy: null, // Intentionally not tested, since it will be removed soon + onpromiseResolve +}); +hooks.enable(); + +function oninit(asyncId, type, triggerAsyncId, resource) { + if (type === 'PROMISE') { + promisesInitState.set(asyncId, 'inited'); + } +} + +function onbefore(asyncId) { + if (!promisesInitState.has(asyncId)) { + return; + } + promisesExecutionState.set(asyncId, 'before'); +} + +function onafter(asyncId) { + if (!promisesInitState.has(asyncId)) { + return; + } + + assert.strictEqual(promisesExecutionState.get(asyncId), 'before', + 'after hook called for promise without prior call' + + 'to before hook'); + assert.strictEqual(promisesInitState.get(asyncId), 'resolved', + 'after hook called for promise without prior call' + + 'to resolve hook'); + promisesExecutionState.set(asyncId, 'after'); +} + +function onpromiseResolve(asyncId) { + assert(promisesInitState.has(asyncId), + 'resolve hook called for promise without prior call to init hook'); + + promisesInitState.set(asyncId, 'resolved'); +} + +const timeout = common.platformTimeout(10); + +function checkPromisesInitState() { + for (const initState of promisesInitState.values()) { + assert.strictEqual(initState, 'resolved', + 'promise initialized without being resolved'); + } +} + +function checkPromisesExecutionState() { + for (const executionState of promisesExecutionState.values()) { + assert.strictEqual(executionState, 'after', + 'mismatch between before and after hook calls'); + } +} + +process.on('beforeExit', common.mustCall(() => { + hooks.disable(); + hooks.sanityCheck('PROMISE'); + + checkPromisesInitState(); + checkPromisesExecutionState(); +})); + +async function asyncFunc() { + await sleep(timeout); +} + +asyncFunc(); |