async_hooks: use parent promise as triggerId · nodejs/node@135f4e6

1+

'use strict';

2+3+

const common = require('../common');

4+

const assert = require('assert');

5+

const initHooks = require('./init-hooks');

6+

const { checkInvocations } = require('./hook-checks');

7+8+

const p = new Promise(common.mustCall(function executor(resolve, reject) {

9+

resolve(5);

10+

}));

11+12+

// init hooks after promise was created

13+

const hooks = initHooks({allowNoInit: true});

14+

hooks.enable();

15+16+

p.then(function afterresolution(val) {

17+

assert.strictEqual(val, 5);

18+

const as = hooks.activitiesOfTypes('PROMISE');

19+

assert.strictEqual(as.length, 1, 'one activity');

20+

checkInvocations(as[0], { init: 1, before: 1 },

21+

'after resolution child promise');

22+

return val;

23+

});

24+25+

process.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.triggerId, a0.uid - 1);

40+

// We expect a destroy hook as well but we cannot guarentee predictable gc.

41+

checkInvocations(a0, { init: 1, before: 1, after: 1 }, 'when process exits');

42+

});