Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert spring-core tests from groovy to java #8166

Merged
merged 8 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.function.ThrowingConsumer;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

public class SimpleAsyncTaskExecutorInstrumentationTest {

@RegisterExtension
private static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

private static final SimpleAsyncTaskExecutor EXECUTOR = new SimpleAsyncTaskExecutor();

@Test
public void executeRunnable() {
abhisesh marked this conversation as resolved.
Show resolved Hide resolved
executeTwoTasks(EXECUTOR::execute);
}

@Test
public void submitRunnable() {
executeTwoTasks(task -> EXECUTOR.submit((Runnable) task));
}

@Test
public void submitCallable() {
executeTwoTasks(task -> EXECUTOR.submit((Callable<?>) task));
}

@Test
public void submitListenableRunnable() {
executeTwoTasks(task -> EXECUTOR.submitListenable((Runnable) task));
}

@Test
public void submitListenableCallable() {
executeTwoTasks(task -> EXECUTOR.submitListenable((Callable<?>) task));
}

public void executeTwoTasks(ThrowingConsumer<AsyncTask> task) {
abhisesh marked this conversation as resolved.
Show resolved Hide resolved
testing.runWithSpan(
"parent",
() -> {
AsyncTask child1 = new AsyncTask(true);
AsyncTask child2 = new AsyncTask(false);
try {
task.accept(child1);
task.accept(child2);
} catch (Throwable throwable) {
throw new AssertionError(throwable);
}
child1.waitForCompletion();
child2.waitForCompletion();
});
testing.waitAndAssertTraces(
trace ->
trace
.hasSize(2)
.hasSpansSatisfyingExactly(
span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(),
span ->
span.hasName("asyncChild")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))));
}

static class AsyncTask implements Runnable, Callable<Object> {

private static final Tracer TRACER = GlobalOpenTelemetry.getTracer("test");

private final boolean startSpan;
private final CountDownLatch latch = new CountDownLatch(1);

@Override
public void run() {
if (startSpan) {
TRACER.spanBuilder("asyncChild").startSpan().end();
}
latch.countDown();
}

@Override
public Object call() {
run();
return null;
}

void waitForCompletion() {
try {
latch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new AssertionError(e);
}
}

public AsyncTask(boolean startSpan) {
abhisesh marked this conversation as resolved.
Show resolved Hide resolved
this.startSpan = startSpan;
}
}
}