forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-async-hooks-correctly-switch-promise-hook.js
61 lines (53 loc) · 1.3 KB
/
test-async-hooks-correctly-switch-promise-hook.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
require('../common');
const assert = require('assert');
const async_hooks = require('async_hooks');
// Regression test for:
// - https://github.com/nodejs/node/issues/38814
// - https://github.com/nodejs/node/issues/38815
const expected = [
['init', 2, 'PROMISE', 1],
['init', 3, 'PROMISE', 1],
['promiseResolve', 3],
['promiseResolve', 2],
['before', 2],
['init', 4, 'PROMISE', 3],
['after', 2],
['before', 4],
['promiseResolve', 2],
['promiseResolve', 4],
['after', 4],
];
const actual = [];
// Only init to start context-based promise hook
async_hooks.createHook({
init() { }
}).enable();
// With destroy, this should switch to native
// and disable context - based promise hook
async_hooks.createHook({
init(asyncId, type, triggerAsyncId) {
actual.push([ 'init', asyncId, type, triggerAsyncId ]);
},
before(asyncId) {
actual.push([ 'before', asyncId ]);
},
after(asyncId) {
actual.push([ 'after', asyncId ]);
},
promiseResolve(asyncId) {
actual.push([ 'promiseResolve', asyncId ]);
},
destroy(asyncId) {
actual.push([ 'destroy', asyncId ]);
}
}).enable();
async function main() {
return Promise.resolve();
}
main();
process.on('exit', () => {
for (const event of expected) {
assert.deepStrictEqual(actual.shift(), event);
}
});