public void testLoopBackKeepsProperEventOrder_OptimisticLocking_ProcessingFails() throws Throwable {
initializeRepository(new OptimisticLockManager());
EventListener el = new EventListener() {
@Override
public void handle(EventMessage event) {
DomainEventMessage domainEvent = (DomainEventMessage) event;
if (event.getPayload() instanceof CounterChangedEvent) {
CounterChangedEvent counterChangedEvent = (CounterChangedEvent) event.getPayload();
if (counterChangedEvent.getCounter() == 1) {
commandBus.dispatch(asCommandMessage(
new ChangeCounterCommand((UUID) domainEvent.getAggregateIdentifier(),
counterChangedEvent.getCounter() + 1)), reportErrorCallback);
commandBus.dispatch(asCommandMessage(
new ChangeCounterCommand((UUID) domainEvent.getAggregateIdentifier(),
counterChangedEvent.getCounter() + 2)),
reportErrorCallback);
} else if (counterChangedEvent.getCounter() == 2) {
throw new RuntimeException("Mock exception");
}
}
}
};
eventBus.subscribe(el);
commandBus.dispatch(asCommandMessage(new ChangeCounterCommand(aggregateIdentifier, 1)), expectErrorCallback);
DomainEventStream storedEvents = eventStore.readEvents("CountingAggregate", aggregateIdentifier);
assertTrue(storedEvents.hasNext());
while (storedEvents.hasNext()) {
DomainEventMessage next = storedEvents.next();
if (next.getPayload() instanceof CounterChangedEvent) {
CounterChangedEvent event = (CounterChangedEvent) next.getPayload();
assertEquals(event.getCounter(), next.getSequenceNumber());
}
}
verify(eventStore, times(3)).appendEvents(eq("CountingAggregate"), isA(DomainEventStream.class));
}