From 8c129ae7530a39aff7b43ed708efc81b18c3b3cc Mon Sep 17 00:00:00 2001 From: Tim Brooks Date: Tue, 20 Dec 2016 13:17:58 -0600 Subject: [PATCH 1/8] Add mocksocket jar. And replace ServerSocket instances with MockServerSocket instances. --- buildSrc/version.properties | 1 + test/framework/build.gradle | 1 + .../ClusterDiscoveryConfiguration.java | 3 ++- .../AbstractSimpleTransportTestCase.java | 18 ++++++++++-------- .../transport/MockTcpTransport.java | 3 ++- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/buildSrc/version.properties b/buildSrc/version.properties index 44835f7227c1a..d1a9302e3e9b3 100644 --- a/buildSrc/version.properties +++ b/buildSrc/version.properties @@ -20,5 +20,6 @@ commonslogging = 1.1.3 commonscodec = 1.10 hamcrest = 1.3 securemock = 1.2 +mocksocket = 1.0 # benchmark dependencies jmh = 1.17.3 diff --git a/test/framework/build.gradle b/test/framework/build.gradle index 8be06574df8ab..6756495e0a16c 100644 --- a/test/framework/build.gradle +++ b/test/framework/build.gradle @@ -32,6 +32,7 @@ dependencies { compile "commons-logging:commons-logging:${versions.commonslogging}" compile "commons-codec:commons-codec:${versions.commonscodec}" compile "org.elasticsearch:securemock:${versions.securemock}" + compile "org.elasticsearch:mocksocket:${versions.mocksocket}" } compileJava.options.compilerArgs << '-Xlint:-cast,-rawtypes,-try,-unchecked' diff --git a/test/framework/src/main/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java b/test/framework/src/main/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java index 3fd2b024a1d9b..ba14dcd4c1463 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java +++ b/test/framework/src/main/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java @@ -20,6 +20,7 @@ import com.carrotsearch.randomizedtesting.RandomizedTest; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.MockServerSocket; import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.network.NetworkModule; import org.elasticsearch.common.network.NetworkUtils; @@ -136,7 +137,7 @@ protected static synchronized int[] unicastHostPorts(int numHosts) { for (int i = 0; i < unicastHostPorts.length; i++) { boolean foundPortInRange = false; while (tries < InternalTestCluster.PORTS_PER_JVM && !foundPortInRange) { - try (ServerSocket serverSocket = new ServerSocket()) { + try (ServerSocket serverSocket = new MockServerSocket()) { // Set SO_REUSEADDR as we may bind here and not be able to reuse the address immediately without it. serverSocket.setReuseAddress(NetworkUtils.defaultReuseAddress()); serverSocket.bind(new InetSocketAddress(IP_ADDR, nextPort)); diff --git a/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java b/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java index 8852b386ffdc4..bcacace09e6c1 100644 --- a/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java @@ -25,6 +25,7 @@ import org.apache.lucene.util.Constants; import org.apache.lucene.util.IOUtils; import org.elasticsearch.ExceptionsHelper; +import org.elasticsearch.MockServerSocket; import org.elasticsearch.Version; import org.elasticsearch.action.ActionListenerResponseHandler; import org.elasticsearch.action.support.PlainActionFuture; @@ -42,6 +43,7 @@ import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; +import org.elasticsearch.mocksocket.MockServerSocket; import org.elasticsearch.node.Node; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.junit.annotations.TestLogging; @@ -1354,7 +1356,7 @@ public void handleException(TransportException exp) { // all is well } - try (Transport.Connection connection = serviceB.openConnection(nodeA, MockTcpTransport.LIGHT_PROFILE)){ + try (Transport.Connection connection = serviceB.openConnection(nodeA, ConnectionProfile.LIGHT_PROFILE)) { serviceB.handshake(connection, 100); fail("exception should be thrown"); } catch (IllegalStateException e) { @@ -1412,7 +1414,7 @@ public void handleException(TransportException exp) { // all is well } - try (Transport.Connection connection = serviceB.openConnection(nodeA, MockTcpTransport.LIGHT_PROFILE)){ + try (Transport.Connection connection = serviceB.openConnection(nodeA, ConnectionProfile.LIGHT_PROFILE)) { serviceB.handshake(connection, 100); fail("exception should be thrown"); } catch (IllegalStateException e) { @@ -1766,7 +1768,7 @@ public void testRegisterHandlerTwice() { public void testTimeoutPerConnection() throws IOException { assumeTrue("Works only on BSD network stacks and apparently windows", Constants.MAC_OS_X || Constants.FREE_BSD || Constants.WINDOWS); - try (ServerSocket socket = new ServerSocket()) { + try (ServerSocket socket = new MockServerSocket()) { // note - this test uses backlog=1 which is implementation specific ie. it might not work on some TCP/IP stacks // on linux (at least newer ones) the listen(addr, backlog=1) should just ignore new connections if the queue is full which // means that once we received an ACK from the client we just drop the packet on the floor (which is what we want) and we run @@ -1811,7 +1813,7 @@ public void testTcpHandshake() throws IOException, InterruptedException { NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Collections.emptyList()); try (MockTcpTransport transport = new MockTcpTransport(Settings.EMPTY, threadPool, BigArrays.NON_RECYCLING_INSTANCE, - new NoneCircuitBreakerService(), namedWriteableRegistry, new NetworkService(Settings.EMPTY, Collections.emptyList())){ + new NoneCircuitBreakerService(), namedWriteableRegistry, new NetworkService(Settings.EMPTY, Collections.emptyList())) { @Override protected String handleRequest(MockChannel mockChannel, String profileName, StreamInput stream, long requestId, int messageLengthBytes, Version version, InetSocketAddress remoteAddress, byte status) @@ -1842,7 +1844,7 @@ protected String handleRequest(MockChannel mockChannel, String profileName, Stre } public void testTcpHandshakeTimeout() throws IOException { - try (ServerSocket socket = new ServerSocket()) { + try (ServerSocket socket = new MockServerSocket()) { socket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0), 1); socket.setReuseAddress(true); DiscoveryNode dummy = new DiscoveryNode("TEST", new TransportAddress(socket.getInetAddress(), @@ -1858,12 +1860,12 @@ public void testTcpHandshakeTimeout() throws IOException { builder.setHandshakeTimeout(TimeValue.timeValueMillis(1)); ConnectTransportException ex = expectThrows(ConnectTransportException.class, () -> serviceA.connectToNode(dummy, builder.build())); - assertEquals("[][" + dummy.getAddress() +"] handshake_timeout[1ms]", ex.getMessage()); + assertEquals("[][" + dummy.getAddress() + "] handshake_timeout[1ms]", ex.getMessage()); } } public void testTcpHandshakeConnectionReset() throws IOException, InterruptedException { - try (ServerSocket socket = new ServerSocket()) { + try (ServerSocket socket = new MockServerSocket()) { socket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0), 1); socket.setReuseAddress(true); DiscoveryNode dummy = new DiscoveryNode("TEST", new TransportAddress(socket.getInetAddress(), @@ -1892,7 +1894,7 @@ public void run() { builder.setHandshakeTimeout(TimeValue.timeValueHours(1)); ConnectTransportException ex = expectThrows(ConnectTransportException.class, () -> serviceA.connectToNode(dummy, builder.build())); - assertEquals(ex.getMessage(), "[][" + dummy.getAddress() +"] general node connection failure"); + assertEquals(ex.getMessage(), "[][" + dummy.getAddress() + "] general node connection failure"); assertThat(ex.getCause().getMessage(), startsWith("handshake failed")); t.join(); } diff --git a/test/framework/src/main/java/org/elasticsearch/transport/MockTcpTransport.java b/test/framework/src/main/java/org/elasticsearch/transport/MockTcpTransport.java index a2d5f10483fea..074c5bb9a27c8 100644 --- a/test/framework/src/main/java/org/elasticsearch/transport/MockTcpTransport.java +++ b/test/framework/src/main/java/org/elasticsearch/transport/MockTcpTransport.java @@ -19,6 +19,7 @@ package org.elasticsearch.transport; import org.apache.lucene.util.IOUtils; +import org.elasticsearch.MockServerSocket; import org.elasticsearch.Version; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.bytes.BytesReference; @@ -110,7 +111,7 @@ protected InetSocketAddress getLocalAddress(MockChannel mockChannel) { @Override protected MockChannel bind(final String name, InetSocketAddress address) throws IOException { - ServerSocket socket = new ServerSocket(); + MockServerSocket socket = new MockServerSocket(); socket.bind(address); socket.setReuseAddress(TCP_REUSE_ADDRESS.get(settings)); ByteSizeValue tcpReceiveBufferSize = TCP_RECEIVE_BUFFER_SIZE.get(settings); From 76b748b4de53010f9ab0c4a9a4147e4fd4b5cd78 Mon Sep 17 00:00:00 2001 From: Tim Brooks Date: Tue, 20 Dec 2016 13:58:11 -0600 Subject: [PATCH 2/8] Replace usage of raw sockets with mocksockets in test classes --- .../transport/netty4/Netty4SizeHeaderFrameDecoderTests.java | 5 +++-- .../org/elasticsearch/plugin/example/ExampleExternalIT.java | 3 ++- .../java/org/elasticsearch/transport/MockTcpTransport.java | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4SizeHeaderFrameDecoderTests.java b/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4SizeHeaderFrameDecoderTests.java index 0e9ebe5f7f46a..7b02215e1abf6 100644 --- a/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4SizeHeaderFrameDecoderTests.java +++ b/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4SizeHeaderFrameDecoderTests.java @@ -19,6 +19,7 @@ package org.elasticsearch.transport.netty4; +import org.elasticsearch.MockSocket; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.network.NetworkService; import org.elasticsearch.common.settings.Settings; @@ -84,7 +85,7 @@ public void testThatTextMessageIsReturnedOnHTTPLikeRequest() throws Exception { String randomMethod = randomFrom("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH"); String data = randomMethod + " / HTTP/1.1"; - try (Socket socket = new Socket(host, port)) { + try (Socket socket = new MockSocket(host, port)) { socket.getOutputStream().write(data.getBytes(StandardCharsets.UTF_8)); socket.getOutputStream().flush(); @@ -95,7 +96,7 @@ public void testThatTextMessageIsReturnedOnHTTPLikeRequest() throws Exception { } public void testThatNothingIsReturnedForOtherInvalidPackets() throws Exception { - try (Socket socket = new Socket(host, port)) { + try (Socket socket = new MockSocket(host, port)) { socket.getOutputStream().write("FOOBAR".getBytes(StandardCharsets.UTF_8)); socket.getOutputStream().flush(); diff --git a/plugins/jvm-example/src/test/java/org/elasticsearch/plugin/example/ExampleExternalIT.java b/plugins/jvm-example/src/test/java/org/elasticsearch/plugin/example/ExampleExternalIT.java index 1f48549aad4ec..7752671678c7d 100644 --- a/plugins/jvm-example/src/test/java/org/elasticsearch/plugin/example/ExampleExternalIT.java +++ b/plugins/jvm-example/src/test/java/org/elasticsearch/plugin/example/ExampleExternalIT.java @@ -19,6 +19,7 @@ package org.elasticsearch.plugin.example; +import org.elasticsearch.MockSocket; import org.elasticsearch.test.ESTestCase; import java.io.BufferedReader; @@ -34,7 +35,7 @@ public void testExample() throws Exception { String stringAddress = Objects.requireNonNull(System.getProperty("external.address")); URL url = new URL("http://" + stringAddress); InetAddress address = InetAddress.getByName(url.getHost()); - try (Socket socket = new Socket(address, url.getPort()); + try (Socket socket = new MockSocket(address, url.getPort()); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8))) { assertEquals("TEST", reader.readLine()); } diff --git a/test/framework/src/main/java/org/elasticsearch/transport/MockTcpTransport.java b/test/framework/src/main/java/org/elasticsearch/transport/MockTcpTransport.java index 074c5bb9a27c8..e2480826fe68d 100644 --- a/test/framework/src/main/java/org/elasticsearch/transport/MockTcpTransport.java +++ b/test/framework/src/main/java/org/elasticsearch/transport/MockTcpTransport.java @@ -20,6 +20,7 @@ import org.apache.lucene.util.IOUtils; import org.elasticsearch.MockServerSocket; +import org.elasticsearch.MockSocket; import org.elasticsearch.Version; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.bytes.BytesReference; @@ -179,7 +180,7 @@ protected NodeChannels connectToChannels(DiscoveryNode node, ConnectionProfile p final MockChannel[] mockChannels = new MockChannel[1]; final NodeChannels nodeChannels = new NodeChannels(node, mockChannels, LIGHT_PROFILE); // we always use light here boolean success = false; - final Socket socket = new Socket(); + final MockSocket socket = new MockSocket(); try { Consumer onClose = (channel) -> { final NodeChannels connected = connectedNodes.get(node); From 30fbd9dc2347fab7fc7f8f9590f1d131941307d8 Mon Sep 17 00:00:00 2001 From: Tim Brooks Date: Tue, 27 Dec 2016 12:57:44 -0600 Subject: [PATCH 3/8] Update mocksocket jar that moves classes inside of a mocksocket package. --- .../transport/netty4/Netty4SizeHeaderFrameDecoderTests.java | 2 +- .../org/elasticsearch/plugin/example/ExampleExternalIT.java | 2 +- .../java/org/elasticsearch/transport/MockTcpTransport.java | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4SizeHeaderFrameDecoderTests.java b/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4SizeHeaderFrameDecoderTests.java index 7b02215e1abf6..15dea8fe87124 100644 --- a/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4SizeHeaderFrameDecoderTests.java +++ b/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4SizeHeaderFrameDecoderTests.java @@ -19,7 +19,6 @@ package org.elasticsearch.transport.netty4; -import org.elasticsearch.MockSocket; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.network.NetworkService; import org.elasticsearch.common.settings.Settings; @@ -27,6 +26,7 @@ import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.MockBigArrays; import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; +import org.elasticsearch.mocksocket.MockSocket; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportSettings; diff --git a/plugins/jvm-example/src/test/java/org/elasticsearch/plugin/example/ExampleExternalIT.java b/plugins/jvm-example/src/test/java/org/elasticsearch/plugin/example/ExampleExternalIT.java index 7752671678c7d..3ed616cb3af90 100644 --- a/plugins/jvm-example/src/test/java/org/elasticsearch/plugin/example/ExampleExternalIT.java +++ b/plugins/jvm-example/src/test/java/org/elasticsearch/plugin/example/ExampleExternalIT.java @@ -19,7 +19,7 @@ package org.elasticsearch.plugin.example; -import org.elasticsearch.MockSocket; +import org.elasticsearch.mocksocket.MockSocket; import org.elasticsearch.test.ESTestCase; import java.io.BufferedReader; diff --git a/test/framework/src/main/java/org/elasticsearch/transport/MockTcpTransport.java b/test/framework/src/main/java/org/elasticsearch/transport/MockTcpTransport.java index e2480826fe68d..3b5b430f60650 100644 --- a/test/framework/src/main/java/org/elasticsearch/transport/MockTcpTransport.java +++ b/test/framework/src/main/java/org/elasticsearch/transport/MockTcpTransport.java @@ -19,8 +19,6 @@ package org.elasticsearch.transport; import org.apache.lucene.util.IOUtils; -import org.elasticsearch.MockServerSocket; -import org.elasticsearch.MockSocket; import org.elasticsearch.Version; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.bytes.BytesReference; @@ -37,6 +35,8 @@ import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.indices.breaker.CircuitBreakerService; +import org.elasticsearch.mocksocket.MockServerSocket; +import org.elasticsearch.mocksocket.MockSocket; import org.elasticsearch.threadpool.ThreadPool; import java.io.BufferedInputStream; From 12089b17a24bc2db8b9d02919dc8cca612c1b053 Mon Sep 17 00:00:00 2001 From: Tim Brooks Date: Tue, 27 Dec 2016 13:05:59 -0600 Subject: [PATCH 4/8] Fix compile issue. --- .../transport/AbstractSimpleTransportTestCase.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java b/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java index bcacace09e6c1..aa97f38b7f115 100644 --- a/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java @@ -1356,7 +1356,7 @@ public void handleException(TransportException exp) { // all is well } - try (Transport.Connection connection = serviceB.openConnection(nodeA, ConnectionProfile.LIGHT_PROFILE)) { + try (Transport.Connection connection = serviceB.openConnection(nodeA, MockTcpTransport.LIGHT_PROFILE)) { serviceB.handshake(connection, 100); fail("exception should be thrown"); } catch (IllegalStateException e) { @@ -1414,7 +1414,7 @@ public void handleException(TransportException exp) { // all is well } - try (Transport.Connection connection = serviceB.openConnection(nodeA, ConnectionProfile.LIGHT_PROFILE)) { + try (Transport.Connection connection = serviceB.openConnection(nodeA, MockTcpTransport.LIGHT_PROFILE)) { serviceB.handshake(connection, 100); fail("exception should be thrown"); } catch (IllegalStateException e) { From 7eaec702dabcc5a4ffdc09720fdb660326864624 Mon Sep 17 00:00:00 2001 From: Tim Brooks Date: Tue, 27 Dec 2016 13:06:58 -0600 Subject: [PATCH 5/8] Remove broken import --- .../elasticsearch/transport/AbstractSimpleTransportTestCase.java | 1 - 1 file changed, 1 deletion(-) diff --git a/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java b/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java index aa97f38b7f115..d462e2b571918 100644 --- a/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java @@ -25,7 +25,6 @@ import org.apache.lucene.util.Constants; import org.apache.lucene.util.IOUtils; import org.elasticsearch.ExceptionsHelper; -import org.elasticsearch.MockServerSocket; import org.elasticsearch.Version; import org.elasticsearch.action.ActionListenerResponseHandler; import org.elasticsearch.action.support.PlainActionFuture; From 7d277b26844e85c19b872b323a2070f673543560 Mon Sep 17 00:00:00 2001 From: Tim Brooks Date: Tue, 27 Dec 2016 16:02:14 -0600 Subject: [PATCH 6/8] Replace Http and Https server usages with MockHttpServer --- .../azure/classic/AzureDiscoveryClusterFormationTests.java | 3 ++- .../discovery/ec2/Ec2DiscoveryClusterFormationTests.java | 3 ++- .../org/elasticsearch/discovery/gce/GceDiscoverTests.java | 5 +++-- .../test/discovery/ClusterDiscoveryConfiguration.java | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/discovery/azure/classic/AzureDiscoveryClusterFormationTests.java b/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/discovery/azure/classic/AzureDiscoveryClusterFormationTests.java index 7f977592e8d72..9787dc136e3b7 100644 --- a/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/discovery/azure/classic/AzureDiscoveryClusterFormationTests.java +++ b/plugins/discovery-azure-classic/src/test/java/org/elasticsearch/discovery/azure/classic/AzureDiscoveryClusterFormationTests.java @@ -32,6 +32,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.discovery.DiscoveryModule; import org.elasticsearch.env.Environment; +import org.elasticsearch.mocksocket.MockHttpServer; import org.elasticsearch.node.Node; import org.elasticsearch.plugin.discovery.azure.classic.AzureDiscoveryPlugin; import org.elasticsearch.plugins.Plugin; @@ -131,7 +132,7 @@ protected Settings nodeSettings(int nodeOrdinal) { public static void startHttpd() throws Exception { logDir = createTempDir(); SSLContext sslContext = getSSLContext(); - httpsServer = HttpsServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0); + httpsServer = MockHttpServer.createHttps(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0); httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); httpsServer.createContext("/subscription/services/hostedservices/myservice", (s) -> { Headers headers = s.getResponseHeaders(); diff --git a/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryClusterFormationTests.java b/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryClusterFormationTests.java index b4a1f55a3c671..4def3737563d6 100644 --- a/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryClusterFormationTests.java +++ b/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryClusterFormationTests.java @@ -29,6 +29,7 @@ import org.elasticsearch.common.io.FileSystemUtils; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.mocksocket.MockHttpServer; import org.elasticsearch.plugin.discovery.ec2.Ec2DiscoveryPlugin; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.ESIntegTestCase; @@ -96,7 +97,7 @@ protected Settings nodeSettings(int nodeOrdinal) { @BeforeClass public static void startHttpd() throws Exception { logDir = createTempDir(); - httpServer = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0); + httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0); httpServer.createContext("/", (s) -> { Headers headers = s.getResponseHeaders(); diff --git a/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceDiscoverTests.java b/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceDiscoverTests.java index 76d7c6408d55d..fc0234671f07a 100644 --- a/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceDiscoverTests.java +++ b/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceDiscoverTests.java @@ -31,6 +31,7 @@ import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.mocksocket.MockHttpServer; import org.elasticsearch.plugin.discovery.gce.GceDiscoveryPlugin; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.ESIntegTestCase; @@ -108,8 +109,8 @@ protected Settings nodeSettings(int nodeOrdinal) { public static void startHttpd() throws Exception { logDir = createTempDir(); SSLContext sslContext = getSSLContext(); - httpsServer = HttpsServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0); - httpServer = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0); + httpsServer = MockHttpServer.createHttps(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0); + httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0); httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); httpServer.createContext("/computeMetadata/v1/instance/service-accounts/default/token", (s) -> { String response = GceMockUtils.readGoogleInternalJsonResponse( diff --git a/test/framework/src/main/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java b/test/framework/src/main/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java index ba14dcd4c1463..f4be0d0d529e3 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java +++ b/test/framework/src/main/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java @@ -20,13 +20,13 @@ import com.carrotsearch.randomizedtesting.RandomizedTest; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.MockServerSocket; import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.network.NetworkModule; import org.elasticsearch.common.network.NetworkUtils; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.env.NodeEnvironment; +import org.elasticsearch.mocksocket.MockServerSocket; import org.elasticsearch.test.InternalTestCluster; import org.elasticsearch.test.NodeConfigurationSource; import org.elasticsearch.transport.TransportSettings; From 5c3a46a58394c79d8cc762218b2f748edfa5488e Mon Sep 17 00:00:00 2001 From: Tim Brooks Date: Tue, 27 Dec 2016 17:39:39 -0600 Subject: [PATCH 7/8] Modify the sniffer and rest packages to use mocksocket --- client/rest/build.gradle | 1 + .../client/RestClientMultipleHostsIntegTests.java | 3 ++- .../elasticsearch/client/RestClientSingleHostIntegTests.java | 3 ++- client/sniffer/build.gradle | 1 + .../client/sniff/ElasticsearchHostsSnifferTests.java | 3 ++- 5 files changed, 8 insertions(+), 3 deletions(-) diff --git a/client/rest/build.gradle b/client/rest/build.gradle index 1c92013da9743..67f8426fb5faa 100644 --- a/client/rest/build.gradle +++ b/client/rest/build.gradle @@ -43,6 +43,7 @@ dependencies { testCompile "junit:junit:${versions.junit}" testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}" testCompile "org.elasticsearch:securemock:${versions.securemock}" + testCompile "org.elasticsearch:mocksocket:${versions.mocksocket}" testCompile "org.codehaus.mojo:animal-sniffer-annotations:1.15" signature "org.codehaus.mojo.signature:java17:1.0@signature" } diff --git a/client/rest/src/test/java/org/elasticsearch/client/RestClientMultipleHostsIntegTests.java b/client/rest/src/test/java/org/elasticsearch/client/RestClientMultipleHostsIntegTests.java index f997f798712e6..da5a960c2e84c 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RestClientMultipleHostsIntegTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RestClientMultipleHostsIntegTests.java @@ -24,6 +24,7 @@ import com.sun.net.httpserver.HttpServer; import org.apache.http.HttpHost; import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement; +import org.elasticsearch.mocksocket.MockHttpServer; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -80,7 +81,7 @@ public static void startHttpServer() throws Exception { } private static HttpServer createHttpServer() throws Exception { - HttpServer httpServer = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); + HttpServer httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); httpServer.start(); //returns a different status code depending on the path for (int statusCode : getAllStatusCodes()) { diff --git a/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostIntegTests.java b/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostIntegTests.java index 4440c1e8f9795..2f94de7154cd6 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostIntegTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostIntegTests.java @@ -29,6 +29,7 @@ import org.apache.http.entity.StringEntity; import org.apache.http.util.EntityUtils; import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement; +import org.elasticsearch.mocksocket.MockHttpServer; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -87,7 +88,7 @@ public static void startHttpServer() throws Exception { } private static HttpServer createHttpServer() throws Exception { - HttpServer httpServer = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); + HttpServer httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); httpServer.start(); //returns a different status code depending on the path for (int statusCode : getAllStatusCodes()) { diff --git a/client/sniffer/build.gradle b/client/sniffer/build.gradle index f35110e4f9e61..5542792835bbd 100644 --- a/client/sniffer/build.gradle +++ b/client/sniffer/build.gradle @@ -43,6 +43,7 @@ dependencies { testCompile "junit:junit:${versions.junit}" testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}" testCompile "org.elasticsearch:securemock:${versions.securemock}" + testCompile "org.elasticsearch:mocksocket:${versions.mocksocket}" testCompile "org.codehaus.mojo:animal-sniffer-annotations:1.15" signature "org.codehaus.mojo.signature:java17:1.0@signature" } diff --git a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/ElasticsearchHostsSnifferTests.java b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/ElasticsearchHostsSnifferTests.java index aeb0620134b55..5221b205dd4f3 100644 --- a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/ElasticsearchHostsSnifferTests.java +++ b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/ElasticsearchHostsSnifferTests.java @@ -35,6 +35,7 @@ import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientTestCase; +import org.elasticsearch.mocksocket.MockHttpServer; import org.junit.After; import org.junit.Before; @@ -141,7 +142,7 @@ public void testSniffNodes() throws IOException { } private static HttpServer createHttpServer(final SniffResponse sniffResponse, final int sniffTimeoutMillis) throws IOException { - HttpServer httpServer = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); + HttpServer httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); httpServer.createContext("/_nodes/http", new ResponseHandler(sniffTimeoutMillis, sniffResponse)); return httpServer; } From 3c32279864e7102b0a658735eb8d369caf51c64c Mon Sep 17 00:00:00 2001 From: Tim Brooks Date: Mon, 2 Jan 2017 11:02:22 -0600 Subject: [PATCH 8/8] Update mocksocket to next version --- buildSrc/version.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buildSrc/version.properties b/buildSrc/version.properties index d1a9302e3e9b3..fb78b98d59b4d 100644 --- a/buildSrc/version.properties +++ b/buildSrc/version.properties @@ -20,6 +20,6 @@ commonslogging = 1.1.3 commonscodec = 1.10 hamcrest = 1.3 securemock = 1.2 -mocksocket = 1.0 +mocksocket = 1.1 # benchmark dependencies jmh = 1.17.3