|
| 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