11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst tick = require('../common/tick');
61cb0ef41Sopenharmony_ciconst initHooks = require('./init-hooks');
71cb0ef41Sopenharmony_ciconst { checkInvocations } = require('./hook-checks');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst hooks = initHooks();
101cb0ef41Sopenharmony_cihooks.enable();
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// Install first immediate
131cb0ef41Sopenharmony_cisetImmediate(common.mustCall(onimmediate));
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst as = hooks.activitiesOfTypes('Immediate');
161cb0ef41Sopenharmony_ciassert.strictEqual(as.length, 1);
171cb0ef41Sopenharmony_ciconst imd1 = as[0];
181cb0ef41Sopenharmony_ciassert.strictEqual(imd1.type, 'Immediate');
191cb0ef41Sopenharmony_ciassert.strictEqual(typeof imd1.uid, 'number');
201cb0ef41Sopenharmony_ciassert.strictEqual(typeof imd1.triggerAsyncId, 'number');
211cb0ef41Sopenharmony_cicheckInvocations(imd1, { init: 1 },
221cb0ef41Sopenharmony_ci                 'imd1: when first set immediate installed');
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cilet imd2;
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_cifunction onimmediate() {
271cb0ef41Sopenharmony_ci  let as = hooks.activitiesOfTypes('Immediate');
281cb0ef41Sopenharmony_ci  assert.strictEqual(as.length, 1);
291cb0ef41Sopenharmony_ci  checkInvocations(imd1, { init: 1, before: 1 },
301cb0ef41Sopenharmony_ci                   'imd1: when first set immediate triggered');
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  // Install second immediate
331cb0ef41Sopenharmony_ci  setImmediate(common.mustCall(onimmediateTwo));
341cb0ef41Sopenharmony_ci  as = hooks.activitiesOfTypes('Immediate');
351cb0ef41Sopenharmony_ci  assert.strictEqual(as.length, 2);
361cb0ef41Sopenharmony_ci  imd2 = as[1];
371cb0ef41Sopenharmony_ci  assert.strictEqual(imd2.type, 'Immediate');
381cb0ef41Sopenharmony_ci  assert.strictEqual(typeof imd2.uid, 'number');
391cb0ef41Sopenharmony_ci  assert.strictEqual(typeof imd2.triggerAsyncId, 'number');
401cb0ef41Sopenharmony_ci  checkInvocations(imd1, { init: 1, before: 1 },
411cb0ef41Sopenharmony_ci                   'imd1: when second set immediate installed');
421cb0ef41Sopenharmony_ci  checkInvocations(imd2, { init: 1 },
431cb0ef41Sopenharmony_ci                   'imd2: when second set immediate installed');
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cifunction onimmediateTwo() {
471cb0ef41Sopenharmony_ci  checkInvocations(imd1, { init: 1, before: 1, after: 1, destroy: 1 },
481cb0ef41Sopenharmony_ci                   'imd1: when second set immediate triggered');
491cb0ef41Sopenharmony_ci  checkInvocations(imd2, { init: 1, before: 1 },
501cb0ef41Sopenharmony_ci                   'imd2: when second set immediate triggered');
511cb0ef41Sopenharmony_ci  tick(1);
521cb0ef41Sopenharmony_ci}
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciprocess.on('exit', onexit);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_cifunction onexit() {
571cb0ef41Sopenharmony_ci  hooks.disable();
581cb0ef41Sopenharmony_ci  hooks.sanityCheck('Immediate');
591cb0ef41Sopenharmony_ci  checkInvocations(imd1, { init: 1, before: 1, after: 1, destroy: 1 },
601cb0ef41Sopenharmony_ci                   'imd1: when process exits');
611cb0ef41Sopenharmony_ci  checkInvocations(imd2, { init: 1, before: 1, after: 1, destroy: 1 },
621cb0ef41Sopenharmony_ci                   'imd2: when process exits');
631cb0ef41Sopenharmony_ci}
64