• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const async_hooks = require('async_hooks');
6const EXPECTED_INITS = 2;
7let p_er = null;
8let p_inits = 0;
9
10// Not useful to place common.mustCall() around 'exit' event b/c it won't be
11// able to check it anyway.
12process.on('exit', (code) => {
13  if (code !== 0)
14    return;
15  if (p_er !== null)
16    throw p_er;
17  // Expecting exactly 2 PROMISE types to reach init.
18  assert.strictEqual(p_inits, EXPECTED_INITS);
19});
20
21const mustCallInit = common.mustCall(function init(id, type, tid, resource) {
22  if (type !== 'PROMISE')
23    return;
24  p_inits++;
25}, EXPECTED_INITS);
26
27const hook = async_hooks.createHook({
28  init: mustCallInit
29// Enable then disable to test whether disable() actually works.
30}).enable().disable().disable();
31
32new Promise(common.mustCall((res) => {
33  res(42);
34})).then(common.mustCall((val) => {
35  hook.enable().enable();
36  const p = new Promise((res) => res(val));
37  hook.disable();
38  return p;
39})).then(common.mustCall((val2) => {
40  hook.enable();
41  const p = new Promise((res) => res(val2));
42  hook.disable();
43  return p;
44})).catch((er) => p_er = er);
45