11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ciconst tick = require('../common/tick'); 61cb0ef41Sopenharmony_ciconst async_hooks = require('async_hooks'); 71cb0ef41Sopenharmony_ciconst { AsyncResource } = async_hooks; 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst initHooks = require('./init-hooks'); 101cb0ef41Sopenharmony_ciconst { checkInvocations } = require('./hook-checks'); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciconst hooks = initHooks(); 131cb0ef41Sopenharmony_cihooks.enable(); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciassert.throws( 161cb0ef41Sopenharmony_ci () => new AsyncResource(), { 171cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 181cb0ef41Sopenharmony_ci name: 'TypeError', 191cb0ef41Sopenharmony_ci }); 201cb0ef41Sopenharmony_ciassert.throws(() => { 211cb0ef41Sopenharmony_ci new AsyncResource('invalid_trigger_id', { triggerAsyncId: null }); 221cb0ef41Sopenharmony_ci}, { 231cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ASYNC_ID', 241cb0ef41Sopenharmony_ci name: 'RangeError', 251cb0ef41Sopenharmony_ci}); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciassert.strictEqual( 281cb0ef41Sopenharmony_ci new AsyncResource('default_trigger_id').triggerAsyncId(), 291cb0ef41Sopenharmony_ci async_hooks.executionAsyncId(), 301cb0ef41Sopenharmony_ci); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci// Create first custom event 'alcazares' with triggerAsyncId derived 331cb0ef41Sopenharmony_ci// from async_hooks executionAsyncId 341cb0ef41Sopenharmony_ciconst alcaTriggerId = async_hooks.executionAsyncId(); 351cb0ef41Sopenharmony_ciconst alcaEvent = new AsyncResource('alcazares', alcaTriggerId); 361cb0ef41Sopenharmony_ciconst alcazaresActivities = hooks.activitiesOfTypes([ 'alcazares' ]); 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci// Alcazares event was constructed and thus only has an `init` call 391cb0ef41Sopenharmony_ciassert.strictEqual(alcazaresActivities.length, 1); 401cb0ef41Sopenharmony_ciconst alcazares = alcazaresActivities[0]; 411cb0ef41Sopenharmony_ciassert.strictEqual(alcazares.type, 'alcazares'); 421cb0ef41Sopenharmony_ciassert.strictEqual(typeof alcazares.uid, 'number'); 431cb0ef41Sopenharmony_ciassert.strictEqual(alcazares.triggerAsyncId, alcaTriggerId); 441cb0ef41Sopenharmony_cicheckInvocations(alcazares, { init: 1 }, 'alcazares constructed'); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ciassert.strictEqual(typeof alcaEvent.asyncId(), 'number'); 471cb0ef41Sopenharmony_ciassert.notStrictEqual(alcaEvent.asyncId(), alcaTriggerId); 481cb0ef41Sopenharmony_ciassert.strictEqual(alcaEvent.triggerAsyncId(), alcaTriggerId); 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_cialcaEvent.runInAsyncScope(() => { 511cb0ef41Sopenharmony_ci checkInvocations(alcazares, { init: 1, before: 1 }, 521cb0ef41Sopenharmony_ci 'alcazares emitted before'); 531cb0ef41Sopenharmony_ci}); 541cb0ef41Sopenharmony_cicheckInvocations(alcazares, { init: 1, before: 1, after: 1 }, 551cb0ef41Sopenharmony_ci 'alcazares emitted after'); 561cb0ef41Sopenharmony_cialcaEvent.runInAsyncScope(() => { 571cb0ef41Sopenharmony_ci checkInvocations(alcazares, { init: 1, before: 2, after: 1 }, 581cb0ef41Sopenharmony_ci 'alcazares emitted before again'); 591cb0ef41Sopenharmony_ci}); 601cb0ef41Sopenharmony_cicheckInvocations(alcazares, { init: 1, before: 2, after: 2 }, 611cb0ef41Sopenharmony_ci 'alcazares emitted after again'); 621cb0ef41Sopenharmony_cialcaEvent.emitDestroy(); 631cb0ef41Sopenharmony_citick(1, common.mustCall(tick1)); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_cifunction tick1() { 661cb0ef41Sopenharmony_ci checkInvocations(alcazares, { init: 1, before: 2, after: 2, destroy: 1 }, 671cb0ef41Sopenharmony_ci 'alcazares emitted destroy'); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci // The below shows that we can pass any number as a trigger id 701cb0ef41Sopenharmony_ci const pobTriggerId = 111; 711cb0ef41Sopenharmony_ci const pobEvent = new AsyncResource('poblado', pobTriggerId); 721cb0ef41Sopenharmony_ci const pobladoActivities = hooks.activitiesOfTypes([ 'poblado' ]); 731cb0ef41Sopenharmony_ci const poblado = pobladoActivities[0]; 741cb0ef41Sopenharmony_ci assert.strictEqual(poblado.type, 'poblado'); 751cb0ef41Sopenharmony_ci assert.strictEqual(typeof poblado.uid, 'number'); 761cb0ef41Sopenharmony_ci assert.strictEqual(poblado.triggerAsyncId, pobTriggerId); 771cb0ef41Sopenharmony_ci checkInvocations(poblado, { init: 1 }, 'poblado constructed'); 781cb0ef41Sopenharmony_ci pobEvent.runInAsyncScope(() => { 791cb0ef41Sopenharmony_ci checkInvocations(poblado, { init: 1, before: 1 }, 801cb0ef41Sopenharmony_ci 'poblado emitted before'); 811cb0ef41Sopenharmony_ci }); 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci checkInvocations(poblado, { init: 1, before: 1, after: 1 }, 841cb0ef41Sopenharmony_ci 'poblado emitted after'); 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci // After we disable the hooks we shouldn't receive any events anymore 871cb0ef41Sopenharmony_ci hooks.disable(); 881cb0ef41Sopenharmony_ci alcaEvent.emitDestroy(); 891cb0ef41Sopenharmony_ci tick(1, common.mustCall(tick2)); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci function tick2() { 921cb0ef41Sopenharmony_ci checkInvocations( 931cb0ef41Sopenharmony_ci alcazares, { init: 1, before: 2, after: 2, destroy: 1 }, 941cb0ef41Sopenharmony_ci 'alcazares emitted destroy a second time after hooks disabled'); 951cb0ef41Sopenharmony_ci pobEvent.emitDestroy(); 961cb0ef41Sopenharmony_ci tick(1, common.mustCall(tick3)); 971cb0ef41Sopenharmony_ci } 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci function tick3() { 1001cb0ef41Sopenharmony_ci checkInvocations(poblado, { init: 1, before: 1, after: 1 }, 1011cb0ef41Sopenharmony_ci 'poblado emitted destroy after hooks disabled'); 1021cb0ef41Sopenharmony_ci } 1031cb0ef41Sopenharmony_ci} 104