@@ -131,6 +131,7 @@ describe('outbox', () => {
131
131
let eventEmitter : DomainEventEmitter < TestEventsType >
132
132
let outboxEventEmitter : OutboxEventEmitter < TestEventsType >
133
133
let outboxStorage : InMemoryOutboxStorage < TestEventsType >
134
+ let inMemoryOutboxAccumulator : InMemoryOutboxAccumulator < TestEventsType >
134
135
135
136
beforeEach ( ( ) => {
136
137
eventEmitter = new DomainEventEmitter ( {
@@ -144,11 +145,12 @@ describe('outbox', () => {
144
145
145
146
outboxStorage = new InMemoryOutboxStorage < TestEventsType > ( )
146
147
outboxEventEmitter = new OutboxEventEmitter < TestEventsType > ( outboxStorage )
148
+ inMemoryOutboxAccumulator = new InMemoryOutboxAccumulator ( )
147
149
outboxProcessor = new OutboxProcessor < TestEventsType > (
148
150
{
149
151
outboxStorage,
150
152
//@ts -ignore
151
- outboxAccumulator : new InMemoryOutboxAccumulator ( ) ,
153
+ outboxAccumulator : inMemoryOutboxAccumulator ,
152
154
eventEmitter,
153
155
} satisfies OutboxDependencies < TestEventsType > ,
154
156
{ maxRetryCount : MAX_RETRY_COUNT , emitBatchSize : 1 } ,
@@ -294,4 +296,32 @@ describe('outbox', () => {
294
296
} ,
295
297
] )
296
298
} )
299
+
300
+ it ( "doesn't emit event again if it's already present in accumulator" , async ( ) => {
301
+ const mockedEventEmitter = vi . spyOn ( eventEmitter , 'emit' )
302
+
303
+ await outboxEventEmitter . emit ( TestEvents . created , createdEventPayload , {
304
+ correlationId : randomUUID ( ) ,
305
+ } )
306
+
307
+ await inMemoryOutboxAccumulator . add ( outboxStorage . entries [ 0 ] )
308
+
309
+ await outboxProcessor . processOutboxEntries ( {
310
+ logger : TestLogger ,
311
+ reqId : randomUUID ( ) ,
312
+ executorId : randomUUID ( ) ,
313
+ } )
314
+
315
+ //We pretended that event was emitted in previous run by adding state to accumulator
316
+ expect ( mockedEventEmitter ) . toHaveBeenCalledTimes ( 0 )
317
+
318
+ //But after the loop, if successful, it should be marked as success anyway
319
+ expect ( outboxStorage . entries ) . toMatchObject ( [
320
+ {
321
+ status : 'SUCCESS' ,
322
+ } ,
323
+ ] )
324
+ //And accumulator should be cleared
325
+ expect ( await inMemoryOutboxAccumulator . getEntries ( ) ) . toHaveLength ( 0 )
326
+ } )
297
327
} )
0 commit comments