MCPcopy Create free account
hub / github.com/nodejs/node-addon-api / installAsyncHooksForTest

Function installAsyncHooksForTest

test/async_worker.js:21–63  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

19module.exports = common.runTest(test);
20
21function installAsyncHooksForTest () {
22 return new Promise((resolve, reject) => {
23 let id;
24 const events = [];
25 /**
26 * TODO(legendecas): investigate why resolving & disabling hooks in
27 * destroy callback causing crash with case 'callbackscope.js'.
28 */
29 let destroyed = false;
30 const interval = setInterval(() => {
31 if (destroyed) {
32 hook.disable();
33 clearInterval(interval);
34 resolve(events);
35 }
36 }, 10);
37
38 const hook = asyncHooks.createHook({
39 init (asyncId, type, triggerAsyncId, resource) {
40 if (id === undefined && type === 'TestResource') {
41 id = asyncId;
42 events.push({ eventName: 'init', type, triggerAsyncId, resource });
43 }
44 },
45 before (asyncId) {
46 if (asyncId === id) {
47 events.push({ eventName: 'before' });
48 }
49 },
50 after (asyncId) {
51 if (asyncId === id) {
52 events.push({ eventName: 'after' });
53 }
54 },
55 destroy (asyncId) {
56 if (asyncId === id) {
57 events.push({ eventName: 'destroy' });
58 destroyed = true;
59 }
60 }
61 }).enable();
62 });
63}
64
65async function test (binding) {
66 const libUvThreadCount = Number(process.env.UV_THREADPOOL_SIZE || 4);

Callers 1

testFunction · 0.70

Calls

no outgoing calls

Tested by 1

testFunction · 0.56