0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/async-hooks/test-immediate.js
Artur Hayrapetyan 2a1492a00d test: move tick.js from test/async-hooks to test/common
PR-URL: https://github.com/nodejs/node/pull/23551
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
2018-10-15 07:41:12 +02:00

64 lines
2.1 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const tick = require('../common/tick');
const initHooks = require('./init-hooks');
const { checkInvocations } = require('./hook-checks');
const hooks = initHooks();
hooks.enable();
// install first immediate
setImmediate(common.mustCall(onimmediate));
const as = hooks.activitiesOfTypes('Immediate');
assert.strictEqual(as.length, 1);
const imd1 = as[0];
assert.strictEqual(imd1.type, 'Immediate');
assert.strictEqual(typeof imd1.uid, 'number');
assert.strictEqual(typeof imd1.triggerAsyncId, 'number');
checkInvocations(imd1, { init: 1 },
'imd1: when first set immediate installed');
let imd2;
function onimmediate() {
let as = hooks.activitiesOfTypes('Immediate');
assert.strictEqual(as.length, 1);
checkInvocations(imd1, { init: 1, before: 1 },
'imd1: when first set immediate triggered');
// install second immediate
setImmediate(common.mustCall(onimmediateTwo));
as = hooks.activitiesOfTypes('Immediate');
assert.strictEqual(as.length, 2);
imd2 = as[1];
assert.strictEqual(imd2.type, 'Immediate');
assert.strictEqual(typeof imd2.uid, 'number');
assert.strictEqual(typeof imd2.triggerAsyncId, 'number');
checkInvocations(imd1, { init: 1, before: 1 },
'imd1: when second set immediate installed');
checkInvocations(imd2, { init: 1 },
'imd2: when second set immediate installed');
}
function onimmediateTwo() {
checkInvocations(imd1, { init: 1, before: 1, after: 1, destroy: 1 },
'imd1: when second set immediate triggered');
checkInvocations(imd2, { init: 1, before: 1 },
'imd2: when second set immediate triggered');
tick(1);
}
process.on('exit', onexit);
function onexit() {
hooks.disable();
hooks.sanityCheck('Immediate');
checkInvocations(imd1, { init: 1, before: 1, after: 1, destroy: 1 },
'imd1: when process exits');
checkInvocations(imd2, { init: 1, before: 1, after: 1, destroy: 1 },
'imd2: when process exits');
}