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'); 8const TIMEOUT = common.platformTimeout(100); 9 10const hooks = initHooks(); 11hooks.enable(); 12 13// Install first timeout 14setTimeout(common.mustCall(ontimeout), TIMEOUT); 15const as = hooks.activitiesOfTypes('Timeout'); 16assert.strictEqual(as.length, 1); 17const t1 = as[0]; 18assert.strictEqual(t1.type, 'Timeout'); 19assert.strictEqual(typeof t1.uid, 'number'); 20assert.strictEqual(typeof t1.triggerAsyncId, 'number'); 21checkInvocations(t1, { init: 1 }, 't1: when first timer installed'); 22 23let timer; 24let t2; 25function ontimeout() { 26 checkInvocations(t1, { init: 1, before: 1 }, 't1: when first timer fired'); 27 28 setTimeout(onSecondTimeout, TIMEOUT).unref(); 29 const as = hooks.activitiesOfTypes('Timeout'); 30 t2 = as[1]; 31 assert.strictEqual(as.length, 2); 32 checkInvocations(t1, { init: 1, before: 1 }, 33 't1: when second timer installed'); 34 checkInvocations(t2, { init: 1 }, 35 't2: when second timer installed'); 36 37 timer = setTimeout(common.mustNotCall(), 2 ** 31 - 1); 38} 39 40function onSecondTimeout() { 41 const as = hooks.activitiesOfTypes('Timeout'); 42 assert.strictEqual(as.length, 3); 43 checkInvocations(t1, { init: 1, before: 1, after: 1 }, 44 't1: when second timer fired'); 45 checkInvocations(t2, { init: 1, before: 1 }, 46 't2: when second timer fired'); 47 clearTimeout(timer); 48 tick(2); 49} 50 51process.on('exit', onexit); 52 53function onexit() { 54 hooks.disable(); 55 hooks.sanityCheck('Timeout'); 56 57 checkInvocations(t1, { init: 1, before: 1, after: 1, destroy: 1 }, 58 't1: when process exits'); 59 checkInvocations(t2, { init: 1, before: 1, after: 1, destroy: 1 }, 60 't2: when process exits'); 61} 62