|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.executors; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +import io.opentelemetry.api.trace.SpanKind; |
| 11 | +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; |
| 12 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 13 | +import java.lang.reflect.Method; |
| 14 | +import java.util.concurrent.Callable; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.junit.jupiter.api.condition.EnabledForJreRange; |
| 17 | +import org.junit.jupiter.api.condition.JRE; |
| 18 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 19 | + |
| 20 | +@EnabledForJreRange(min = JRE.JAVA_21) |
| 21 | +class StructuredTaskScopeTest { |
| 22 | + |
| 23 | + @RegisterExtension |
| 24 | + static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); |
| 25 | + |
| 26 | + @Test |
| 27 | + 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"); |
| 38 | + |
| 39 | + Callable<String> callable1 = |
| 40 | + () -> { |
| 41 | + testing.runWithSpan("task1", () -> {}); |
| 42 | + return "a"; |
| 43 | + }; |
| 44 | + Callable<String> callable2 = |
| 45 | + () -> { |
| 46 | + testing.runWithSpan("task2", () -> {}); |
| 47 | + return "b"; |
| 48 | + }; |
| 49 | + |
| 50 | + String result = |
| 51 | + testing.runWithSpan( |
| 52 | + "parent", |
| 53 | + () -> { |
| 54 | + try { |
| 55 | + Object fork1 = forkMethod.invoke(taskScope, callable1); |
| 56 | + Object fork2 = forkMethod.invoke(taskScope, callable2); |
| 57 | + joinMethod.invoke(taskScope); |
| 58 | + |
| 59 | + return "" + getMethod.invoke(fork1) + getMethod.invoke(fork2); |
| 60 | + } catch (Exception e) { |
| 61 | + throw new AssertionError(e); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + assertThat(result).isEqualTo("ab"); |
| 66 | + |
| 67 | + testing.waitAndAssertTraces( |
| 68 | + trace -> |
| 69 | + trace.hasSpansSatisfyingExactlyInAnyOrder( |
| 70 | + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), |
| 71 | + span -> |
| 72 | + span.hasName("task1").hasKind(SpanKind.INTERNAL).hasParent(trace.getSpan(0)), |
| 73 | + span -> |
| 74 | + span.hasName("task2").hasKind(SpanKind.INTERNAL).hasParent(trace.getSpan(0)))); |
| 75 | + |
| 76 | + closeMethod.invoke(taskScope); |
| 77 | + } |
| 78 | +} |
0 commit comments