From 6a27f9c408eef1d5187cc5342757b3291aacae50 Mon Sep 17 00:00:00 2001 From: Ryan Lamb <4955475+kinyoklion@users.noreply.github.com> Date: Wed, 5 Jul 2023 08:51:48 -0700 Subject: [PATCH] chore: Fix timing issues for flaky tests. --- .../__tests__/LDClient.evaluation.test.ts | 15 +++++++++++++-- .../sdk-server/__tests__/LDClientImpl.test.ts | 8 +++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/shared/sdk-server/__tests__/LDClient.evaluation.test.ts b/packages/shared/sdk-server/__tests__/LDClient.evaluation.test.ts index 6fa1f77b0c..2a7461525e 100644 --- a/packages/shared/sdk-server/__tests__/LDClient.evaluation.test.ts +++ b/packages/shared/sdk-server/__tests__/LDClient.evaluation.test.ts @@ -1,5 +1,5 @@ import { LDClientImpl } from '../src'; -import { LDFeatureStore } from '../src/api/subsystems'; +import { LDFeatureStore, LDStreamProcessor } from '../src/api/subsystems'; import NullUpdateProcessor from '../src/data_sources/NullUpdateProcessor'; import TestData from '../src/integrations/test_data/TestData'; import AsyncStoreFacade from '../src/store/AsyncStoreFacade'; @@ -158,6 +158,17 @@ describe('given an offline client', () => { }); }); +class InertUpdateProcessor implements LDStreamProcessor { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + start(fn?: ((err?: any) => void) | undefined) { + // Never initialize. + } + + stop() {} + + close() {} +} + describe('given a client and store that are uninitialized', () => { let store: LDFeatureStore; let client: LDClientImpl; @@ -178,7 +189,7 @@ describe('given a client and store that are uninitialized', () => { 'sdk-key', basicPlatform, { - updateProcessor: new NullUpdateProcessor(), + updateProcessor: new InertUpdateProcessor(), sendEvents: false, featureStore: store, }, diff --git a/packages/shared/sdk-server/__tests__/LDClientImpl.test.ts b/packages/shared/sdk-server/__tests__/LDClientImpl.test.ts index 3d62905aca..000bd1940d 100644 --- a/packages/shared/sdk-server/__tests__/LDClientImpl.test.ts +++ b/packages/shared/sdk-server/__tests__/LDClientImpl.test.ts @@ -54,6 +54,7 @@ it('isOffline returns true in offline mode', (done) => { describe('when waiting for initialization', () => { let client: LDClientImpl; + let resolve: Function; beforeEach(() => { client = new LDClientImpl( @@ -62,9 +63,7 @@ describe('when waiting for initialization', () => { { updateProcessor: { start: (fn: (err?: any) => void) => { - setTimeout(() => { - fn(); - }, 0); + resolve = fn; }, stop: () => {}, close: () => {}, @@ -81,10 +80,12 @@ describe('when waiting for initialization', () => { }); it('resolves when ready', async () => { + resolve(); await client.waitForInitialization(); }); it('resolves immediately if the client is already ready', async () => { + resolve(); await client.waitForInitialization(); await client.waitForInitialization(); }); @@ -92,6 +93,7 @@ describe('when waiting for initialization', () => { it('creates only one Promise', async () => { const p1 = client.waitForInitialization(); const p2 = client.waitForInitialization(); + resolve(); expect(p2).toBe(p1); }); });