|
| 1 | +import { getCurrentHub, Hub, runWithAsyncContext, setAsyncContextStrategy } from '@sentry/core'; |
| 2 | + |
| 3 | +import { setHooksAsyncContextStrategy } from '../../src/async/hooks'; |
| 4 | +import { conditionalTest } from '../utils'; |
| 5 | + |
| 6 | +conditionalTest({ min: 12 })('async_hooks', () => { |
| 7 | + afterAll(() => { |
| 8 | + // clear the strategy |
| 9 | + setAsyncContextStrategy(undefined); |
| 10 | + }); |
| 11 | + |
| 12 | + test('without context', () => { |
| 13 | + const hub = getCurrentHub(); |
| 14 | + expect(hub).toEqual(new Hub()); |
| 15 | + }); |
| 16 | + |
| 17 | + test('without strategy hubs should be equal', () => { |
| 18 | + runWithAsyncContext(hub1 => { |
| 19 | + runWithAsyncContext(hub2 => { |
| 20 | + expect(hub1).toBe(hub2); |
| 21 | + }); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + test('hub scope inheritance', () => { |
| 26 | + setHooksAsyncContextStrategy(); |
| 27 | + |
| 28 | + const globalHub = getCurrentHub(); |
| 29 | + globalHub.setExtra('a', 'b'); |
| 30 | + |
| 31 | + runWithAsyncContext(hub1 => { |
| 32 | + expect(hub1).toEqual(globalHub); |
| 33 | + |
| 34 | + hub1.setExtra('c', 'd'); |
| 35 | + expect(hub1).not.toEqual(globalHub); |
| 36 | + |
| 37 | + runWithAsyncContext(hub2 => { |
| 38 | + expect(hub2).toEqual(hub1); |
| 39 | + expect(hub2).not.toEqual(globalHub); |
| 40 | + |
| 41 | + hub2.setExtra('e', 'f'); |
| 42 | + expect(hub2).not.toEqual(hub1); |
| 43 | + }); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + test('context single instance', () => { |
| 48 | + setHooksAsyncContextStrategy(); |
| 49 | + |
| 50 | + runWithAsyncContext(hub => { |
| 51 | + expect(hub).toBe(getCurrentHub()); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + test('context within a context not reused', () => { |
| 56 | + setHooksAsyncContextStrategy(); |
| 57 | + |
| 58 | + runWithAsyncContext(hub1 => { |
| 59 | + runWithAsyncContext(hub2 => { |
| 60 | + expect(hub1).not.toBe(hub2); |
| 61 | + }); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + test('context within a context reused when requested', () => { |
| 66 | + setHooksAsyncContextStrategy(); |
| 67 | + |
| 68 | + runWithAsyncContext(hub1 => { |
| 69 | + runWithAsyncContext( |
| 70 | + hub2 => { |
| 71 | + expect(hub1).toBe(hub2); |
| 72 | + }, |
| 73 | + { reuseExisting: true }, |
| 74 | + ); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + test('concurrent hub contexts', done => { |
| 79 | + setHooksAsyncContextStrategy(); |
| 80 | + |
| 81 | + let d1done = false; |
| 82 | + let d2done = false; |
| 83 | + |
| 84 | + runWithAsyncContext(hub => { |
| 85 | + hub.getStack().push({ client: 'process' } as any); |
| 86 | + expect(hub.getStack()[1]).toEqual({ client: 'process' }); |
| 87 | + // Just in case so we don't have to worry which one finishes first |
| 88 | + // (although it always should be d2) |
| 89 | + setTimeout(() => { |
| 90 | + d1done = true; |
| 91 | + if (d2done) { |
| 92 | + done(); |
| 93 | + } |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + runWithAsyncContext(hub => { |
| 98 | + hub.getStack().push({ client: 'local' } as any); |
| 99 | + expect(hub.getStack()[1]).toEqual({ client: 'local' }); |
| 100 | + setTimeout(() => { |
| 101 | + d2done = true; |
| 102 | + if (d1done) { |
| 103 | + done(); |
| 104 | + } |
| 105 | + }); |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
0 commit comments