From f3af0359413c1f665dfe3b989ea605bb683bf233 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 15 Feb 2022 16:17:21 +0100 Subject: [PATCH] Remove usage of SocketUtils in tests Closes gh-29821 --- .../netty/NettyRSocketServerFactoryTests.java | 8 ++-- .../TomcatReactiveWebServerFactoryTests.java | 45 +++---------------- ...AbstractReactiveWebServerFactoryTests.java | 29 +++++++++--- .../AbstractServletWebServerFactoryTests.java | 12 ++--- 4 files changed, 38 insertions(+), 56 deletions(-) diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactoryTests.java index fd88751014..0a79bea428 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactoryTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -51,7 +51,6 @@ import org.springframework.core.io.buffer.NettyDataBufferFactory; import org.springframework.http.client.reactive.ReactorResourceFactory; import org.springframework.messaging.rsocket.RSocketRequester; import org.springframework.messaging.rsocket.RSocketStrategies; -import org.springframework.util.SocketUtils; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; @@ -97,11 +96,10 @@ class NettyRSocketServerFactoryTests { void specificPort() { NettyRSocketServerFactory factory = getFactory(); int specificPort = doWithRetry(() -> { - int port = SocketUtils.findAvailableTcpPort(41000); - factory.setPort(port); + factory.setPort(0); this.server = factory.create(new EchoRequestResponseAcceptor()); this.server.start(); - return port; + return this.server.address().getPort(); }); this.requester = createRSocketTcpClient(); assertThat(this.server.address().getPort()).isEqualTo(specificPort); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactoryTests.java index d047fff2b2..9bc2a93a70 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactoryTests.java @@ -16,10 +16,7 @@ package org.springframework.boot.web.embedded.tomcat; -import java.io.IOException; import java.net.ConnectException; -import java.net.InetSocketAddress; -import java.net.ServerSocket; import java.time.Duration; import java.util.Arrays; import java.util.Map; @@ -48,7 +45,6 @@ import org.springframework.boot.web.server.PortInUseException; import org.springframework.boot.web.server.Shutdown; import org.springframework.boot.web.server.WebServerException; import org.springframework.http.server.reactive.HttpHandler; -import org.springframework.util.SocketUtils; import org.springframework.web.reactive.function.client.WebClient; import static org.assertj.core.api.Assertions.assertThat; @@ -225,15 +221,13 @@ class TomcatReactiveWebServerFactoryTests extends AbstractReactiveWebServerFacto } @Test - void portClashOfPrimaryConnectorResultsInPortInUseException() throws IOException { - doWithBlockedPort((port) -> { - assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> { - AbstractReactiveWebServerFactory factory = getFactory(); - factory.setPort(port); - this.webServer = factory.getWebServer(mock(HttpHandler.class)); - this.webServer.start(); - }).satisfies((ex) -> handleExceptionCausedByBlockedPortOnPrimaryConnector(ex, port)); - }); + void portClashOfPrimaryConnectorResultsInPortInUseException() throws Exception { + doWithBlockedPort((port) -> assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> { + AbstractReactiveWebServerFactory factory = getFactory(); + factory.setPort(port); + this.webServer = factory.getWebServer(mock(HttpHandler.class)); + this.webServer.start(); + }).satisfies((ex) -> handleExceptionCausedByBlockedPortOnPrimaryConnector(ex, port))); } @Override @@ -279,34 +273,9 @@ class TomcatReactiveWebServerFactoryTests extends AbstractReactiveWebServerFacto assertThat(webServerReference).hasValue(webServer); } - private void doWithBlockedPort(BlockedPortAction action) throws IOException { - int port = SocketUtils.findAvailableTcpPort(40000); - ServerSocket serverSocket = new ServerSocket(); - for (int i = 0; i < 10; i++) { - try { - serverSocket.bind(new InetSocketAddress(port)); - break; - } - catch (Exception ex) { - } - } - try { - action.run(port); - } - finally { - serverSocket.close(); - } - } - private void handleExceptionCausedByBlockedPortOnPrimaryConnector(RuntimeException ex, int blockedPort) { assertThat(ex).isInstanceOf(PortInUseException.class); assertThat(((PortInUseException) ex).getPort()).isEqualTo(blockedPort); } - interface BlockedPortAction { - - void run(int port); - - } - } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java index f8ece0a60d..ad0b5f098d 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.InetSocketAddress; +import java.net.ServerSocket; import java.nio.charset.StandardCharsets; import java.security.KeyStore; import java.time.Duration; @@ -73,7 +74,6 @@ import org.springframework.http.client.reactive.ReactorClientHttpConnector; import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; -import org.springframework.util.SocketUtils; import org.springframework.util.unit.DataSize; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.WebClient; @@ -109,11 +109,10 @@ public abstract class AbstractReactiveWebServerFactoryTests { void specificPort() throws Exception { AbstractReactiveWebServerFactory factory = getFactory(); int specificPort = doWithRetry(() -> { - int port = SocketUtils.findAvailableTcpPort(41000); - factory.setPort(port); + factory.setPort(0); this.webServer = factory.getWebServer(new EchoHandler()); this.webServer.start(); - return port; + return this.webServer.getPort(); }); Mono result = getWebClient(this.webServer.getPort()).build().post().uri("/test") .contentType(MediaType.TEXT_PLAIN).body(BodyInserters.fromValue("Hello World")).retrieve() @@ -569,6 +568,26 @@ public abstract class AbstractReactiveWebServerFactoryTests { throw new IllegalStateException("Action was not successful in 10 attempts", lastFailure); } + protected final void doWithBlockedPort(BlockedPortAction action) throws Exception { + ServerSocket serverSocket = new ServerSocket(); + int blockedPort = doWithRetry(() -> { + serverSocket.bind(null); + return serverSocket.getLocalPort(); + }); + try { + action.run(blockedPort); + } + finally { + serverSocket.close(); + } + } + + public interface BlockedPortAction { + + void run(int port); + + } + protected static class EchoHandler implements HttpHandler { public EchoHandler() { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java index 37d299c9fb..702061bba0 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java @@ -22,7 +22,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.net.InetAddress; -import java.net.InetSocketAddress; import java.net.MalformedURLException; import java.net.ServerSocket; import java.net.URI; @@ -140,7 +139,6 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.util.ClassUtils; import org.springframework.util.FileCopyUtils; -import org.springframework.util.SocketUtils; import org.springframework.util.StreamUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -319,11 +317,10 @@ public abstract class AbstractServletWebServerFactoryTests { void specificPort() throws Exception { AbstractServletWebServerFactory factory = getFactory(); int specificPort = doWithRetry(() -> { - int port = SocketUtils.findAvailableTcpPort(41000); - factory.setPort(port); + factory.setPort(0); this.webServer = factory.getWebServer(exampleServletRegistration()); this.webServer.start(); - return port; + return this.webServer.getPort(); }); assertThat(getResponse("http://localhost:" + specificPort + "/hello")).isEqualTo("Hello World"); assertThat(this.webServer.getPort()).isEqualTo(specificPort); @@ -1405,9 +1402,8 @@ public abstract class AbstractServletWebServerFactoryTests { protected final void doWithBlockedPort(BlockedPortAction action) throws Exception { ServerSocket serverSocket = new ServerSocket(); int blockedPort = doWithRetry(() -> { - int port = SocketUtils.findAvailableTcpPort(40000); - serverSocket.bind(new InetSocketAddress(port)); - return port; + serverSocket.bind(null); + return serverSocket.getLocalPort(); }); try { action.run(blockedPort);