Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions packages/shared/sdk-server/__tests__/LDClientImpl.events.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { LDClientImpl } from '../src';
import { createBasicPlatform } from './createBasicPlatform';
import TestLogger from './Logger';
import makeCallbacks from './makeCallbacks';

it('flushes events successfully and executes the callback', async () => {
const platform = createBasicPlatform();
platform.requests.fetch.mockImplementation(() =>
Promise.resolve({ status: 200, headers: new Headers() }),
);

const client = new LDClientImpl(
'sdk-key-events',
platform,
{
logger: new TestLogger(),
stream: false,
},
makeCallbacks(false),
);

client.identify({ key: 'user' });
client.variation('dev-test-flag', { key: 'user' }, false);

const flushCallback = jest.fn();

await client.flush(flushCallback);

expect(platform.requests.fetch).toHaveBeenCalledWith(
'https://events.launchdarkly.com/bulk',
expect.objectContaining({
method: 'POST',
body: expect.any(String),
}),
);
expect(flushCallback).toHaveBeenCalledWith(null, true);
expect(flushCallback).toHaveBeenCalledTimes(1);
});

it('flushes events successfully', async () => {
const platform = createBasicPlatform();
platform.requests.fetch.mockImplementation(() =>
Promise.resolve({ status: 200, headers: new Headers() }),
);

const client = new LDClientImpl(
'sdk-key-events',
platform,
{
logger: new TestLogger(),
stream: false,
},
makeCallbacks(false),
);

client.identify({ key: 'user' });
client.variation('dev-test-flag', { key: 'user' }, false);

await client.flush();

expect(platform.requests.fetch).toHaveBeenCalledWith(
'https://events.launchdarkly.com/bulk',
expect.objectContaining({
method: 'POST',
body: expect.any(String),
}),
);
});

it('calls error callback once when flush fails with http status code', async () => {
const platform = createBasicPlatform();
platform.requests.fetch.mockImplementation(() =>
Promise.resolve({ status: 401, headers: new Headers() }),
);

const flushCallback = jest.fn();
const client = new LDClientImpl(
'sdk-key-events',
platform,
{
logger: new TestLogger(),
stream: false,
},
makeCallbacks(false),
);

client.identify({ key: 'user' });
client.variation('dev-test-flag', { key: 'user' }, false);

await client.flush(flushCallback);

expect(flushCallback).toHaveBeenCalledWith(expect.any(Error), false);
expect(flushCallback).toHaveBeenCalledTimes(1);
});

it('calls error callback once when flush fails with exception', async () => {
const platform = createBasicPlatform();
platform.requests.fetch.mockImplementation(() => Promise.reject(new Error('test error')));

const flushCallback = jest.fn();
const client = new LDClientImpl(
'sdk-key-events',
platform,
{
logger: new TestLogger(),
stream: false,
},
makeCallbacks(false),
);

client.identify({ key: 'user' });
client.variation('dev-test-flag', { key: 'user' }, false);

await client.flush(flushCallback);

expect(flushCallback).toHaveBeenCalledWith(expect.any(Error), false);
expect(flushCallback).toHaveBeenCalledTimes(1);
});
4 changes: 2 additions & 2 deletions packages/shared/sdk-server/src/LDClientImpl.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on our interface documentation there is another issue with this implementation. We say the function rejects, but it doesn't actually. I am more concerned about changing that as it is likely to introduce problems. We may instead consider changing the API documentation and suggesting using the callback if the error is required.

Additionally you cannot retry if it fails, at least not the same payload, as those events will have been discarded by that time.

Original file line number Diff line number Diff line change
Expand Up @@ -773,9 +773,9 @@ export default class LDClientImpl implements LDClient {
try {
await this._eventProcessor.flush();
} catch (err) {
callback?.(err as Error, false);
return callback?.(err as Error, false);
}
callback?.(null, true);
return callback?.(null, true);
}

addHook(hook: Hook): void {
Expand Down