Skip to content

Commit 243ec0b

Browse files
fix(virtual-thread): Reduce test flakiness (#10834)
fix(virtual-thread): Reduce test flakiness Introduce generic blockUntil method to wait for trace pattern before evaluating them Co-authored-by: bruce.bujon <bruce.bujon@datadoghq.com>
1 parent 52d1e19 commit 243ec0b

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

dd-java-agent/instrumentation-testing/src/main/java/datadog/trace/agent/test/AbstractInstrumentationTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
import datadog.trace.core.TraceCollector;
2323
import java.lang.instrument.ClassFileTransformer;
2424
import java.lang.instrument.Instrumentation;
25+
import java.util.List;
2526
import java.util.ServiceLoader;
2627
import java.util.concurrent.TimeUnit;
2728
import java.util.concurrent.TimeoutException;
2829
import java.util.function.Function;
30+
import java.util.function.Predicate;
2931
import net.bytebuddy.agent.ByteBuddyAgent;
3032
import org.junit.jupiter.api.AfterEach;
3133
import org.junit.jupiter.api.BeforeEach;
@@ -138,6 +140,26 @@ protected void assertTraces(
138140
TraceAssertions.assertTraces(this.writer, options, matchers);
139141
}
140142

143+
/**
144+
* Blocks the current thread until the traces written match the given predicate or the timeout
145+
* occurs.
146+
*
147+
* @param predicate the condition that must be satisfied by the list of traces
148+
*/
149+
protected void blockUntilTracesMatch(Predicate<List<List<DDSpan>>> predicate) {
150+
long deadline = System.currentTimeMillis() + TIMEOUT_MILLIS;
151+
while (!predicate.test(this.writer)) {
152+
if (System.currentTimeMillis() > deadline) {
153+
throw new RuntimeException(new TimeoutException("Timed out waiting for traces/spans."));
154+
}
155+
try {
156+
Thread.sleep(10);
157+
} catch (InterruptedException e) {
158+
Thread.currentThread().interrupt();
159+
}
160+
}
161+
}
162+
141163
protected void blockUntilChildSpansFinished(final int numberOfSpans) {
142164
blockUntilChildSpansFinished(this.tracer.activeSpan(), numberOfSpans);
143165
}
@@ -147,7 +169,7 @@ static void blockUntilChildSpansFinished(AgentSpan span, int numberOfSpans) {
147169
TraceCollector traceCollector = ((DDSpan) span).context().getTraceCollector();
148170
if (!(traceCollector instanceof PendingTrace)) {
149171
throw new IllegalStateException(
150-
"Expected $PendingTrace.name trace collector, got $traceCollector.class.name");
172+
"Expected PendingTrace trace collector, got " + traceCollector.getClass().getName());
151173
}
152174

153175
PendingTrace pendingTrace = (PendingTrace) traceCollector;

dd-java-agent/instrumentation/java/java-lang/java-lang-21.0/src/test/java/testdog/trace/instrumentation/java/lang/jdk21/VirtualThreadApiInstrumentationTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import datadog.trace.agent.test.AbstractInstrumentationTest;
88
import datadog.trace.api.Trace;
9-
import java.util.concurrent.CountDownLatch;
109
import java.util.concurrent.ThreadFactory;
1110
import java.util.concurrent.TimeoutException;
1211
import org.junit.jupiter.api.DisplayName;
@@ -96,7 +95,6 @@ public void run() {
9695
@Test
9796
void testNestedVirtualThreads() throws InterruptedException, TimeoutException {
9897
Thread.Builder.OfVirtual threadBuilder = Thread.ofVirtual();
99-
CountDownLatch latch = new CountDownLatch(3);
10098

10199
new Runnable() {
102100
@Trace(operationName = "parent")
@@ -118,19 +116,17 @@ public void run() {
118116
@Override
119117
public void run() {
120118
System.out.println("complete");
121-
latch.countDown();
122119
}
123120
});
124-
latch.countDown();
125121
}
126122
});
127-
latch.countDown();
128123
}
129124
});
130125
}
131126
}.run();
132127

133-
latch.await();
128+
// Block test thread until child spans are reported
129+
blockUntilTracesMatch(traces -> traces.size() == 1 && traces.get(0).size() == 4);
134130

135131
assertTraces(
136132
trace(

0 commit comments

Comments
 (0)