Skip to content

Commit

Permalink
Remove incorrect try/catch block around the send().handle() call
Browse files Browse the repository at this point in the history
Remove incorrect try/catch block around the send().handle() call, as the
 CommandHandlerInvoker will pass these possible exceptions along to the
 DisruptorCommandBus. The DisruptorCommandBus can then perform the
 proper logging if necessary.

#220
  • Loading branch information
smcvb committed Jul 2, 2018
1 parent bce3bdc commit 0483900
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 62 deletions.
Expand Up @@ -314,17 +314,11 @@ private void removeFromCache(String aggregateIdentifier) {
} }
} }


@SuppressWarnings("Duplicates")
@Override @Override
public void send(Message<?> message, ScopeDescriptor scopeDescription) throws Exception { public void send(Message<?> message, ScopeDescriptor scopeDescription) throws Exception {
if (canResolve(scopeDescription)) { if (canResolve(scopeDescription)) {
String aggregateIdentifier = ((AggregateScopeDescriptor) scopeDescription).getIdentifier().toString(); String aggregateIdentifier = ((AggregateScopeDescriptor) scopeDescription).getIdentifier().toString();
try { load(aggregateIdentifier).handle(message);
load(aggregateIdentifier).handle(message);
} catch (AggregateNotFoundException e) {
logger.debug("Aggregate (with id: [{}]) cannot be loaded. Hence, message '[{}]' cannot be handled.",
aggregateIdentifier, message);
}
} }
} }


Expand Down
Expand Up @@ -39,9 +39,7 @@
import org.axonframework.eventsourcing.eventstore.EventStore; import org.axonframework.eventsourcing.eventstore.EventStore;
import org.axonframework.messaging.MessageHandler; import org.axonframework.messaging.MessageHandler;
import org.axonframework.messaging.annotation.ClasspathParameterResolverFactory; import org.axonframework.messaging.annotation.ClasspathParameterResolverFactory;
import org.axonframework.messaging.annotation.MessageHandlerInvocationException;
import org.axonframework.messaging.annotation.ParameterResolverFactory; import org.axonframework.messaging.annotation.ParameterResolverFactory;
import org.axonframework.messaging.unitofwork.CurrentUnitOfWork;
import org.junit.*; import org.junit.*;
import org.mockito.*; import org.mockito.*;


Expand All @@ -53,7 +51,6 @@
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function; import java.util.function.Function;


import static junit.framework.Assert.assertFalse;
import static junit.framework.TestCase.assertTrue; import static junit.framework.TestCase.assertTrue;
import static org.axonframework.commandhandling.model.AggregateLifecycle.apply; import static org.axonframework.commandhandling.model.AggregateLifecycle.apply;
import static org.junit.Assert.*; import static org.junit.Assert.*;
Expand Down Expand Up @@ -124,11 +121,10 @@ public void testLoadFromRepositoryStoresLoadedAggregateInCache() throws Exceptio
ClasspathParameterResolverFactory.forClass(StubAggregate.class)); ClasspathParameterResolverFactory.forClass(StubAggregate.class));
when(mockCommandHandler.handle(eq(mockCommandMessage))) when(mockCommandHandler.handle(eq(mockCommandMessage)))
.thenAnswer(invocationOnMock -> repository.load(aggregateIdentifier)); .thenAnswer(invocationOnMock -> repository.load(aggregateIdentifier));
when(mockEventStore.readEvents(anyObject())).thenReturn(DomainEventStream when(mockEventStore.readEvents(any()))
.of(new GenericDomainEventMessage<>("type", .thenReturn(DomainEventStream.of(
aggregateIdentifier, new GenericDomainEventMessage<>("type", aggregateIdentifier, 0, aggregateIdentifier)
0, ));
aggregateIdentifier)));
testSubject.onEvent(commandHandlingEntry, 0, true); testSubject.onEvent(commandHandlingEntry, 0, true);


verify(mockCache).get(aggregateIdentifier); verify(mockCache).get(aggregateIdentifier);
Expand Down Expand Up @@ -237,7 +233,7 @@ public void testCanResolveReturnsFalseForNonMatchingAggregateType() {
} }


