1'use strict'; 2 3const common = require('../common'); 4const assert = require('assert'); 5const tick = require('../common/tick'); 6const initHooks = require('./init-hooks'); 7const { checkInvocations } = require('./hook-checks'); 8 9const hooks = initHooks(); 10hooks.enable(); 11 12// Install first immediate 13setImmediate(common.mustCall(onimmediate)); 14 15const as = hooks.activitiesOfTypes('Immediate'); 16assert.strictEqual(as.length, 1); 17const imd1 = as[0]; 18assert.strictEqual(imd1.type, 'Immediate'); 19assert.strictEqual(typeof imd1.uid, 'number'); 20assert.strictEqual(typeof imd1.triggerAsyncId, 'number'); 21checkInvocations(imd1, { init: 1 }, 22 'imd1: when first set immediate installed'); 23 24let imd2; 25 26function onimmediate() { 27 let as = hooks.activitiesOfTypes('Immediate'); 28 assert.strictEqual(as.length, 1); 29 checkInvocations(imd1, { init: 1, before: 1 }, 30 'imd1: when first set immediate triggered'); 31 32 // Install second immediate 33 setImmediate(common.mustCall(onimmediateTwo)); 34 as = hooks.activitiesOfTypes('Immediate'); 35 assert.strictEqual(as.length, 2); 36 imd2 = as[1]; 37 assert.strictEqual(imd2.type, 'Immediate'); 38 assert.strictEqual(typeof imd2.uid, 'number'); 39 assert.strictEqual(typeof imd2.triggerAsyncId, 'number'); 40 checkInvocations(imd1, { init: 1, before: 1 }, 41 'imd1: when second set immediate installed'); 42 checkInvocations(imd2, { init: 1 }, 43 'imd2: when second set immediate installed'); 44} 45 46function onimmediateTwo() { 47 checkInvocations(imd1, { init: 1, before: 1, after: 1, destroy: 1 }, 48 'imd1: when second set immediate triggered'); 49 checkInvocations(imd2, { init: 1, before: 1 }, 50 'imd2: when second set immediate triggered'); 51 tick(1); 52} 53 54process.on('exit', onexit); 55 56function onexit() { 57 hooks.disable(); 58 hooks.sanityCheck('Immediate'); 59 checkInvocations(imd1, { init: 1, before: 1, after: 1, destroy: 1 }, 60 'imd1: when process exits'); 61 checkInvocations(imd2, { init: 1, before: 1, after: 1, destroy: 1 }, 62 'imd2: when process exits'); 63} 64