Skip to content

Commit da2ed19

Browse files
laurithannahchan
authored andcommitted
Remove reflection from structured concurrency test (open-telemetry#11304)
1 parent 22c3e35 commit da2ed19

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

instrumentation/executors/jdk21-testing/build.gradle.kts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import kotlin.math.max
2+
13
plugins {
24
id("otel.javaagent-testing")
35
}
@@ -10,11 +12,28 @@ dependencies {
1012
}
1113

1214
otelJava {
13-
minJavaVersionSupported.set(JavaVersion.VERSION_21)
15+
// StructuredTaskScopeTest that uses preview feature, requires that the test is compiled for the
16+
// same vm version that is going to execute the test. Choose whichever is greater 21 or the
17+
// version of the vm that is going to run test
18+
val testJavaVersion =
19+
gradle.startParameter.projectProperties["testJavaVersion"]?.let(JavaVersion::toVersion)
20+
?: JavaVersion.current()
21+
minJavaVersionSupported.set(JavaVersion.toVersion(max(
22+
testJavaVersion.majorVersion.toInt(),
23+
JavaVersion.VERSION_21.majorVersion.toInt()
24+
)))
25+
}
26+
27+
tasks.withType<JavaCompile>().configureEach {
28+
with(options) {
29+
compilerArgs.add("--enable-preview")
30+
}
1431
}
1532

1633
tasks.withType<Test>().configureEach {
1734
// needed for VirtualThreadTest
1835
jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED")
1936
jvmArgs("-XX:+IgnoreUnrecognizedVMOptions")
37+
// needed for structured concurrency test
38+
jvmArgs("--enable-preview")
2039
}
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,20 @@
1010
import io.opentelemetry.api.trace.SpanKind;
1111
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
1212
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
13-
import java.lang.reflect.Method;
1413
import java.util.concurrent.Callable;
14+
import java.util.concurrent.StructuredTaskScope;
1515
import org.junit.jupiter.api.Test;
16-
import org.junit.jupiter.api.condition.EnabledForJreRange;
17-
import org.junit.jupiter.api.condition.JRE;
1816
import org.junit.jupiter.api.extension.RegisterExtension;
1917

20-
@EnabledForJreRange(min = JRE.JAVA_21)
18+
@SuppressWarnings("preview")
2119
class StructuredTaskScopeTest {
2220

2321
@RegisterExtension
2422
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
2523

2624
@Test
2725
void multipleForkJoin() throws Exception {
28-
Class<?> sofTaskScopeClass =
29-
Class.forName("java.util.concurrent.StructuredTaskScope$ShutdownOnFailure");
30-
Object taskScope = sofTaskScopeClass.getDeclaredConstructor().newInstance();
31-
Class<?> taskScopeClass = Class.forName("java.util.concurrent.StructuredTaskScope");
32-
Method forkMethod = taskScopeClass.getDeclaredMethod("fork", Callable.class);
33-
Method joinMethod = taskScopeClass.getDeclaredMethod("join");
34-
Method closeMethod = taskScopeClass.getDeclaredMethod("close");
35-
36-
Class<?> subtaskClass = Class.forName("java.util.concurrent.StructuredTaskScope$Subtask");
37-
Method getMethod = subtaskClass.getDeclaredMethod("get");
26+
StructuredTaskScope<Object> taskScope = new StructuredTaskScope.ShutdownOnFailure();
3827

3928
Callable<String> callable1 =
4029
() -> {
@@ -51,15 +40,11 @@ void multipleForkJoin() throws Exception {
5140
testing.runWithSpan(
5241
"parent",
5342
() -> {
54-
try {
55-
Object fork1 = forkMethod.invoke(taskScope, callable1);
56-
Object fork2 = forkMethod.invoke(taskScope, callable2);
57-
joinMethod.invoke(taskScope);
43+
StructuredTaskScope.Subtask<String> fork1 = taskScope.fork(callable1);
44+
StructuredTaskScope.Subtask<String> fork2 = taskScope.fork(callable2);
45+
taskScope.join();
5846

59-
return "" + getMethod.invoke(fork1) + getMethod.invoke(fork2);
60-
} catch (Exception e) {
61-
throw new AssertionError(e);
62-
}
47+
return "" + fork1.get() + fork2.get();
6348
});
6449

6550
assertThat(result).isEqualTo("ab");
@@ -73,6 +58,6 @@ void multipleForkJoin() throws Exception {
7358
span ->
7459
span.hasName("task2").hasKind(SpanKind.INTERNAL).hasParent(trace.getSpan(0))));
7560

76-
closeMethod.invoke(taskScope);
61+
taskScope.close();
7762
}
7863
}

0 commit comments

Comments
 (0)