|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | + |
| 4 | +// This test ensures async hooks are being properly called |
| 5 | +// when using async-await mechanics. This involves: |
| 6 | +// 1. Checking that all initialized promises are being resolved |
| 7 | +// 2. Checking that for each 'before' corresponding hook 'after' hook is called |
| 8 | + |
| 9 | +const assert = require('assert'); |
| 10 | +const initHooks = require('./init-hooks'); |
| 11 | + |
| 12 | +const util = require('util'); |
| 13 | + |
| 14 | +const sleep = util.promisify(setTimeout); |
| 15 | +// either 'inited' or 'resolved' |
| 16 | +const promisesInitState = new Map(); |
| 17 | +// either 'before' or 'after' AND asyncId must be present in the other map |
| 18 | +const promisesExecutionState = new Map(); |
| 19 | + |
| 20 | +const hooks = initHooks({ |
| 21 | + oninit, |
| 22 | + onbefore, |
| 23 | + onafter, |
| 24 | + ondestroy: null, // Intentionally not tested, since it will be removed soon |
| 25 | + onpromiseResolve |
| 26 | +}); |
| 27 | +hooks.enable(); |
| 28 | + |
| 29 | +function oninit(asyncId, type, triggerAsyncId, resource) { |
| 30 | + if (type === 'PROMISE') { |
| 31 | + promisesInitState.set(asyncId, 'inited'); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function onbefore(asyncId) { |
| 36 | + if (!promisesInitState.has(asyncId)) { |
| 37 | + return; |
| 38 | + } |
| 39 | + promisesExecutionState.set(asyncId, 'before'); |
| 40 | +} |
| 41 | + |
| 42 | +function onafter(asyncId) { |
| 43 | + if (!promisesInitState.has(asyncId)) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + assert.strictEqual(promisesExecutionState.get(asyncId), 'before', |
| 48 | + 'after hook called for promise without prior call' + |
| 49 | + 'to before hook'); |
| 50 | + assert.strictEqual(promisesInitState.get(asyncId), 'resolved', |
| 51 | + 'after hook called for promise without prior call' + |
| 52 | + 'to resolve hook'); |
| 53 | + promisesExecutionState.set(asyncId, 'after'); |
| 54 | +} |
| 55 | + |
| 56 | +function onpromiseResolve(asyncId) { |
| 57 | + assert(promisesInitState.has(asyncId), |
| 58 | + 'resolve hook called for promise without prior call to init hook'); |
| 59 | + |
| 60 | + promisesInitState.set(asyncId, 'resolved'); |
| 61 | +} |
| 62 | + |
| 63 | +const timeout = common.platformTimeout(10); |
| 64 | + |
| 65 | +function checkPromisesInitState() { |
| 66 | + for (const initState of promisesInitState.values()) { |
| 67 | + assert.strictEqual(initState, 'resolved', |
| 68 | + 'promise initialized without being resolved'); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +function checkPromisesExecutionState() { |
| 73 | + for (const executionState of promisesExecutionState.values()) { |
| 74 | + assert.strictEqual(executionState, 'after', |
| 75 | + 'mismatch between before and after hook calls'); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +process.on('beforeExit', common.mustCall(() => { |
| 80 | + hooks.disable(); |
| 81 | + hooks.sanityCheck('PROMISE'); |
| 82 | + |
| 83 | + checkPromisesInitState(); |
| 84 | + checkPromisesExecutionState(); |
| 85 | +})); |
| 86 | + |
| 87 | +async function asyncFunc() { |
| 88 | + await sleep(timeout); |
| 89 | +} |
| 90 | + |
| 91 | +asyncFunc(); |
0 commit comments