1 | import EventEmitterAsyncResource from '..';
|
---|
2 | import { createHook, executionAsyncId, executionAsyncResource } from 'async_hooks';
|
---|
3 | import { test } from 'tap';
|
---|
4 | import { promisify } from 'util';
|
---|
5 |
|
---|
6 | const tick = promisify(setImmediate);
|
---|
7 |
|
---|
8 | interface InitAsyncEvent {
|
---|
9 | name : 'init',
|
---|
10 | type : string,
|
---|
11 | triggerAsyncId : number,
|
---|
12 | resource: object
|
---|
13 | }
|
---|
14 | interface OtherAsyncEvent {
|
---|
15 | name : 'before' | 'after' | 'destroy'
|
---|
16 | }
|
---|
17 | type AsyncEvent = InitAsyncEvent | OtherAsyncEvent;
|
---|
18 |
|
---|
19 | function makeHook (trackedTypes : string[]) {
|
---|
20 | const eventMap = new Map<number, AsyncEvent[]>();
|
---|
21 |
|
---|
22 | function log (asyncId : number, name : 'before' | 'after' | 'destroy') {
|
---|
23 | const entry = eventMap.get(asyncId);
|
---|
24 | if (entry !== undefined) entry.push({ name });
|
---|
25 | }
|
---|
26 |
|
---|
27 | const hook = createHook({
|
---|
28 | init (asyncId, type, triggerAsyncId, resource) {
|
---|
29 | if (trackedTypes.includes(type)) {
|
---|
30 | eventMap.set(asyncId, [
|
---|
31 | { name: 'init', type, triggerAsyncId, resource }
|
---|
32 | ]);
|
---|
33 | }
|
---|
34 | },
|
---|
35 |
|
---|
36 | before (asyncId) { log(asyncId, 'before'); },
|
---|
37 | after (asyncId) { log(asyncId, 'after'); },
|
---|
38 | destroy (asyncId) { log(asyncId, 'destroy'); }
|
---|
39 | }).enable();
|
---|
40 | return {
|
---|
41 | done () {
|
---|
42 | hook.disable();
|
---|
43 | return new Set(eventMap.values());
|
---|
44 | },
|
---|
45 | ids () {
|
---|
46 | return new Set(eventMap.keys());
|
---|
47 | }
|
---|
48 | };
|
---|
49 | }
|
---|
50 |
|
---|
51 | test('tracks emit() calls correctly using async_hooks', async ({ is, isDeep }) => {
|
---|
52 | const tracer = makeHook(['Foo']);
|
---|
53 |
|
---|
54 | class Foo extends EventEmitterAsyncResource {}
|
---|
55 |
|
---|
56 | const origExecutionAsyncId = executionAsyncId();
|
---|
57 | const foo = new Foo();
|
---|
58 |
|
---|
59 | let called = false;
|
---|
60 | foo.on('someEvent', () => {
|
---|
61 | if (typeof executionAsyncResource === 'function') { // Node.js 12+ only
|
---|
62 | is(executionAsyncResource(), foo.asyncResource);
|
---|
63 | }
|
---|
64 | called = true;
|
---|
65 | });
|
---|
66 | foo.emit('someEvent');
|
---|
67 | is(called, true);
|
---|
68 |
|
---|
69 | isDeep([foo.asyncId()], [...tracer.ids()]);
|
---|
70 | is(foo.triggerAsyncId(), origExecutionAsyncId);
|
---|
71 | is(foo.asyncResource.eventEmitter, foo);
|
---|
72 |
|
---|
73 | foo.emitDestroy();
|
---|
74 |
|
---|
75 | await tick();
|
---|
76 |
|
---|
77 | isDeep(tracer.done(), new Set<AsyncEvent[]>([
|
---|
78 | [
|
---|
79 | { name: 'init', type: 'Foo', triggerAsyncId: origExecutionAsyncId, resource: foo.asyncResource },
|
---|
80 | { name: 'before' },
|
---|
81 | { name: 'after' },
|
---|
82 | { name: 'destroy' }
|
---|
83 | ]
|
---|
84 | ]));
|
---|
85 | });
|
---|
86 |
|
---|
87 | test('can explicitly specify name as positional arg', async ({ isDeep }) => {
|
---|
88 | const tracer = makeHook(['ResourceName']);
|
---|
89 |
|
---|
90 | const origExecutionAsyncId = executionAsyncId();
|
---|
91 | class Foo extends EventEmitterAsyncResource {}
|
---|
92 |
|
---|
93 | const foo = new Foo('ResourceName');
|
---|
94 |
|
---|
95 | isDeep(tracer.done(), new Set<AsyncEvent[]>([
|
---|
96 | [
|
---|
97 | { name: 'init', type: 'ResourceName', triggerAsyncId: origExecutionAsyncId, resource: foo.asyncResource }
|
---|
98 | ]
|
---|
99 | ]));
|
---|
100 | });
|
---|
101 |
|
---|
102 | test('can explicitly specify name as option', async ({ isDeep }) => {
|
---|
103 | const tracer = makeHook(['ResourceName']);
|
---|
104 |
|
---|
105 | const origExecutionAsyncId = executionAsyncId();
|
---|
106 | class Foo extends EventEmitterAsyncResource {}
|
---|
107 |
|
---|
108 | const foo = new Foo({ name: 'ResourceName' });
|
---|
109 |
|
---|
110 | isDeep(tracer.done(), new Set<AsyncEvent[]>([
|
---|
111 | [
|
---|
112 | { name: 'init', type: 'ResourceName', triggerAsyncId: origExecutionAsyncId, resource: foo.asyncResource }
|
---|
113 | ]
|
---|
114 | ]));
|
---|
115 | });
|
---|
116 |
|
---|
117 | test('is provided as export on itself', async ({ is }) => {
|
---|
118 | is(EventEmitterAsyncResource.EventEmitterAsyncResource, EventEmitterAsyncResource);
|
---|
119 | });
|
---|