1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const async_hooks = require('async_hooks'); 5 6// Checks that enabling async hooks in a callback actually 7// triggers after & destroy as expected. 8 9const fnsToTest = [setTimeout, (cb) => { 10 setImmediate(() => { 11 cb(); 12 13 // We need to keep the event loop open for this to actually work 14 // since destroy hooks are triggered in unrefed Immediates 15 setImmediate(() => { 16 hook.disable(); 17 }); 18 }); 19}, (cb) => { 20 setImmediate(() => { 21 process.nextTick(() => { 22 cb(); 23 24 // We need to keep the event loop open for this to actually work 25 // since destroy hooks are triggered in unrefed Immediates 26 setImmediate(() => { 27 hook.disable(); 28 assert.strictEqual(fnsToTest.length, 0); 29 }); 30 }); 31 }); 32}]; 33 34const hook = async_hooks.createHook({ 35 before: common.mustNotCall(), 36 after: common.mustCall(() => {}, 3), 37 destroy: common.mustCall(() => { 38 hook.disable(); 39 nextTest(); 40 }, 3) 41}); 42 43nextTest(); 44 45function nextTest() { 46 if (fnsToTest.length > 0) { 47 fnsToTest.shift()(common.mustCall(() => { 48 hook.enable(); 49 })); 50 } 51} 52