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 batch tests to java #12004

Merged
merged 17 commits into from
Sep 13, 2024
Prev Previous commit
Next Next commit
simplify
  • Loading branch information
zeitlinger committed Sep 10, 2024
commit 1ee72be166c9f76504ee69a3e61362d755621632
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.chunk;


import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.runner.JobRunner;
import io.opentelemetry.sdk.testing.assertj.SpanDataAssert;
import io.opentelemetry.sdk.testing.assertj.TraceAssert;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

abstract class CustomSpanEventTest {

private static final boolean VERSION_GREATER_THAN_4_0 = Boolean.getBoolean("testLatestDeps");

private final JobRunner runner;

@RegisterExtension
Expand All @@ -30,92 +38,88 @@ void shouldBeAbleToCallSpanCurrentAndAddCustomInfoToSpans() {
runner.runJob("customSpanEventsItemsJob");

testing.waitAndAssertTraces(
trace ->
itemSpans(
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("BatchJob customSpanEventsItemsJob")
.hasKind(SpanKind.INTERNAL)
.hasEventsSatisfyingExactly(
event -> event.hasName("job.before"),
event -> event.hasName("job.after")),
span ->
span.hasName("BatchJob customSpanEventsItemsJob.customSpanEventsItemStep")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))
.satisfies(
spanData -> {
// CompositeChunkListener has broken ordering that causes
// listeners that do not override order() to appear first at all
// times
// because of that a custom ChunkListener will always see a Step
// span when using spring-batch versions [3, 4)
// that bug was fixed in 4.0
if (VERSION_GREATER_THAN_4_0) {
assertThat(spanData)
.hasEventsSatisfyingExactly(
event -> event.hasName("step.before"),
event -> event.hasName("step.after"));
} else {
assertThat(spanData)
.hasEventsSatisfyingExactly(
event -> event.hasName("step.before"),
event -> event.hasName("chunk.before"),
event -> event.hasName("chunk.after"),
event -> event.hasName("step.after"));
}
}),
span ->
span.hasName(
"BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.Chunk")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(1))
.satisfies(
spanData -> {
// CompositeChunkListener has broken ordering that causes
// listeners that do not override order() to appear first at all
// times
// because of that a custom ChunkListener will always see a Step
// span when using spring-batch versions [3, 4)
// that bug was fixed in 4.0
if (VERSION_GREATER_THAN_4_0) {
assertThat(spanData)
.hasEventsSatisfyingExactly(
event -> event.hasName("chunk.before"),
event -> event.hasName("chunk.after"));
} else {
assertThat(spanData.getEvents()).isEmpty();
}
}),
span -> {}, // ignore
span -> {}, // ignore
span -> {}, // ignore
span -> {} // ignore
)));
trace -> {
List<Consumer<SpanDataAssert>> assertions =
new ArrayList<>(
Arrays.asList(
span ->
span.hasName("BatchJob customSpanEventsItemsJob")
.hasKind(SpanKind.INTERNAL)
.hasEventsSatisfyingExactly(
event -> event.hasName("job.before"),
event -> event.hasName("job.after")),
span ->
span.hasName("BatchJob customSpanEventsItemsJob.customSpanEventsItemStep")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))
.satisfies(
spanData -> {
// CompositeChunkListener has broken ordering that causes
// listeners that do not override order() to appear first at all
// times
// because of that a custom ChunkListener will always see a Step
// span when using spring-batch versions [3, 4)
// that bug was fixed in 4.0
if (VERSION_GREATER_THAN_4_0) {
assertThat(spanData)
.hasEventsSatisfyingExactly(
event -> event.hasName("step.before"),
event -> event.hasName("step.after"));
} else {
assertThat(spanData)
.hasEventsSatisfyingExactly(
event -> event.hasName("step.before"),
event -> event.hasName("chunk.before"),
event -> event.hasName("chunk.after"),
event -> event.hasName("step.after"));
}
}),
span ->
span.hasName(
"BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.Chunk")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(1))
.satisfies(
spanData -> {
// CompositeChunkListener has broken ordering that causes
// listeners that do not override order() to appear first at all
// times
// because of that a custom ChunkListener will always see a Step
// span when using spring-batch versions [3, 4)
// that bug was fixed in 4.0
if (VERSION_GREATER_THAN_4_0) {
assertThat(spanData)
.hasEventsSatisfyingExactly(
event -> event.hasName("chunk.before"),
event -> event.hasName("chunk.after"));
} else {
assertThat(spanData.getEvents()).isEmpty();
}
})));
itemSpans(trace, assertions);
trace.hasSpansSatisfyingExactly(assertions);
});
}

