mirror of
https://github.com/nodejs/node.git
synced 2024-11-24 20:29:23 +01:00
cf46746b8a
PR-URL: https://github.com/nodejs/node/pull/45549 Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
'use strict';
|
|
// Flags: --expose-internals
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const async_hooks = require('internal/async_hooks');
|
|
const initHooks = require('./init-hooks');
|
|
|
|
const expectedId = async_hooks.newAsyncId();
|
|
const expectedTriggerId = async_hooks.newAsyncId();
|
|
const expectedType = 'test_emit_before_after_type';
|
|
|
|
// Verify that if there is no registered hook, then nothing will happen.
|
|
async_hooks.emitBefore(expectedId, expectedTriggerId);
|
|
async_hooks.emitAfter(expectedId);
|
|
|
|
const chkBefore = common.mustCall((id) => assert.strictEqual(id, expectedId));
|
|
const chkAfter = common.mustCall((id) => assert.strictEqual(id, expectedId));
|
|
|
|
const checkOnce = (fn) => {
|
|
let called = false;
|
|
return (...args) => {
|
|
if (called) return;
|
|
|
|
called = true;
|
|
fn(...args);
|
|
};
|
|
};
|
|
|
|
initHooks({
|
|
onbefore: checkOnce(chkBefore),
|
|
onafter: checkOnce(chkAfter),
|
|
allowNoInit: true,
|
|
}).enable();
|
|
|
|
async_hooks.emitInit(expectedId, expectedType, expectedTriggerId);
|
|
async_hooks.emitBefore(expectedId, expectedTriggerId);
|
|
async_hooks.emitAfter(expectedId);
|