Skip to content

Commit f3af035

Browse files
committed
Remove usage of SocketUtils in tests
Closes gh-29821
1 parent f180397 commit f3af035

File tree

4 files changed

+38
-56
lines changed

4 files changed

+38
-56
lines changed

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactoryTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,7 +51,6 @@
5151
import org.springframework.http.client.reactive.ReactorResourceFactory;
5252
import org.springframework.messaging.rsocket.RSocketRequester;
5353
import org.springframework.messaging.rsocket.RSocketStrategies;
54-
import org.springframework.util.SocketUtils;
5554

5655
import static org.assertj.core.api.Assertions.assertThat;
5756
import static org.mockito.ArgumentMatchers.any;
@@ -97,11 +96,10 @@ private NettyRSocketServerFactory getFactory() {
9796
void specificPort() {
9897
NettyRSocketServerFactory factory = getFactory();
9998
int specificPort = doWithRetry(() -> {
100-
int port = SocketUtils.findAvailableTcpPort(41000);
101-
factory.setPort(port);
99+
factory.setPort(0);
102100
this.server = factory.create(new EchoRequestResponseAcceptor());
103101
this.server.start();
104-
return port;
102+
return this.server.address().getPort();
105103
});
106104
this.requester = createRSocketTcpClient();
107105
assertThat(this.server.address().getPort()).isEqualTo(specificPort);

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

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717
package org.springframework.boot.web.embedded.tomcat;
1818

19-
import java.io.IOException;
2019
import java.net.ConnectException;
21-
import java.net.InetSocketAddress;
22-
import java.net.ServerSocket;
2320
import java.time.Duration;
2421
import java.util.Arrays;
2522
import java.util.Map;
@@ -48,7 +45,6 @@
4845
import org.springframework.boot.web.server.Shutdown;
4946
import org.springframework.boot.web.server.WebServerException;
5047
import org.springframework.http.server.reactive.HttpHandler;
51-
import org.springframework.util.SocketUtils;
5248
import org.springframework.web.reactive.function.client.WebClient;
5349

5450
import static org.assertj.core.api.Assertions.assertThat;
@@ -225,15 +221,13 @@ void referenceClearingIsDisabled() {
225221
}
226222

227223
@Test
228-
void portClashOfPrimaryConnectorResultsInPortInUseException() throws IOException {
229-
doWithBlockedPort((port) -> {
230-
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> {
231-
AbstractReactiveWebServerFactory factory = getFactory();
232-
factory.setPort(port);
233-
this.webServer = factory.getWebServer(mock(HttpHandler.class));
234-
this.webServer.start();
235-
}).satisfies((ex) -> handleExceptionCausedByBlockedPortOnPrimaryConnector(ex, port));
236-
});
224+
void portClashOfPrimaryConnectorResultsInPortInUseException() throws Exception {
225+
doWithBlockedPort((port) -> assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> {
226+
AbstractReactiveWebServerFactory factory = getFactory();
227+
factory.setPort(port);
228+
this.webServer = factory.getWebServer(mock(HttpHandler.class));
229+
this.webServer.start();
230+
}).satisfies((ex) -> handleExceptionCausedByBlockedPortOnPrimaryConnector(ex, port)));
237231
}
238232

239233
@Override
@@ -279,34 +273,9 @@ protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
279273
assertThat(webServerReference).hasValue(webServer);
280274
}
281275

282-
private void doWithBlockedPort(BlockedPortAction action) throws IOException {
283-
int port = SocketUtils.findAvailableTcpPort(40000);
284-
ServerSocket serverSocket = new ServerSocket();
285-
for (int i = 0; i < 10; i++) {
286-
try {
287-
serverSocket.bind(new InetSocketAddress(port));
288-
break;
289-
}
290-
catch (Exception ex) {
291-
}
292-
}
293-
try {
294-
action.run(port);
295-
}
296-
finally {
297-
serverSocket.close();
298-
}
299-
}
300-
301276
private void handleExceptionCausedByBlockedPortOnPrimaryConnector(RuntimeException ex, int blockedPort) {
302277
assertThat(ex).isInstanceOf(PortInUseException.class);
303278
assertThat(((PortInUseException) ex).getPort()).isEqualTo(blockedPort);
304279
}
305280

