Skip to content

Commit

Permalink
Merge pull request #5 from peternied/hamcrest
Browse files Browse the repository at this point in the history
Better error messaging with hamcrest matchers
  • Loading branch information
gregschohn authored Jun 6, 2024
2 parents 949b57d + 84f5b4d commit ef62644
Showing 1 changed file with 21 additions and 29 deletions.
50 changes: 21 additions & 29 deletions RFS/src/test/java/com/rfs/common/RestClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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{
Expand All @@ -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));
}
}

Expand Down

0 comments on commit ef62644

Please sign in to comment.