|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const tick = require('./tick'); |
| 6 | +const initHooks = require('./init-hooks'); |
| 7 | +const { checkInvocations } = require('./hook-checks'); |
| 8 | +const TIMEOUT = common.platformTimeout(100); |
| 9 | + |
| 10 | +const hooks = initHooks(); |
| 11 | +hooks.enable(); |
| 12 | + |
| 13 | +// install first timeout |
| 14 | +setTimeout(common.mustCall(ontimeout), TIMEOUT); |
| 15 | +const as = hooks.activitiesOfTypes('Timeout'); |
| 16 | +assert.strictEqual(as.length, 1); |
| 17 | +const t1 = as[0]; |
| 18 | +assert.strictEqual(t1.type, 'Timeout'); |
| 19 | +assert.strictEqual(typeof t1.uid, 'number'); |
| 20 | +assert.strictEqual(typeof t1.triggerAsyncId, 'number'); |
| 21 | +checkInvocations(t1, { init: 1 }, 't1: when first timer installed'); |
| 22 | + |
| 23 | +let timer; |
| 24 | +let t2; |
| 25 | +function 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 | + |
| 40 | +function 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 | + |
| 51 | +process.on('exit', onexit); |
| 52 | + |
| 53 | +function 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 | +} |
0 commit comments