From 9956e7721d04cbbf54890928974b1086f055135d Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Thu, 13 Nov 2014 15:42:52 +0100 Subject: [PATCH] Tests: Improve netty test behaviour Based on some test failures, this commit fixes two minor things * Bind ports only on so called ephemeral ports to prevent try to bind to ports where elasticsearch already runs on * Remove @Network annotation as it was used in a wrong scope --- .../NettyTransportMultiPortIntegrationTests.java | 13 ++++--------- .../netty/NettyTransportMultiPortTests.java | 11 ++--------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/src/test/java/org/elasticsearch/transport/netty/NettyTransportMultiPortIntegrationTests.java b/src/test/java/org/elasticsearch/transport/netty/NettyTransportMultiPortIntegrationTests.java index 70a2e79fe48..4a9c1d20461 100644 --- a/src/test/java/org/elasticsearch/transport/netty/NettyTransportMultiPortIntegrationTests.java +++ b/src/test/java/org/elasticsearch/transport/netty/NettyTransportMultiPortIntegrationTests.java @@ -24,7 +24,6 @@ import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.test.ElasticsearchIntegrationTest; -import org.elasticsearch.test.junit.annotations.Network; import org.elasticsearch.transport.TransportModule; import org.junit.Test; @@ -35,10 +34,7 @@ import static org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope; import static org.elasticsearch.test.ElasticsearchIntegrationTest.Scope; import static org.hamcrest.Matchers.is; -/** - * - */ -@ClusterScope(scope = Scope.SUITE, numDataNodes = 1, enableRandomBenchNodes = false) +@ClusterScope(scope = Scope.SUITE, numDataNodes = 1, enableRandomBenchNodes = false, numClientNodes = 0) public class NettyTransportMultiPortIntegrationTests extends ElasticsearchIntegrationTest { private static int randomPort = -1; @@ -47,7 +43,7 @@ public class NettyTransportMultiPortIntegrationTests extends ElasticsearchIntegr @Override protected Settings nodeSettings(int nodeOrdinal) { if (randomPort == -1) { - randomPort = randomIntBetween(1025, 65000); + randomPort = randomIntBetween(49152, 65525); randomPortRange = String.format(Locale.ROOT, "%s-%s", randomPort, randomPort+10); } return settingsBuilder() @@ -61,14 +57,13 @@ public class NettyTransportMultiPortIntegrationTests extends ElasticsearchIntegr } @Test - @Network public void testThatTransportClientCanConnect() throws Exception { Settings settings = settingsBuilder() .put("cluster.name", internalCluster().getClusterName()) .put(TransportModule.TRANSPORT_TYPE_KEY, NettyTransport.class.getName()) .build(); - try (TransportClient transportClient = new TransportClient(settings)) { - transportClient.addTransportAddress(new InetSocketTransportAddress("localhost", randomPort)); + try (TransportClient transportClient = new TransportClient(settings, false)) { + transportClient.addTransportAddress(new InetSocketTransportAddress("127.0.0.1", randomPort)); ClusterHealthResponse response = transportClient.admin().cluster().prepareHealth().get(); assertThat(response.getStatus(), is(ClusterHealthStatus.GREEN)); } diff --git a/src/test/java/org/elasticsearch/transport/netty/NettyTransportMultiPortTests.java b/src/test/java/org/elasticsearch/transport/netty/NettyTransportMultiPortTests.java index dcf218fe460..f6ae551b195 100644 --- a/src/test/java/org/elasticsearch/transport/netty/NettyTransportMultiPortTests.java +++ b/src/test/java/org/elasticsearch/transport/netty/NettyTransportMultiPortTests.java @@ -31,7 +31,6 @@ import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; import org.elasticsearch.test.ElasticsearchTestCase; import org.elasticsearch.test.cache.recycler.MockBigArrays; -import org.elasticsearch.test.junit.annotations.Network; import org.elasticsearch.threadpool.ThreadPool; import org.junit.After; import org.junit.Test; @@ -47,9 +46,6 @@ import static org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope; import static org.elasticsearch.test.ElasticsearchIntegrationTest.Scope; import static org.hamcrest.Matchers.is; -/** - * - */ @ClusterScope(scope = Scope.TEST, numDataNodes = 1) public class NettyTransportMultiPortTests extends ElasticsearchTestCase { @@ -135,7 +131,6 @@ public class NettyTransportMultiPortTests extends ElasticsearchTestCase { } @Test - @Network public void testThatBindingOnDifferentHostsWorks() throws Exception { int[] ports = getRandomPorts(2); InetAddress firstNonLoopbackAddress = NetworkUtils.getFirstNonLoopbackAddress(NetworkUtils.StackType.IPv4); @@ -155,15 +150,13 @@ public class NettyTransportMultiPortTests extends ElasticsearchTestCase { assertConnectionRefused(ports[1]); } - // TODO, make sure one can check more settings and that they are applied correctly - private int[] getRandomPorts(int numberOfPorts) { IntOpenHashSet ports = new IntOpenHashSet(); for (int i = 0; i < numberOfPorts; i++) { - int port = randomIntBetween(1025, 65000); + int port = randomIntBetween(49152, 65535); while (ports.contains(port)) { - port = randomIntBetween(1025, 65000); + port = randomIntBetween(49152, 65535); } ports.add(port); }