|
| 1 | +/* |
| 2 | + * Tests ported from @opentelemetry/instrumentation-lru-memoizer@0.62.0 |
| 3 | + * Original source: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/instrumentation-lru-memoizer |
| 4 | + * Licensed under the Apache License, Version 2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 8 | +import * as Sentry from '../../../src'; |
| 9 | +import { LruMemoizerInstrumentation } from '../../../src/integrations/tracing/lrumemoizer/vendored/instrumentation'; |
| 10 | +import { cleanupOtel, mockSdkInit } from '../../helpers/mockSdkInit'; |
| 11 | + |
| 12 | +type MemoizerCallback = (err: Error | null, result?: string) => void; |
| 13 | +type Memoizer = (param: unknown, callback?: MemoizerCallback | null) => void; |
| 14 | +type MemoizerModule = ((options: unknown) => Memoizer) & { sync: (options: unknown) => (param: unknown) => string }; |
| 15 | + |
| 16 | +describe('lru-memoizer instrumentation', () => { |
| 17 | + let instrumentation: LruMemoizerInstrumentation; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + mockSdkInit({ tracesSampleRate: 1 }); |
| 21 | + instrumentation = new LruMemoizerInstrumentation(); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + instrumentation.disable(); |
| 26 | + cleanupOtel(); |
| 27 | + }); |
| 28 | + |
| 29 | + // Create a fake `lru-memoizer`. |
| 30 | + // The fake queues the instrumented callback so it can be invoked from outside the originating span. |
| 31 | + function getMemoizer(): { memoizer: MemoizerModule; queuedCallbacks: MemoizerCallback[] } { |
| 32 | + const queuedCallbacks: MemoizerCallback[] = []; |
| 33 | + const fakeModule = Object.assign( |
| 34 | + (_options: unknown) => (_param: unknown, callback?: MemoizerCallback | null) => { |
| 35 | + if (callback) { |
| 36 | + queuedCallbacks.push(callback); |
| 37 | + } |
| 38 | + }, |
| 39 | + { sync: (_options: unknown) => (_param: unknown) => 'foo' }, |
| 40 | + ) as MemoizerModule; |
| 41 | + |
| 42 | + const def = instrumentation.getModuleDefinitions()[0]; |
| 43 | + if (!def?.patch) { |
| 44 | + throw new Error('Expected a module definition with a patch function'); |
| 45 | + } |
| 46 | + return { memoizer: def.patch(fakeModule) as MemoizerModule, queuedCallbacks }; |
| 47 | + } |
| 48 | + |
| 49 | + describe('async', () => { |
| 50 | + it('should invoke load callback with original context', async () => { |
| 51 | + const { memoizer, queuedCallbacks } = getMemoizer(); |
| 52 | + const memoizedFoo = memoizer({ max: 10, load: () => {}, hash: () => 'bar' }); |
| 53 | + |
| 54 | + await new Promise<void>((resolve, reject) => { |
| 55 | + Sentry.startSpan({ name: 'memoized invocation' }, span => { |
| 56 | + memoizedFoo({ foo: 'bar' }, err => { |
| 57 | + try { |
| 58 | + expect(Sentry.getActiveSpan()).toBe(span); |
| 59 | + resolve(); |
| 60 | + } catch (e) { |
| 61 | + reject(e as Error); |
| 62 | + } |
| 63 | + if (err) { |
| 64 | + reject(err); |
| 65 | + } |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + // we invoke the callback from outside of the above span's context. |
| 70 | + // however, we expect that the callback is called with the context of the original invocation |
| 71 | + queuedCallbacks[0]!(null, 'result'); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should invoke callback with right context when serving 2 parallel async requests', () => { |
| 76 | + const { memoizer, queuedCallbacks } = getMemoizer(); |
| 77 | + const memoizedFoo = memoizer({ max: 10, load: () => {}, hash: () => 'bar' }); |
| 78 | + |
| 79 | + const observed: Array<{ expected: unknown; actual: unknown }> = []; |
| 80 | + |
| 81 | + Sentry.startSpan({ name: 'first request' }, firstSpan => { |
| 82 | + memoizedFoo({ foo: 'bar' }, () => { |
| 83 | + observed.push({ expected: firstSpan, actual: Sentry.getActiveSpan() }); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + Sentry.startSpan({ name: 'second request' }, secondSpan => { |
| 88 | + memoizedFoo({ foo: 'bar' }, () => { |
| 89 | + observed.push({ expected: secondSpan, actual: Sentry.getActiveSpan() }); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + expect(queuedCallbacks.length).toBe(2); |
| 94 | + queuedCallbacks[0]!(null, 'result'); |
| 95 | + queuedCallbacks[1]!(null, 'result'); |
| 96 | + |
| 97 | + expect(observed).toHaveLength(2); |
| 98 | + observed.forEach(({ expected, actual }) => { |
| 99 | + expect(actual).toBe(expected); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should not throw when last argument is not callback', () => { |
| 104 | + const { memoizer } = getMemoizer(); |
| 105 | + const memoizedFoo = memoizer({ max: 10, load: () => 'foo', hash: () => 'bar' }); |
| 106 | + |
| 107 | + // this is not valid but we want to make sure it does not throw or act badly |
| 108 | + expect(() => memoizedFoo({ foo: 'bar' }, null)).not.toThrow(); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('sync', () => { |
| 113 | + it('should not break sync memoizer', () => { |
| 114 | + const { memoizer } = getMemoizer(); |
| 115 | + |
| 116 | + // the sync memoizer is passed through untouched by the patch |
| 117 | + const memoizedFoo = memoizer.sync({ max: 10, load: () => 'foo', hash: () => 'bar' }); |
| 118 | + expect(memoizedFoo({ foo: 'bar' })).toBe('foo'); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments