1'use strict'; 2 3const common = require('../common'); 4if (!common.hasCrypto) 5 common.skip('missing crypto'); 6if (!common.isMainThread) 7 common.skip('Worker bootstrapping works differently -> different async IDs'); 8 9const assert = require('assert'); 10const tick = require('../common/tick'); 11const initHooks = require('./init-hooks'); 12const { checkInvocations } = require('./hook-checks'); 13const crypto = require('crypto'); 14 15 16const hooks = initHooks(); 17 18hooks.enable(); 19crypto.randomBytes(1, common.mustCall(onrandomBytes)); 20 21function onrandomBytes() { 22 const as = hooks.activitiesOfTypes('RANDOMBYTESREQUEST'); 23 const a = as[0]; 24 checkInvocations(a, { init: 1, before: 1 }, 25 'while in onrandomBytes callback'); 26 tick(2); 27} 28 29process.on('exit', onexit); 30 31function onexit() { 32 hooks.disable(); 33 hooks.sanityCheck('RANDOMBYTESREQUEST'); 34 35 const as = hooks.activitiesOfTypes('RANDOMBYTESREQUEST'); 36 assert.strictEqual(as.length, 1); 37 38 const a = as[0]; 39 assert.strictEqual(a.type, 'RANDOMBYTESREQUEST'); 40 assert.strictEqual(typeof a.uid, 'number'); 41 assert.strictEqual(a.triggerAsyncId, 1); 42 checkInvocations(a, { init: 1, before: 1, after: 1, destroy: 1 }, 43 'when process exits'); 44} 45