-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathevent_processor_factory.spec.ts
More file actions
439 lines (359 loc) · 15.9 KB
/
event_processor_factory.spec.ts
File metadata and controls
439 lines (359 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/**
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, it, expect, beforeEach, vi, MockInstance } from 'vitest';
import { getBatchEventProcessor } from './event_processor_factory';
import { BatchEventProcessor, BatchEventProcessorConfig, EventWithId,DEFAULT_MAX_BACKOFF, DEFAULT_MIN_BACKOFF } from './batch_event_processor';
import { ExponentialBackoff, IntervalRepeater } from '../utils/repeater/repeater';
import { getMockSyncCache } from '../tests/mock/mock_cache';
import { LogLevel } from '../logging/logger';
vi.mock('./batch_event_processor', () => {
// Create proper mock constructor that works in browser mode
const BatchEventProcessor = vi.fn();
return {
BatchEventProcessor,
DEFAULT_MAX_BACKOFF: 60000,
DEFAULT_MIN_BACKOFF: 1000,
};
});
vi.mock('../utils/repeater/repeater', () => {
// Create proper mock constructors that work in browser mode
const IntervalRepeater = vi.fn();
const ExponentialBackoff = vi.fn();
const ConstantBackoff = vi.fn();
return {
IntervalRepeater,
ExponentialBackoff,
ConstantBackoff,
__platforms: ['__universal__'],
};
});
const getMockEventDispatcher = () => {
return {
dispatchEvent: vi.fn(),
}
};
describe('getBatchEventProcessor', () => {
const MockBatchEventProcessor = vi.mocked(BatchEventProcessor);
const MockExponentialBackoff = vi.mocked(ExponentialBackoff);
const MockIntervalRepeater = vi.mocked(IntervalRepeater);
beforeEach(() => {
MockBatchEventProcessor.mockReset();
MockExponentialBackoff.mockReset();
MockIntervalRepeater.mockReset();
});
it('should throw an error if provided eventDispatcher is not valid', () => {
expect(() => getBatchEventProcessor({
eventDispatcher: undefined as any,
defaultFlushInterval: 10000,
defaultBatchSize: 10,
})).toThrow('Invalid event dispatcher');
expect(() => getBatchEventProcessor({
eventDispatcher: null as any,
defaultFlushInterval: 10000,
defaultBatchSize: 10,
})).toThrow('Invalid event dispatcher');
expect(() => getBatchEventProcessor({
eventDispatcher: 'abc' as any,
defaultFlushInterval: 10000,
defaultBatchSize: 10,
})).toThrow('Invalid event dispatcher');
expect(() => getBatchEventProcessor({
eventDispatcher: {} as any,
defaultFlushInterval: 10000,
defaultBatchSize: 10,
})).toThrow('Invalid event dispatcher');
expect(() => getBatchEventProcessor({
eventDispatcher: { dispatchEvent: 'abc' } as any,
defaultFlushInterval: 10000,
defaultBatchSize: 10,
})).toThrow('Invalid event dispatcher');
});
it('should throw and error if provided event store is invalid', () => {
expect(() => getBatchEventProcessor({
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 10000,
defaultBatchSize: 10,
eventStore: 'abc' as any,
})).toThrow('Invalid store');
expect(() => getBatchEventProcessor({
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 10000,
defaultBatchSize: 10,
eventStore: 123 as any,
})).toThrow('Invalid store');
expect(() => getBatchEventProcessor({
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 10000,
defaultBatchSize: 10,
eventStore: {} as any,
})).toThrow('Invalid store method set, Invalid store method get, Invalid store method remove, Invalid store method getKeys');
expect(() => getBatchEventProcessor({
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 10000,
defaultBatchSize: 10,
eventStore: { set: 'abc', get: 'abc', remove: 'abc', getKeys: 'abc' } as any,
})).toThrow('Invalid store method set, Invalid store method get, Invalid store method remove, Invalid store method getKeys');
const noop = () => {};
expect(() => getBatchEventProcessor({
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 10000,
defaultBatchSize: 10,
eventStore: { set: noop, get: 'abc' } as any,
})).toThrow('Invalid store method get, Invalid store method remove, Invalid store method getKeys');
expect(() => getBatchEventProcessor({
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 10000,
defaultBatchSize: 10,
eventStore: { set: noop, get: noop, remove: 'abc' } as any,
})).toThrow('Invalid store method remove, Invalid store method getKeys');
expect(() => getBatchEventProcessor({
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 10000,
defaultBatchSize: 10,
eventStore: { set: noop, get: noop, remove: noop, getKeys: 'abc' } as any,
})).toThrow('Invalid store method getKeys');
});
it('returns an instane of BatchEventProcessor if no subclass constructor is provided', () => {
const options = {
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 1000,
defaultBatchSize: 10,
};
const processor = getBatchEventProcessor(options);
expect(processor instanceof BatchEventProcessor).toBe(true);
});
it('returns an instane of the provided subclass constructor', () => {
class CustomEventProcessor extends BatchEventProcessor {
constructor(opts: BatchEventProcessorConfig) {
super(opts);
}
}
const options = {
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 1000,
defaultBatchSize: 10,
};
const processor = getBatchEventProcessor(options, CustomEventProcessor);
expect(processor instanceof CustomEventProcessor).toBe(true);
});
it('does not use retry if retryOptions is not provided', () => {
const options = {
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 1000,
defaultBatchSize: 10,
};
const processor = getBatchEventProcessor(options);
expect(Object.is(processor, MockBatchEventProcessor.mock.instances[0])).toBe(true);
expect(MockBatchEventProcessor.mock.calls[0][0].retryConfig).toBe(undefined);
});
it('uses the correct maxRetries value when retryOptions is provided', () => {
const options = {
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 1000,
defaultBatchSize: 10,
retryOptions: {
maxRetries: 10,
},
};
const processor = getBatchEventProcessor(options);
expect(Object.is(processor, MockBatchEventProcessor.mock.instances[0])).toBe(true);
expect(MockBatchEventProcessor.mock.calls[0][0].retryConfig?.maxRetries).toBe(10);
});
it('uses exponential backoff with default parameters when retryOptions is provided without backoff values', () => {
const options = {
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 1000,
defaultBatchSize: 10,
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?.();
expect(Object.is(backoff, MockExponentialBackoff.mock.instances[0])).toBe(true);
expect(MockExponentialBackoff).toHaveBeenNthCalledWith(1, DEFAULT_MIN_BACKOFF, DEFAULT_MAX_BACKOFF, 50);
});
it('uses exponential backoff with provided backoff values in retryOptions', () => {
const options = {
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 1000,
defaultBatchSize: 10,
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);
const backoff = backoffProvider?.();
expect(Object.is(backoff, MockExponentialBackoff.mock.instances[0])).toBe(true);
expect(MockExponentialBackoff).toHaveBeenNthCalledWith(1, 1000, 2000, 50);
});
it('uses a IntervalRepeater with default flush interval and adds a startup log if flushInterval is not provided', () => {
const options = {
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 12345,
defaultBatchSize: 77,
};
const processor = getBatchEventProcessor(options);
expect(Object.is(processor, MockBatchEventProcessor.mock.instances[0])).toBe(true);
const usedRepeater = MockBatchEventProcessor.mock.calls[0][0].dispatchRepeater;
expect(Object.is(usedRepeater, MockIntervalRepeater.mock.instances[0])).toBe(true);
expect(MockIntervalRepeater).toHaveBeenNthCalledWith(1, 12345);
const startupLogs = MockBatchEventProcessor.mock.calls[0][0].startupLogs;
expect(startupLogs).toEqual(expect.arrayContaining([{
level: LogLevel.Warn,
message: 'Invalid flushInterval %s, defaulting to %s',
params: [undefined, 12345],
}]));
});
it('uses default flush interval and adds a startup log if flushInterval is less than 1', () => {
const options = {
eventDispatcher: getMockEventDispatcher(),
flushInterval: -1,
defaultFlushInterval: 12345,
defaultBatchSize: 77,
};
const processor = getBatchEventProcessor(options);
expect(Object.is(processor, MockBatchEventProcessor.mock.instances[0])).toBe(true);
const usedRepeater = MockBatchEventProcessor.mock.calls[0][0].dispatchRepeater;
expect(Object.is(usedRepeater, MockIntervalRepeater.mock.instances[0])).toBe(true);
expect(MockIntervalRepeater).toHaveBeenNthCalledWith(1, 12345);
const startupLogs = MockBatchEventProcessor.mock.calls[0][0].startupLogs;
expect(startupLogs).toEqual(expect.arrayContaining([{
level: LogLevel.Warn,
message: 'Invalid flushInterval %s, defaulting to %s',
params: [-1, 12345],
}]));
});
it('uses a IntervalRepeater with provided flushInterval and adds no startup log if provided flushInterval is valid', () => {
const options = {
eventDispatcher: getMockEventDispatcher(),
flushInterval: 12345,
defaultFlushInterval: 1000,
defaultBatchSize: 77,
};
const processor = getBatchEventProcessor(options);
expect(Object.is(processor, MockBatchEventProcessor.mock.instances[0])).toBe(true);
const usedRepeater = MockBatchEventProcessor.mock.calls[0][0].dispatchRepeater;
expect(Object.is(usedRepeater, MockIntervalRepeater.mock.instances[0])).toBe(true);
expect(MockIntervalRepeater).toHaveBeenNthCalledWith(1, 12345);
const startupLogs = MockBatchEventProcessor.mock.calls[0][0].startupLogs;
expect(startupLogs?.find((log) => log.message === 'Invalid flushInterval %s, defaulting to %s')).toBe(undefined);
});
it('uses default batch size and adds a startup log if batchSize is not provided', () => {
const options = {
eventDispatcher: getMockEventDispatcher(),
defaultBatchSize: 77,
defaultFlushInterval: 12345,
};
const processor = getBatchEventProcessor(options);
expect(Object.is(processor, MockBatchEventProcessor.mock.instances[0])).toBe(true);
expect(MockBatchEventProcessor.mock.calls[0][0].batchSize).toBe(77);
const startupLogs = MockBatchEventProcessor.mock.calls[0][0].startupLogs;
expect(startupLogs).toEqual(expect.arrayContaining([{
level: LogLevel.Warn,
message: 'Invalid batchSize %s, defaulting to %s',
params: [undefined, 77],
}]));
});
it('uses default size and adds a startup log if provided batchSize is less than 1', () => {
const options = {
eventDispatcher: getMockEventDispatcher(),
batchSize: -1,
defaultBatchSize: 77,
defaultFlushInterval: 12345,
};
const processor = getBatchEventProcessor(options);
expect(Object.is(processor, MockBatchEventProcessor.mock.instances[0])).toBe(true);
expect(MockBatchEventProcessor.mock.calls[0][0].batchSize).toBe(77);
const startupLogs = MockBatchEventProcessor.mock.calls[0][0].startupLogs;
expect(startupLogs).toEqual(expect.arrayContaining([{
level: LogLevel.Warn,
message: 'Invalid batchSize %s, defaulting to %s',
params: [-1, 77],
}]));
});
it('does not use a failedEventRepeater if failedEventRetryInterval is not provided', () => {
const options = {
eventDispatcher: getMockEventDispatcher(),
defaultBatchSize: 77,
defaultFlushInterval: 12345,
};
const processor = getBatchEventProcessor(options);
expect(Object.is(processor, MockBatchEventProcessor.mock.instances[0])).toBe(true);
expect(MockBatchEventProcessor.mock.calls[0][0].failedEventRepeater).toBe(undefined);
});
it('uses a IntervalRepeater with provided failedEventRetryInterval as failedEventRepeater', () => {
const options = {
eventDispatcher: getMockEventDispatcher(),
failedEventRetryInterval: 12345,
defaultBatchSize: 77,
defaultFlushInterval: 12345,
};
const processor = getBatchEventProcessor(options);
expect(Object.is(processor, MockBatchEventProcessor.mock.instances[0])).toBe(true);
expect(Object.is(MockBatchEventProcessor.mock.calls[0][0].failedEventRepeater, MockIntervalRepeater.mock.instances[1])).toBe(true);
expect(MockIntervalRepeater).toHaveBeenNthCalledWith(2, 12345);
});
it('uses the provided eventDispatcher', () => {
const eventDispatcher = getMockEventDispatcher();
const options = {
eventDispatcher,
defaultBatchSize: 77,
defaultFlushInterval: 12345,
};
const processor = getBatchEventProcessor(options);
expect(Object.is(processor, MockBatchEventProcessor.mock.instances[0])).toBe(true);
expect(MockBatchEventProcessor.mock.calls[0][0].eventDispatcher).toBe(eventDispatcher);
});
it('does not use any closingEventDispatcher if not provided', () => {
const options = {
eventDispatcher: getMockEventDispatcher(),
defaultBatchSize: 77,
defaultFlushInterval: 12345,
};
const processor = getBatchEventProcessor(options);
expect(Object.is(processor, MockBatchEventProcessor.mock.instances[0])).toBe(true);
expect(MockBatchEventProcessor.mock.calls[0][0].closingEventDispatcher).toBe(undefined);
});
it('uses the provided closingEventDispatcher', () => {
const closingEventDispatcher = getMockEventDispatcher();
const options = {
eventDispatcher: getMockEventDispatcher(),
closingEventDispatcher,
defaultBatchSize: 77,
defaultFlushInterval: 12345,
};
const processor = getBatchEventProcessor(options);
expect(Object.is(processor, MockBatchEventProcessor.mock.instances[0])).toBe(true);
expect(MockBatchEventProcessor.mock.calls[0][0].closingEventDispatcher).toBe(closingEventDispatcher);
});
it('uses the provided eventStore', () => {
const eventStore = getMockSyncCache<EventWithId>();
const options = {
eventDispatcher: getMockEventDispatcher(),
eventStore,
defaultBatchSize: 77,
defaultFlushInterval: 12345,
};
const processor = getBatchEventProcessor(options);
expect(Object.is(processor, MockBatchEventProcessor.mock.instances[0])).toBe(true);
expect(MockBatchEventProcessor.mock.calls[0][0].eventStore).toBe(eventStore);
});
});