From 84f5b4d519391ff351d76405adddc82167c2464b Mon Sep 17 00:00:00 2001 From: Peter Nied Date: Tue, 4 Jun 2024 18:18:15 +0000 Subject: [PATCH] Better error messaging with hamcrest matchers Signed-off-by: Peter Nied --- .../java/com/rfs/common/RestClientTest.java | 50 ++++++++----------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/RFS/src/test/java/com/rfs/common/RestClientTest.java b/RFS/src/test/java/com/rfs/common/RestClientTest.java index ad41daf45..c55211dfd 100644 --- a/RFS/src/test/java/com/rfs/common/RestClientTest.java +++ b/RFS/src/test/java/com/rfs/common/RestClientTest.java @@ -2,6 +2,8 @@ import com.rfs.tracing.RfsContexts; import com.rfs.tracing.TestContext; + +import io.opentelemetry.api.trace.Span; import io.opentelemetry.sdk.trace.data.SpanData; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Assertions; @@ -18,9 +20,11 @@ import java.util.Set; import java.util.stream.Collectors; -import static org.junit.jupiter.api.Assertions.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.equalTo; +import static java.util.stream.Collectors.toList; -@Slf4j class RestClientTest { @Test public void testGetEmitsInstrumentation() throws Exception{ @@ -38,51 +42,39 @@ public void testGetEmitsInstrumentation() throws Exception{ var allMetricData = rootContext.instrumentationBundle.getFinishedMetrics(); for (var kvp : Map.of( - "createGetSnapshotContext", new int[]{133, 66}, - "createSnapshotContext", new int[]{139, 66}, - "", new int[]{272, 132}).entrySet()) { + "createGetSnapshotContext", new long[]{133, 66}, + "createSnapshotContext", new long[]{139, 66}, + "", new long[]{272, 132}).entrySet()) { long bytesSent = allMetricData.stream().filter(md -> md.getName().startsWith("bytesSent")) .reduce((a, b) -> b).get().getLongSumData().getPoints() .stream() .filter(pd -> pd.getAttributes().asMap().values().stream().map(o -> (String) o).collect(Collectors.joining()) .equals(kvp.getKey())) .reduce((a, b) -> b).get().getValue(); - Assertions.assertEquals(kvp.getValue()[0], bytesSent); - - long bytesRead = allMetricData.stream().filter(md -> md.getName().startsWith("bytesRead")) .reduce((a, b) -> b).get().getLongSumData().getPoints() .stream() .filter(pd -> pd.getAttributes().asMap().values().stream().map(o -> (String) o).collect(Collectors.joining()) .equals(kvp.getKey())) .reduce((a, b) -> b).get().getValue(); - Assertions.assertEquals(kvp.getValue()[1], bytesRead); - } - var finishedSpans = rootContext.instrumentationBundle.getFinishedSpans().stream() - .sorted(Comparator.comparing(SpanData::getName) - .thenComparing(s->s.getAttributes() - .get(RfsContexts.GenericRequestContext.CALL_TYPE_ATTR)).reversed()) - .collect(Collectors.toList()); - Assertions.assertTrue(!finishedSpans.isEmpty()); - - try { - Assertions.assertEquals(String.join("\n", List.of("httpRequest", "httpRequest", "createSnapshot")), - finishedSpans.stream().map(SpanData::getName).collect(Collectors.joining("\n"))); - } catch (Throwable e) { - log.error(finishedSpans.stream().map(Object::toString).collect(Collectors.joining("\n"))); - throw e; + assertThat("Checking bytes {send, read} for context '" + kvp.getKey() + "'", new long[]{bytesSent, bytesRead}, equalTo(kvp.getValue())); } + final var finishedSpans = rootContext.instrumentationBundle.getFinishedSpans(); + final var finishedSpanNames = finishedSpans.stream().map(SpanData::getName).collect(toList()); + assertThat(finishedSpanNames, containsInAnyOrder("httpRequest", "httpRequest", "createSnapshot")); + final var httpRequestSpansByTime = finishedSpans.stream() + .filter(sd -> sd.getName().equals("httpRequest")) + .sorted(Comparator.comparing(SpanData::getEndEpochNanos)).collect(toList()); int i = 0; - for (var counts : List.of( + for (var expectedBytes : List.of( new long[]{139,66}, new long[]{133,66})) { - var span = finishedSpans.get(i++); - Assertions.assertEquals(span.getAttributes().get(RfsContexts.GenericRequestContext.BYTES_SENT_ATTR), - counts[0]); - Assertions.assertEquals(span.getAttributes().get(RfsContexts.GenericRequestContext.BYTES_READ_ATTR), - counts[1]); + var span = httpRequestSpansByTime.get(i++); + long bytesSent = span.getAttributes().get(RfsContexts.GenericRequestContext.BYTES_SENT_ATTR); + long bytesRead = span.getAttributes().get(RfsContexts.GenericRequestContext.BYTES_READ_ATTR); + assertThat("Checking bytes {send, read} for httpRequest " + i, new long[]{bytesSent, bytesRead}, equalTo(expectedBytes)); } }