1'use strict'; 2 3const common = require('../common'); 4if (!common.hasCrypto) 5 common.skip('missing crypto'); 6 7const assert = require('assert'); 8const async_hooks = require('async_hooks'); 9const call_log = [0, 0, 0, 0]; // [before, callback, exception, after]; 10let call_id = null; 11let hooks = null; 12 13 14process.on('beforeExit', common.mustCall(() => { 15 process.removeAllListeners('uncaughtException'); 16 hooks.disable(); 17 assert.strictEqual(typeof call_id, 'number'); 18 assert.deepStrictEqual(call_log, [1, 1, 1, 1]); 19})); 20 21 22hooks = async_hooks.createHook({ 23 init(id, type) { 24 if (type === 'RANDOMBYTESREQUEST') 25 call_id = id; 26 }, 27 before(id) { 28 if (id === call_id) call_log[0]++; 29 }, 30 after(id) { 31 if (id === call_id) call_log[3]++; 32 }, 33}).enable(); 34 35 36process.on('uncaughtException', common.mustCall(() => { 37 assert.strictEqual(call_id, async_hooks.executionAsyncId()); 38 call_log[2]++; 39})); 40 41 42require('crypto').randomBytes(1, common.mustCall(() => { 43 assert.strictEqual(call_id, async_hooks.executionAsyncId()); 44 call_log[1]++; 45 throw new Error(); 46})); 47