-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathdiagnostics-test.ts
More file actions
120 lines (109 loc) · 3.96 KB
/
Copy pathdiagnostics-test.ts
File metadata and controls
120 lines (109 loc) · 3.96 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* eslint-disable n/no-unsupported-features/node-builtins */
import dc from 'node:diagnostics_channel';
import { describe, it } from 'node:test';
import { expect } from 'chai';
import { invariant } from '../jsutils/invariant.ts';
import type { MinimalChannel, MinimalTracingChannel } from '../diagnostics.ts';
import {
executeChannel,
executeRootSelectionSetChannel,
executeVariableCoercionChannel,
parseChannel,
resolveChannel,
shouldTrace,
subscribeChannel,
validateChannel,
} from '../diagnostics.ts';
describe('diagnostics', () => {
it('auto-registers the GraphQL tracing channels', () => {
invariant(parseChannel !== undefined);
invariant(validateChannel !== undefined);
invariant(executeChannel !== undefined);
invariant(executeVariableCoercionChannel !== undefined);
invariant(executeRootSelectionSetChannel !== undefined);
invariant(subscribeChannel !== undefined);
invariant(resolveChannel !== undefined);
// Node.js `tracingChannel(name)` returns a fresh wrapper per call but
// the underlying sub-channels are cached by name, so compare those.
expect(parseChannel.start).to.equal(
dc.channel('tracing:graphql:parse:start'),
);
expect(validateChannel.start).to.equal(
dc.channel('tracing:graphql:validate:start'),
);
expect(executeChannel.start).to.equal(
dc.channel('tracing:graphql:execute:start'),
);
expect(executeVariableCoercionChannel.start).to.equal(
dc.channel('tracing:graphql:execute:variableCoercion:start'),
);
expect(executeRootSelectionSetChannel.start).to.equal(
dc.channel('tracing:graphql:execute:rootSelectionSet:start'),
);
expect(subscribeChannel.start).to.equal(
dc.channel('tracing:graphql:subscribe:start'),
);
expect(resolveChannel.start).to.equal(
dc.channel('tracing:graphql:resolve:start'),
);
});
describe('shouldTrace', () => {
function makeSubChannel(hasSubscribers: boolean): MinimalChannel {
return {
hasSubscribers,
publish: () => undefined,
runStores<T, ContextType extends object>(
context: ContextType,
fn: (this: ContextType, ...args: Array<unknown>) => T,
): T {
return fn.call(context);
},
};
}
function makeFallbackTracingChannel(
subscribedSubChannel?: keyof Pick<
MinimalTracingChannel,
'start' | 'end' | 'asyncStart' | 'asyncEnd' | 'error'
>,
): MinimalTracingChannel {
return {
hasSubscribers: undefined,
start: makeSubChannel(subscribedSubChannel === 'start'),
end: makeSubChannel(subscribedSubChannel === 'end'),
asyncStart: makeSubChannel(subscribedSubChannel === 'asyncStart'),
asyncEnd: makeSubChannel(subscribedSubChannel === 'asyncEnd'),
error: makeSubChannel(subscribedSubChannel === 'error'),
traceSync<T>(fn: (...args: Array<unknown>) => T): T {
return fn();
},
};
}
it('returns false when channel is undefined', () => {
expect(shouldTrace(undefined)).to.equal(false);
});
it('reflects the aggregate hasSubscribers on a real tracing channel', () => {
const tc = dc.tracingChannel(
'shouldTrace:aggregate',
) as unknown as MinimalTracingChannel;
expect(shouldTrace(tc)).to.equal(false);
const handler = {
start: () => undefined,
end: () => undefined,
asyncStart: () => undefined,
asyncEnd: () => undefined,
error: () => undefined,
};
const realTC = dc.tracingChannel('shouldTrace:aggregate');
realTC.subscribe(handler);
try {
expect(shouldTrace(tc)).to.equal(true);
} finally {
realTC.unsubscribe(handler);
}
});
it('falls back to sub-channel subscribers when aggregate is missing', () => {
expect(shouldTrace(makeFallbackTracingChannel('error'))).to.equal(true);
expect(shouldTrace(makeFallbackTracingChannel())).to.equal(false);
});
});
});