Skip to content

Commit 17e53f2

Browse files
committed
feat(java-lang): Migrate tests to JUnit
1 parent 7420cd1 commit 17e53f2

File tree

4 files changed

+177
-169
lines changed

4 files changed

+177
-169
lines changed

dd-java-agent/instrumentation/java/java-lang/java-lang-21.0/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ idea {
2424

2525
// Set all compile tasks to use JDK21 but let instrumentation code targets 1.8 compatibility
2626
tasks.withType(AbstractCompile).configureEach {
27-
configureCompiler(it, 21, JavaVersion.VERSION_1_8)
27+
if (name.contains("Test")) {
28+
configureCompiler(it, 21)
29+
} else {
30+
configureCompiler(it, 21, JavaVersion.VERSION_1_8)
31+
}
2832
}
2933

3034
dependencies {

dd-java-agent/instrumentation/java/java-lang/java-lang-21.0/src/test/groovy/VirtualThreadApiTest.groovy

Lines changed: 0 additions & 168 deletions
This file was deleted.

dd-java-agent/instrumentation/java/java-lang/java-lang-21.0/src/test/java/JavaAsyncChild.java renamed to dd-java-agent/instrumentation/java/java-lang/java-lang-21.0/src/test/java/testdog/trace/instrumentation/java/lang/jdk21/JavaAsyncChild.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package testdog.trace.instrumentation.java.lang.jdk21;
2+
13
import datadog.trace.api.Trace;
24
import java.util.concurrent.Callable;
35
import java.util.concurrent.atomic.AtomicBoolean;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package testdog.trace.instrumentation.java.lang.jdk21;
2+
3+
import static java.util.Collections.emptyList;
4+
import static java.util.Comparator.comparing;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.fail;
7+
8+
import datadog.trace.agent.test.InstrumentationTest;
9+
import datadog.trace.api.Trace;
10+
import datadog.trace.core.DDSpan;
11+
import java.util.List;
12+
import java.util.concurrent.CountDownLatch;
13+
import java.util.concurrent.ThreadFactory;
14+
import java.util.concurrent.TimeoutException;
15+
import org.junit.jupiter.api.DisplayName;
16+
import org.junit.jupiter.api.Test;
17+
18+
public class VirtualThreadApiJavaTest extends InstrumentationTest {
19+
20+
@DisplayName("test Thread.Builder.OfVirtual.start()")
21+
@Test
22+
void testBuilderOfVirtualStart() throws InterruptedException, TimeoutException {
23+
Thread.Builder.OfVirtual threadBuilder = Thread.ofVirtual().name("builder - started");
24+
25+
new Runnable() {
26+
@Override
27+
@Trace(operationName = "parent")
28+
public void run() {
29+
// this child will have a span
30+
threadBuilder.start(new JavaAsyncChild());
31+
// this child won't
32+
threadBuilder.start(new JavaAsyncChild(false, false));
33+
blockUntilChildSpansFinished(1);
34+
}
35+
}.run();
36+
37+
assertConnectedTrace();
38+
}
39+
40+
@DisplayName("test Thread.Builder.OfVirtual.unstarted()")
41+
@Test
42+
void testBuilderOfVirtualUnstarted() throws InterruptedException, TimeoutException {
43+
Thread.Builder.OfVirtual threadBuilder = Thread.ofVirtual().name("builder - started");
44+
45+
new Runnable() {
46+
@Override
47+
@Trace(operationName = "parent")
48+
public void run() {
49+
// this child will have a span
50+
threadBuilder.unstarted(new JavaAsyncChild()).start();
51+
// this child won't
52+
threadBuilder.unstarted(new JavaAsyncChild(false, false)).start();
53+
blockUntilChildSpansFinished(1);
54+
}
55+
}.run();
56+
57+
assertConnectedTrace();
58+
}
59+
60+
@DisplayName("test Thread.startVirtual()")
61+
@Test
62+
void testThreadStartVirtual() throws InterruptedException, TimeoutException {
63+
new Runnable() {
64+
@Override
65+
@Trace(operationName = "parent")
66+
public void run() {
67+
// this child will have a span
68+
Thread.startVirtualThread(new JavaAsyncChild());
69+
// this child won't
70+
Thread.startVirtualThread(new JavaAsyncChild(false, false));
71+
blockUntilChildSpansFinished(1);
72+
}
73+
}.run();
74+
75+
assertConnectedTrace();
76+
}
77+
78+
@DisplayName("test Thread.Builder.OfVirtual.factory()")
79+
@Test
80+
void testThreadOfVirtualFactory() throws InterruptedException, TimeoutException {
81+
ThreadFactory factory = Thread.ofVirtual().factory();
82+
83+
new Runnable() {
84+
@Override
85+
@Trace(operationName = "parent")
86+
public void run() {
87+
// this child will have a span
88+
factory.newThread(new JavaAsyncChild()).start();
89+
// this child won't
90+
factory.newThread(new JavaAsyncChild(false, false)).start();
91+
blockUntilChildSpansFinished(1);
92+
}
93+
}.run();
94+
95+
assertConnectedTrace();
96+
}
97+
98+
@DisplayName("test nested virtual threads")
99+
@Test
100+
void testNestedVirtualThreads() throws InterruptedException, TimeoutException {
101+
Thread.Builder.OfVirtual threadBuilder = Thread.ofVirtual();
102+
CountDownLatch latch = new CountDownLatch(3);
103+
104+
new Runnable() {
105+
@Trace(operationName = "parent")
106+
@Override
107+
public void run() {
108+
threadBuilder.start(
109+
new Runnable() {
110+
@Trace(operationName = "child")
111+
@Override
112+
public void run() {
113+
threadBuilder.start(
114+
new Runnable() {
115+
@Trace(operationName = "great-child")
116+
@Override
117+
public void run() {
118+
threadBuilder.start(
119+
new Runnable() {
120+
@Trace(operationName = "great-great-child")
121+
@Override
122+
public void run() {
123+
System.out.println("complete");
124+
latch.countDown();
125+
}
126+
});
127+
latch.countDown();
128+
}
129+
});
130+
latch.countDown();
131+
}
132+
});
133+
}
134+
}.run();
135+
136+
latch.await();
137+
138+
var trace = getTrace();
139+
trace.sort(comparing(DDSpan::getStartTimeNano));
140+
assertEquals(4, trace.size());
141+
assertEquals("parent", trace.get(0).getOperationName());
142+
assertEquals("child", trace.get(1).getOperationName());
143+
assertEquals("great-child", trace.get(2).getOperationName());
144+
assertEquals("great-great-child", trace.get(3).getOperationName());
145+
assertEquals(trace.get(0).getSpanId(), trace.get(1).getParentId());
146+
assertEquals(trace.get(1).getSpanId(), trace.get(2).getParentId());
147+
assertEquals(trace.get(2).getSpanId(), trace.get(3).getParentId());
148+
}
149+
150+
/** Verifies the parent / child span relation. */
151+
void assertConnectedTrace() {
152+
var trace = getTrace();
153+
trace.sort(comparing(DDSpan::getStartTimeNano));
154+
assertEquals(2, trace.size());
155+
assertEquals("parent", trace.get(0).getOperationName());
156+
assertEquals("asyncChild", trace.get(1).getOperationName());
157+
assertEquals(trace.get(0).getSpanId(), trace.get(1).getParentId());
158+
}
159+
160+
List<DDSpan> getTrace() {
161+
try {
162+
writer.waitForTraces(1);
163+
assertEquals(1, writer.size());
164+
return writer.getFirst();
165+
} catch (InterruptedException | TimeoutException e) {
166+
fail("Failed to wait for trace to finish.", e);
167+
return emptyList();
168+
}
169+
}
170+
}

0 commit comments

Comments
 (0)