Skip to content

Commit c927899

Browse files
committed
Populate RandomizedContext for virtual thread group
1 parent a38c29f commit c927899

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
*/
99
package org.elasticsearch.test;
1010

11+
import com.carrotsearch.randomizedtesting.RandomizedContext;
12+
import com.carrotsearch.randomizedtesting.RandomizedRunner;
1113
import com.carrotsearch.randomizedtesting.RandomizedTest;
1214
import com.carrotsearch.randomizedtesting.annotations.Listeners;
1315
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters;
@@ -171,6 +173,7 @@
171173
import java.lang.annotation.RetentionPolicy;
172174
import java.lang.annotation.Target;
173175
import java.lang.invoke.MethodHandles;
176+
import java.lang.reflect.Method;
174177
import java.math.BigInteger;
175178
import java.net.InetAddress;
176179
import java.net.URI;
@@ -433,6 +436,29 @@ private static void forceImmutableCollectionsSeed(long seed) {
433436
}
434437
}
435438

439+
/**
440+
* This is an awful hack to work around the fact the virtual threads' thread group
441+
* doesn't have a {@link RandomizedContext}. There's probably a way to do this
442+
* that isn't a nasty hack, but that's a job for another day.
443+
*/
444+
@Before
445+
public void fudgeRandomContextForVirtualThreads() {
446+
final RandomizedContext current = RandomizedContext.current();
447+
try {
448+
Method create = RandomizedContext.class.getDeclaredMethod("create", ThreadGroup.class, Class.class, RandomizedRunner.class);
449+
create.setAccessible(true);
450+
Thread.ofVirtual().start(() -> {
451+
try {
452+
create.invoke(null, Thread.currentThread().getThreadGroup(), current.getTargetClass(), current.getRunner());
453+
} catch (Exception e) {
454+
fail(e, "Hack didn't work");
455+
}
456+
});
457+
} catch (NoSuchMethodException e) {
458+
fail(e, "Hack didn't work");
459+
}
460+
}
461+
436462
@SuppressForbidden(reason = "force log4j and netty sysprops")
437463
private static void setTestSysProps(Random random) {
438464
System.setProperty("log4j.shutdownHookEnabled", "false");

0 commit comments

Comments
 (0)