Skip to content

Commit

Permalink
Otel Scheduler IT: make OpenTelemetrySchedulerTest more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba committed Jan 9, 2024
1 parent 42ffdef commit ddf0a26
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import static io.restassured.RestAssured.get;
import static io.restassured.RestAssured.given;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import org.junit.jupiter.api.Test;

Expand All @@ -33,7 +36,7 @@ public void schedulerSpanTest() {
assertCounter("/scheduler/count/job-definition", 1, Duration.ofSeconds(3));

// ------- SPAN ASSERTS -------
List<Map<String, Object>> spans = getSpans();
List<Map<String, Object>> spans = getSpans("myCounter", "myJobDefinition");

assertJobSpan(spans, "myCounter", DURATION_IN_NANOSECONDS); // identity
assertJobSpan(spans, "myJobDefinition", DURATION_IN_NANOSECONDS); // identity
Expand Down Expand Up @@ -62,9 +65,20 @@ private void assertCounter(String counterPath, int expectedCount, Duration timeo

}

private List<Map<String, Object>> getSpans() {
return get("/export").body().as(new TypeRef<>() {
private List<Map<String, Object>> getSpans(String... expectedNames) {
AtomicReference<List<Map<String, Object>>> ret = new AtomicReference<>(Collections.emptyList());
await().atMost(15, SECONDS).until(() -> {
List<Map<String, Object>> spans = get("/export").body().as(new TypeRef<>() {
});
for (String name : expectedNames) {
if (spans.stream().filter(map -> map.get("name").equals(name)).findAny().isEmpty()) {
return false;
}
}
ret.set(spans);
return true;
});
return ret.get();
}

private void assertJobSpan(List<Map<String, Object>> spans, String expectedName, long expectedDuration) {
Expand All @@ -82,6 +96,7 @@ private void assertJobSpan(List<Map<String, Object>> spans, String expectedName,
"' is not longer than 100ms, actual duration: " + delta + " (ns)");
}

@SuppressWarnings("unchecked")
private void assertErrorJobSpan(List<Map<String, Object>> spans, String expectedName, long expectedDuration,
String expectedErrorMessage) {
assertJobSpan(spans, expectedName, expectedDuration);
Expand Down

0 comments on commit ddf0a26

Please sign in to comment.