10
10
import io .opentelemetry .api .trace .SpanKind ;
11
11
import io .opentelemetry .instrumentation .testing .junit .AgentInstrumentationExtension ;
12
12
import io .opentelemetry .instrumentation .testing .junit .InstrumentationExtension ;
13
- import java .lang .reflect .Method ;
14
13
import java .util .concurrent .Callable ;
14
+ import java .util .concurrent .StructuredTaskScope ;
15
15
import org .junit .jupiter .api .Test ;
16
- import org .junit .jupiter .api .condition .EnabledForJreRange ;
17
- import org .junit .jupiter .api .condition .JRE ;
18
16
import org .junit .jupiter .api .extension .RegisterExtension ;
19
17
20
- @ EnabledForJreRange ( min = JRE . JAVA_21 )
18
+ @ SuppressWarnings ( "preview" )
21
19
class StructuredTaskScopeTest {
22
20
23
21
@ RegisterExtension
24
22
static final InstrumentationExtension testing = AgentInstrumentationExtension .create ();
25
23
26
24
@ Test
27
25
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 ();
38
27
39
28
Callable <String > callable1 =
40
29
() -> {
@@ -51,15 +40,11 @@ void multipleForkJoin() throws Exception {
51
40
testing .runWithSpan (
52
41
"parent" ,
53
42
() -> {
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 ();
58
46
59
- return "" + getMethod .invoke (fork1 ) + getMethod .invoke (fork2 );
60
- } catch (Exception e ) {
61
- throw new AssertionError (e );
62
- }
47
+ return "" + fork1 .get () + fork2 .get ();
63
48
});
64
49
65
50
assertThat (result ).isEqualTo ("ab" );
@@ -73,6 +58,6 @@ void multipleForkJoin() throws Exception {
73
58
span ->
74
59
span .hasName ("task2" ).hasKind (SpanKind .INTERNAL ).hasParent (trace .getSpan (0 ))));
75
60
76
- closeMethod . invoke ( taskScope );
61
+ taskScope . close ( );
77
62
}
78
63
}
0 commit comments