Skip to content

Commit c377e89

Browse files
authored
fix: Fix issue where flush callback could be called twice. (#779)
Relates to #777
1 parent 1926b49 commit c377e89

File tree

2 files changed

+120
-2
lines changed

2 files changed

+120
-2
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { LDClientImpl } from '../src';
2+
import { createBasicPlatform } from './createBasicPlatform';
3+
import TestLogger from './Logger';
4+
import makeCallbacks from './makeCallbacks';
5+
6+
it('flushes events successfully and executes the callback', async () => {
7+
const platform = createBasicPlatform();
8+
platform.requests.fetch.mockImplementation(() =>
9+
Promise.resolve({ status: 200, headers: new Headers() }),
10+
);
11+
12+
const client = new LDClientImpl(
13+
'sdk-key-events',
14+
platform,
15+
{
16+
logger: new TestLogger(),
17+
stream: false,
18+
},
19+
makeCallbacks(false),
20+
);
21+
22+
client.identify({ key: 'user' });
23+
client.variation('dev-test-flag', { key: 'user' }, false);
24+
25+
const flushCallback = jest.fn();
26+
27+
await client.flush(flushCallback);
28+
29+
expect(platform.requests.fetch).toHaveBeenCalledWith(
30+
'https://events.launchdarkly.com/bulk',
31+
expect.objectContaining({
32+
method: 'POST',
33+
body: expect.any(String),
34+
}),
35+
);
36+
expect(flushCallback).toHaveBeenCalledWith(null, true);
37+
expect(flushCallback).toHaveBeenCalledTimes(1);
38+
});
39+
40+
it('flushes events successfully', async () => {
41+
const platform = createBasicPlatform();
42+
platform.requests.fetch.mockImplementation(() =>
43+
Promise.resolve({ status: 200, headers: new Headers() }),
44+
);
45+
46+
const client = new LDClientImpl(
47+
'sdk-key-events',
48+
platform,
49+
{
50+
logger: new TestLogger(),
51+
stream: false,
52+
},
53+
makeCallbacks(false),
54+
);
55+
56+
client.identify({ key: 'user' });
57+
client.variation('dev-test-flag', { key: 'user' }, false);
58+
59+
await client.flush();
60+
61+
expect(platform.requests.fetch).toHaveBeenCalledWith(
62+
'https://events.launchdarkly.com/bulk',
63+
expect.objectContaining({
64+
method: 'POST',
65+
body: expect.any(String),
66+
}),
67+
);
68+
});
69+
70+
it('calls error callback once when flush fails with http status code', async () => {
71+
const platform = createBasicPlatform();
72+
platform.requests.fetch.mockImplementation(() =>
73+
Promise.resolve({ status: 401, headers: new Headers() }),
74+
);
75+
76+
const flushCallback = jest.fn();
77+
const client = new LDClientImpl(
78+
'sdk-key-events',
79+
platform,
80+
{
81+
logger: new TestLogger(),
82+
stream: false,
83+
},
84+
makeCallbacks(false),
85+
);
86+
87+
client.identify({ key: 'user' });
88+
client.variation('dev-test-flag', { key: 'user' }, false);
89+
90+
await client.flush(flushCallback);
91+
92+
expect(flushCallback).toHaveBeenCalledWith(expect.any(Error), false);
93+
expect(flushCallback).toHaveBeenCalledTimes(1);
94+
});
95+
96+
it('calls error callback once when flush fails with exception', async () => {
97+
const platform = createBasicPlatform();
98+
platform.requests.fetch.mockImplementation(() => Promise.reject(new Error('test error')));
99+
100+
const flushCallback = jest.fn();
101+
const client = new LDClientImpl(
102+
'sdk-key-events',
103+
platform,
104+
{
105+
logger: new TestLogger(),
106+
stream: false,
107+
},
108+
makeCallbacks(false),
109+
);
110+
111+
client.identify({ key: 'user' });
112+
client.variation('dev-test-flag', { key: 'user' }, false);
113+
114+
await client.flush(flushCallback);
115+
116+
expect(flushCallback).toHaveBeenCalledWith(expect.any(Error), false);
117+
expect(flushCallback).toHaveBeenCalledTimes(1);
118+
});

packages/shared/sdk-server/src/LDClientImpl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,9 +773,9 @@ export default class LDClientImpl implements LDClient {
773773
try {
774774
await this._eventProcessor.flush();
775775
} catch (err) {
776-
callback?.(err as Error, false);
776+
return callback?.(err as Error, false);
777777
}
778-
callback?.(null, true);
778+
return callback?.(null, true);
779779
}
780780

781781
addHook(hook: Hook): void {

0 commit comments

Comments
 (0)