|
8 | 8 | */ |
9 | 9 | package org.elasticsearch.test; |
10 | 10 |
|
| 11 | +import com.carrotsearch.randomizedtesting.RandomizedContext; |
| 12 | +import com.carrotsearch.randomizedtesting.RandomizedRunner; |
11 | 13 | import com.carrotsearch.randomizedtesting.RandomizedTest; |
12 | 14 | import com.carrotsearch.randomizedtesting.annotations.Listeners; |
13 | 15 | import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters; |
|
171 | 173 | import java.lang.annotation.RetentionPolicy; |
172 | 174 | import java.lang.annotation.Target; |
173 | 175 | import java.lang.invoke.MethodHandles; |
| 176 | +import java.lang.reflect.Method; |
174 | 177 | import java.math.BigInteger; |
175 | 178 | import java.net.InetAddress; |
176 | 179 | import java.net.URI; |
@@ -433,6 +436,29 @@ private static void forceImmutableCollectionsSeed(long seed) { |
433 | 436 | } |
434 | 437 | } |
435 | 438 |
|
| 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 | + |
436 | 462 | @SuppressForbidden(reason = "force log4j and netty sysprops") |
437 | 463 | private static void setTestSysProps(Random random) { |
438 | 464 | System.setProperty("log4j.shutdownHookEnabled", "false"); |
|
0 commit comments