Skip to content

Commit 07857f7

Browse files
committed
Merge branch '2.2.x'
Closes gh-19418
2 parents 6009baa + 8ed0b1a commit 07857f7

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ void defaultUriEncoding() {
328328
}
329329

330330
@Test
331-
void startupFailureDoesNotResultInUnstoppedThreadsBeingReported(CapturedOutput output) throws IOException {
331+
void startupFailureDoesNotResultInUnstoppedThreadsBeingReported(CapturedOutput output) throws Exception {
332332
super.portClashOfPrimaryConnectorResultsInPortInUseException();
333333
assertThat(output).doesNotContain("appears to have started a thread named [main]");
334334
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.security.KeyStore;
2424
import java.time.Duration;
2525
import java.util.Arrays;
26+
import java.util.concurrent.Callable;
2627

2728
import javax.net.ssl.KeyManagerFactory;
2829
import javax.net.ssl.SSLException;
@@ -87,12 +88,15 @@ void tearDown() {
8788
protected abstract AbstractReactiveWebServerFactory getFactory();
8889

8990
@Test
90-
void specificPort() {
91+
void specificPort() throws Exception {
9192
AbstractReactiveWebServerFactory factory = getFactory();
92-
int specificPort = SocketUtils.findAvailableTcpPort(41000);
93-
factory.setPort(specificPort);
94-
this.webServer = factory.getWebServer(new EchoHandler());
95-
this.webServer.start();
93+
int specificPort = doWithRetry(() -> {
94+
int port = SocketUtils.findAvailableTcpPort(41000);
95+
factory.setPort(port);
96+
this.webServer = factory.getWebServer(new EchoHandler());
97+
this.webServer.start();
98+
return port;
99+
});
96100
Mono<String> result = getWebClient().build().post().uri("/test").contentType(MediaType.TEXT_PLAIN)
97101
.body(BodyInserters.fromValue("Hello World")).exchange()
98102
.flatMap((response) -> response.bodyToMono(String.class));
@@ -108,6 +112,7 @@ void basicSslFromClassPath() {
108112
@Test
109113
void basicSslFromFileSystem() {
110114
testBasicSslWithKeyStore("src/test/resources/test.jks", "password");
115+
111116
}
112117

113118
protected final void testBasicSslWithKeyStore(String keyStore, String keyPassword) {
@@ -329,6 +334,19 @@ protected void assertForwardHeaderIsUsed(AbstractReactiveWebServerFactory factor
329334
assertThat(body).isEqualTo("https");
330335
}
331336

337+
private <T> T doWithRetry(Callable<T> action) throws Exception {
338+
Exception lastFailure = null;
339+
for (int i = 0; i < 10; i++) {
340+
try {
341+
return action.call();
342+
}
343+
catch (Exception ex) {
344+
lastFailure = ex;
345+
}
346+
}
347+
throw new IllegalStateException("Action was not successful in 10 attempts", lastFailure);
348+
}
349+
332350
protected static class EchoHandler implements HttpHandler {
333351

334352
public EchoHandler() {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.HashMap;
4545
import java.util.Locale;
4646
import java.util.Map;
47+
import java.util.concurrent.Callable;
4748
import java.util.concurrent.atomic.AtomicBoolean;
4849
import java.util.concurrent.atomic.AtomicReference;
4950
import java.util.zip.GZIPInputStream;
@@ -246,10 +247,13 @@ void loadOnStartAfterContextIsInitialized() {
246247
@Test
247248
void specificPort() throws Exception {
248249
AbstractServletWebServerFactory factory = getFactory();
249-
int specificPort = SocketUtils.findAvailableTcpPort(41000);
250-
factory.setPort(specificPort);
251-
this.webServer = factory.getWebServer(exampleServletRegistration());
252-
this.webServer.start();
250+
int specificPort = doWithRetry(() -> {
251+
int port = SocketUtils.findAvailableTcpPort(41000);
252+
factory.setPort(port);
253+
this.webServer = factory.getWebServer(exampleServletRegistration());
254+
this.webServer.start();
255+
return port;
256+
});
253257
assertThat(getResponse("http://localhost:" + specificPort + "/hello")).isEqualTo("Hello World");
254258
assertThat(this.webServer.getPort()).isEqualTo(specificPort);
255259
}
@@ -823,7 +827,7 @@ void serverHeaderIsDisabledByDefault() throws Exception {
823827
}
824828

825829
@Test
826-
protected void portClashOfPrimaryConnectorResultsInPortInUseException() throws IOException {
830+
protected void portClashOfPrimaryConnectorResultsInPortInUseException() throws Exception {
827831
doWithBlockedPort((port) -> {
828832
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> {
829833
AbstractServletWebServerFactory factory = getFactory();
@@ -835,7 +839,7 @@ protected void portClashOfPrimaryConnectorResultsInPortInUseException() throws I
835839
}
836840

837841
@Test
838-
void portClashOfSecondaryConnectorResultsInPortInUseException() throws IOException {
842+
void portClashOfSecondaryConnectorResultsInPortInUseException() throws Exception {
839843
doWithBlockedPort((port) -> {
840844
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> {
841845
AbstractServletWebServerFactory factory = getFactory();
@@ -1131,19 +1135,28 @@ public void service(ServletRequest request, ServletResponse response) throws IOE
11311135
return bean;
11321136
}
11331137

1134-
protected final void doWithBlockedPort(BlockedPortAction action) throws IOException {
1135-
int port = SocketUtils.findAvailableTcpPort(40000);
1136-
ServerSocket serverSocket = new ServerSocket();
1138+
private <T> T doWithRetry(Callable<T> action) throws Exception {
1139+
Exception lastFailure = null;
11371140
for (int i = 0; i < 10; i++) {
11381141
try {
1139-
serverSocket.bind(new InetSocketAddress(port));
1140-
break;
1142+
return action.call();
11411143
}
11421144
catch (Exception ex) {
1145+
lastFailure = ex;
11431146
}
11441147
}
1148+
throw new IllegalStateException("Action was not successful in 10 attempts", lastFailure);
1149+
}
1150+
1151+
protected final void doWithBlockedPort(BlockedPortAction action) throws Exception {
1152+
ServerSocket serverSocket = new ServerSocket();
1153+
int blockedPort = doWithRetry(() -> {
1154+
int port = SocketUtils.findAvailableTcpPort(40000);
1155+
serverSocket.bind(new InetSocketAddress(port));
1156+
return port;
1157+
});
11451158
try {
1146-
action.run(port);
1159+
action.run(blockedPort);
11471160
}
11481161
finally {
11491162
serverSocket.close();

0 commit comments

Comments
 (0)