|
|
|
@ -44,6 +44,7 @@ import java.util.EnumSet; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Locale; |
|
|
|
import java.util.Locale; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
import java.util.concurrent.Callable; |
|
|
|
import java.util.concurrent.atomic.AtomicBoolean; |
|
|
|
import java.util.concurrent.atomic.AtomicBoolean; |
|
|
|
import java.util.concurrent.atomic.AtomicReference; |
|
|
|
import java.util.concurrent.atomic.AtomicReference; |
|
|
|
import java.util.zip.GZIPInputStream; |
|
|
|
import java.util.zip.GZIPInputStream; |
|
|
|
@ -248,10 +249,13 @@ public abstract class AbstractServletWebServerFactoryTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void specificPort() throws Exception { |
|
|
|
public void specificPort() throws Exception { |
|
|
|
AbstractServletWebServerFactory factory = getFactory(); |
|
|
|
AbstractServletWebServerFactory factory = getFactory(); |
|
|
|
int specificPort = SocketUtils.findAvailableTcpPort(41000); |
|
|
|
int specificPort = doWithRetry(() -> { |
|
|
|
factory.setPort(specificPort); |
|
|
|
int port = SocketUtils.findAvailableTcpPort(41000); |
|
|
|
this.webServer = factory.getWebServer(exampleServletRegistration()); |
|
|
|
factory.setPort(port); |
|
|
|
this.webServer.start(); |
|
|
|
this.webServer = factory.getWebServer(exampleServletRegistration()); |
|
|
|
|
|
|
|
this.webServer.start(); |
|
|
|
|
|
|
|
return port; |
|
|
|
|
|
|
|
}); |
|
|
|
assertThat(getResponse("http://localhost:" + specificPort + "/hello")).isEqualTo("Hello World"); |
|
|
|
assertThat(getResponse("http://localhost:" + specificPort + "/hello")).isEqualTo("Hello World"); |
|
|
|
assertThat(this.webServer.getPort()).isEqualTo(specificPort); |
|
|
|
assertThat(this.webServer.getPort()).isEqualTo(specificPort); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -822,7 +826,7 @@ public abstract class AbstractServletWebServerFactoryTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void portClashOfPrimaryConnectorResultsInPortInUseException() throws IOException { |
|
|
|
public void portClashOfPrimaryConnectorResultsInPortInUseException() throws Exception { |
|
|
|
doWithBlockedPort((port) -> { |
|
|
|
doWithBlockedPort((port) -> { |
|
|
|
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> { |
|
|
|
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> { |
|
|
|
AbstractServletWebServerFactory factory = getFactory(); |
|
|
|
AbstractServletWebServerFactory factory = getFactory(); |
|
|
|
@ -834,7 +838,7 @@ public abstract class AbstractServletWebServerFactoryTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void portClashOfSecondaryConnectorResultsInPortInUseException() throws IOException { |
|
|
|
public void portClashOfSecondaryConnectorResultsInPortInUseException() throws Exception { |
|
|
|
doWithBlockedPort((port) -> { |
|
|
|
doWithBlockedPort((port) -> { |
|
|
|
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> { |
|
|
|
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> { |
|
|
|
AbstractServletWebServerFactory factory = getFactory(); |
|
|
|
AbstractServletWebServerFactory factory = getFactory(); |
|
|
|
@ -1128,19 +1132,28 @@ public abstract class AbstractServletWebServerFactoryTests { |
|
|
|
return bean; |
|
|
|
return bean; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected final void doWithBlockedPort(BlockedPortAction action) throws IOException { |
|
|
|
private <T> T doWithRetry(Callable<T> action) throws Exception { |
|
|
|
int port = SocketUtils.findAvailableTcpPort(40000); |
|
|
|
Exception lastFailure = null; |
|
|
|
ServerSocket serverSocket = new ServerSocket(); |
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++) { |
|
|
|
for (int i = 0; i < 10; i++) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
serverSocket.bind(new InetSocketAddress(port)); |
|
|
|
return action.call(); |
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) { |
|
|
|
catch (Exception ex) { |
|
|
|
|
|
|
|
lastFailure = ex; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
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(() -> { |
|
|
|
|
|
|
|
int port = SocketUtils.findAvailableTcpPort(40000); |
|
|
|
|
|
|
|
serverSocket.bind(new InetSocketAddress(port)); |
|
|
|
|
|
|
|
return port; |
|
|
|
|
|
|
|
}); |
|
|
|
try { |
|
|
|
try { |
|
|
|
action.run(port); |
|
|
|
action.run(blockedPort); |
|
|
|
} |
|
|
|
} |
|
|
|
finally { |
|
|
|
finally { |
|
|
|
serverSocket.close(); |
|
|
|
serverSocket.close(); |
|
|
|
|