Minimise our usage of SocketUtils.findAvailableTcpPort

Closes gh-9382
This commit is contained in:
Andy Wilkinson
2017-06-08 10:37:40 +01:00
parent 10868519e1
commit f7e9ec5f42
26 changed files with 442 additions and 286 deletions

View File

@@ -23,7 +23,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.util.SocketUtils;
/**
* Sample Application to show Tomcat running two connectors
@@ -34,11 +33,6 @@ import org.springframework.util.SocketUtils;
@SpringBootApplication
public class SampleTomcatTwoConnectorsApplication {
@Bean
public Integer port() {
return SocketUtils.findAvailableTcpPort();
}
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
@@ -48,7 +42,7 @@ public class SampleTomcatTwoConnectorsApplication {
private Connector createStandardConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(port());
connector.setPort(0);
return connector;
}

View File

@@ -22,19 +22,26 @@ import java.net.HttpURLConnection;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.catalina.Service;
import org.apache.catalina.connector.Connector;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import sample.tomcat.multiconnector.SampleTomcatTwoConnectorsApplicationTests.Ports;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
@@ -53,10 +60,11 @@ import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
@Import(Ports.class)
public class SampleTomcatTwoConnectorsApplicationTests {
@LocalServerPort
private String port;
private int port;
@Autowired
private ApplicationContext context;
@@ -97,27 +105,19 @@ public class SampleTomcatTwoConnectorsApplicationTests {
public void testHello() throws Exception {
RestTemplate template = new RestTemplate();
final MySimpleClientHttpRequestFactory factory = new MySimpleClientHttpRequestFactory(
new HostnameVerifier() {
@Override
public boolean verify(final String hostname,
final SSLSession session) {
return true; // these guys are alright by me...
}
});
(hostname, session) -> true);
template.setRequestFactory(factory);
Ports ports = this.context.getBean(Ports.class);
assertThat(ports.getHttpsPort()).isEqualTo(this.port);
assertThat(ports.getHttpPort()).isNotEqualTo(this.port);
ResponseEntity<String> entity = template.getForEntity(
"http://localhost:" + this.context.getBean("port") + "/hello",
String.class);
"http://localhost:" + ports.getHttpPort() + "/hello", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("hello");
ResponseEntity<String> httpsEntity = template
.getForEntity("https://localhost:" + this.port + "/hello", String.class);
assertThat(httpsEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(httpsEntity.getBody()).isEqualTo("hello");
}
/**
@@ -141,4 +141,35 @@ public class SampleTomcatTwoConnectorsApplicationTests {
}
}
@TestConfiguration
static class Ports implements ApplicationListener<WebServerInitializedEvent> {
private int httpPort;
private int httpsPort;
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
Service service = ((TomcatWebServer) event.getWebServer()).getTomcat()
.getService();
for (Connector connector : service.findConnectors()) {
if (connector.getSecure()) {
this.httpsPort = connector.getLocalPort();
}
else {
this.httpPort = connector.getLocalPort();
}
}
}
int getHttpPort() {
return this.httpPort;
}
int getHttpsPort() {
return this.httpsPort;
}
}
}

View File

@@ -37,13 +37,13 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.SocketUtils;
import org.springframework.web.socket.client.WebSocketConnectionManager;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
@@ -51,20 +51,21 @@ import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { SampleJettyWebSocketsApplication.class,
CustomContainerConfiguration.class }, webEnvironment = WebEnvironment.DEFINED_PORT)
CustomContainerConfiguration.class }, webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class CustomContainerWebSocketsApplicationTests {
private static Log logger = LogFactory
.getLog(CustomContainerWebSocketsApplicationTests.class);
private static int PORT = SocketUtils.findAvailableTcpPort();
@LocalServerPort
private int port;
@Test
public void echoEndpoint() throws Exception {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
ClientConfiguration.class, PropertyPlaceholderAutoConfiguration.class)
.properties("websocket.uri:ws://localhost:" + PORT
.properties("websocket.uri:ws://localhost:" + this.port
+ "/ws/echo/websocket")
.run("--spring.main.web_environment=false");
long count = context.getBean(ClientConfiguration.class).latch.getCount();
@@ -80,8 +81,8 @@ public class CustomContainerWebSocketsApplicationTests {
public void reverseEndpoint() throws Exception {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
ClientConfiguration.class, PropertyPlaceholderAutoConfiguration.class)
.properties(
"websocket.uri:ws://localhost:" + PORT + "/ws/reverse")
.properties("websocket.uri:ws://localhost:" + this.port
+ "/ws/reverse")
.run("--spring.main.web_environment=false");
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context
@@ -96,7 +97,7 @@ public class CustomContainerWebSocketsApplicationTests {
@Bean
public ServletWebServerFactory webServerFactory() {
return new JettyServletWebServerFactory("/ws", PORT);
return new JettyServletWebServerFactory("/ws", 0);
}
}

View File

@@ -37,13 +37,13 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.SocketUtils;
import org.springframework.web.socket.client.WebSocketConnectionManager;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
@@ -51,20 +51,21 @@ import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { SampleTomcatWebSocketApplication.class,
CustomContainerConfiguration.class }, webEnvironment = WebEnvironment.DEFINED_PORT)
CustomContainerConfiguration.class }, webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class CustomContainerWebSocketsApplicationTests {
private static Log logger = LogFactory
.getLog(CustomContainerWebSocketsApplicationTests.class);
private static int PORT = SocketUtils.findAvailableTcpPort();
@LocalServerPort
private int port;
@Test
public void echoEndpoint() throws Exception {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
ClientConfiguration.class, PropertyPlaceholderAutoConfiguration.class)
.properties("websocket.uri:ws://localhost:" + PORT
.properties("websocket.uri:ws://localhost:" + this.port
+ "/ws/echo/websocket")
.run("--spring.main.web_environment=false");
long count = context.getBean(ClientConfiguration.class).latch.getCount();
@@ -80,8 +81,8 @@ public class CustomContainerWebSocketsApplicationTests {
public void reverseEndpoint() throws Exception {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
ClientConfiguration.class, PropertyPlaceholderAutoConfiguration.class)
.properties(
"websocket.uri:ws://localhost:" + PORT + "/ws/reverse")
.properties("websocket.uri:ws://localhost:" + this.port
+ "/ws/reverse")
.run("--spring.main.web_environment=false");
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context
@@ -96,7 +97,7 @@ public class CustomContainerWebSocketsApplicationTests {
@Bean
public ServletWebServerFactory webServerFactory() {
return new TomcatServletWebServerFactory("/ws", PORT);
return new TomcatServletWebServerFactory("/ws", 0);
}
}

View File

@@ -37,13 +37,13 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.SocketUtils;
import org.springframework.web.socket.client.WebSocketConnectionManager;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
@@ -51,20 +51,21 @@ import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { SampleUndertowWebSocketsApplication.class,
CustomContainerConfiguration.class }, webEnvironment = WebEnvironment.DEFINED_PORT)
CustomContainerConfiguration.class }, webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class CustomContainerWebSocketsApplicationTests {
private static Log logger = LogFactory
.getLog(CustomContainerWebSocketsApplicationTests.class);
private static int PORT = SocketUtils.findAvailableTcpPort();
@LocalServerPort
private int port;
@Test
public void echoEndpoint() throws Exception {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
ClientConfiguration.class, PropertyPlaceholderAutoConfiguration.class)
.properties("websocket.uri:ws://localhost:" + PORT
.properties("websocket.uri:ws://localhost:" + this.port
+ "/ws/echo/websocket")
.run("--spring.main.web_environment=false");
long count = context.getBean(ClientConfiguration.class).latch.getCount();
@@ -80,8 +81,8 @@ public class CustomContainerWebSocketsApplicationTests {
public void reverseEndpoint() throws Exception {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
ClientConfiguration.class, PropertyPlaceholderAutoConfiguration.class)
.properties(
"websocket.uri:ws://localhost:" + PORT + "/ws/reverse")
.properties("websocket.uri:ws://localhost:" + this.port
+ "/ws/reverse")
.run("--spring.main.web_environment=false");
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context
@@ -96,7 +97,7 @@ public class CustomContainerWebSocketsApplicationTests {
@Bean
public ServletWebServerFactory webServerFactory() {
return new UndertowServletWebServerFactory("/ws", PORT);
return new UndertowServletWebServerFactory("/ws", 0);
}
}