From 38a1df7da1da889cda857e75a2e652fd8cc2b278 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Thu, 6 Jul 2017 09:14:52 +0200 Subject: [PATCH] Use a port range per JVM in MockTransportService (#25565) Some tests use MockTransportService to do network based testing. Yet, we run tests in multiple JVMs that means concurrent tests could claim port that another JVM just released and if that test tries to simulate a disconnect it might be smart enough to re-connect depending on what is tested. To reduce the risk, since this is very hard to debug we use a different default port range per JVM unless the incoming settings overriding it. Closes #25301 --- .../test/transport/MockTransportService.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/framework/src/main/java/org/elasticsearch/test/transport/MockTransportService.java b/test/framework/src/main/java/org/elasticsearch/test/transport/MockTransportService.java index a885e69ee44..b7245df1457 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/transport/MockTransportService.java +++ b/test/framework/src/main/java/org/elasticsearch/test/transport/MockTransportService.java @@ -19,6 +19,7 @@ package org.elasticsearch.test.transport; +import com.carrotsearch.randomizedtesting.SysGlobals; import org.elasticsearch.Version; import org.elasticsearch.cluster.ClusterModule; import org.elasticsearch.cluster.node.DiscoveryNode; @@ -43,6 +44,7 @@ import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; import org.elasticsearch.node.Node; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.tasks.TaskManager; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.tasks.MockTaskManager; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.ConnectTransportException; @@ -89,6 +91,7 @@ import java.util.function.Function; public final class MockTransportService extends TransportService { private final Map> openConnections = new HashMap<>(); + private static final int JVM_ORDINAL = Integer.parseInt(System.getProperty(SysGlobals.CHILDVM_SYSPROP_JVM_ID, "0")); public static class TestPlugin extends Plugin { @Override @@ -99,6 +102,12 @@ public final class MockTransportService extends TransportService { public static MockTransportService createNewService(Settings settings, Version version, ThreadPool threadPool, @Nullable ClusterSettings clusterSettings) { + // some tests use MockTransportService to do network based testing. Yet, we run tests in multiple JVMs that means + // concurrent tests could claim port that another JVM just released and if that test tries to simulate a disconnect it might + // be smart enough to re-connect depending on what is tested. To reduce the risk, since this is very hard to debug we use + // a different default port range per JVM unless the incoming settings override it + int basePort = 9300 + (JVM_ORDINAL * 100); + settings = Settings.builder().put(TcpTransport.PORT.getKey(), basePort + "-" + (basePort+100)).put(settings).build(); NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(ClusterModule.getNamedWriteables()); final Transport transport = new MockTcpTransport(settings, threadPool, BigArrays.NON_RECYCLING_INSTANCE, new NoneCircuitBreakerService(), namedWriteableRegistry, new NetworkService(Collections.emptyList()), version);