Skip to content

Commit b23af1b

Browse files
abhiseshMateusz Rzeszutek
and
Mateusz Rzeszutek
authored
Convert spring-core tests from groovy to java (#8166)
Related to #7195. Converts spring core instrumentation tests from groovy to java. --------- Co-authored-by: Mateusz Rzeszutek <[email protected]>
1 parent 67aabce commit b23af1b

File tree

3 files changed

+118
-93
lines changed

3 files changed

+118
-93
lines changed

instrumentation/spring/spring-core-2.0/javaagent/build.gradle.kts

+7
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ dependencies {
1919
// 4.0 introduces submitListenable() methods
2020
testLibrary("org.springframework:spring-core:4.0.0.RELEASE")
2121
}
22+
23+
// spring 6 requires java 17
24+
if (findProperty("testLatestDeps") as Boolean) {
25+
otelJava {
26+
minJavaVersionSupported.set(JavaVersion.VERSION_17)
27+
}
28+
}

instrumentation/spring/spring-core-2.0/javaagent/src/test/groovy/SimpleAsyncTaskExecutorInstrumentationTest.groovy

-93
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import io.opentelemetry.api.GlobalOpenTelemetry;
7+
import io.opentelemetry.api.trace.SpanKind;
8+
import io.opentelemetry.api.trace.Tracer;
9+
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
10+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
11+
import java.util.concurrent.Callable;
12+
import java.util.concurrent.CountDownLatch;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.extension.RegisterExtension;
15+
import org.junit.jupiter.api.function.ThrowingConsumer;
16+
import org.springframework.core.task.SimpleAsyncTaskExecutor;
17+
18+
public class SimpleAsyncTaskExecutorInstrumentationTest {
19+
20+
@RegisterExtension
21+
private static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
22+
23+
private static final SimpleAsyncTaskExecutor EXECUTOR = new SimpleAsyncTaskExecutor();
24+
25+
@Test
26+
void executeRunnable() {
27+
executeTwoTasks(EXECUTOR::execute);
28+
}
29+
30+
@Test
31+
void submitRunnable() {
32+
executeTwoTasks(task -> EXECUTOR.submit((Runnable) task));
33+
}
34+
35+
@Test
36+
void submitCallable() {
37+
executeTwoTasks(task -> EXECUTOR.submit((Callable<?>) task));
38+
}
39+
40+
@Test
41+
void submitListenableRunnable() {
42+
executeTwoTasks(task -> EXECUTOR.submitListenable((Runnable) task));
43+
}
44+
45+
@Test
46+
void submitListenableCallable() {
47+
executeTwoTasks(task -> EXECUTOR.submitListenable((Callable<?>) task));
48+
}
49+
50+
private static void executeTwoTasks(ThrowingConsumer<AsyncTask> task) {
51+
testing.runWithSpan(
52+
"parent",
53+
() -> {
54+
AsyncTask child1 = new AsyncTask(true);
55+
AsyncTask child2 = new AsyncTask(false);
56+
try {
57+
task.accept(child1);
58+
task.accept(child2);
59+
} catch (Throwable throwable) {
60+
throw new AssertionError(throwable);
61+
}
62+
child1.waitForCompletion();
63+
child2.waitForCompletion();
64+
});
65+
testing.waitAndAssertTraces(
66+
trace ->
67+
trace
68+
.hasSize(2)
69+
.hasSpansSatisfyingExactly(
70+
span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(),
71+
span ->
72+
span.hasName("asyncChild")
73+
.hasKind(SpanKind.INTERNAL)
74+
.hasParent(trace.getSpan(0))));
75+
}
76+
77+
static class AsyncTask implements Runnable, Callable<Object> {
78+
79+
private static final Tracer TRACER = GlobalOpenTelemetry.getTracer("test");
80+
81+
private final boolean startSpan;
82+
private final CountDownLatch latch = new CountDownLatch(1);
83+
84+
public AsyncTask(boolean startSpan) {
85+
this.startSpan = startSpan;
86+
}
87+
88+
@Override
89+
public void run() {
90+
if (startSpan) {
91+
TRACER.spanBuilder("asyncChild").startSpan().end();
92+
}
93+
latch.countDown();
94+
}
95+
96+
@Override
97+
public Object call() {
98+
run();
99+
return null;
100+
}
101+
102+
void waitForCompletion() {
103+
try {
104+
latch.await();
105+
} catch (InterruptedException e) {
106+
Thread.currentThread().interrupt();
107+
throw new AssertionError(e);
108+
}
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)