2017-06-05 15:35:06 +08:00
|
|
|
'use strict';
|
2017-11-13 01:46:55 +08:00
|
|
|
// Flags: --expose-internals
|
2017-06-05 15:35:06 +08:00
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
2017-11-13 01:46:55 +08:00
|
|
|
const async_hooks = require('internal/async_hooks');
|
2017-06-05 15:35:06 +08:00
|
|
|
const initHooks = require('./init-hooks');
|
|
|
|
|
2018-02-12 05:35:59 +08: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 21:59:23 +08:00
|
|
|
async_hooks.emitBefore(expectedId, expectedTriggerId);
|
2017-06-05 15:35:06 +08:00
|
|
|
async_hooks.emitAfter(expectedId);
|
|
|
|
|
2019-12-15 04:02:50 +08: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-15 04:02:50 +08:00
|
|
|
onbefore: checkOnce(chkBefore),
|
|
|
|
onafter: checkOnce(chkAfter),
|
2022-11-22 01:43:47 +08:00
|
|
|
allowNoInit: true,
|
2017-06-05 15:35:06 +08:00
|
|
|
}).enable();
|
|
|
|
|
|
|
|
async_hooks.emitInit(expectedId, expectedType, expectedTriggerId);
|
2017-07-02 21:59:23 +08:00
|
|
|
async_hooks.emitBefore(expectedId, expectedTriggerId);
|
2017-06-05 15:35:06 +08:00
|
|
|
async_hooks.emitAfter(expectedId);
|