1
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' ;
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' ;
4
4
import { ExponentialBackoff , IntervalRepeater } from '../utils/repeater/repeater' ;
5
+ import { getMockSyncCache } from '../tests/mock/mock_cache' ;
6
+ import { LogLevel } from '../modules/logging' ;
5
7
6
8
vi . mock ( './batch_event_processor' ) ;
7
9
vi . mock ( '../utils/repeater/repeater' ) ;
@@ -17,10 +19,12 @@ const getMockEventDispatcher = () => {
17
19
describe ( 'getBatchEventProcessor' , ( ) => {
18
20
const MockBatchEventProcessor = vi . mocked ( BatchEventProcessor ) ;
19
21
const MockExponentialBackoff = vi . mocked ( ExponentialBackoff ) ;
22
+ const MockIntervalRepeater = vi . mocked ( IntervalRepeater ) ;
20
23
21
24
beforeEach ( ( ) => {
22
25
MockBatchEventProcessor . mockReset ( ) ;
23
26
MockExponentialBackoff . mockReset ( ) ;
27
+ MockIntervalRepeater . mockReset ( ) ;
24
28
} ) ;
25
29
26
30
it ( 'returns an instane of BatchEventProcessor if no subclass constructor is provided' , ( ) => {
@@ -55,6 +59,7 @@ describe('getBatchEventProcessor', () => {
55
59
} ;
56
60
57
61
const processor = getBatchEventProcessor ( options ) ;
62
+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
58
63
expect ( MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryConfig ) . toBe ( undefined ) ;
59
64
} ) ;
60
65
@@ -66,6 +71,7 @@ describe('getBatchEventProcessor', () => {
66
71
67
72
let processor = getBatchEventProcessor ( options ) ;
68
73
74
+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
69
75
const usedRetryConfig = MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryConfig ;
70
76
expect ( usedRetryConfig ) . not . toBe ( undefined ) ;
71
77
expect ( usedRetryConfig ?. backoffProvider ) . not . toBe ( undefined ) ;
@@ -80,6 +86,7 @@ describe('getBatchEventProcessor', () => {
80
86
} ;
81
87
82
88
let processor1 = getBatchEventProcessor ( options1 ) ;
89
+ expect ( Object . is ( processor1 , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
83
90
expect ( MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryConfig ?. maxRetries ) . toBe ( 10 ) ;
84
91
85
92
const options2 = {
@@ -88,6 +95,7 @@ describe('getBatchEventProcessor', () => {
88
95
} ;
89
96
90
97
let processor2 = getBatchEventProcessor ( options2 ) ;
98
+ expect ( Object . is ( processor2 , MockBatchEventProcessor . mock . instances [ 1 ] ) ) . toBe ( true ) ;
91
99
expect ( MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryConfig ) . not . toBe ( undefined ) ;
92
100
expect ( MockBatchEventProcessor . mock . calls [ 1 ] [ 0 ] . retryConfig ?. maxRetries ) . toBe ( undefined ) ;
93
101
} ) ;
@@ -99,8 +107,9 @@ describe('getBatchEventProcessor', () => {
99
107
} ;
100
108
101
109
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 ) ;
103
111
112
+ const backoffProvider = MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryConfig ?. backoffProvider ;
104
113
expect ( backoffProvider ) . not . toBe ( undefined ) ;
105
114
const backoff = backoffProvider ?.( ) ;
106
115
expect ( Object . is ( backoff , MockExponentialBackoff . mock . instances [ 0 ] ) ) . toBe ( true ) ;
@@ -114,11 +123,181 @@ describe('getBatchEventProcessor', () => {
114
123
} ;
115
124
116
125
let processor = getBatchEventProcessor ( options ) ;
126
+ expect ( Object . is ( processor , MockBatchEventProcessor . mock . instances [ 0 ] ) ) . toBe ( true ) ;
117
127
const backoffProvider = MockBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryConfig ?. backoffProvider ;
118
128
119
129
expect ( backoffProvider ) . not . toBe ( undefined ) ;
120
130
const backoff = backoffProvider ?.( ) ;
121
131
expect ( Object . is ( backoff , MockExponentialBackoff . mock . instances [ 0 ] ) ) . toBe ( true ) ;
122
132
expect ( MockExponentialBackoff ) . toHaveBeenNthCalledWith ( 1 , 1000 , 2000 , 500 ) ;
123
133
} ) ;
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
+ } ) ;
124
303
} ) ;
0 commit comments