• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const initHooks = require('./init-hooks');
6const { checkInvocations } = require('./hook-checks');
7
8const p = new Promise(common.mustCall(function executor(resolve) {
9  resolve(5);
10}));
11
12// Init hooks after promise was created
13const hooks = initHooks({ allowNoInit: true });
14hooks.enable();
15
16p.then(function afterResolution(val) {
17  assert.strictEqual(val, 5);
18  const as = hooks.activitiesOfTypes('PROMISE');
19  assert.strictEqual(as.length, 1);
20  checkInvocations(as[0], { init: 1, before: 1 },
21                   'after resolution child promise');
22  return val;
23});
24
25process.on('exit', function onexit() {
26  hooks.disable();
27  hooks.sanityCheck('PROMISE');
28
29  const as = hooks.activitiesOfTypes('PROMISE');
30  assert.strictEqual(as.length, 1);
31
32  const a0 = as[0];
33  assert.strictEqual(a0.type, 'PROMISE');
34  assert.strictEqual(typeof a0.uid, 'number');
35  // We can't get the asyncId from the parent dynamically, since init was
36  // never called. However, it is known that the parent promise was created
37  // immediately before the child promise, thus there should only be one
38  // difference in id.
39  assert.strictEqual(a0.triggerAsyncId, a0.uid - 1);
40  // We expect a destroy hook as well but we cannot guarantee predictable gc.
41  checkInvocations(a0, { init: 1, before: 1, after: 1 }, 'when process exits');
42});
43