8000 [FSSDK-11510] update retryConfig for batch event processor factories by raju-opti · Pull Request #1057 · optimizely/javascript-sdk · GitHub
[go: up one dir, main page]

Skip to content

[FSSDK-11510] update retryConfig for batch event processor factories #1057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on G 8000 itHub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2025
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
8000
Diff view
49 changes: 1 addition & 48 deletions lib/event_processor/batch_event_processor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024, Optimizely
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -639,53 +639,6 @@ describe('BatchEventProcessor', async () => {
}
});

it('should retry indefinitely using the provided backoffController if maxRetry is undefined', async () => {
const eventDispatcher = getMockDispatcher();
const mockDispatch: MockInstance<typeof eventDispatcher.dispatchEvent> = eventDispatcher.dispatchEvent;
mockDispatch.mockRejectedValue(new Error());
const dispatchRepeater = getMockRepeater();

const backoffController = {
backoff: vi.fn().mockReturnValue(1000),
reset: vi.fn(),
};

const processor = new BatchEventProcessor({
eventDispatcher,
dispatchRepeater,
retryConfig: {
backoffProvider: () => backoffController,
},
batchSize: 100,
});

processor.start();
await processor.onRunning();

const events: ProcessableEvent[] = [];
for(let i = 0; i < 10; i++) {
const event = createImpressionEvent(`id-${i}`);
events.push(event);
await processor.process(event);
}

expect(eventDispatcher.dispatchEvent).toHaveBeenCalledTimes(0);
await dispatchRepeater.execute(0);

for(let i = 0; i < 200; i++) {
await exhaustMicrotasks();
await advanceTimersByTime(1000);
}

expect(eventDispatcher.dispatchEvent).toHaveBeenCalledTimes(201);
expect(backoffController.backoff).toHaveBeenCalledTimes(200);

const request = buildLogEvent(events);
for(let i = 0; i < 201; i++) {
expect(eventDispatcher.dispatchEvent.mock.calls[i][0]).toEqual(request);
}
});

it('should remove the events from the eventStore after dispatch is successfull', async () => {
const eventDispatcher = getMockDispatcher();
const mockDispatch: MockInstance<typeof eventDispatcher.dispatchEvent> = eventDispatcher.dispatchEvent;
Expand Down
2 changes: 1 addition & 1 deletion lib/event_processor/batch_event_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type EventWithId = {
};

export type RetryConfig = {
maxRetries?: number;
maxRetries: number;
backoffProvider: Producer<BackoffController>;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/event_processor/event_processor_factory.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ describe('createBatchEventProcessor', () => {
expect(mockGetOpaqueBatchEventProcessor.mock.calls[1][0].batchSize).toBe(undefined);
});

it('uses maxRetries value of 10', () => {
it('uses maxRetries value of 5', () => {
const processor = createBatchEventProcessor({ });
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].retryOptions?.maxRetries).toBe(10);
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].retryOptions?.maxRetries).toBe(5);
});

it('uses no failed event retry if an eventStore is not provided', () => {
Expand Down
3 changes: 2 additions & 1 deletion lib/event_processor/event_processor_factory.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export const createBatchEventProcessor = (
defaultFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL,
defaultBatchSize: DEFAULT_EVENT_BATCH_SIZE,
retryOptions: {
maxRetries: 10,
maxRetries: 5,

},
failedEventRetryInterval: eventStore ? FAILED_EVENT_RETRY_INTERVAL : undefined,
eventStore,
Expand Down
45 changes: 11 additions & 34 deletions lib/event_processor/event_processor_factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024, Optimizely
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -83,24 +83,8 @@ describe('getBatchEventProcessor', () => {
expect(MockBatchEventProcessor.mock.calls[0][0].retryConfig).toBe(undefined);
});

it('uses retry when retryOptions is provided', () => {
const options = {
eventDispatcher: getMockEventDispatcher(),
retryOptions: {},
defaultFlushInterval: 1000,
defaultBatchSize: 10,
};

const processor = getBatchEventProcessor(options);

expect(Object.is(processor, MockBatchEventProcessor.mock.instances[0])).toBe(true);
const usedRetryConfig = MockBatchEventProcessor.mock.calls[0][0].retryConfig;
expect(usedRetryConfig).not.toBe(undefined);
expect(usedRetryConfig?.backoffProvider).not.toBe(undefined);
});

it('uses the correct maxRetries value when retryOptions is provided', () => {
const options1 = {
const options = {
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 1000,
defaultBatchSize: 10,
Expand All @@ -109,34 +93,24 @@ describe('getBatchEventProcessor', () => {
},
};

const processor1 = getBatchEventProcessor(options1);
expect(Object.is(processor1, MockBatchEventProcessor.mock.instances[0])).toBe(true);
const processor = getBatchEventProcessor(options);
expect(Object.is(processor, MockBatchEventProcessor.mock.instances[0])).toBe(true);
expect(MockBatchEventProcessor.mock.calls[0][0].retryConfig?.maxRetries).toBe(10);

const options2 = {
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 1000,
defaultBatchSize: 10,
retryOptions: {},
};

const processor2 = getBatchEventProcessor(options2);
expect(Object.is(processor2, MockBatchEventProcessor.mock.instances[1])).toBe(true);
expect(MockBatchEventProcessor.mock.calls[0][0].retryConfig).not.toBe(undefined);
expect(MockBatchEventProcessor.mock.calls[1][0].retryConfig?.maxRetries).toBe(undefined);
});

it('uses exponential backoff with default parameters when retryOptions is provided without backoff values', () => {
const options = {
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 1000,
defaultBatchSize: 10,
retryOptions: {},
retryOptions: { maxRetries: 2 },
};

const processor = getBatchEventProcessor(options);
expect(Object.is(processor, MockBatchEventProcessor.mock.instances[0])).toBe(true);

expect(MockBatchEventProcessor.mock.calls[0][0].retryConfig?.maxRetries).toBe(2);

const backoffProvider = MockBatchEventProcessor.mock.calls[0][0].retryConfig?.backoffProvider;
expect(backoffProvider).not.toBe(undefined);
const backoff = backoffProvider?.();
Expand All @@ -149,11 +123,14 @@ describe('getBatchEventProcessor', () => {
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 1000,
defaultBatchSize: 10,
retryOptions: { minBackoff: 1000, maxBackoff: 2000 },
retryOptions: { maxRetries: 2, minBackoff: 1000, maxBackoff: 2000 },
};

const processor = getBatchEventProcessor(options);
expect(Object.is(processor, MockBatchEventProcessor.mock.instances[0])).toBe(true);

expect(MockBatchEventProcessor.mock.calls[0][0].retryConfig?.maxRetries).toBe(2);

const backoffProvider = MockBatchEventProcessor.mock.calls[0][0].retryConfig?.backoffProvider;

expect(backoffProvider).not.toBe(undefined);
Expand Down
2 changes: 1 addition & 1 deletion lib/event_processor/event_processor_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export type BatchEventProcessorFactoryOptions = Omit<BatchEventProcessorOptions,
defaultBatchSize: number;
eventStore?: Store<EventWithId>;
retryOptions?: {
maxRetries?: number;
maxRetries: number;
minBackoff?: number;
maxBackoff?: number;
};
Expand Down
Loading
0