Skip to content

Commit 2c411ca

Browse files
committed
new test
1 parent 6a64167 commit 2c411ca

20 files changed

+167
-2725
lines changed

lib/event_processor/event_processor_factory.browser.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
import { getForwardingEventProcessor } from './forwarding_event_processor';
1818
import { EventDispatcher } from './eventDispatcher';
1919
import { EventProcessor } from './eventProcessor';
20-
import { BatchEventProcessor, BatchEventProcessorConfig } from './batch_event_processor';
21-
import { getQueuingEventProcessor, QueueingEventProcessorOptions } from './event_processor_factory';
20+
import { BatchEventProcessor, BatchEventProcessorConfig, EventWithId } from './batch_event_processor';
21+
import { getBatchEventProcessor, QueueingEventProcessorOptions } from './event_processor_factory';
2222
import defaultEventDispatcher from './default_dispatcher.browser';
2323
import sendBeaconEventDispatcher from '../plugins/event_dispatcher/send_beacon_dispatcher';
24+
import { LocalStorageCache } from '../utils/cache/local_storage_cache.browser';
25+
import { SyncPrefixCache } from '../utils/cache/cache';
2426

2527
export const FAILED_EVENT_RETRY_INTERVAL = 20 * 1000;
28+
export const EVENT_STORE_PREFIX = 'fs_optly_pending_events';
2629