306-
interface BlockedPortAction {
307-
308-
void run(int port);
309-
310-
}
311-
312281
}

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.io.InputStream;
2222
import java.net.InetSocketAddress;
23+
import java.net.ServerSocket;
2324
import java.nio.charset.StandardCharsets;
2425
import java.security.KeyStore;
2526
import java.time.Duration;
@@ -73,7 +74,6 @@
7374
import org.springframework.http.server.reactive.HttpHandler;
7475
import org.springframework.http.server.reactive.ServerHttpRequest;
7576
import org.springframework.http.server.reactive.ServerHttpResponse;
76-
import org.springframework.util.SocketUtils;
7777
import org.springframework.util.unit.DataSize;
7878
import org.springframework.web.reactive.function.BodyInserters;
7979
import org.springframework.web.reactive.function.client.WebClient;
@@ -109,11 +109,10 @@ void tearDown() {
109109
void specificPort() throws Exception {
110110
AbstractReactiveWebServerFactory factory = getFactory();
111111
int specificPort = doWithRetry(() -> {
112-
int port = SocketUtils.findAvailableTcpPort(41000);
113-
factory.setPort(port);
112+
factory.setPort(0);
114113
this.webServer = factory.getWebServer(new EchoHandler());
115114
this.webServer.start();
116-
return port;
115+
return this.webServer.getPort();
117116
});
118117
Mono<String> result = getWebClient(this.webServer.getPort()).build().post().uri("/test")
119118
.contentType(MediaType.TEXT_PLAIN).body(BodyInserters.fromValue("Hello World")).retrieve()
@@ -569,6 +568,26 @@ private <T> T doWithRetry(Callable<T> action) throws Exception {
569568
throw new IllegalStateException("Action was not successful in 10 attempts", lastFailure);
570569
}
571570

571+
protected final void doWithBlockedPort(BlockedPortAction action) throws Exception {
572+
ServerSocket serverSocket = new ServerSocket();
573+
int blockedPort = doWithRetry(() -> {
574+
serverSocket.bind(null);
575+
return serverSocket.getLocalPort();
576+
});
577+
try {
578+
action.run(blockedPort);
579+
}
580+
finally {
581+
serverSocket.close();
582+
}
583+
}
584+
585+
public interface BlockedPortAction {
586+
587+
void run(int port);
588+
589+
}
590+
572591
protected static class EchoHandler implements HttpHandler {
573592

574593
public EchoHandler() {

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.InputStream;
2323
import java.io.PrintWriter;
2424
import java.net.InetAddress;
25-
import java.net.InetSocketAddress;
2625
import java.net.MalformedURLException;
2726
import java.net.ServerSocket;
2827
import java.net.URI;
@@ -140,7 +139,6 @@
140139
import org.springframework.test.util.ReflectionTestUtils;
141140
import org.springframework.util.ClassUtils;
142141
import org.springframework.util.FileCopyUtils;
143-
import org.springframework.util.SocketUtils;
144142
import org.springframework.util.StreamUtils;
145143

146144
import static org.assertj.core.api.Assertions.assertThat;
@@ -319,11 +317,10 @@ void portIsMinusOneWhenConnectionIsClosed() {
319317
void specificPort() throws Exception {
320318
AbstractServletWebServerFactory factory = getFactory();
321319
int specificPort = doWithRetry(() -> {
322-
int port = SocketUtils.findAvailableTcpPort(41000);
323-
factory.setPort(port);
320+
factory.setPort(0);
324321
this.webServer = factory.getWebServer(exampleServletRegistration());
325322
this.webServer.start();
326-
return port;
323+
return this.webServer.getPort();
327324
});
328325
assertThat(getResponse("http://localhost:" + specificPort + "/hello")).isEqualTo("Hello World");
329326
assertThat(this.webServer.getPort()).isEqualTo(specificPort);
@@ -1405,9 +1402,8 @@ private <T> T doWithRetry(Callable<T> action) throws Exception {
14051402
protected final void doWithBlockedPort(BlockedPortAction action) throws Exception {
14061403
ServerSocket serverSocket = new ServerSocket();
14071404
int blockedPort = doWithRetry(() -> {
1408-
int port = SocketUtils.findAvailableTcpPort(40000);
1409-
serverSocket.bind(new InetSocketAddress(port));
1410-
return port;
1405+
serverSocket.bind(null);
1406+
return serverSocket.getLocalPort();
14111407
});
14121408
try {
14131409
action.run(blockedPort);

0 commit comments

Comments
 (0)