@Test @Test
public void testSendWorksAsExpected() throws Exception { public void testSendDeliversMessageAtDescribedAggregateInstance() throws Exception {
String testAggregateId = "some-identifier"; String testAggregateId = "some-identifier";
DeadlineMessage<DeadlinePayload> testMsg = DeadlineMessage<DeadlinePayload> testMsg =
GenericDeadlineMessage.asDeadlineMessage("deadline-name", new DeadlinePayload()); GenericDeadlineMessage.asDeadlineMessage("deadline-name", new DeadlinePayload());
Expand All @@ -249,61 +245,21 @@ public void testSendWorksAsExpected() throws Exception {
new GenericAggregateFactory<>(StubAggregate.class), new GenericAggregateFactory<>(StubAggregate.class),
snapshotTriggerDefinition, snapshotTriggerDefinition,
ClasspathParameterResolverFactory.forClass(StubAggregate.class)); ClasspathParameterResolverFactory.forClass(StubAggregate.class));
CurrentUnitOfWork.set(commandHandlingEntry);
when(mockEventStore.readEvents(any())) when(mockEventStore.readEvents(any()))
.thenReturn(DomainEventStream.of(new GenericDomainEventMessage<>( .thenReturn(DomainEventStream.of(new GenericDomainEventMessage<>(
StubAggregate.class.getSimpleName(), testAggregateId, 0, testAggregateId StubAggregate.class.getSimpleName(), testAggregateId, 0, testAggregateId
))); )));


testRepository.send(testMsg, testDescriptor); commandHandlingEntry.start();
try {
testRepository.send(testMsg, testDescriptor);
} finally {
commandHandlingEntry.pause();
}


assertEquals(1, messageHandlingCounter.get()); assertEquals(1, messageHandlingCounter.get());
} }


@Test(expected = MessageHandlerInvocationException.class)
public void testSendThrowsMessageHandlerInvocationExceptionIfHandleFails() throws Exception {
String testAggregateId = "some-identifier";
DeadlineMessage<FailingPayload> testMsg =
GenericDeadlineMessage.asDeadlineMessage("deadline-name", new FailingPayload());
AggregateScopeDescriptor testDescriptor =
new AggregateScopeDescriptor(StubAggregate.class.getSimpleName(), testAggregateId);

Repository<StubAggregate> testRepository =
testSubject.createRepository(mockEventStore,
new GenericAggregateFactory<>(StubAggregate.class),
snapshotTriggerDefinition,
ClasspathParameterResolverFactory.forClass(StubAggregate.class));
CurrentUnitOfWork.set(commandHandlingEntry);
when(mockEventStore.readEvents(any())).thenReturn(DomainEventStream.of(new GenericDomainEventMessage<>(
StubAggregate.class.getSimpleName(), testAggregateId, 0, testAggregateId
)));

testRepository.send(testMsg, testDescriptor);

assertEquals(0, messageHandlingCounter.get());
}

@Test
public void testSendFailsSilentlyOnAggregateNotFoundException() throws Exception {
String testAggregateId = "some-identifier";
DeadlineMessage<DeadlinePayload> testMsg =
GenericDeadlineMessage.asDeadlineMessage("deadline-name", new DeadlinePayload());
AggregateScopeDescriptor testDescriptor =
new AggregateScopeDescriptor(StubAggregate.class.getSimpleName(), testAggregateId);

Repository<StubAggregate> testRepository =
testSubject.createRepository(mockEventStore,
new GenericAggregateFactory<>(StubAggregate.class),
snapshotTriggerDefinition,
ClasspathParameterResolverFactory.forClass(StubAggregate.class));
CurrentUnitOfWork.set(commandHandlingEntry);
when(mockEventStore.readEvents(any())).thenReturn(DomainEventStream.of(Collections.emptyList()));

testRepository.send(testMsg, testDescriptor);

assertEquals(0, messageHandlingCounter.get());
}

private static class FailingPayload { private static class FailingPayload {


} }
Expand Down

0 comments on commit 0483900

Please sign in to comment.