2730
export const createForwardingEventProcessor = (
2831
eventDispatcher: EventDispatcher = defaultEventDispatcher,
@@ -33,14 +36,23 @@ export const createForwardingEventProcessor = (
3336
export const createQueueingEventProcessor = (
3437
options: QueueingEventProcessorOptions
3538
): EventProcessor => {
36-
return getQueuingEventProcessor({
39+
const localStorageCache = new LocalStorageCache<string>();
40+
const eventStore = new SyncPrefixCache<string, EventWithId>(
41+
localStorageCache, EVENT_STORE_PREFIX,
42+
JSON.parse,
43+
JSON.stringify
44+
);
45+
46+
return getBatchEventProcessor({
3747
eventDispatcher: options.eventDispatcher || defaultEventDispatcher,
3848
closingEventDispatcher: options.closingEventDispatcher ||
3949
(options.eventDispatcher ? options.eventDispatcher : sendBeaconEventDispatcher),
4050
flushInterval: options.flushInterval,
4151
batchSize: options.batchSize,
42-
retryOptions: {},
52+
retryOptions: {
53+
maxRetries: 5,
54+
},
4355
failedEventRetryInterval: FAILED_EVENT_RETRY_INTERVAL,
56+
eventStore,
4457
});
4558
};
46-
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import { describe, it, expect, beforeEach, vi, MockInstance } from 'vitest';
2+
import { DEFAULT_MAX_BACKOFF, DEFAULT_MIN_BACKOFF, getBatchEventProcessor } from './event_processor_factory';
3+
import { BatchEventProcessor, BatchEventProcessorConfig } from './batch_event_processor';
4+
import { ExponentialBackoff, IntervalRepeater } from '../utils/repeater/repeater';
5+
6+
vi.mock('./batch_event_processor');
7+
vi.mock('../utils/repeater/repeater');
8+
9+
type BatchEventProcessorConstructor = typeof BatchEventProcessor;
10+
11+
const getMockEventDispatcher = () => {
12+
return {
13+
dispatchEvent: vi.fn(),
14+
}
15+
};
16+
17+
describe('getBatchEventProcessor', () => {
18+
const MockBatchEventProcessor = vi.mocked(BatchEventProcessor);
19+
const MockExponentialBackoff = vi.mocked(ExponentialBackoff);
20+
21+
beforeEach(() => {
22+
MockBatchEventProcessor.mockReset();
23+
MockExponentialBackoff.mockReset();
24+
});
25+
26+
it('returns an instane of BatchEventProcessor if no subclass constructor is provided', () => {
27+
const options = {
28+
eventDispatcher: getMockEventDispatcher(),
29+
};
30+
31+
const processor = getBatchEventProcessor(options);
32+
33+
expect(processor instanceof BatchEventProcessor).toBe(true);
34+
});
35+
36+
it('returns an instane of the provided subclass constructor', () => {
37+
class CustomEventProcessor extends BatchEventProcessor {
38+
constructor(opts: BatchEventProcessorConfig) {
39+
super(opts);
40+
}
41+
}
42+
43+
const options = {
44+
eventDispatcher: getMockEventDispatcher(),
45+
};
46+
47+
const processor = getBatchEventProcessor(options, CustomEventProcessor);
48+
49+
expect(processor instanceof CustomEventProcessor).toBe(true);
50+
});
51+
52+
it('does not use retry if retryOptions is not provided', () => {
53+
const options = {
54+
eventDispatcher: getMockEventDispatcher(),
55+
};
56+
57+
const processor = getBatchEventProcessor(options);
58+
expect(MockBatchEventProcessor.mock.calls[0][0].retryConfig).toBe(undefined);
59+
});
60+
61+
it('uses retry when retryOptions is provided', () => {
62+
const options = {
63+
eventDispatcher: getMockEventDispatcher(),
64+
retryOptions: {},
65+
};
66+
67+
let processor = getBatchEventProcessor(options);
68+
69+
const usedRetryConfig = MockBatchEventProcessor.mock.calls[0][0].retryConfig;
70+
expect(usedRetryConfig).not.toBe(undefined);
71+
expect(usedRetryConfig?.backoffProvider).not.toBe(undefined);
72+
});
73+
74+
it('uses the correct maxRetries value when retryOptions is provided', () => {
75+
const options1 = {
76+
eventDispatcher: getMockEventDispatcher(),
77+
retryOptions: {
78+
maxRetries: 10,
79+
},
80+
};
81+
82+
let processor1 = getBatchEventProcessor(options1);
83+
expect(MockBatchEventProcessor.mock.calls[0][0].retryConfig?.maxRetries).toBe(10);
84+
85+
const options2 = {
86+
eventDispatcher: getMockEventDispatcher(),
87+
retryOptions: {},
88+
};
89+
90+
let processor2 = getBatchEventProcessor(options2);
91+
expect(MockBatchEventProcessor.mock.calls[0][0].retryConfig).not.toBe(undefined);
92+
expect(MockBatchEventProcessor.mock.calls[1][0].retryConfig?.maxRetries).toBe(undefined);
93+
});
94+
95+
it('uses exponential backoff with default parameters when retryOptions is provided without backoff values', () => {
96+
const options = {
97+
eventDispatcher: getMockEventDispatcher(),
98+
retryOptions: {},
99+
};
100+
101+
let processor = getBatchEventProcessor(options);
102+
const backoffProvider = MockBatchEventProcessor.mock.calls[0][0].retryConfig?.backoffProvider;
103+
104+
expect(backoffProvider).not.toBe(undefined);
105+
const backoff = backoffProvider?.();
106+
expect(Object.is(backoff, MockExponentialBackoff.mock.instances[0])).toBe(true);
107+
expect(MockExponentialBackoff).toHaveBeenNthCalledWith(1, DEFAULT_MIN_BACKOFF, DEFAULT_MAX_BACKOFF, 500);
108+
});
109+
110+
it('uses exponential backoff with provided backoff values in retryOptions', () => {
111+
const options = {
112+
eventDispatcher: getMockEventDispatcher(),
113+
retryOptions: { minBackoff: 1000, maxBackoff: 2000 },
114+
};
115+
116+
let processor = getBatchEventProcessor(options);
117+
const backoffProvider = MockBatchEventProcessor.mock.calls[0][0].retryConfig?.backoffProvider;
118+
119+
expect(backoffProvider).not.toBe(undefined);
120+
const backoff = backoffProvider?.();
121+
expect(Object.is(backoff, MockExponentialBackoff.mock.instances[0])).toBe(true);
122+
expect(MockExponentialBackoff).toHaveBeenNthCalledWith(1, 1000, 2000, 500);
123+
});
124+
});

lib/event_processor/event_processor_factory.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { StartupLog } from "../service";
33
import { ExponentialBackoff, IntervalRepeater } from "../utils/repeater/repeater";
44
import { EventDispatcher } from "./eventDispatcher";
55
import { EventProcessor } from "./eventProcessor";
6-
import { BatchEventProcessor, RetryConfig } from "./batch_event_processor";
6+
import { BatchEventProcessor, EventWithId, RetryConfig } from "./batch_event_processor";
7+
import { Cache } from "../utils/cache/cache";
78

89
export const DEFAULT_EVENT_BATCH_SIZE = 10;
910
export const DEFAULT_EVENT_FLUSH_INTERVAL = 1000;
@@ -21,18 +22,19 @@ export type QueueingEventProcessorOptions = {
2122
export type QueueingEventProcessorFactoryOptions = Omit<QueueingEventProcessorOptions, 'eventDispatcher'> & {
2223
eventDispatcher: EventDispatcher;
2324
failedEventRetryInterval?: number;
25+
eventStore?: Cache<EventWithId>;
2426
retryOptions?: {
2527
maxRetries?: number;
2628
minBackoff?: number;
2729
maxBackoff?: number;
2830
};
2931
}
3032

31-
export const getQueuingEventProcessor = (
33+
export const getBatchEventProcessor = (
3234
options: QueueingEventProcessorFactoryOptions,
3335
EventProcessorConstructor: typeof BatchEventProcessor = BatchEventProcessor
3436
): EventProcessor => {
35-
const { eventDispatcher, closingEventDispatcher, retryOptions } = options;
37+
const { eventDispatcher, closingEventDispatcher, retryOptions, eventStore } = options;
3638

3739
const retryConfig: RetryConfig | undefined = retryOptions ? {
3840
maxRetries: retryOptions.maxRetries,
@@ -78,6 +80,7 @@ export const getQueuingEventProcessor = (
7880
failedEventRepeater,
7981
retryConfig,
8082
batchSize,
81-
startupLogs
83+
eventStore,
84+
startupLogs,
8285
});
8386
};

lib/event_processor/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,4 @@
1717
export * from './events'
1818
export * from './eventProcessor'
1919
export * from './eventDispatcher'
20-
export * from './pendingEventsDispatcher'
2120
export * from './v1/buildEventV1'
22-
export * from './v1/v1EventProcessor'

lib/event_processor/pendingEventsDispatcher.ts

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)