From e94117c5c54d44690978496aa294b8e38413b8b0 Mon Sep 17 00:00:00 2001 From: Adrian Sutton Date: Wed, 8 May 2019 15:12:40 +1000 Subject: [PATCH 1/2] Use port 0 when starting a websocket server in tests. --- .../ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java | 3 +++ .../jsonrpc/websocket/WebSocketHostWhitelistTest.java | 9 +++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java index 8f08302be7..db8c4f9b19 100644 --- a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java +++ b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java @@ -228,6 +228,7 @@ private JsonRpcConfiguration createJsonRpcConfiguration() { private WebSocketConfiguration createWebSocketConfiguration() { final WebSocketConfiguration config = WebSocketConfiguration.createDefault(); config.setEnabled(true); + configuration.setPort(0); return config; } @@ -266,6 +267,8 @@ private JsonRpcHttpService createJsonRpcHttpService( final Set supportedCapabilities = new HashSet<>(); supportedCapabilities.add(EthProtocol.ETH62); supportedCapabilities.add(EthProtocol.ETH63); + jsonRpcConfiguration.setPort(0); + webSocketConfiguration.setPort(0); final Map rpcMethods = spy( diff --git a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/websocket/WebSocketHostWhitelistTest.java b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/websocket/WebSocketHostWhitelistTest.java index 8070e9f117..774f749bb5 100644 --- a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/websocket/WebSocketHostWhitelistTest.java +++ b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/websocket/WebSocketHostWhitelistTest.java @@ -20,6 +20,7 @@ import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.methods.WebSocketMethodsFactory; import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.SubscriptionManager; +import java.net.InetSocketAddress; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -56,9 +57,11 @@ public class WebSocketHostWhitelistTest { private WebSocketService websocketService; private HttpClient httpClient; private static final int VERTX_AWAIT_TIMEOUT_MILLIS = 10000; + private int websocketPort; @Before public void initServerAndClient() { + webSocketConfiguration.setPort(0); vertx = Vertx.vertx(); final Map websocketMethods = @@ -68,11 +71,13 @@ public void initServerAndClient() { websocketService = new WebSocketService(vertx, webSocketConfiguration, webSocketRequestHandlerSpy); websocketService.start().join(); + final InetSocketAddress inetSocketAddress = websocketService.socketAddress(); + websocketPort = inetSocketAddress.getPort(); final HttpClientOptions httpClientOptions = new HttpClientOptions() .setDefaultHost(webSocketConfiguration.getHost()) - .setDefaultPort(webSocketConfiguration.getPort()); + .setDefaultPort(websocketPort); httpClient = vertx.createHttpClient(httpClientOptions); } @@ -171,7 +176,7 @@ private void doHttpRequestAndVerify( final HttpClientRequest request = httpClient.post( - webSocketConfiguration.getPort(), + websocketPort, webSocketConfiguration.getHost(), "/", response -> { From 05d063e68f333b906a3f9bd0518101ee9d33a936 Mon Sep 17 00:00:00 2001 From: Adrian Sutton Date: Wed, 8 May 2019 15:15:01 +1000 Subject: [PATCH 2/2] Remove extra setPort(0). --- .../pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java index db8c4f9b19..929828f7bc 100644 --- a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java +++ b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java @@ -228,7 +228,6 @@ private JsonRpcConfiguration createJsonRpcConfiguration() { private WebSocketConfiguration createWebSocketConfiguration() { final WebSocketConfiguration config = WebSocketConfiguration.createDefault(); config.setEnabled(true); - configuration.setPort(0); return config; }