2017-06-05 15:35:06 +08:00
|
|
|
'use strict';
|
2017-11-12 18:46:55 +01:00
|
|
|
// Flags: --expose-internals
|
2017-06-05 15:35:06 +08:00
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
2017-11-12 18:46:55 +01:00
|
|
|
const async_hooks = require('internal/async_hooks');
|
2017-06-05 15:35:06 +08:00
|
|
|
const initHooks = require('./init-hooks');
|
|
|
|
|
2018-02-11 16:35:59 -05:00
|
|
|
const expectedId = async_hooks.newAsyncId();
|
|
|
|
const expectedTriggerId = async_hooks.newAsyncId();
|
2017-06-05 15:35:06 +08:00
|
|
|
const expectedType = 'test_emit_before_after_type';
|
|
|
|
|
|
|
|
// Verify that if there is no registered hook, then nothing will happen.
|
2017-07-02 15:59:23 +02:00
|
|
|
async_hooks.emitBefore(expectedId, expectedTriggerId);
|
2017-06-05 15:35:06 +08:00
|
|
|
async_hooks.emitAfter(expectedId);
|
|
|
|
|
2019-12-14 15:02:50 -05:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-06-05 15:35:06 +08:00
|
|
|
initHooks({
|
2019-12-14 15:02:50 -05:00
|
|
|
onbefore: checkOnce(chkBefore),
|
|
|
|
onafter: checkOnce(chkAfter),
|
2022-11-21 12:43:47 -05:00
|
|
|
allowNoInit: true,
|
2017-06-05 15:35:06 +08:00
|
|
|
}).enable();
|
|
|
|
|
|
|
|
async_hooks.emitInit(expectedId, expectedType, expectedTriggerId);
|
2017-07-02 15:59:23 +02:00
|
|
|
async_hooks.emitBefore(expectedId, expectedTriggerId);
|
2017-06-05 15:35:06 +08:00
|
|
|
async_hooks.emitAfter(expectedId);
|