forked from open-telemetry/opentelemetry-js
/
AsyncHooksContextManager.test.ts
494 lines (448 loc) · 17 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
* Copyright The 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,
AsyncLocalStorageContextManager,
} from '../src';
import { EventEmitter } from 'events';
import { createContextKey, ROOT_CONTEXT } from '@opentelemetry/api';
for (const contextManagerClass of [
AsyncHooksContextManager,
AsyncLocalStorageContextManager,
]) {
describe(contextManagerClass.name, () => {
let contextManager:
| AsyncHooksContextManager
| AsyncLocalStorageContextManager;
const key1 = createContextKey('test key 1');
let otherContextManager:
| AsyncHooksContextManager
| AsyncLocalStorageContextManager;
before(function () {
if (
contextManagerClass.name === 'AsyncLocalStorageContextManager' &&
(process.version.startsWith('v8.') ||
process.version.startsWith('v10.'))
) {
this.skip();
}
});
beforeEach(() => {
contextManager = new contextManagerClass();
contextManager.enable();
});
afterEach(() => {
contextManager.disable();
otherContextManager?.disable();
});
describe('.enable()', () => {
it('should work', () => {
assert.doesNotThrow(() => {
contextManager = new contextManagerClass();
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(ROOT_CONTEXT, done);
});
it('should run the callback (object as target)', done => {
const test = 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(ROOT_CONTEXT, () => {
contextManager.enable();
return done();
});
});
it('should rethrow errors', done => {
assert.throws(() => {
contextManager.with(ROOT_CONTEXT, () => {
throw new Error('This should be rethrown');
});
});
return done();
});
it('should forward this, arguments and return value', () => {
function fnWithThis(this: string, a: string, b: number): string {
assert.strictEqual(this, 'that');
assert.strictEqual(arguments.length, 2);
assert.strictEqual(a, 'one');
assert.strictEqual(b, 2);
return 'done';
}
const res = contextManager.with(
ROOT_CONTEXT,
fnWithThis,
'that',
'one',
2
);
assert.strictEqual(res, 'done');
assert.strictEqual(
contextManager.with(ROOT_CONTEXT, () => 3.14),
3.14
);
});
it('should finally restore an old context', done => {
const ctx1 = ROOT_CONTEXT.setValue(key1, 'ctx1');
const ctx2 = 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 = 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(), 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(), 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(), 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(), 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(), ROOT_CONTEXT);
});
it('should work with timers using the same timeout', done => {
let cnt = 3;
function countDown() {
cnt--;
if (cnt === 0) done();
if (cnt < 0) throw new Error('too many calls to countDown()');
}
const time1 = 2;
const time2 = time1 + 1;
const rootCtx = contextManager.active();
const innerCtx = rootCtx.setValue(Symbol('test'), 23);
contextManager.with(innerCtx, () => {
setTimeout(() => {
assert.strictEqual(contextManager.active(), innerCtx);
countDown();
}, time1);
});
setTimeout(() => {
assert.strictEqual(contextManager.active(), rootCtx);
countDown();
}, time1);
setTimeout(() => {
assert.strictEqual(contextManager.active(), rootCtx);
countDown();
}, time2);
});
it('should not influence other instances', () => {
otherContextManager = new contextManagerClass();
otherContextManager.enable();
const context = ROOT_CONTEXT.setValue(key1, 2);
const otherContext = ROOT_CONTEXT.setValue(key1, 3);
contextManager.with(context, () => {
assert.strictEqual(contextManager.active(), context);
assert.strictEqual(otherContextManager.active(), ROOT_CONTEXT);
otherContextManager.with(otherContext, () => {
assert.strictEqual(contextManager.active(), context);
assert.strictEqual(otherContextManager.active(), otherContext);
});
});
});
});
describe('.bind(function)', () => {
it('should return the same target (when enabled)', () => {
const test = { a: 1 };
assert.deepStrictEqual(contextManager.bind(ROOT_CONTEXT, test), test);
});
it('should return the same target (when disabled)', () => {
contextManager.disable();
const test = { a: 1 };
assert.deepStrictEqual(contextManager.bind(ROOT_CONTEXT, test), test);
contextManager.enable();
});
it('should return current context (when enabled)', done => {
const context = ROOT_CONTEXT.setValue(key1, 1);
const fn = contextManager.bind(context, () => {
assert.strictEqual(
contextManager.active(),
context,
'should have context'
);
return done();
});
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 = ROOT_CONTEXT.setValue(key1, 1);
const fn = contextManager.bind(context, () => {
assert.strictEqual(
contextManager.active(),
context,
'should have context'
);
return done();
});
fn();
});
it('should fail to return current context with async op', done => {
const context = ROOT_CONTEXT.setValue(key1, 1);
const fn = contextManager.bind(context, () => {
assert.strictEqual(contextManager.active(), context);
setTimeout(() => {
assert.strictEqual(
contextManager.active(),
context,
'should have no context'
);
return done();
}, 100);
});
fn();
});
it('should not influence other instances', () => {
otherContextManager = new contextManagerClass();
otherContextManager.enable();
const context = ROOT_CONTEXT.setValue(key1, 2);
const otherContext = ROOT_CONTEXT.setValue(key1, 3);
const fn = otherContextManager.bind(
otherContext,
contextManager.bind(context, () => {
assert.strictEqual(contextManager.active(), context);
assert.strictEqual(otherContextManager.active(), otherContext);
}));
fn();
});
});
describe('.bind(event-emitter)', () => {
it('should return the same target (when enabled)', () => {
const ee = new EventEmitter();
assert.deepStrictEqual(contextManager.bind(ROOT_CONTEXT, ee), ee);
});
it('should return the same target (when disabled)', () => {
const ee = new EventEmitter();
contextManager.disable();
assert.deepStrictEqual(contextManager.bind(ROOT_CONTEXT, ee), ee);
});
it('should return current context and removeListener (when enabled)', done => {
const ee = new EventEmitter();
const context = ROOT_CONTEXT.setValue(key1, 1);
const patchedEE = contextManager.bind(context, ee);
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 = ROOT_CONTEXT.setValue(key1, 1);
const patchedEE = contextManager.bind(context, ee);
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');
});
it('should return current context and removeAllListeners (when enabled)', done => {
const ee = new EventEmitter();
const context = ROOT_CONTEXT.setValue(key1, 1);
const patchedEE = contextManager.bind(context, ee);
const handler = () => {
assert.deepStrictEqual(contextManager.active(), context);
patchedEE.removeAllListeners();
assert.strictEqual(patchedEE.listeners('test').length, 0);
assert.strictEqual(patchedEE.listeners('test1').length, 0);
return done();
};
patchedEE.on('test', handler);
patchedEE.on('test1', handler);
assert.strictEqual(patchedEE.listeners('test').length, 1);
assert.strictEqual(patchedEE.listeners('test1').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 = ROOT_CONTEXT.setValue(key1, 1);
const patchedEE = contextManager.bind(context, ee);
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 = ROOT_CONTEXT.setValue(key1, 1);
const patchedEE = contextManager.bind(context, ee);
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');
});
it('should not influence other instances', () => {
const ee = new EventEmitter();
otherContextManager = new contextManagerClass();
otherContextManager.enable();
const context = ROOT_CONTEXT.setValue(key1, 2);
const otherContext = ROOT_CONTEXT.setValue(key1, 3);
const patchedEE = otherContextManager.bind(
otherContext,
contextManager.bind(context, ee),
);
const handler = () => {
assert.strictEqual(contextManager.active(), context);
assert.strictEqual(otherContextManager.active(), otherContext);
};
patchedEE.on('test', handler);
patchedEE.emit('test');
});
});
});
}