Skip to content

Commit c6032b3

Browse files
authored
feat: Add support for optional timeout for waitForInitialization. (#437)
1 parent 881a745 commit c6032b3

20 files changed

+238
-57
lines changed

contract-tests/sdkClientEntity.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,7 @@ export async function newSdkClientEntity(options) {
109109
makeSdkConfig(options.configuration, options.tag),
110110
);
111111
try {
112-
await Promise.race([
113-
client.waitForInitialization(),
114-
new Promise((resolve) => setTimeout(resolve, timeout)),
115-
]);
112+
await client.waitForInitialization({timeout: timeout});
116113
} catch (_) {
117114
// if waitForInitialization() rejects, the client failed to initialize, see next line
118115
}

packages/sdk/server-node/__tests__/LDClientNode.bigSegments.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe('given test data with big segments', () => {
9393
bigSegments: bigSegmentsConfig,
9494
});
9595

96-
await client.waitForInitialization();
96+
await client.waitForInitialization({ timeout: 10 });
9797
});
9898

9999
it('can require status', async () => {
@@ -135,7 +135,7 @@ describe('given test data with big segments', () => {
135135
logger,
136136
});
137137

138-
await client.waitForInitialization();
138+
await client.waitForInitialization({ timeout: 10 });
139139
});
140140

141141
it('Can observe the status change', (done) => {

packages/sdk/server-node/__tests__/LDClientNode.fileDataSource.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe('When using a file data source', () => {
9797
sendEvents: false,
9898
});
9999

100-
await client.waitForInitialization();
100+
await client.waitForInitialization({ timeout: 10 });
101101

102102
const f1Var = await client.variation(flag1Key, { key: 'user1' }, 'default');
103103
expect(f1Var).toEqual('off');
@@ -121,7 +121,7 @@ describe('When using a file data source', () => {
121121
sendEvents: false,
122122
});
123123

124-
await client.waitForInitialization();
124+
await client.waitForInitialization({ timeout: 10 });
125125

126126
const f1Var = await client.variation(flag1Key, { key: 'user1' }, 'default');
127127
expect(f1Var).toEqual('off');
@@ -143,7 +143,7 @@ describe('When using a file data source', () => {
143143
sendEvents: false,
144144
});
145145

146-
await client.waitForInitialization();
146+
await client.waitForInitialization({ timeout: 10 });
147147

148148
const f1Var = await client.variation(flag1Key, { key: 'user1' }, 'default');
149149
expect(f1Var).toEqual('off');

packages/sdk/server-node/__tests__/LDClientNode.proxy.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('When using a proxy', () => {
5353

5454
closeable.push(proxy, server, client);
5555

56-
await client.waitForInitialization();
56+
await client.waitForInitialization({ timeout: 10 });
5757
expect(client.initialized()).toBe(true);
5858

5959
// If the proxy server did not log a request then the SDK did not actually use the proxy
@@ -81,7 +81,7 @@ describe('When using a proxy', () => {
8181

8282
closeable.push(proxy, server, events, client);
8383

84-
await client.waitForInitialization();
84+
await client.waitForInitialization({ timeout: 10 });
8585
expect(client.initialized()).toBe(true);
8686

8787
// If the proxy server did not log a request then the SDK did not actually use the proxy
@@ -110,7 +110,7 @@ describe('When using a proxy', () => {
110110

111111
closeable.push(proxy, pollingServer, eventsServer, client);
112112

113-
await client.waitForInitialization();
113+
await client.waitForInitialization({ timeout: 10 });
114114
expect(client.initialized()).toBe(true);
115115

116116
// If the proxy server did not log a request then the SDK did not actually use the proxy

packages/sdk/server-node/__tests__/LDClientNode.tls.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('When using a TLS connection', () => {
2727
tlsParams: { ca: server.certificate },
2828
diagnosticOptOut: true,
2929
});
30-
await client.waitForInitialization();
30+
await client.waitForInitialization({ timeout: 10 });
3131
});
3232

3333
it('cannot connect via HTTPS to a server with a self-signed certificate, using default config', async () => {
@@ -67,7 +67,7 @@ describe('When using a TLS connection', () => {
6767
});
6868

6969
// this won't return until the stream receives the "put" event
70-
await client.waitForInitialization();
70+
await client.waitForInitialization({ timeout: 10 });
7171
events.close();
7272
});
7373

@@ -85,7 +85,7 @@ describe('When using a TLS connection', () => {
8585
logger,
8686
});
8787

88-
await client.waitForInitialization();
88+
await client.waitForInitialization({ timeout: 10 });
8989
client.identify({ key: 'user' });
9090
await client.flush();
9191

packages/shared/mocks/src/streamingProcessor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const setupMockStreamingProcessor = (
1414
patchResponseJson?: any,
1515
deleteResponseJson?: any,
1616
errorTimeoutSeconds: number = 0,
17+
initTimeoutMs: number = 0,
1718
) => {
1819
MockStreamingProcessor.mockImplementation(
1920
(
@@ -35,7 +36,7 @@ export const setupMockStreamingProcessor = (
3536
}, errorTimeoutSeconds * 1000);
3637
} else {
3738
// execute put which will resolve the identify promise
38-
setTimeout(() => listeners.get('put')?.processJson(putResponseJson));
39+
setTimeout(() => listeners.get('put')?.processJson(putResponseJson), initTimeoutMs);
3940

4041
if (patchResponseJson) {
4142
setTimeout(() => listeners.get('patch')?.processJson(patchResponseJson));

packages/shared/sdk-server-edge/src/api/LDClient.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Edge LDClient', () => {
2323
const client = new LDClient('client-side-id', basicPlatform.info, {
2424
sendEvents: true,
2525
});
26-
await client.waitForInitialization();
26+
await client.waitForInitialization({ timeout: 10 });
2727
const passedConfig = mockEventProcessor.mock.calls[0][0];
2828

2929
expect(passedConfig).toMatchObject({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('given an LDClient with test data', () => {
2626
makeCallbacks(true),
2727
);
2828

29-
await client.waitForInitialization();
29+
await client.waitForInitialization({ timeout: 10 });
3030
});
3131

3232
afterEach(() => {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('given an LDClient with test data', () => {
4444
makeCallbacks(true),
4545
);
4646

47-
await client.waitForInitialization();
47+
await client.waitForInitialization({ timeout: 10 });
4848
});
4949

5050
afterEach(() => {
@@ -298,15 +298,15 @@ describe('given an offline client', () => {
298298
});
299299

300300
it('returns the default value for variation', async () => {
301-
await client.waitForInitialization();
301+
await client.waitForInitialization({ timeout: 10 });
302302
td.update(td.flag('flagkey').variations('value').variationForAll(0));
303303
const result = await client.variation('flagkey', defaultUser, 'default');
304304
expect(result).toEqual('default');
305305
expect(logger.getCount(LogLevel.Info)).toEqual(1);
306306
});
307307

308308
it('returns the default value for variationDetail', async () => {
309-
await client.waitForInitialization();
309+
await client.waitForInitialization({ timeout: 10 });
310310
td.update(td.flag('flagkey').variations('value').variationForAll(0));
311311
const result = await client.variationDetail('flagkey', defaultUser, 'default');
312312
expect(result).toMatchObject({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('given a client with mock event processor', () => {
3333
},
3434
makeCallbacks(false),
3535
);
36-
await client.waitForInitialization();
36+
await client.waitForInitialization({ timeout: 10 });
3737
});
3838

3939
afterEach(() => {

0 commit comments

Comments
 (0)