Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,18 @@ public final void refresh() throws BeansException, IllegalStateException {
try {
super.refresh();
}
catch (RuntimeException ex) {
catch (RuntimeException refreshEx) {
WebServer webServer = getWebServer();
if (webServer != null) {
webServer.stop();
webServer.destroy();
try {
webServer.stop();
webServer.destroy();
}
catch (RuntimeException stopOrDestroyEx) {
refreshEx.addSuppressed(stopOrDestroyEx);
}
}
throw ex;
throw refreshEx;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,18 @@ public final void refresh() throws BeansException, IllegalStateException {
try {
super.refresh();
}
catch (RuntimeException ex) {
WebServer webServer = this.webServer;
if (webServer != null) {
webServer.stop();
webServer.destroy();
catch (RuntimeException refreshEx) {
try {
WebServer webServer = this.webServer;
if (webServer != null) {
webServer.stop();
webServer.destroy();
}
}
throw ex;
catch (RuntimeException stopOrDestroyEx) {
refreshEx.addSuppressed(stopOrDestroyEx);
}
throw refreshEx;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.BDDMockito.then;
import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.times;

/**
Expand Down Expand Up @@ -133,6 +134,40 @@ void whenContextRefreshFailedThenWebServerIsStoppedAndDestroyed() {
then(webServer).should().destroy();
}

@Test
void whenContextRefreshFailedThenWebServerStopFailedCatchStopException() {
addWebServerFactoryBean();
addHttpHandlerBean();
this.context.registerBeanDefinition("refreshFailure", new RootBeanDefinition(RefreshFailure.class, () -> {
willThrow(new RuntimeException("WebServer has failed to stop")).willCallRealMethod()
.given(this.context.getWebServer())
.stop();
return new RefreshFailure();
}));
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(this.context::refresh)
.withStackTraceContaining("WebServer has failed to stop");
WebServer webServer = this.context.getWebServer();
then(webServer).should().stop();
then(webServer).should(times(0)).destroy();
}

@Test
void whenContextRefreshFailedThenWebServerIsStoppedAndDestroyFailedCatchDestroyException() {
addWebServerFactoryBean();
addHttpHandlerBean();
this.context.registerBeanDefinition("refreshFailure", new RootBeanDefinition(RefreshFailure.class, () -> {
willThrow(new RuntimeException("WebServer has failed to destroy")).willCallRealMethod()
.given(this.context.getWebServer())
.destroy();
return new RefreshFailure();
}));
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(this.context::refresh)
.withStackTraceContaining("WebServer has failed to destroy");
WebServer webServer = this.context.getWebServer();
then(webServer).should().stop();
then(webServer).should().destroy();
}

@Test
void whenContextIsClosedThenWebServerIsStoppedAndDestroyed() {
addWebServerFactoryBean();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,6 +51,7 @@
import org.springframework.boot.testsupport.system.CapturedOutput;
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
import org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer;
import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletContextInitializer;
Expand Down Expand Up @@ -81,6 +82,7 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.atMost;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -211,6 +213,48 @@ void whenContextIsNotActiveThenCloseDoesNotChangeTheApplicationAvailability() {
assertThat(listener.receivedEvents()).isEmpty();
}

@Test
void whenContextRefreshFailedThenWebServerIsStoppedAndDestroyed() {
addWebServerFactoryBean();
this.context.registerBeanDefinition("refreshFailure", new RootBeanDefinition(RefreshFailure.class));
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(this.context::refresh);
WebServer webServer = this.context.getWebServer();
then(webServer).should(times(2)).stop();
then(webServer).should().destroy();
}

@Test
void whenContextRefreshFailedThenWebServerStopFailedCatchStopException() {
addWebServerFactoryBean();
this.context.registerBeanDefinition("refreshFailure", new RootBeanDefinition(RefreshFailure.class, () -> {
willThrow(new RuntimeException("WebServer has failed to stop")).willCallRealMethod()
.given(this.context.getWebServer())
.stop();
return new RefreshFailure();
}));
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(this.context::refresh)
.withStackTraceContaining("WebServer has failed to stop");
WebServer webServer = this.context.getWebServer();
then(webServer).should().stop();
then(webServer).should(times(0)).destroy();
}

@Test
void whenContextRefreshFailedThenWebServerIsStoppedAndDestroyFailedCatchDestroyException() {
addWebServerFactoryBean();
this.context.registerBeanDefinition("refreshFailure", new RootBeanDefinition(RefreshFailure.class, () -> {
willThrow(new RuntimeException("WebServer has failed to destroy")).willCallRealMethod()
.given(this.context.getWebServer())
.destroy();
return new RefreshFailure();
}));
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(this.context::refresh)
.withStackTraceContaining("WebServer has failed to destroy");
WebServer webServer = this.context.getWebServer();
then(webServer).should().stop();
then(webServer).should().destroy();
}

@Test
void cannotSecondRefresh() {
addWebServerFactoryBean();
Expand Down