From a37be87df8667278928ab88d21c6e765568ee7db Mon Sep 17 00:00:00 2001 From: bm1549 Date: Tue, 10 Mar 2026 15:43:06 -0400 Subject: [PATCH] Fix flaky nested virtual threads test by waiting for child spans The test was using a CountDownLatch that only ensured child threads had started executing, not that their @Trace spans had completed. Replace the latch with blockUntilChildSpansFinished(1) at each nesting level, matching the pattern used by other tests in this class. Co-Authored-By: Claude Opus 4.6 --- .../jdk21/VirtualThreadApiInstrumentationTest.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/dd-java-agent/instrumentation/java/java-lang/java-lang-21.0/src/test/java/testdog/trace/instrumentation/java/lang/jdk21/VirtualThreadApiInstrumentationTest.java b/dd-java-agent/instrumentation/java/java-lang/java-lang-21.0/src/test/java/testdog/trace/instrumentation/java/lang/jdk21/VirtualThreadApiInstrumentationTest.java index e0bfd5b9088..9429638261d 100644 --- a/dd-java-agent/instrumentation/java/java-lang/java-lang-21.0/src/test/java/testdog/trace/instrumentation/java/lang/jdk21/VirtualThreadApiInstrumentationTest.java +++ b/dd-java-agent/instrumentation/java/java-lang/java-lang-21.0/src/test/java/testdog/trace/instrumentation/java/lang/jdk21/VirtualThreadApiInstrumentationTest.java @@ -6,7 +6,6 @@ import datadog.trace.agent.test.AbstractInstrumentationTest; import datadog.trace.api.Trace; -import java.util.concurrent.CountDownLatch; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeoutException; import org.junit.jupiter.api.DisplayName; @@ -96,7 +95,6 @@ public void run() { @Test void testNestedVirtualThreads() throws InterruptedException, TimeoutException { Thread.Builder.OfVirtual threadBuilder = Thread.ofVirtual(); - CountDownLatch latch = new CountDownLatch(3); new Runnable() { @Trace(operationName = "parent") @@ -118,20 +116,18 @@ public void run() { @Override public void run() { System.out.println("complete"); - latch.countDown(); } }); - latch.countDown(); + blockUntilChildSpansFinished(1); } }); - latch.countDown(); + blockUntilChildSpansFinished(1); } }); + blockUntilChildSpansFinished(1); } }.run(); - latch.await(); - assertTraces( trace( SORT_BY_START_TIME,