|
| 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 | +}); |
0 commit comments