|
|
@ -1,5 +1,5 @@
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Copyright 2012-2021 the original author or authors.
|
|
|
|
* Copyright 2012-2022 the original author or authors.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
@ -26,6 +26,7 @@ import java.util.concurrent.CountDownLatch;
|
|
|
|
import org.awaitility.Awaitility;
|
|
|
|
import org.awaitility.Awaitility;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.BeanCreationException;
|
|
|
|
import org.springframework.beans.factory.InitializingBean;
|
|
|
|
import org.springframework.beans.factory.InitializingBean;
|
|
|
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
|
|
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
|
|
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
|
|
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
|
|
@ -36,12 +37,14 @@ import org.springframework.context.support.GenericApplicationContext;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Tests for {@link SpringApplicationShutdownHook}.
|
|
|
|
* Tests for {@link SpringApplicationShutdownHook}.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @author Phillip Webb
|
|
|
|
* @author Phillip Webb
|
|
|
|
* @author Andy Wilkinson
|
|
|
|
* @author Andy Wilkinson
|
|
|
|
|
|
|
|
* @author Brian Clozel
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
class SpringApplicationShutdownHookTests {
|
|
|
|
class SpringApplicationShutdownHookTests {
|
|
|
|
|
|
|
|
|
|
|
@ -154,6 +157,29 @@ class SpringApplicationShutdownHookTests {
|
|
|
|
.withMessage("Shutdown in progress");
|
|
|
|
.withMessage("Shutdown in progress");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
void failsWhenDeregisterActiveContext() {
|
|
|
|
|
|
|
|
TestSpringApplicationShutdownHook shutdownHook = new TestSpringApplicationShutdownHook();
|
|
|
|
|
|
|
|
ConfigurableApplicationContext context = new GenericApplicationContext();
|
|
|
|
|
|
|
|
shutdownHook.registerApplicationContext(context);
|
|
|
|
|
|
|
|
context.refresh();
|
|
|
|
|
|
|
|
assertThatThrownBy(() -> shutdownHook.deregisterFailedApplicationContext(context))
|
|
|
|
|
|
|
|
.isInstanceOf(IllegalStateException.class);
|
|
|
|
|
|
|
|
assertThat(shutdownHook.isApplicationContextRegistered(context)).isTrue();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
void deregistersFailedContext() {
|
|
|
|
|
|
|
|
TestSpringApplicationShutdownHook shutdownHook = new TestSpringApplicationShutdownHook();
|
|
|
|
|
|
|
|
GenericApplicationContext context = new GenericApplicationContext();
|
|
|
|
|
|
|
|
shutdownHook.registerApplicationContext(context);
|
|
|
|
|
|
|
|
context.registerBean(FailingBean.class);
|
|
|
|
|
|
|
|
assertThatThrownBy(context::refresh).isInstanceOf(BeanCreationException.class);
|
|
|
|
|
|
|
|
assertThat(shutdownHook.isApplicationContextRegistered(context)).isTrue();
|
|
|
|
|
|
|
|
shutdownHook.deregisterFailedApplicationContext(context);
|
|
|
|
|
|
|
|
assertThat(shutdownHook.isApplicationContextRegistered(context)).isFalse();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static class TestSpringApplicationShutdownHook extends SpringApplicationShutdownHook {
|
|
|
|
static class TestSpringApplicationShutdownHook extends SpringApplicationShutdownHook {
|
|
|
|
|
|
|
|
|
|
|
|
private boolean runtimeShutdownHookAdded;
|
|
|
|
private boolean runtimeShutdownHookAdded;
|
|
|
@ -259,4 +285,13 @@ class SpringApplicationShutdownHookTests {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static class FailingBean implements InitializingBean {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void afterPropertiesSet() throws Exception {
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("test failure");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|