Skip to content

Commit 1b21500

Browse files
committed
Fix client and server test.
1 parent c9b0a1d commit 1b21500

13 files changed

+97
-78
lines changed

packages/shared/sdk-client/__tests__/setupCrypto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Hasher } from '../src/api';
1+
import { Hasher } from '@launchdarkly/js-sdk-common';
22

33
export const setupCrypto = () => {
44
let counter = 0;

packages/shared/sdk-server/__tests__/LDClient.allFlags.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('given an LDClient with test data', () => {
1515
logger = new TestLogger();
1616
td = new TestData();
1717
client = new LDClientImpl(
18-
'sdk-key',
18+
'sdk-key-all-flags-test-data',
1919
createBasicPlatform(),
2020
{
2121
updateProcessor: td.getFactory(),
@@ -279,7 +279,7 @@ describe('given an offline client', () => {
279279
logger = new TestLogger();
280280
td = new TestData();
281281
client = new LDClientImpl(
282-
'sdk-key',
282+
'sdk-key-all-flags-offline',
283283
createBasicPlatform(),
284284
{
285285
offline: true,

packages/shared/sdk-server/__tests__/LDClient.evaluation.test.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,6 @@ import VersionedDataKinds from '../src/store/VersionedDataKinds';
88
import { createBasicPlatform } from './createBasicPlatform';
99
import TestLogger, { LogLevel } from './Logger';
1010
import makeCallbacks from './makeCallbacks';
11-
import { MockStreamingProcessor, setupMockStreamingProcessor } from './streamingProcessor';
12-
13-
jest.mock('@launchdarkly/js-sdk-common', () => {
14-
const actual = jest.requireActual('@launchdarkly/js-sdk-common');
15-
return {
16-
...actual,
17-
...{
18-
internal: {
19-
...actual.internal,
20-
StreamingProcessor: MockStreamingProcessor,
21-
},
22-
},
23-
};
24-
});
2511

2612
const defaultUser = { key: 'user' };
2713

@@ -32,7 +18,7 @@ describe('given an LDClient with test data', () => {
3218
beforeEach(async () => {
3319
td = new TestData();
3420
client = new LDClientImpl(
35-
'sdk-key',
21+
'sdk-key-evaluation-test-data',
3622
createBasicPlatform(),
3723
{
3824
updateProcessor: td.getFactory(),
@@ -278,7 +264,7 @@ describe('given an offline client', () => {
278264
logger = new TestLogger();
279265
td = new TestData();
280266
client = new LDClientImpl(
281-
'sdk-key',
267+
'sdk-key-evaluation-offline',
282268
createBasicPlatform(),
283269
{
284270
offline: true,
@@ -342,7 +328,7 @@ describe('given a client and store that are uninitialized', () => {
342328
});
343329

344330
client = new LDClientImpl(
345-
'sdk-key',
331+
'sdk-key-evaluation-uninitialized-store',
346332
createBasicPlatform(),
347333
{
348334
updateProcessor: new InertUpdateProcessor(),
@@ -389,14 +375,18 @@ describe('given a client that is un-initialized and store that is initialized',
389375
},
390376
segments: {},
391377
});
392-
setupMockStreamingProcessor(true);
393378

394379
client = new LDClientImpl(
395-
'sdk-key',
380+
'sdk-key-initialized-store',
396381
createBasicPlatform(),
397382
{
398383
sendEvents: false,
399384
featureStore: store,
385+
updateProcessor: () => ({
386+
start: jest.fn(),
387+
stop: jest.fn(),
388+
close: jest.fn(),
389+
}),
400390
},
401391
makeCallbacks(true),
402392
);

packages/shared/sdk-server/__tests__/LDClient.events.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('given a client with mock event processor', () => {
2929

3030
td = new TestData();
3131
client = new LDClientImpl(
32-
'sdk-key',
32+
'sdk-key-events',
3333
createBasicPlatform(),
3434
{
3535
updateProcessor: td.getFactory(),

packages/shared/sdk-server/__tests__/LDClient.hooks.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('given an LDClient with test data', () => {
1919
testHook = new TestHook();
2020
td = new TestData();
2121
client = new LDClientImpl(
22-
'sdk-key',
22+
'sdk-key-hooks-test-data',
2323
createBasicPlatform(),
2424
{
2525
updateProcessor: td.getFactory(),
@@ -350,7 +350,7 @@ it('can add a hook after initialization', async () => {
350350
const logger = new TestLogger();
351351
const td = new TestData();
352352
const client = new LDClientImpl(
353-
'sdk-key',
353+
'sdk-key-hook-after-init',
354354
createBasicPlatform(),
355355
{
356356
updateProcessor: td.getFactory(),

packages/shared/sdk-server/__tests__/LDClient.migrations.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('given an LDClient with test data', () => {
3232
td = new TestData();
3333
[errors, callbacks] = makeCallbacks();
3434
client = new LDClientImpl(
35-
'sdk-key',
35+
'sdk-key-migration',
3636
createBasicPlatform(),
3737
{
3838
updateProcessor: td.getFactory(),

packages/shared/sdk-server/__tests__/LDClientImpl.bigSegments.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('given test data with big segments', () => {
7575
};
7676

7777
client = new LDClientImpl(
78-
'sdk-key',
78+
'sdk-key-big-segments-test-data',
7979
{ ...createBasicPlatform(), crypto },
8080
{
8181
updateProcessor: td.getFactory(),
@@ -114,7 +114,7 @@ describe('given test data with big segments', () => {
114114
};
115115

116116
client = new LDClientImpl(
117-
'sdk-key',
117+
'sdk-key-big-segments-with-user',
118118
{ ...createBasicPlatform(), crypto },
119119
{
120120
updateProcessor: td.getFactory(),
@@ -153,7 +153,7 @@ describe('given test data with big segments', () => {
153153
};
154154

155155
client = new LDClientImpl(
156-
'sdk-key',
156+
'sdk-key-big-segments-store-error',
157157
{ ...createBasicPlatform(), crypto },
158158
{
159159
updateProcessor: td.getFactory(),
@@ -180,7 +180,7 @@ describe('given test data with big segments', () => {
180180
describe('given a client without big segment support.', () => {
181181
beforeEach(async () => {
182182
client = new LDClientImpl(
183-
'sdk-key',
183+
'sdk-key-big-segments-no-store',
184184
{ ...createBasicPlatform(), crypto },
185185
{
186186
updateProcessor: td.getFactory(),

packages/shared/sdk-server/__tests__/LDClientImpl.listeners.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('given an LDClient with test data', () => {
1717
queue = new AsyncQueue();
1818
td = new TestData();
1919
client = new LDClientImpl(
20-
'sdk-key',
20+
'sdk-key-listeners',
2121
createBasicPlatform(),
2222
{
2323
updateProcessor: td.getFactory(),

packages/shared/sdk-server/__tests__/LDClientImpl.test.ts

Lines changed: 70 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,46 @@
1-
import { LDClientImpl, LDOptions } from '../src';
1+
import { LDClientContext, LDStreamingError } from '@launchdarkly/js-sdk-common';
2+
3+
import { LDOptions } from '../src/api/options/LDOptions';
4+
import { LDFeatureStore } from '../src/api/subsystems/LDFeatureStore';
5+
import LDClientImpl from '../src/LDClientImpl';
26
import { createBasicPlatform } from './createBasicPlatform';
37
import TestLogger, { LogLevel } from './Logger';
4-
import { MockStreamingProcessor, setupMockStreamingProcessor } from './streamingProcessor';
5-
6-
jest.mock('@launchdarkly/js-sdk-common', () => {
7-
const actual = jest.requireActual('@launchdarkly/js-sdk-common');
8-
return {
9-
...actual,
10-
...{
11-
internal: {
12-
...actual.internal,
13-
StreamingProcessor: MockStreamingProcessor,
14-
},
15-
},
16-
};
17-
});
8+
9+
function getUpdateProcessorFactory(shouldError: boolean = false, initTimeoutMs: number = 0) {
10+
let initTimeoutHandle: any;
11+
let patchTimeoutHandle: any;
12+
let deleteTimeoutHandle: any;
13+
14+
return (
15+
_clientContext: LDClientContext,
16+
featureStore: LDFeatureStore,
17+
initSuccessHandler: VoidFunction,
18+
errorHandler?: (e: Error) => void,
19+
) => ({
20+
start: jest.fn(async () => {
21+
if (shouldError) {
22+
initTimeoutHandle = setTimeout(() => {
23+
const unauthorized = new Error('test-error') as LDStreamingError;
24+
// @ts-ignore
25+
unauthorized.code = 401;
26+
errorHandler?.(unauthorized);
27+
}, 0);
28+
} else {
29+
// execute put which will resolve the identify promise
30+
initTimeoutHandle = setTimeout(() => {
31+
featureStore.init({}, () => {});
32+
initSuccessHandler();
33+
}, initTimeoutMs);
34+
}
35+
}),
36+
close: jest.fn(() => {
37+
clearTimeout(initTimeoutHandle);
38+
clearTimeout(patchTimeoutHandle);
39+
clearTimeout(deleteTimeoutHandle);
40+
}),
41+
eventSource: {},
42+
});
43+
}
1844

1945
describe('LDClientImpl', () => {
2046
let client: LDClientImpl;
@@ -26,19 +52,15 @@ describe('LDClientImpl', () => {
2652
hasEventListeners: jest.fn().mockName('hasEventListeners'),
2753
};
2854
const createClient = (options: LDOptions = {}) =>
29-
new LDClientImpl('sdk-key', createBasicPlatform(), options, callbacks);
30-
31-
beforeEach(() => {
32-
setupMockStreamingProcessor();
33-
});
55+
new LDClientImpl('sdk-key-ldclientimpl.test', createBasicPlatform(), options, callbacks);
3456

3557
afterEach(() => {
3658
client.close();
3759
jest.resetAllMocks();
3860
});
3961

4062
it('fires ready event in online mode', async () => {
41-
client = createClient();
63+
client = createClient({ updateProcessor: getUpdateProcessorFactory() });
4264
const initializedClient = await client.waitForInitialization({ timeout: 10 });
4365

4466
expect(initializedClient).toEqual(client);
@@ -49,8 +71,7 @@ describe('LDClientImpl', () => {
4971
});
5072

5173
it('wait for initialization completes even if initialization completes before it is called', (done) => {
52-
setupMockStreamingProcessor();
53-
client = createClient();
74+
client = createClient({ updateProcessor: getUpdateProcessorFactory() });
5475

5576
setTimeout(async () => {
5677
const initializedClient = await client.waitForInitialization({ timeout: 10 });
@@ -60,7 +81,7 @@ describe('LDClientImpl', () => {
6081
});
6182

6283
it('waiting for initialization the second time produces the same result', async () => {
63-
client = createClient();
84+
client = createClient({ updateProcessor: getUpdateProcessorFactory() });
6485
await client.waitForInitialization({ timeout: 10 });
6586

6687
const initializedClient = await client.waitForInitialization({ timeout: 10 });
@@ -79,9 +100,7 @@ describe('LDClientImpl', () => {
79100
});
80101

81102
it('initialization fails: failed event fires and initialization promise rejects', async () => {
82-
setupMockStreamingProcessor(true);
83-
client = createClient();
84-
103+
client = createClient({ updateProcessor: getUpdateProcessorFactory(true) });
85104
await expect(client.waitForInitialization({ timeout: 10 })).rejects.toThrow('failed');
86105

87106
expect(client.initialized()).toBeFalsy();
@@ -91,8 +110,7 @@ describe('LDClientImpl', () => {
91110
});
92111

93112
it('initialization promise is rejected even if the failure happens before wait is called', (done) => {
94-
setupMockStreamingProcessor(true);
95-
client = createClient();
113+
client = createClient({ updateProcessor: getUpdateProcessorFactory(true) });
96114

97115
setTimeout(async () => {
98116
await expect(client.waitForInitialization({ timeout: 10 })).rejects.toThrow('failed');
@@ -106,8 +124,7 @@ describe('LDClientImpl', () => {
106124
});
107125

108126
it('waiting a second time results in the same rejection', async () => {
109-
setupMockStreamingProcessor(true);
110-
client = createClient();
127+
client = createClient({ updateProcessor: getUpdateProcessorFactory(true) });
111128

112129
await expect(client.waitForInitialization({ timeout: 10 })).rejects.toThrow('failed');
113130
await expect(client.waitForInitialization({ timeout: 10 })).rejects.toThrow('failed');
@@ -124,31 +141,34 @@ describe('LDClientImpl', () => {
124141
});
125142

126143
it('resolves immediately if the client is already ready', async () => {
127-
client = createClient();
144+
client = createClient({ updateProcessor: getUpdateProcessorFactory() });
128145
await client.waitForInitialization({ timeout: 10 });
129146
await client.waitForInitialization({ timeout: 10 });
130147
});
131148

132149
it('creates only one Promise when waiting for initialization - when not using a timeout', async () => {
133-
client = createClient();
150+
client = createClient({ updateProcessor: getUpdateProcessorFactory() });
134151
const p1 = client.waitForInitialization();
135152
const p2 = client.waitForInitialization();
136153

137154
expect(p2).toBe(p1);
138155
});
139156

140157
it('rejects the returned promise when initialization does not complete within the timeout', async () => {
141-
setupMockStreamingProcessor(undefined, undefined, undefined, undefined, undefined, 10000);
142-
client = createClient();
158+
client = createClient({
159+
updateProcessor: getUpdateProcessorFactory(false, 10000),
160+
});
143161
await expect(async () => client.waitForInitialization({ timeout: 1 })).rejects.toThrow(
144162
'waitForInitialization timed out after 1 seconds.',
145163
);
146164
});
147165

148166
it('logs an error when the initialization does not complete within the timeout', async () => {
149-
setupMockStreamingProcessor(undefined, undefined, undefined, undefined, undefined, 10000);
150167
const logger = new TestLogger();
151-
client = createClient({ logger });
168+
client = createClient({
169+
logger,
170+
updateProcessor: getUpdateProcessorFactory(false, 10000),
171+
});
152172
try {
153173
await client.waitForInitialization({ timeout: 1 });
154174
} catch {
@@ -163,14 +183,18 @@ describe('LDClientImpl', () => {
163183
});
164184

165185
it('does not reject the returned promise when initialization completes within the timeout', async () => {
166-
setupMockStreamingProcessor(undefined, undefined, undefined, undefined, undefined, 1000);
167-
client = createClient();
168-
await expect(async () => client.waitForInitialization({ timeout: 5 })).not.toThrow();
186+
client = createClient({
187+
updateProcessor: getUpdateProcessorFactory(false, 100),
188+
});
189+
await expect(client.waitForInitialization({ timeout: 3 })).resolves.not.toThrow();
169190
});
170191

171192
it('logs when no timeout is set', async () => {
172193
const logger = new TestLogger();
173-
client = createClient({ logger });
194+
client = createClient({
195+
logger,
196+
updateProcessor: getUpdateProcessorFactory(),
197+
});
174198
await client.waitForInitialization();
175199
logger.expectMessages([
176200
{
@@ -183,7 +207,10 @@ describe('LDClientImpl', () => {
183207

184208
it('logs when the timeout is too high', async () => {
185209
const logger = new TestLogger();
186-
client = createClient({ logger });
210+
client = createClient({
211+
logger,
212+
updateProcessor: getUpdateProcessorFactory(),
213+
});
187214
await client.waitForInitialization({ timeout: Number.MAX_SAFE_INTEGER });
188215

189216
logger.expectMessages([
@@ -199,7 +226,7 @@ describe('LDClientImpl', () => {
199226
'does not log when timeout is under high timeout threshold',
200227
async (timeout) => {
201228
const logger = new TestLogger();
202-
client = createClient({ logger });
229+
client = createClient({ logger, updateProcessor: getUpdateProcessorFactory() });
203230
await client.waitForInitialization({ timeout });
204231
expect(logger.getCount(LogLevel.Warn)).toBe(0);
205232
},

0 commit comments

Comments
 (0)