forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AsyncHooksContextManager.test.ts
353 lines (321 loc) · 11.8 KB
/
AsyncHooksContextManager.test.ts
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*!
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as assert from 'assert';
import { AsyncHooksContextManager } from '../src';
import { EventEmitter } from 'events';
import { Context } from '@opentelemetry/context-base';
describe('AsyncHooksContextManager', () => {
let contextManager: AsyncHooksContextManager;
const key1 = Context.createKey('test key 1');
beforeEach(() => {
contextManager = new AsyncHooksContextManager();
contextManager.enable();
});
afterEach(() => {
contextManager.disable();
});
describe('.enable()', () => {
it('should work', () => {
assert.doesNotThrow(() => {
contextManager = new AsyncHooksContextManager();
assert(
contextManager.enable() === contextManager,
'should return this'
);
});
});
});
describe('.disable()', () => {
it('should work', () => {
assert.doesNotThrow(() => {
assert(
contextManager.disable() === contextManager,
'should return this'
);
});
contextManager.enable();
});
});
describe('.with()', () => {
it('should run the callback (null as target)', done => {
contextManager.with(Context.ROOT_CONTEXT, done);
});
it('should run the callback (object as target)', done => {
const test = Context.ROOT_CONTEXT.setValue(key1, 1);
contextManager.with(test, () => {
assert.strictEqual(
contextManager.active(),
test,
'should have context'
);
return done();
});
});
it('should run the callback (when disabled)', done => {
contextManager.disable();
contextManager.with(Context.ROOT_CONTEXT, () => {
contextManager.enable();
return done();
});
});
it('should rethrow errors', done => {
assert.throws(() => {
contextManager.with(Context.ROOT_CONTEXT, () => {
throw new Error('This should be rethrown');
});
});
return done();
});
it('should finally restore an old context', done => {
const ctx1 = Context.ROOT_CONTEXT.setValue(key1, 'ctx1');
const ctx2 = Context.ROOT_CONTEXT.setValue(key1, 'ctx2');
contextManager.with(ctx1, () => {
assert.strictEqual(contextManager.active(), ctx1);
contextManager.with(ctx2, () => {
assert.strictEqual(contextManager.active(), ctx2);
});
assert.strictEqual(contextManager.active(), ctx1);
return done();
});
});
it('should finally restore an old context', done => {
const ctx1 = Context.ROOT_CONTEXT.setValue(key1, 'ctx1');
contextManager.with(ctx1, () => {
assert.strictEqual(contextManager.active(), ctx1);
setTimeout(() => {
assert.strictEqual(contextManager.active(), ctx1);
return done();
});
});
});
it('async function called from nested "with" sync function should return nested context', done => {
const scope1 = '1' as any;
const scope2 = '2' as any;
const asyncFuncCalledDownstreamFromSync = async () => {
await (async () => {})();
assert.strictEqual(contextManager.active(), scope2);
return done();
};
contextManager.with(scope1, () => {
assert.strictEqual(contextManager.active(), scope1);
contextManager.with(scope2, () => asyncFuncCalledDownstreamFromSync());
assert.strictEqual(contextManager.active(), scope1);
});
assert.strictEqual(contextManager.active(), Context.ROOT_CONTEXT);
});
it('should not loose the context', done => {
const scope1 = '1' as any;
contextManager.with(scope1, async () => {
assert.strictEqual(contextManager.active(), scope1);
await new Promise(resolve => setTimeout(resolve, 100));
assert.strictEqual(contextManager.active(), scope1);
return done();
});
assert.strictEqual(contextManager.active(), Context.ROOT_CONTEXT);
});
it('should correctly restore context using async/await', async () => {
const scope1 = '1' as any;
const scope2 = '2' as any;
const scope3 = '3' as any;
const scope4 = '4' as any;
await contextManager.with(scope1, async () => {
assert.strictEqual(contextManager.active(), scope1);
await contextManager.with(scope2, async () => {
assert.strictEqual(contextManager.active(), scope2);
await contextManager.with(scope3, async () => {
assert.strictEqual(contextManager.active(), scope3);
await contextManager.with(scope4, async () => {
assert.strictEqual(contextManager.active(), scope4);
});
assert.strictEqual(contextManager.active(), scope3);
});
assert.strictEqual(contextManager.active(), scope2);
});
assert.strictEqual(contextManager.active(), scope1);
});
assert.strictEqual(contextManager.active(), Context.ROOT_CONTEXT);
});
it('should works with multiple concurrent operations', done => {
const scope1 = '1' as any;
const scope2 = '2' as any;
const scope3 = '3' as any;
const scope4 = '4' as any;
let scope4Called = false;
contextManager.with(scope1, async () => {
assert.strictEqual(contextManager.active(), scope1);
setTimeout(async () => {
await contextManager.with(scope3, async () => {
assert.strictEqual(contextManager.active(), scope3);
});
assert.strictEqual(contextManager.active(), scope1);
assert.strictEqual(scope4Called, true);
return done();
}, 100);
assert.strictEqual(contextManager.active(), scope1);
});
assert.strictEqual(contextManager.active(), Context.ROOT_CONTEXT);
contextManager.with(scope2, async () => {
assert.strictEqual(contextManager.active(), scope2);
setTimeout(() => {
contextManager.with(scope4, async () => {
assert.strictEqual(contextManager.active(), scope4);
scope4Called = true;
});
assert.strictEqual(contextManager.active(), scope2);
}, 20);
assert.strictEqual(contextManager.active(), scope2);
});
assert.strictEqual(contextManager.active(), Context.ROOT_CONTEXT);
});
});
describe('.bind(function)', () => {
it('should return the same target (when enabled)', () => {
const test = { a: 1 };
assert.deepStrictEqual(
contextManager.bind(test, Context.ROOT_CONTEXT),
test
);
});
it('should return the same target (when disabled)', () => {
contextManager.disable();
const test = { a: 1 };
assert.deepStrictEqual(
contextManager.bind(test, Context.ROOT_CONTEXT),
test
);
contextManager.enable();
});
it('should return current context (when enabled)', done => {
const context = Context.ROOT_CONTEXT.setValue(key1, 1);
const fn = contextManager.bind(() => {
assert.strictEqual(
contextManager.active(),
context,
'should have context'
);
return done();
}, context);
fn();
});
/**
* Even if asynchooks is disabled, the context propagation will
* still works but it might be lost after any async op.
*/
it('should return current context (when disabled)', done => {
contextManager.disable();
const context = Context.ROOT_CONTEXT.setValue(key1, 1);
const fn = contextManager.bind(() => {
assert.strictEqual(
contextManager.active(),
context,
'should have context'
);
return done();
}, context);
fn();
});
it('should fail to return current context with async op', done => {
const context = Context.ROOT_CONTEXT.setValue(key1, 1);
const fn = contextManager.bind(() => {
assert.strictEqual(contextManager.active(), context);
setTimeout(() => {
assert.strictEqual(
contextManager.active(),
context,
'should have no context'
);
return done();
}, 100);
}, context);
fn();
});
});
describe('.bind(event-emitter)', () => {
it('should return the same target (when enabled)', () => {
const ee = new EventEmitter();
assert.deepStrictEqual(contextManager.bind(ee, Context.ROOT_CONTEXT), ee);
});
it('should return the same target (when disabled)', () => {
const ee = new EventEmitter();
contextManager.disable();
assert.deepStrictEqual(contextManager.bind(ee, Context.ROOT_CONTEXT), ee);
});
it('should return current context and removeListener (when enabled)', done => {
const ee = new EventEmitter();
const context = Context.ROOT_CONTEXT.setValue(key1, 1);
const patchedEe = contextManager.bind(ee, context);
const handler = () => {
assert.deepStrictEqual(contextManager.active(), context);
patchedEe.removeListener('test', handler);
assert.strictEqual(patchedEe.listeners('test').length, 0);
return done();
};
patchedEe.on('test', handler);
assert.strictEqual(patchedEe.listeners('test').length, 1);
patchedEe.emit('test');
});
it('should return current context and removeAllListener (when enabled)', done => {
const ee = new EventEmitter();
const context = Context.ROOT_CONTEXT.setValue(key1, 1);
const patchedEe = contextManager.bind(ee, context);
const handler = () => {
assert.deepStrictEqual(contextManager.active(), context);
patchedEe.removeAllListeners('test');
assert.strictEqual(patchedEe.listeners('test').length, 0);
return done();
};
patchedEe.on('test', handler);
assert.strictEqual(patchedEe.listeners('test').length, 1);
patchedEe.emit('test');
});
/**
* Even if asynchooks is disabled, the context propagation will
* still works but it might be lost after any async op.
*/
it('should return context (when disabled)', done => {
contextManager.disable();
const ee = new EventEmitter();
const context = Context.ROOT_CONTEXT.setValue(key1, 1);
const patchedEe = contextManager.bind(ee, context);
const handler = () => {
assert.deepStrictEqual(contextManager.active(), context);
patchedEe.removeListener('test', handler);
assert.strictEqual(patchedEe.listeners('test').length, 0);
return done();
};
patchedEe.on('test', handler);
assert.strictEqual(patchedEe.listeners('test').length, 1);
patchedEe.emit('test');
});
it('should not return current context with async op', done => {
const ee = new EventEmitter();
const context = Context.ROOT_CONTEXT.setValue(key1, 1);
const patchedEe = contextManager.bind(ee, context);
const handler = () => {
assert.deepStrictEqual(contextManager.active(), context);
setImmediate(() => {
assert.deepStrictEqual(contextManager.active(), context);
patchedEe.removeAllListeners('test');
assert.strictEqual(patchedEe.listeners('test').length, 0);
return done();
});
};
patchedEe.on('test', handler);
assert.strictEqual(patchedEe.listeners('test').length, 1);
patchedEe.emit('test');
});
});
});