1'use strict'; 2 3const common = require('../common'); 4 5const assert = require('assert'); 6const initHooks = require('./init-hooks'); 7const { checkInvocations } = require('./hook-checks'); 8 9if (!common.isMainThread) 10 common.skip('Worker bootstrapping works differently -> different async IDs'); 11 12const hooks = initHooks(); 13 14hooks.enable(); 15 16const p = new Promise(common.mustCall(executor)); 17p.then(function afterResolution(val) { 18 assert.strictEqual(val, 5); 19 const as = hooks.activitiesOfTypes('PROMISE'); 20 assert.strictEqual(as.length, 2); 21 checkInvocations(as[0], { init: 1 }, 'after resolution parent promise'); 22 checkInvocations(as[1], { init: 1, before: 1 }, 23 'after resolution child promise'); 24}); 25 26function executor(resolve) { 27 const as = hooks.activitiesOfTypes('PROMISE'); 28 assert.strictEqual(as.length, 1); 29 const a = as[0]; 30 checkInvocations(a, { init: 1 }, 'while in promise executor'); 31 resolve(5); 32} 33 34process.on('exit', onexit); 35function onexit() { 36 hooks.disable(); 37 hooks.sanityCheck('PROMISE'); 38 39 const as = hooks.activitiesOfTypes('PROMISE'); 40 assert.strictEqual(as.length, 2); 41 42 const a0 = as[0]; 43 assert.strictEqual(a0.type, 'PROMISE'); 44 assert.strictEqual(typeof a0.uid, 'number'); 45 assert.strictEqual(a0.triggerAsyncId, 1); 46 checkInvocations(a0, { init: 1 }, 'when process exits'); 47 48 const a1 = as[1]; 49 assert.strictEqual(a1.type, 'PROMISE'); 50 assert.strictEqual(typeof a1.uid, 'number'); 51 assert.strictEqual(a1.triggerAsyncId, a0.uid); 52 // We expect a destroy hook as well but we cannot guarantee predictable gc. 53 checkInvocations(a1, { init: 1, before: 1, after: 1 }, 'when process exits'); 54} 55