|
| 1 | +import { AutoEnvAttributes, clone, LDContext } from '@launchdarkly/js-sdk-common'; |
| 2 | +import { basicPlatform, logger, setupMockStreamingProcessor } from '@launchdarkly/private-js-mocks'; |
| 3 | + |
| 4 | +import * as mockResponseJson from './evaluation/mockResponse.json'; |
| 5 | +import LDClientImpl from './LDClientImpl'; |
| 6 | +import { Flags } from './types'; |
| 7 | +import { toMulti } from './utils/addAutoEnv'; |
| 8 | + |
| 9 | +jest.mock('@launchdarkly/js-sdk-common', () => { |
| 10 | + const actual = jest.requireActual('@launchdarkly/js-sdk-common'); |
| 11 | + const m = jest.requireActual('@launchdarkly/private-js-mocks'); |
| 12 | + return { |
| 13 | + ...actual, |
| 14 | + ...{ |
| 15 | + internal: { |
| 16 | + ...actual.internal, |
| 17 | + StreamingProcessor: m.MockStreamingProcessor, |
| 18 | + }, |
| 19 | + }, |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +const testSdkKey = 'test-sdk-key'; |
| 24 | +const carContext: LDContext = { kind: 'car', key: 'test-car' }; |
| 25 | + |
| 26 | +let ldc: LDClientImpl; |
| 27 | +let defaultPutResponse: Flags; |
| 28 | + |
| 29 | +describe('sdk-client identify timeout', () => { |
| 30 | + beforeAll(() => { |
| 31 | + jest.useFakeTimers(); |
| 32 | + }); |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + defaultPutResponse = clone<Flags>(mockResponseJson); |
| 36 | + |
| 37 | + // simulate streamer error after a long timeout |
| 38 | + setupMockStreamingProcessor(true, defaultPutResponse, undefined, undefined, 30); |
| 39 | + |
| 40 | + ldc = new LDClientImpl(testSdkKey, AutoEnvAttributes.Enabled, basicPlatform, { |
| 41 | + logger, |
| 42 | + sendEvents: false, |
| 43 | + }); |
| 44 | + jest |
| 45 | + .spyOn(LDClientImpl.prototype as any, 'createStreamUriPath') |
| 46 | + .mockReturnValue('/stream/path'); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + jest.resetAllMocks(); |
| 51 | + }); |
| 52 | + |
| 53 | + // streamer is setup to error in beforeEach to cause a timeout |
| 54 | + test('rejects with default timeout of 5s', async () => { |
| 55 | + jest.advanceTimersByTimeAsync(ldc.identifyTimeout * 1000).then(); |
| 56 | + await expect(ldc.identify(carContext)).rejects.toThrow(/identify timed out/); |
| 57 | + expect(logger.error).toHaveBeenCalledWith(expect.stringMatching(/identify timed out/)); |
| 58 | + }); |
| 59 | + |
| 60 | + // streamer is setup to error in beforeEach to cause a timeout |
| 61 | + test('rejects with custom timeout', async () => { |
| 62 | + const timeout = 15; |
| 63 | + jest.advanceTimersByTimeAsync(timeout * 1000).then(); |
| 64 | + |
| 65 | + await expect(ldc.identify(carContext, { timeout })).rejects.toThrow(/identify timed out/); |
| 66 | + }); |
| 67 | + |
| 68 | + test('resolves with default timeout', async () => { |
| 69 | + setupMockStreamingProcessor(false, defaultPutResponse); |
| 70 | + jest.advanceTimersByTimeAsync(ldc.identifyTimeout * 1000).then(); |
| 71 | + |
| 72 | + await expect(ldc.identify(carContext)).resolves.toBeUndefined(); |
| 73 | + |
| 74 | + expect(ldc.getContext()).toEqual(expect.objectContaining(toMulti(carContext))); |
| 75 | + expect(ldc.allFlags()).toEqual({ |
| 76 | + 'dev-test-flag': true, |
| 77 | + 'easter-i-tunes-special': false, |
| 78 | + 'easter-specials': 'no specials', |
| 79 | + fdsafdsafdsafdsa: true, |
| 80 | + 'log-level': 'warn', |
| 81 | + 'moonshot-demo': true, |
| 82 | + test1: 's1', |
| 83 | + 'this-is-a-test': true, |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + test('resolves with custom timeout', async () => { |
| 88 | + const timeout = 15; |
| 89 | + setupMockStreamingProcessor(false, defaultPutResponse); |
| 90 | + jest.advanceTimersByTimeAsync(timeout).then(); |
| 91 | + |
| 92 | + await expect(ldc.identify(carContext, { timeout })).resolves.toBeUndefined(); |
| 93 | + |
| 94 | + expect(ldc.getContext()).toEqual(expect.objectContaining(toMulti(carContext))); |
| 95 | + expect(ldc.allFlags()).toEqual({ |
| 96 | + 'dev-test-flag': true, |
| 97 | + 'easter-i-tunes-special': false, |
| 98 | + 'easter-specials': 'no specials', |
| 99 | + fdsafdsafdsafdsa: true, |
| 100 | + 'log-level': 'warn', |
| 101 | + 'moonshot-demo': true, |
| 102 | + test1: 's1', |
| 103 | + 'this-is-a-test': true, |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + test('setting high timeout threshold with internalOptions', async () => { |
| 108 | + const highTimeoutThreshold = 20; |
| 109 | + setupMockStreamingProcessor(false, defaultPutResponse); |
| 110 | + ldc = new LDClientImpl( |
| 111 | + testSdkKey, |
| 112 | + AutoEnvAttributes.Enabled, |
| 113 | + basicPlatform, |
| 114 | + { |
| 115 | + logger, |
| 116 | + sendEvents: false, |
| 117 | + }, |
| 118 | + { highTimeoutThreshold }, |
| 119 | + ); |
| 120 | + const customTimeout = 10; |
| 121 | + jest.advanceTimersByTimeAsync(customTimeout * 1000).then(); |
| 122 | + await ldc.identify(carContext, { timeout: customTimeout }); |
| 123 | + |
| 124 | + expect(ldc.identifyTimeout).toEqual(10); |
| 125 | + expect(logger.warn).not.toHaveBeenCalledWith(expect.stringMatching(/high timeout/)); |
| 126 | + }); |
| 127 | + |
| 128 | + test('warning when timeout is too high', async () => { |
| 129 | + const highTimeout = 60; |
| 130 | + setupMockStreamingProcessor(false, defaultPutResponse); |
| 131 | + jest.advanceTimersByTimeAsync(highTimeout * 1000).then(); |
| 132 | + |
| 133 | + await ldc.identify(carContext, { timeout: highTimeout }); |
| 134 | + |
| 135 | + expect(logger.warn).toHaveBeenCalledWith(expect.stringMatching(/high timeout/)); |
| 136 | + }); |
| 137 | + |
| 138 | + test('safe timeout should not warn', async () => { |
| 139 | + const { identifyTimeout } = ldc; |
| 140 | + const safeTimeout = identifyTimeout; |
| 141 | + setupMockStreamingProcessor(false, defaultPutResponse); |
| 142 | + jest.advanceTimersByTimeAsync(identifyTimeout * 1000).then(); |
| 143 | + |
| 144 | + await ldc.identify(carContext, { timeout: safeTimeout }); |
| 145 | + |
| 146 | + expect(logger.warn).not.toHaveBeenCalledWith(expect.stringMatching(/high timeout/)); |
| 147 | + }); |
| 148 | +}); |
0 commit comments