11import { 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' ;
2+ import { DEFAULT_EVENT_BATCH_SIZE , DEFAULT_EVENT_FLUSH_INTERVAL , DEFAULT_MAX_BACKOFF , DEFAULT_MIN_BACKOFF , getBatchEventProcessor } from './event_processor_factory' ;
3+ import { BatchEventProcessor , BatchEventProcessorConfig , EventWithId } from './batch_event_processor' ;
44import { ExponentialBackoff , IntervalRepeater } from '../utils/repeater/repeater' ;
5+ import { getMockSyncCache } from '../tests/mock/mock_cache' ;
6+ import { LogLevel } from '../modules/logging' ;
57
68vi . mock ( './batch_event_processor' ) ;
79vi . mock ( '../utils/repeater/repeater' ) ;
@@ -17,10 +19,12 @@ const getMockEventDispatcher = () => {
1719describe ( 'getBatchEventProcessor' , ( ) => {
1820 const MockBatchEventProcessor = vi . mocked ( BatchEventProcessor ) ;
1921 const MockExponentialBackoff = vi . mocked ( ExponentialBackoff ) ;
22+ const MockIntervalRepeater = vi . mocked ( IntervalRepeater ) ;
2023
2124 beforeEach ( ( ) => {
2225 MockBatchEventProcessor . mockReset ( ) ;
2326 MockExponentialBackoff . mockReset ( ) ;
27+ MockIntervalRepeater . mockReset ( ) ;
2428 } ) ;
2529
2630 it ( 'returns an instane of BatchEventProcessor if no subclass constructor is provided' , ( ) => {
@@ -55,6 +59,7 @@ describe('getBatchEventProcessor', () => {
5559 } ;
5660
5761 const processor = getBatchEventProcessor ( options ) ;
62+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
5863 expect ( MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryConfig ) . toBe ( undefined ) ;
5964 } ) ;
6065
@@ -66,6 +71,7 @@ describe('getBatchEventProcessor', () => {
6671
6772 let processor = getBatchEventProcessor ( options ) ;
6873
74+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
6975 const usedRetryConfig = MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryConfig ;
7076 expect ( usedRetryConfig ) . not . toBe ( undefined ) ;
7177 expect ( usedRetryConfig ?. backoffProvider ) . not . toBe ( undefined ) ;
@@ -80,6 +86,7 @@ describe('getBatchEventProcessor', () => {
8086 } ;
8187
8288 let processor1 = getBatchEventProcessor ( options1 ) ;
89+ expect ( Object . is ( processor1 , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
8390 expect ( MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryConfig ?. maxRetries ) . toBe ( 10 ) ;
8491
8592 const options2 = {
@@ -88,6 +95,7 @@ describe('getBatchEventProcessor', () => {
8895 } ;
8996
9097 let processor2 = getBatchEventProcessor ( options2 ) ;
98+ expect ( Object . is ( processor2 , MockBatchEventProcessor . mock . instances [ 1 ] ) ) . toBe ( true ) ;
9199 expect ( MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryConfig ) . not . toBe ( undefined ) ;
92100 expect ( MockBatchEventProcessor . mock . calls [ 1 ] [ 0 ] . retryConfig ?. maxRetries ) . toBe ( undefined ) ;
93101 } ) ;
@@ -99,8 +107,9 @@ describe('getBatchEventProcessor', () => {
99107 } ;
100108
101109 let processor = getBatchEventProcessor ( options ) ;
102- const backoffProvider = MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryConfig ?. backoffProvider ;
110+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
103111
112+ const backoffProvider = MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryConfig ?. backoffProvider ;
104113 expect ( backoffProvider ) . not . toBe ( undefined ) ;
105114 const backoff = backoffProvider ?.( ) ;
106115 expect ( Object . is ( backoff , MockExponentialBackoff . mock . instances [ 0 ] ) ) . toBe ( true ) ;
@@ -114,11 +123,181 @@ describe('getBatchEventProcessor', () => {
114123 } ;
115124
116125 let processor = getBatchEventProcessor ( options ) ;
126+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
117127 const backoffProvider = MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryConfig ?. backoffProvider ;
118128
119129 expect ( backoffProvider ) . not . toBe ( undefined ) ;
120130 const backoff = backoffProvider ?.( ) ;
121131 expect ( Object . is ( backoff , MockExponentialBackoff . mock . instances [ 0 ] ) ) . toBe ( true ) ;
122132 expect ( MockExponentialBackoff ) . toHaveBeenNthCalledWith ( 1 , 1000 , 2000 , 500 ) ;
123133 } ) ;
134+
135+ it ( 'uses a IntervalRepeater with default flush interval and adds a startup log if flushInterval is not provided' , ( ) => {
136+ const options = {
137+ eventDispatcher : getMockEventDispatcher ( ) ,
138+ } ;
139+
140+ let processor = getBatchEventProcessor ( options ) ;
141+
142+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
143+ const usedRepeater = MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . dispatchRepeater ;
144+ expect ( Object . is ( usedRepeater , MockIntervalRepeater . mock . instances [ 0 ] ) ) . toBe ( true ) ;
145+ expect ( MockIntervalRepeater ) . toHaveBeenNthCalledWith ( 1 , DEFAULT_EVENT_FLUSH_INTERVAL ) ;
146+
147+ const startupLogs = MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . startupLogs ;
148+ expect ( startupLogs ) . toEqual ( expect . arrayContaining ( [ {
149+ level : LogLevel . WARNING ,
150+ message : 'Invalid flushInterval %s, defaulting to %s' ,
151+ params : [ undefined , DEFAULT_EVENT_FLUSH_INTERVAL ] ,
152+ } ] ) ) ;
153+ } ) ;
154+
155+ it ( 'uses default flush interval and adds a startup log if flushInterval is less than 1' , ( ) => {
156+ const options = {
157+ eventDispatcher : getMockEventDispatcher ( ) ,
158+ flushInterval : - 1 ,
159+ } ;
160+
161+ let processor = getBatchEventProcessor ( options ) ;
162+
163+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
164+ const usedRepeater = MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . dispatchRepeater ;
165+ expect ( Object . is ( usedRepeater , MockIntervalRepeater . mock . instances [ 0 ] ) ) . toBe ( true ) ;
166+ expect ( MockIntervalRepeater ) . toHaveBeenNthCalledWith ( 1 , DEFAULT_EVENT_FLUSH_INTERVAL ) ;
167+
168+ const startupLogs = MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . startupLogs ;
169+ expect ( startupLogs ) . toEqual ( expect . arrayContaining ( [ {
170+ level : LogLevel . WARNING ,
171+ message : 'Invalid flushInterval %s, defaulting to %s' ,
172+ params : [ - 1 , DEFAULT_EVENT_FLUSH_INTERVAL ] ,
173+ } ] ) ) ;
174+ } ) ;
175+
176+ it ( 'uses a IntervalRepeater with provided flushInterval and adds no startup log if provided flushInterval is valid' , ( ) => {
177+ const options = {
178+ eventDispatcher : getMockEventDispatcher ( ) ,
179+ flushInterval : 12345 ,
180+ } ;
181+
182+ let processor = getBatchEventProcessor ( options ) ;
183+
184+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
185+ const usedRepeater = MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . dispatchRepeater ;
186+ expect ( Object . is ( usedRepeater , MockIntervalRepeater . mock . instances [ 0 ] ) ) . toBe ( true ) ;
187+ expect ( MockIntervalRepeater ) . toHaveBeenNthCalledWith ( 1 , 12345 ) ;
188+
189+ const startupLogs = MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . startupLogs ;
190+ expect ( startupLogs ?. find ( ( log ) => log . message === 'Invalid flushInterval %s, defaulting to %s' ) ) . toBe ( undefined ) ;
191+ } ) ;
192+
193+
194+ it ( 'uses a IntervalRepeater with default flush interval and adds a startup log if flushInterval is not provided' , ( ) => {
195+ const options = {
196+ eventDispatcher : getMockEventDispatcher ( ) ,
197+ } ;
198+
199+ let processor = getBatchEventProcessor ( options ) ;
200+
201+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
202+ expect ( MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . batchSize ) . toBe ( DEFAULT_EVENT_BATCH_SIZE ) ;
203+
204+ const startupLogs = MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . startupLogs ;
205+ expect ( startupLogs ) . toEqual ( expect . arrayContaining ( [ {
206+ level : LogLevel . WARNING ,
207+ message : 'Invalid batchSize %s, defaulting to %s' ,
208+ params : [ undefined , DEFAULT_EVENT_BATCH_SIZE ] ,
209+ } ] ) ) ;
210+ } ) ;
211+
212+ it ( 'uses default size and adds a startup log if provided batchSize is less than 1' , ( ) => {
213+ const options = {
214+ eventDispatcher : getMockEventDispatcher ( ) ,
215+ batchSize : - 1 ,
216+ } ;
217+
218+ let processor = getBatchEventProcessor ( options ) ;
219+
220+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
221+ expect ( MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . batchSize ) . toBe ( DEFAULT_EVENT_BATCH_SIZE ) ;
222+
223+ const startupLogs = MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . startupLogs ;
224+ expect ( startupLogs ) . toEqual ( expect . arrayContaining ( [ {
225+ level : LogLevel . WARNING ,
226+ message : 'Invalid batchSize %s, defaulting to %s' ,
227+ params : [ - 1 , DEFAULT_EVENT_BATCH_SIZE ] ,
228+ } ] ) ) ;
229+ } ) ;
230+
231+ it ( 'does not use a failedEventRepeater if failedEventRetryInterval is not provided' , ( ) => {
232+ const options = {
233+ eventDispatcher : getMockEventDispatcher ( ) ,
234+ } ;
235+
236+ let processor = getBatchEventProcessor ( options ) ;
237+
238+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
239+ expect ( MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . failedEventRepeater ) . toBe ( undefined ) ;
240+ } ) ;
241+
242+ it ( 'uses a IntervalRepeater with provided failedEventRetryInterval as failedEventRepeater' , ( ) => {
243+ const options = {
244+ eventDispatcher : getMockEventDispatcher ( ) ,
245+ failedEventRetryInterval : 12345 ,
246+ } ;
247+
248+ let processor = getBatchEventProcessor ( options ) ;
249+
250+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
251+ expect ( Object . is ( MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . failedEventRepeater , MockIntervalRepeater . mock . instances [ 1 ] ) ) . toBe ( true ) ;
252+ expect ( MockIntervalRepeater ) . toHaveBeenNthCalledWith ( 2 , 12345 ) ;
253+ } ) ;
254+
255+ it ( 'uses the provided eventDispatcher' , ( ) => {
256+ const eventDispatcher = getMockEventDispatcher ( ) ;
257+ const options = {
258+ eventDispatcher,
259+ } ;
260+
261+ let processor = getBatchEventProcessor ( options ) ;
262+
263+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
264+ expect ( MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventDispatcher ) . toBe ( eventDispatcher ) ;
265+ } ) ;
266+
267+ it ( 'does not use any closingEventDispatcher if not provided' , ( ) => {
268+ const options = {
269+ eventDispatcher : getMockEventDispatcher ( ) ,
270+ } ;
271+
272+ let processor = getBatchEventProcessor ( options ) ;
273+
274+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
275+ expect ( MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . closingEventDispatcher ) . toBe ( undefined ) ;
276+ } ) ;
277+
278+ it ( 'uses the provided closingEventDispatcher' , ( ) => {
279+ const closingEventDispatcher = getMockEventDispatcher ( ) ;
280+ const options = {
281+ eventDispatcher : getMockEventDispatcher ( ) ,
282+ closingEventDispatcher,
283+ } ;
284+
285+ let processor = getBatchEventProcessor ( options ) ;
286+
287+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
288+ expect ( MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . closingEventDispatcher ) . toBe ( closingEventDispatcher ) ;
289+ } ) ;
290+
291+ it ( 'uses the provided eventStore' , ( ) => {
292+ const eventStore = getMockSyncCache < EventWithId > ( ) ;
293+ const options = {
294+ eventDispatcher : getMockEventDispatcher ( ) ,
295+ eventStore,
296+ } ;
297+
298+ let processor = getBatchEventProcessor ( options ) ;
299+
300+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
301+ expect ( MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventStore ) . toBe ( eventStore ) ;
302+ } ) ;
124303} ) ;
0 commit comments