Merge pull request #11541 from kwoyke/JAVA-8748
JAVA-8748: Make each test get available port on its own
This commit is contained in:
commit
d1fe4362b1
|
@ -4,37 +4,26 @@ import org.apache.catalina.LifecycleException;
|
||||||
import org.apache.catalina.startup.Tomcat;
|
import org.apache.catalina.startup.Tomcat;
|
||||||
import org.eclipse.jetty.server.Server;
|
import org.eclipse.jetty.server.Server;
|
||||||
import org.eclipse.jetty.server.ServerConnector;
|
import org.eclipse.jetty.server.ServerConnector;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.util.SocketUtils;
|
import org.springframework.util.SocketUtils;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.*;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.fail;
|
||||||
|
|
||||||
// fixing in JAVA-8748
|
public class FindFreePortUnitTest {
|
||||||
public class FindFreePortManualTest {
|
|
||||||
|
|
||||||
private static int FREE_PORT_NUMBER;
|
private static final int DEFAULT_RANDOM_PORT = 34307;
|
||||||
private static int[] FREE_PORT_RANGE;
|
|
||||||
|
|
||||||
@BeforeAll
|
|
||||||
public static void getExplicitFreePortNumberAndRange() {
|
|
||||||
try (ServerSocket serverSocket = new ServerSocket(0)) {
|
|
||||||
FREE_PORT_NUMBER = serverSocket.getLocalPort();
|
|
||||||
FREE_PORT_RANGE = new int[] {FREE_PORT_NUMBER, FREE_PORT_NUMBER + 1, FREE_PORT_NUMBER + 2};
|
|
||||||
} catch (IOException e) {
|
|
||||||
fail("No free port is available");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenExplicitFreePort_whenCreatingServerSocket_thenThatPortIsAssigned() {
|
public void givenExplicitFreePort_whenCreatingServerSocket_thenThatPortIsAssigned() {
|
||||||
try (ServerSocket serverSocket = new ServerSocket(FREE_PORT_NUMBER)) {
|
int freePort = getFreePort();
|
||||||
|
|
||||||
|
try (ServerSocket serverSocket = new ServerSocket(freePort)) {
|
||||||
assertThat(serverSocket).isNotNull();
|
assertThat(serverSocket).isNotNull();
|
||||||
assertThat(serverSocket.getLocalPort()).isEqualTo(FREE_PORT_NUMBER);
|
assertThat(serverSocket.getLocalPort()).isEqualTo(freePort);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
fail("Port is not available");
|
fail("Port is not available");
|
||||||
}
|
}
|
||||||
|
@ -42,8 +31,10 @@ public class FindFreePortManualTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenExplicitOccupiedPort_whenCreatingServerSocket_thenExceptionIsThrown() {
|
public void givenExplicitOccupiedPort_whenCreatingServerSocket_thenExceptionIsThrown() {
|
||||||
try (ServerSocket serverSocket = new ServerSocket(FREE_PORT_NUMBER)) {
|
int freePort = getFreePort();
|
||||||
new ServerSocket(FREE_PORT_NUMBER);
|
|
||||||
|
try (ServerSocket serverSocket = new ServerSocket(freePort)) {
|
||||||
|
new ServerSocket(freePort);
|
||||||
fail("Same port cannot be used twice");
|
fail("Same port cannot be used twice");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
assertThat(e).hasMessageContaining("Address already in use");
|
assertThat(e).hasMessageContaining("Address already in use");
|
||||||
|
@ -52,7 +43,7 @@ public class FindFreePortManualTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenExplicitPortRange_whenCreatingServerSocket_thenOnePortIsAssigned() {
|
public void givenExplicitPortRange_whenCreatingServerSocket_thenOnePortIsAssigned() {
|
||||||
for (int port : FREE_PORT_RANGE) {
|
for (int port : getFreePorts()) {
|
||||||
try (ServerSocket serverSocket = new ServerSocket(port)) {
|
try (ServerSocket serverSocket = new ServerSocket(port)) {
|
||||||
assertThat(serverSocket).isNotNull();
|
assertThat(serverSocket).isNotNull();
|
||||||
assertThat(serverSocket.getLocalPort()).isEqualTo(port);
|
assertThat(serverSocket.getLocalPort()).isEqualTo(port);
|
||||||
|
@ -105,11 +96,12 @@ public class FindFreePortManualTest {
|
||||||
public void givenExplicitFreePort_whenCreatingJettyServer_thenThatPortIsAssigned() throws Exception {
|
public void givenExplicitFreePort_whenCreatingJettyServer_thenThatPortIsAssigned() throws Exception {
|
||||||
Server jettyServer = new Server();
|
Server jettyServer = new Server();
|
||||||
ServerConnector serverConnector = new ServerConnector(jettyServer);
|
ServerConnector serverConnector = new ServerConnector(jettyServer);
|
||||||
serverConnector.setPort(FREE_PORT_NUMBER);
|
int freePort = getFreePort();
|
||||||
|
serverConnector.setPort(freePort);
|
||||||
jettyServer.addConnector(serverConnector);
|
jettyServer.addConnector(serverConnector);
|
||||||
try {
|
try {
|
||||||
jettyServer.start();
|
jettyServer.start();
|
||||||
assertThat(serverConnector.getLocalPort()).isEqualTo(FREE_PORT_NUMBER);
|
assertThat(serverConnector.getLocalPort()).isEqualTo(freePort);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
fail("Failed to start Jetty server");
|
fail("Failed to start Jetty server");
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -136,10 +128,11 @@ public class FindFreePortManualTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenExplicitFreePort_whenCreatingTomcatServer_thenThatPortIsAssigned() throws Exception {
|
public void givenExplicitFreePort_whenCreatingTomcatServer_thenThatPortIsAssigned() throws Exception {
|
||||||
Tomcat tomcatServer = new Tomcat();
|
Tomcat tomcatServer = new Tomcat();
|
||||||
tomcatServer.setPort(FREE_PORT_NUMBER);
|
int freePort = getFreePort();
|
||||||
|
tomcatServer.setPort(freePort);
|
||||||
try {
|
try {
|
||||||
tomcatServer.start();
|
tomcatServer.start();
|
||||||
assertThat(tomcatServer.getConnector().getLocalPort()).isEqualTo(FREE_PORT_NUMBER);
|
assertThat(tomcatServer.getConnector().getLocalPort()).isEqualTo(freePort);
|
||||||
} catch (LifecycleException e) {
|
} catch (LifecycleException e) {
|
||||||
fail("Failed to start Tomcat server");
|
fail("Failed to start Tomcat server");
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -148,4 +141,16 @@ public class FindFreePortManualTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int[] getFreePorts() {
|
||||||
|
int freePort = getFreePort();
|
||||||
|
return new int[]{freePort - 1, freePort, freePort + 1};
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getFreePort() {
|
||||||
|
try(ServerSocket serverSocket = new ServerSocket(0)){
|
||||||
|
return serverSocket.getLocalPort();
|
||||||
|
} catch (IOException ex){
|
||||||
|
return DEFAULT_RANDOM_PORT;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue