Skip to content

Commit

Permalink
Merge pull request #2482 from AxonFramework/bug/2481
Browse files Browse the repository at this point in the history
[#2481] Check `MessageHandlerRegistrar` registration to be non-null
  • Loading branch information
smcvb committed Nov 28, 2022
2 parents 7c187b9 + 2972eb2 commit cc2e134
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
Expand Up @@ -19,7 +19,10 @@
import org.axonframework.common.Registration;
import org.axonframework.lifecycle.Lifecycle;
import org.axonframework.lifecycle.Phase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.invoke.MethodHandles;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
Expand All @@ -39,6 +42,8 @@
*/
public class MessageHandlerRegistrar implements Lifecycle {

private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private final Supplier<Configuration> configurationSupplier;
private final Function<Configuration, Object> messageHandlerBuilder;
private final BiFunction<Configuration, Object, Registration> messageHandlerSubscriber;
Expand Down Expand Up @@ -90,6 +95,10 @@ public void start() {
* through the {@link #start()} method.
*/
public void shutdown() {
if (handlerRegistration == null) {
logger.info("Shutting down a message handler registrar before it was started.");
return;
}
handlerRegistration.cancel();
}
}
Expand Up @@ -705,6 +705,17 @@ void registeringUpcastersReturnsUpcasterChainOverRulingRegisteredUpcasterChainCo
verifyNoInteractions(testUpcasterChain);
}

@Test
void shuttingDownTheConfigurationBeforeItStartedWithConfiguredMessageHandlersDoesNotCauseAnyExceptions() {
Configuration configuration = DefaultConfigurer.defaultConfiguration()
.registerCommandHandler(c -> new Object())
.registerEventHandler(c -> new Object())
.registerQueryHandler(c -> new Object())
.registerMessageHandler(c -> new Object())
.buildConfiguration();
assertDoesNotThrow(configuration::shutdown);
}

@SuppressWarnings("unused")
@Entity(name = "StubAggregate")
private static class StubAggregate {
Expand Down
Expand Up @@ -65,36 +65,39 @@ void startRegistersCreatedMessageHandler(@Mock Configuration config) {
assertTrue(isRegistered.get());
}

/**
* The thrown {@link NullPointerException} will always be wrapped in a {@link org.axonframework.lifecycle.LifecycleHandlerInvocationException}
* since the {@link MessageHandlerRegistrar#shutdown()} will be wrapped in a {@link LifecycleHandler}.
*/
@Test
void shutdownThrowsNullPointerExceptionIfRegistrationDidNotHappen(@Mock Configuration config) {
void shutdownCancelsMessageHandlerRegistration(@Mock Configuration config) {
AtomicBoolean isCanceled = new AtomicBoolean(false);

MessageHandlerRegistrar testSubject = new MessageHandlerRegistrar(
() -> config, c -> new SomeMessageHandler(),
(c, msgHandler) -> TEST_REGISTRATION_IMPLEMENTATION
(c, msgHandler) -> () -> {
isCanceled.set(true);
return false;
}
);
testSubject.start();

assertThrows(NullPointerException.class, testSubject::shutdown);
testSubject.shutdown();

assertTrue(isCanceled.get());
}

@Test
void shutdownCancelsMessageHandlerRegistration(@Mock Configuration config) {
void shutdownDoesNotThrowExceptionsIfWhenTheRegistrarHasNotStartedYet(@Mock Configuration config) {
AtomicBoolean isCanceled = new AtomicBoolean(false);

MessageHandlerRegistrar testSubject = new MessageHandlerRegistrar(
() -> config, c -> new SomeMessageHandler(),
() -> config,
c -> new SomeMessageHandler(),
(c, msgHandler) -> () -> {
isCanceled.set(true);
return false;
}
);
testSubject.start();

testSubject.shutdown();

assertTrue(isCanceled.get());
assertDoesNotThrow(testSubject::shutdown);
assertFalse(isCanceled.get());
}

private static class SomeMessageHandler {
Expand Down

0 comments on commit cc2e134

Please sign in to comment.