• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2// Flags: --expose-internals
3
4const common = require('../common');
5const assert = require('assert');
6const async_hooks = require('internal/async_hooks');
7const initHooks = require('./init-hooks');
8
9const expectedId = async_hooks.newAsyncId();
10const expectedTriggerId = async_hooks.newAsyncId();
11const expectedType = 'test_emit_before_after_type';
12
13// Verify that if there is no registered hook, then nothing will happen.
14async_hooks.emitBefore(expectedId, expectedTriggerId);
15async_hooks.emitAfter(expectedId);
16
17const chkBefore = common.mustCall((id) => assert.strictEqual(id, expectedId));
18const chkAfter = common.mustCall((id) => assert.strictEqual(id, expectedId));
19
20const checkOnce = (fn) => {
21  let called = false;
22  return (...args) => {
23    if (called) return;
24
25    called = true;
26    fn(...args);
27  };
28};
29
30initHooks({
31  onbefore: checkOnce(chkBefore),
32  onafter: checkOnce(chkAfter),
33  allowNoInit: true
34}).enable();
35
36async_hooks.emitInit(expectedId, expectedType, expectedTriggerId);
37async_hooks.emitBefore(expectedId, expectedTriggerId);
38async_hooks.emitAfter(expectedId);
39