Skip to content

Commit 9b2734c

Browse files
committed
test: simplify verification logic
1 parent b480e7d commit 9b2734c

1 file changed

Lines changed: 42 additions & 31 deletions

File tree

  • java-showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it

java-showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITOtelTracing.java

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import io.opentelemetry.sdk.trace.data.SpanData;
5959
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
6060
import java.util.List;
61-
import java.util.Optional;
6261
import org.junit.jupiter.api.AfterEach;
6362
import org.junit.jupiter.api.BeforeEach;
6463
import org.junit.jupiter.api.Test;
@@ -254,21 +253,27 @@ void testTracing_retry_grpc() throws Exception {
254253
assertThat(spans).hasSize(attempts); // Expect exactly one span for the successful retry
255254

256255
// This single span represents the successful retry, which has resend_count=1
257-
for (int resendCount = 1; resendCount < attempts; resendCount++) {
258-
Optional<SpanData> found =
259-
spans.stream()
260-
.filter(
261-
span ->
262-
span.getAttributes()
263-
.asMap()
264-
.getOrDefault(
265-
AttributeKey.longKey(
266-
ObservabilityAttributes.GRPC_RESEND_COUNT_ATTRIBUTE),
267-
-1L)
268-
.equals(1L))
269-
.findFirst();
270-
assertThat(found).isPresent();
271-
}
256+
// The first attempt has no resend_count. The subsequent retries will have a resend_count,
257+
// starting from 1.
258+
List<Long> resendCounts =
259+
spans.stream()
260+
.map(
261+
span ->
262+
(Long)
263+
span.getAttributes()
264+
.asMap()
265+
.get(
266+
AttributeKey.longKey(
267+
ObservabilityAttributes.GRPC_RESEND_COUNT_ATTRIBUTE)))
268+
.filter(java.util.Objects::nonNull)
269+
.sorted()
270+
.collect(java.util.stream.Collectors.toList());
271+
272+
List<Long> expectedCounts =
273+
java.util.stream.LongStream.range(1, attempts)
274+
.boxed()
275+
.collect(java.util.stream.Collectors.toList());
276+
assertThat(resendCounts).containsExactlyElementsIn(expectedCounts).inOrder();
272277
}
273278

274279
@Test
@@ -323,20 +328,26 @@ void testTracing_retry_httpjson() throws Exception {
323328
assertThat(spans).hasSize(attempts); // Expect exactly one span for the successful retry
324329

325330
// This single span represents the successful retry, which has resend_count=1
326-
for (int resendCount = 1; resendCount < attempts; resendCount++) {
327-
Optional<SpanData> found =
328-
spans.stream()
329-
.filter(
330-
span ->
331-
span.getAttributes()
332-
.asMap()
333-
.getOrDefault(
334-
AttributeKey.longKey(
335-
ObservabilityAttributes.HTTP_RESEND_COUNT_ATTRIBUTE),
336-
-1L)
337-
.equals(1L))
338-
.findFirst();
339-
assertThat(found).isPresent();
340-
}
331+
// The first attempt has no resend_count. The subsequent retries will have a resend_count,
332+
// starting from 1.
333+
List<Long> resendCounts =
334+
spans.stream()
335+
.map(
336+
span ->
337+
(Long)
338+
span.getAttributes()
339+
.asMap()
340+
.get(
341+
AttributeKey.longKey(
342+
ObservabilityAttributes.HTTP_RESEND_COUNT_ATTRIBUTE)))
343+
.filter(java.util.Objects::nonNull)
344+
.sorted()
345+
.collect(java.util.stream.Collectors.toList());
346+
347+
List<Long> expectedCounts =
348+
java.util.stream.LongStream.range(1, attempts)
349+
.boxed()
350+
.collect(java.util.stream.Collectors.toList());
351+
assertThat(resendCounts).containsExactlyElementsIn(expectedCounts).inOrder();
341352
}
342353
}

0 commit comments

Comments
 (0)