Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure background listeners are called even if a foreground listener throws #227

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ It consists of the following submodules:
* `@message-queue-toolkit/sqs` - SQS (AWS Simple Queue Service)
* `@message-queue-toolkit/sns` - SNS (AWS Simple Notification Service)

## Setup

In order to be able to run tests, you first need to run `npm run build`.

## Basic Usage

### Publishers
Expand Down
18 changes: 18 additions & 0 deletions packages/core/lib/events/DomainEventEmitter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,24 @@ describe('AutopilotEventEmitter', () => {
false,
)
})

it('emits event to background even if foreground listener throws', async () => {
// Given
const fakeSyncListener = new FakeListener()
vi.spyOn(fakeSyncListener, 'handleEvent').mockRejectedValue(new Error('Test error'))
eventEmitter.on('entity.created', fakeSyncListener)

const fakeAsyncListener = new FakeListener()
eventEmitter.on('entity.created', fakeAsyncListener, true)

// When
const emittedEventPromise = eventEmitter.emit(TestEvents.created, createdEventPayload)

// Then
expect(emittedEventPromise).rejects.toThrow('Test error')
await eventEmitter.handlerSpy.waitForMessage({ type: 'entity.created' }, 'consumed')
expect(fakeAsyncListener.receivedEvents).toHaveLength(1)
})
})

describe('dispose', () => {
Expand Down
38 changes: 20 additions & 18 deletions packages/core/lib/events/DomainEventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,25 +167,27 @@ export class DomainEventEmitter<SupportedEvents extends CommonEventDefinition[]>
const eventHandlers = this.eventHandlerMap.get(event.type)
if (!eventHandlers) return

for (const handler of eventHandlers.foreground) {
await this.executeEventHandler(event, handler, false)
try {
for (const handler of eventHandlers.foreground) {
await this.executeEventHandler(event, handler, false)
}
} finally {
const bgPromise = Promise.all(
eventHandlers.background.map((handler) => this.executeEventHandler(event, handler, true)),
).then(() => {
this.inProgressBackgroundHandlerByEventId.delete(event.id)
if (!this._handlerSpy) return
this._handlerSpy.addProcessedMessage(
{
// @ts-ignore
message: event,
processingResult: 'consumed',
},
event.id,
)
})
this.inProgressBackgroundHandlerByEventId.set(event.id, bgPromise)
}

const bgPromise = Promise.all(
eventHandlers.background.map((handler) => this.executeEventHandler(event, handler, true)),
).then(() => {
this.inProgressBackgroundHandlerByEventId.delete(event.id)
if (!this._handlerSpy) return
this._handlerSpy.addProcessedMessage(
{
// @ts-ignore
message: event,
processingResult: 'consumed',
},
event.id,
)
})
this.inProgressBackgroundHandlerByEventId.set(event.id, bgPromise)
}

private async executeEventHandler<SupportedEvent extends SupportedEvents[number]>(
Expand Down
Loading