Ensure shutdown hook is registered when a handler is added
Closes gh-34627
This commit is contained in:
parent
3dac258aa8
commit
28b7c583a9
|
@ -170,6 +170,7 @@ class SpringApplicationShutdownHook implements Runnable {
|
|||
@Override
|
||||
public void add(Runnable action) {
|
||||
Assert.notNull(action, "Action must not be null");
|
||||
addRuntimeShutdownHookIfNecessary();
|
||||
synchronized (SpringApplicationShutdownHook.class) {
|
||||
assertNotInProgress();
|
||||
this.actions.add(action);
|
||||
|
|
|
@ -57,6 +57,15 @@ class SpringApplicationShutdownHookTests {
|
|||
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shutdownHookIsNotAddedUntilHandlerIsRegistered() {
|
||||
TestSpringApplicationShutdownHook shutdownHook = new TestSpringApplicationShutdownHook();
|
||||
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isFalse();
|
||||
shutdownHook.getHandlers().add(() -> {
|
||||
});
|
||||
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void runClosesContextsBeforeRunningHandlerActions() {
|
||||
TestSpringApplicationShutdownHook shutdownHook = new TestSpringApplicationShutdownHook();
|
||||
|
|
Loading…
Reference in New Issue