protected void itemSpans(TraceAssert trace) {
trace.hasSpansSatisfyingExactly(
span -> {}, // already checked in the outer method
span -> {}, // already checked in the outer method
span -> {}, // already checked in the outer method
span ->
span.hasName("BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.ItemRead")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(2)),
span ->
span.hasName("BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.ItemRead")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(2)),
span ->
span.hasName("BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.ItemProcess")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(2)),
span ->
span.hasName("BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.ItemWrite")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(2)));
protected void itemSpans(TraceAssert trace, List<Consumer<SpanDataAssert>> assertions) {
assertions.addAll(
Arrays.asList(
span ->
span.hasName("BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.ItemRead")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(2)),
span ->
span.hasName("BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.ItemRead")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(2)),
span ->
span.hasName(
"BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.ItemProcess")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(2)),
span ->
span.hasName("BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.ItemWrite")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(2))));
}

private static final boolean VERSION_GREATER_THAN_4_0 = Boolean.getBoolean("testLatestDeps");
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.runner.JavaxBatchConfigRunner;
import io.opentelemetry.sdk.testing.assertj.SpanDataAssert;
import io.opentelemetry.sdk.testing.assertj.TraceAssert;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import org.junit.jupiter.api.extension.RegisterExtension;

public class JsrConfigCustomSpanEventTest extends CustomSpanEventTest {
Expand All @@ -19,36 +23,35 @@ public JsrConfigCustomSpanEventTest() {
}

@Override
protected void itemSpans(TraceAssert trace) {
trace.hasSpansSatisfyingExactly(
span -> {}, // already checked in the outer method
span -> {}, // already checked in the outer method
span -> {}, // already checked in the outer method
span ->
span.hasName("BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.ItemRead")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(2))
.hasEventsSatisfyingExactly(
event -> event.hasName("item.read.before"),
event -> event.hasName("item.read.after")),
span ->
span.hasName("BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.ItemProcess")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(2))
.hasEventsSatisfyingExactly(
event -> event.hasName("item.process.before"),
event -> event.hasName("item.process.after")),
span ->
span.hasName("BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.ItemRead")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(2))
.hasEventsSatisfyingExactly(event -> event.hasName("item.read.before")),
span ->
span.hasName("BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.ItemWrite")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(2))
.hasEventsSatisfyingExactly(
event -> event.hasName("item.write.before"),
event -> event.hasName("item.write.after")));
protected void itemSpans(TraceAssert trace, List<Consumer<SpanDataAssert>> assertions) {
assertions.addAll(
Arrays.asList(
span ->
span.hasName("BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.ItemRead")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(2))
.hasEventsSatisfyingExactly(
event -> event.hasName("item.read.before"),
event -> event.hasName("item.read.after")),
span ->
span.hasName(
"BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.ItemProcess")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(2))
.hasEventsSatisfyingExactly(
event -> event.hasName("item.process.before"),
event -> event.hasName("item.process.after")),
span ->
span.hasName("BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.ItemRead")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(2))
.hasEventsSatisfyingExactly(event -> event.hasName("item.read.before")),
span ->
span.hasName("BatchJob customSpanEventsItemsJob.customSpanEventsItemStep.ItemWrite")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(2))
.hasEventsSatisfyingExactly(
event -> event.hasName("item.write.before"),
event -> event.hasName("item.write.after"))));
}
}