Refactor random port in reactive module tests
This commit removes the use of SocketUtils#findAvailableTcpPort in favor of letting servers pick a dynamic port by specifying port 0. This should make integration tests more stable because the port is chosen at the place where it needs to be used. It gives servers a chance to try to open a socket on some port and start using the socket if successful.
This commit is contained in:
@@ -18,6 +18,8 @@ package org.springframework.http.server.reactive;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -29,11 +31,12 @@ import org.springframework.http.server.reactive.bootstrap.ReactorHttpServer;
|
||||
import org.springframework.http.server.reactive.bootstrap.RxNettyHttpServer;
|
||||
import org.springframework.http.server.reactive.bootstrap.TomcatHttpServer;
|
||||
import org.springframework.http.server.reactive.bootstrap.UndertowHttpServer;
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public abstract class AbstractHttpHandlerIntegrationTests {
|
||||
|
||||
protected Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
protected int port;
|
||||
|
||||
@Parameterized.Parameter(0)
|
||||
@@ -55,16 +58,18 @@ public abstract class AbstractHttpHandlerIntegrationTests {
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.port = SocketUtils.findAvailableTcpPort();
|
||||
this.server.setPort(this.port);
|
||||
this.server.setHandler(createHttpHandler());
|
||||
this.server.afterPropertiesSet();
|
||||
this.server.start();
|
||||
|
||||
// Set dynamically chosen port
|
||||
this.port = this.server.getPort();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
this.server.stop();
|
||||
this.port = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ public abstract class AbstractHttpServer implements HttpServer {
|
||||
|
||||
private String host = "0.0.0.0";
|
||||
|
||||
private int port = -1;
|
||||
private int port = 0;
|
||||
|
||||
private HttpHandler httpHandler;
|
||||
|
||||
@@ -40,6 +40,7 @@ public abstract class AbstractHttpServer implements HttpServer {
|
||||
private final Object lifecycleMonitor = new Object();
|
||||
|
||||
|
||||
@Override
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
@@ -48,14 +49,17 @@ public abstract class AbstractHttpServer implements HttpServer {
|
||||
return host;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHandler(HttpHandler handler) {
|
||||
this.httpHandler = handler;
|
||||
}
|
||||
@@ -138,13 +142,16 @@ public abstract class AbstractHttpServer implements HttpServer {
|
||||
}
|
||||
}
|
||||
|
||||
protected void reset() {
|
||||
this.host = "0.0.0.0";
|
||||
this.port = -1;
|
||||
this.httpHandler = null;
|
||||
this.handlerMap = null;
|
||||
}
|
||||
|
||||
protected abstract void stopInternal() throws Exception;
|
||||
|
||||
private void reset() {
|
||||
this.host = "0.0.0.0";
|
||||
this.port = 0;
|
||||
this.httpHandler = null;
|
||||
this.handlerMap = null;
|
||||
resetInternal();
|
||||
}
|
||||
|
||||
protected abstract void resetInternal();
|
||||
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ public interface HttpServer extends InitializingBean, Lifecycle {
|
||||
|
||||
void setPort(int port);
|
||||
|
||||
int getPort();
|
||||
|
||||
void setHandler(HttpHandler handler);
|
||||
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ public class JettyHttpServer extends AbstractHttpServer {
|
||||
@Override
|
||||
protected void startInternal() throws Exception {
|
||||
this.jettyServer.start();
|
||||
setPort(((ServerConnector) this.jettyServer.getConnectors()[0]).getLocalPort());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -84,8 +85,7 @@ public class JettyHttpServer extends AbstractHttpServer {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void reset() {
|
||||
super.reset();
|
||||
protected void resetInternal() {
|
||||
try {
|
||||
if (this.jettyServer.isRunning()) {
|
||||
this.jettyServer.setStopTimeout(5000);
|
||||
|
||||
@@ -49,7 +49,6 @@ public class ReactorHttpServer extends AbstractHttpServer implements Loopback {
|
||||
new ReactorHttpHandlerAdapter(getHttpHandler());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object connectedInput() {
|
||||
return this.reactorServer;
|
||||
@@ -63,6 +62,7 @@ public class ReactorHttpServer extends AbstractHttpServer implements Loopback {
|
||||
@Override
|
||||
protected void startInternal() {
|
||||
NettyContext nettyContext = this.reactorServer.newHandler(this.reactorHandler).block();
|
||||
setPort(nettyContext.address().getPort());
|
||||
this.nettyContext.set(nettyContext);
|
||||
}
|
||||
|
||||
@@ -72,8 +72,7 @@ public class ReactorHttpServer extends AbstractHttpServer implements Loopback {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void reset() {
|
||||
super.reset();
|
||||
protected void resetInternal() {
|
||||
this.reactorServer = null;
|
||||
this.reactorHandler = null;
|
||||
this.nettyContext.set(null);
|
||||
|
||||
@@ -37,8 +37,7 @@ public class RxNettyHttpServer extends AbstractHttpServer {
|
||||
@Override
|
||||
protected void initServer() throws Exception {
|
||||
this.rxNettyHandler = createHttpHandlerAdapter();
|
||||
this.rxNettyServer = io.reactivex.netty.protocol.http.server.HttpServer
|
||||
.newServer(new InetSocketAddress(getHost(), getPort()));
|
||||
this.rxNettyServer = io.reactivex.netty.protocol.http.server.HttpServer.newServer(getPort());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -52,6 +51,7 @@ public class RxNettyHttpServer extends AbstractHttpServer {
|
||||
@Override
|
||||
protected void startInternal() {
|
||||
this.rxNettyServer.start(this.rxNettyHandler);
|
||||
setPort(this.rxNettyServer.getServerPort());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -60,8 +60,7 @@ public class RxNettyHttpServer extends AbstractHttpServer {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void reset() {
|
||||
super.reset();
|
||||
protected void resetInternal() {
|
||||
this.rxNettyServer = null;
|
||||
this.rxNettyHandler = null;
|
||||
}
|
||||
|
||||
@@ -77,6 +77,7 @@ public class TomcatHttpServer extends AbstractHttpServer {
|
||||
@Override
|
||||
protected void startInternal() throws LifecycleException {
|
||||
this.tomcatServer.start();
|
||||
setPort(this.tomcatServer.getConnector().getLocalPort());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -86,7 +87,7 @@ public class TomcatHttpServer extends AbstractHttpServer {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void reset() {
|
||||
protected void resetInternal() {
|
||||
this.tomcatServer = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.http.server.reactive.bootstrap;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import io.undertow.Undertow;
|
||||
|
||||
import org.springframework.http.server.reactive.UndertowHttpHandlerAdapter;
|
||||
@@ -44,6 +46,8 @@ public class UndertowHttpServer extends AbstractHttpServer {
|
||||
@Override
|
||||
protected void startInternal() {
|
||||
this.server.start();
|
||||
Undertow.ListenerInfo info = this.server.getListenerInfo().get(0);
|
||||
setPort(((InetSocketAddress) info.getAddress()).getPort());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -52,8 +56,7 @@ public class UndertowHttpServer extends AbstractHttpServer {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void reset() {
|
||||
super.reset();
|
||||
protected void resetInternal() {
|
||||
this.server = null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user