Skip to content

Commit 043c626

Browse files
committed
AP-5046 Test.
1 parent c3b08ea commit 043c626

File tree

1 file changed

+65
-9
lines changed

1 file changed

+65
-9
lines changed

packages/outbox-core/lib/outbox.spec.ts

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ class InMemoryOutboxStorage<SupportedEvents extends CommonEventDefinition[]>
111111
}
112112
}
113113

114+
const MAX_RETRY_COUNT = 2
115+
114116
describe('outbox', () => {
115117
let outboxProcessor: OutboxProcessor<TestEventsType>
116118
let eventEmitter: DomainEventEmitter<TestEventsType>
@@ -129,7 +131,11 @@ describe('outbox', () => {
129131

130132
outboxStorage = new InMemoryOutboxStorage<TestEventsType>()
131133
outboxEventEmitter = new OutboxEventEmitter<TestEventsType>(outboxStorage)
132-
outboxProcessor = new OutboxProcessor<TestEventsType>(outboxStorage, eventEmitter, 2)
134+
outboxProcessor = new OutboxProcessor<TestEventsType>(
135+
outboxStorage,
136+
eventEmitter,
137+
MAX_RETRY_COUNT,
138+
)
133139
})
134140

135141
afterEach(() => {
@@ -141,7 +147,7 @@ describe('outbox', () => {
141147
correlationId: randomUUID(),
142148
})
143149

144-
const entries = await outboxStorage.getEntries(2)
150+
const entries = await outboxStorage.getEntries(MAX_RETRY_COUNT)
145151

146152
expect(entries).toHaveLength(1)
147153
})
@@ -157,7 +163,7 @@ describe('outbox', () => {
157163
executorId: randomUUID(),
158164
})
159165

160-
const entries = await outboxStorage.getEntries(2)
166+
const entries = await outboxStorage.getEntries(MAX_RETRY_COUNT)
161167

162168
expect(entries).toHaveLength(0)
163169
expect(outboxStorage.entries).toMatchObject([
@@ -168,10 +174,6 @@ describe('outbox', () => {
168174
})
169175

170176
it('saves outbox entry and process it with error and retries', async () => {
171-
await outboxEventEmitter.emit(TestEvents.created, createdEventPayload, {
172-
correlationId: randomUUID(),
173-
})
174-
175177
const mockedEventEmitter = vi.spyOn(eventEmitter, 'emit')
176178
mockedEventEmitter.mockImplementationOnce(() => {
177179
throw new Error('Could not emit event.')
@@ -190,13 +192,17 @@ describe('outbox', () => {
190192
}),
191193
)
192194

195+
await outboxEventEmitter.emit(TestEvents.created, createdEventPayload, {
196+
correlationId: randomUUID(),
197+
})
198+
193199
await outboxProcessor.processOutboxEntries({
194200
logger: TestLogger,
195201
reqId: randomUUID(),
196202
executorId: randomUUID(),
197203
})
198204

199-
let entries = await outboxStorage.getEntries(2)
205+
let entries = await outboxStorage.getEntries(MAX_RETRY_COUNT)
200206
expect(entries).toHaveLength(1)
201207
expect(outboxStorage.entries).toMatchObject([
202208
{
@@ -212,7 +218,7 @@ describe('outbox', () => {
212218
executorId: randomUUID(),
213219
})
214220

215-
entries = await outboxStorage.getEntries(2)
221+
entries = await outboxStorage.getEntries(MAX_RETRY_COUNT)
216222
expect(entries).toHaveLength(0) //Nothing to process anymore
217223
expect(outboxStorage.entries).toMatchObject([
218224
{
@@ -221,4 +227,54 @@ describe('outbox', () => {
221227
},
222228
])
223229
})
230+
231+
it('no longer processes the event if exceeded retry count', async () => {
232+
//Let's always fail the event
233+
const mockedEventEmitter = vi.spyOn(eventEmitter, 'emit')
234+
mockedEventEmitter.mockImplementation(() => {
235+
throw new Error('Could not emit event.')
236+
})
237+
238+
//Persist the event
239+
await outboxEventEmitter.emit(TestEvents.created, createdEventPayload, {
240+
correlationId: randomUUID(),
241+
})
242+
243+
//Initially event is present in outbox storage.
244+
expect(await outboxStorage.getEntries(MAX_RETRY_COUNT)).toHaveLength(1)
245+
246+
//Retry +1
247+
await outboxProcessor.processOutboxEntries({
248+
logger: TestLogger,
249+
reqId: randomUUID(),
250+
executorId: randomUUID(),
251+
})
252+
//Still present
253+
expect(await outboxStorage.getEntries(MAX_RETRY_COUNT)).toHaveLength(1)
254+
255+
//Retry +2
256+
await outboxProcessor.processOutboxEntries({
257+
logger: TestLogger,
258+
reqId: randomUUID(),
259+
executorId: randomUUID(),
260+
})
261+
//Stil present
262+
expect(await outboxStorage.getEntries(MAX_RETRY_COUNT)).toHaveLength(1)
263+
264+
//Retry +3
265+
await outboxProcessor.processOutboxEntries({
266+
logger: TestLogger,
267+
reqId: randomUUID(),
268+
executorId: randomUUID(),
269+
})
270+
//Now it's gone, we no longer try to process it
271+
expect(await outboxStorage.getEntries(MAX_RETRY_COUNT)).toHaveLength(0)
272+
273+
expect(outboxStorage.entries).toMatchObject([
274+
{
275+
status: 'FAILED',
276+
retryCount: 3,
277+
},
278+
])
279+
})
224280
})

0 commit comments

Comments
 (0)