Skip to content

Commit d090fa5

Browse files
committed
Use unique ports per test worker (#43983)
* Use unique ports per test worker * Add test for system property * check presence of tests.gradle * Revert "check presence of tests.gradle" This reverts commit 2fee751.
1 parent 06df0c0 commit d090fa5

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,15 @@ public static void resetPortCounter() {
201201
portGenerator.set(0);
202202
}
203203

204+
// Allows distinguishing between parallel test processes
205+
public static final int TEST_WORKER_VM;
206+
207+
protected static final String TEST_WORKER_SYS_PROPERTY = "org.gradle.test.worker";
208+
204209
static {
210+
// org.gradle.test.worker starts counting at 1, but we want to start counting at 0 here
211+
// in case system property is not defined (e.g. when running test from IDE), just use 0
212+
TEST_WORKER_VM = RandomizedTest.systemPropertyAsInt(TEST_WORKER_SYS_PROPERTY, 1) - 1;
205213
setTestSysProps();
206214
LogConfigurator.loadLog4jPlugins();
207215

test/framework/src/main/java/org/elasticsearch/test/InternalTestCluster.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
2424
import com.carrotsearch.randomizedtesting.RandomizedTest;
2525
import com.carrotsearch.randomizedtesting.SeedUtils;
26-
import com.carrotsearch.randomizedtesting.SysGlobals;
2726
import com.carrotsearch.randomizedtesting.generators.RandomNumbers;
2827
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
2928
import com.carrotsearch.randomizedtesting.generators.RandomStrings;
@@ -526,8 +525,7 @@ private static Settings getRandomNodeSettings(long seed) {
526525

527526
public static String clusterName(String prefix, long clusterSeed) {
528527
StringBuilder builder = new StringBuilder(prefix);
529-
final int childVM = RandomizedTest.systemPropertyAsInt(SysGlobals.CHILDVM_SYSPROP_JVM_ID, 0);
530-
builder.append("-CHILD_VM=[").append(childVM).append(']');
528+
builder.append("-TEST_WORKER_VM=[").append(ESTestCase.TEST_WORKER_VM).append(']');
531529
builder.append("-CLUSTER_SEED=[").append(clusterSeed).append(']');
532530
// if multiple maven task run on a single host we better have an identifier that doesn't rely on input params
533531
builder.append("-HASH=[").append(SeedUtils.formatSeed(System.nanoTime())).append(']');

test/framework/src/main/java/org/elasticsearch/test/transport/MockTransportService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.test.transport;
2121

22-
import com.carrotsearch.randomizedtesting.SysGlobals;
2322
import org.apache.logging.log4j.LogManager;
2423
import org.apache.logging.log4j.Logger;
2524
import org.elasticsearch.Version;
@@ -46,6 +45,7 @@
4645
import org.elasticsearch.node.Node;
4746
import org.elasticsearch.plugins.Plugin;
4847
import org.elasticsearch.tasks.TaskManager;
48+
import org.elasticsearch.test.ESTestCase;
4949
import org.elasticsearch.test.tasks.MockTaskManager;
5050
import org.elasticsearch.threadpool.ThreadPool;
5151
import org.elasticsearch.transport.ConnectTransportException;
@@ -92,7 +92,6 @@ public final class MockTransportService extends TransportService {
9292
private static final Logger logger = LogManager.getLogger(MockTransportService.class);
9393

9494
private final Map<DiscoveryNode, List<Transport.Connection>> openConnections = new HashMap<>();
95-
private static final int JVM_ORDINAL = Integer.parseInt(System.getProperty(SysGlobals.CHILDVM_SYSPROP_JVM_ID, "0"));
9695

9796
public static class TestPlugin extends Plugin {
9897
@Override
@@ -112,7 +111,8 @@ public static MockNioTransport newMockTransport(Settings settings, Version versi
112111
// concurrent tests could claim port that another JVM just released and if that test tries to simulate a disconnect it might
113112
// be smart enough to re-connect depending on what is tested. To reduce the risk, since this is very hard to debug we use
114113
// a different default port range per JVM unless the incoming settings override it
115-
int basePort = 10300 + (JVM_ORDINAL * 100); // use a non-default port otherwise some cluster in this JVM might reuse a port
114+
// use a non-default base port otherwise some cluster in this JVM might reuse a port
115+
int basePort = 10300 + (ESTestCase.TEST_WORKER_VM * 100);
116116
settings = Settings.builder().put(TransportSettings.PORT.getKey(), basePort + "-" + (basePort + 100)).put(settings).build();
117117
NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(ClusterModule.getNamedWriteables());
118118
return new MockNioTransport(settings, version, threadPool, new NetworkService(Collections.emptyList()),

test/framework/src/test/java/org/elasticsearch/test/test/ESTestCaseTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.test.test;
2121

22+
import com.carrotsearch.randomizedtesting.RandomizedTest;
2223
import junit.framework.AssertionFailedError;
2324

2425
import org.elasticsearch.common.bytes.BytesReference;
@@ -181,4 +182,11 @@ public void testRandomValueOtherThan() {
181182
Supplier<Object> usuallyNull = () -> usually() ? null : randomInt();
182183
assertNotNull(randomValueOtherThan(null, usuallyNull));
183184
}
185+
186+
public void testWorkerSystemProperty() {
187+
assumeTrue("requires running tests with Gradle", System.getProperty("tests.gradle") != null);
188+
// org.gradle.test.worker starts counting at 1
189+
assertThat(RandomizedTest.systemPropertyAsInt(TEST_WORKER_SYS_PROPERTY, -1), greaterThan(0));
190+
assertEquals(RandomizedTest.systemPropertyAsInt(TEST_WORKER_SYS_PROPERTY, -1) - 1, TEST_WORKER_VM);
191+
}
184192
}

0 commit comments

Comments
 (0)