summaryrefslogtreecommitdiff
path: root/test/addons/async-hooks-promise/test.js
blob: a6c48e94a34f074f22d702acfe7d5c81643f5924 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';

const common = require('../../common');
const assert = require('assert');
const async_hooks = require('async_hooks');
const binding = require(`./build/${common.buildType}/binding`);

if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) {
  common.skip('cannot test with env var NODE_TEST_WITH_ASYNC_HOOKS');
  return;
}

// Baseline to make sure the internal field isn't being set.
assert.strictEqual(
  binding.getPromiseField(Promise.resolve(1)),
  0);

const hook0 = async_hooks.createHook({}).enable();

// Check that no PromiseWrap is created when there are no hook callbacks.
assert.strictEqual(
  binding.getPromiseField(Promise.resolve(1)),
  0);

hook0.disable();

let pwrap = null;
const hook1 = async_hooks.createHook({
  init(id, type, tid, resource) {
    pwrap = resource;
  }
}).enable();

// Check that the internal field returns the same PromiseWrap passed to init().
assert.strictEqual(
  binding.getPromiseField(Promise.resolve(1)),
  pwrap);

hook1.disable();

// Check that internal fields are no longer being set. This needs to be delayed
// a bit because the `disable()` call only schedules disabling the hook in a
// future microtask.
setImmediate(() => {
  assert.strictEqual(
    binding.getPromiseField(Promise.resolve(1)),
    0);
});