Skip to content

Commit a41383a

Browse files
committed
add failing test around parked virtual thread
1 parent 9f2354e commit a41383a

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import static datadog.trace.agent.test.assertions.SpanMatcher.span;
44
import static datadog.trace.agent.test.assertions.TraceMatcher.SORT_BY_START_TIME;
55
import static datadog.trace.agent.test.assertions.TraceMatcher.trace;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
67

78
import datadog.trace.agent.test.AbstractInstrumentationTest;
9+
import datadog.trace.api.CorrelationIdentifier;
810
import datadog.trace.api.Trace;
911
import java.util.concurrent.ThreadFactory;
1012
import java.util.concurrent.TimeoutException;
13+
import java.util.concurrent.atomic.AtomicReference;
1114
import org.junit.jupiter.api.DisplayName;
1215
import org.junit.jupiter.api.Test;
1316

@@ -137,6 +140,68 @@ public void run() {
137140
span().childOfPrevious().operationName("great-great-child")));
138141
}
139142

143+
@DisplayName("test CorrelationIdentifier across virtual thread remount")
144+
@Test
145+
void testCorrelationIdentifierAcrossVirtualThreadRemount() throws InterruptedException {
146+
AtomicReference<String> parentTraceId = new AtomicReference<>();
147+
AtomicReference<String> parentSpanId = new AtomicReference<>();
148+
AtomicReference<String> traceIdBeforeRemount = new AtomicReference<>();
149+
AtomicReference<String> spanIdBeforeRemount = new AtomicReference<>();
150+
AtomicReference<String> traceIdAfterRemount = new AtomicReference<>();
151+
AtomicReference<String> spanIdAfterRemount = new AtomicReference<>();
152+
153+
new Runnable() {
154+
@Override
155+
@Trace(operationName = "parent")
156+
public void run() {
157+
parentTraceId.set(CorrelationIdentifier.getTraceId());
158+
parentSpanId.set(CorrelationIdentifier.getSpanId());
159+
160+
Thread thread =
161+
Thread.startVirtualThread(
162+
() -> {
163+
traceIdBeforeRemount.set(CorrelationIdentifier.getTraceId());
164+
spanIdBeforeRemount.set(CorrelationIdentifier.getSpanId());
165+
166+
try {
167+
// Sleeping should park and later remount the virtual thread.
168+
Thread.sleep(10);
169+
} catch (InterruptedException e) {
170+
throw new RuntimeException(e);
171+
}
172+
173+
traceIdAfterRemount.set(CorrelationIdentifier.getTraceId());
174+
spanIdAfterRemount.set(CorrelationIdentifier.getSpanId());
175+
});
176+
177+
try {
178+
thread.join();
179+
} catch (InterruptedException e) {
180+
throw new RuntimeException(e);
181+
}
182+
}
183+
}.run();
184+
185+
assertEquals(
186+
parentTraceId.get(),
187+
traceIdBeforeRemount.get(),
188+
"trace id should be visible before the virtual thread remounts");
189+
assertEquals(
190+
parentSpanId.get(),
191+
spanIdBeforeRemount.get(),
192+
"span id should be visible before the virtual thread remounts");
193+
assertEquals(
194+
parentTraceId.get(),
195+
traceIdAfterRemount.get(),
196+
"trace id should survive a virtual thread remount");
197+
assertEquals(
198+
parentSpanId.get(),
199+
spanIdAfterRemount.get(),
200+
"span id should survive a virtual thread remount");
201+
202+
assertTraces(trace(span().root().operationName("parent")));
203+
}
204+
140205
/** Verifies the parent / child span relation. */
141206
void assertConnectedTrace() {
142207
assertTraces(

0 commit comments

Comments
 (0)