From adc3e20bc5e85446bfb131589be898c034880e11 Mon Sep 17 00:00:00 2001 From: Andre Kurait Date: Mon, 23 Nov 2026 12:36:34 -0600 Subject: [PATCH] test(cloudwatch): await complete metric filter aggregates (#9292) The metric-filter compatibility test stopped polling at the first nonempty aggregate, so a later log batch could still add its statistics after the read. assertSeries now waits through TestFixtures.await until every statistic the fixture records matches, within the same ten-second deadline, with the last read in the timeout message. An absent series is still read once, and the JSON and Query assertions are unchanged. Signed-off-by: Andre Kurait --- .../test/CloudWatchLogsMetricFilterTest.java | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudWatchLogsMetricFilterTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudWatchLogsMetricFilterTest.java index 9cb17a1bd6..2951304c5e 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudWatchLogsMetricFilterTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudWatchLogsMetricFilterTest.java @@ -24,10 +24,12 @@ import software.amazon.awssdk.services.cloudwatchlogs.model.ResourceNotFoundExce import software.amazon.awssdk.services.cloudwatchlogs.model.StandardUnit; import software.amazon.awssdk.services.sts.StsClient; +import java.time.Duration; import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -359,22 +361,28 @@ class CloudWatchLogsMetricFilterTest { } /** - * A present series is awaited: the publisher writes it shortly after PutLogEvents returns, as + * A present series is awaited until all recorded statistics match: separate log batches may + * become visible separately. The publisher writes shortly after PutLogEvents returns, as * AWS does. An absent series is asserted once; callers await a later positive control first. * The window ends at second 59 so a sample stamped on the next minute is never in scope. */ private static void assertSeries(String metric, Instant minute, List dimensions, JsonNode expected) throws Exception { boolean present = expected != null && expected.has("Sum"); - List statistics = MetricFilterQueryAssertions.STATISTICS.stream().map(Statistic::fromValue).toList(); - List points = statistics(metric, minute, dimensions, statistics); - long deadline = System.nanoTime() + 10_000_000_000L; - while (present && points.isEmpty() && System.nanoTime() < deadline) { - Thread.sleep(50); - points = statistics(metric, minute, dimensions, statistics); - } - assertThat(points).as(metric + " at " + minute + " " + dimensions).hasSize(present ? 1 : 0); List recorded = present ? MetricFilterQueryAssertions.recorded(expected) : List.of(); + List statistics = MetricFilterQueryAssertions.STATISTICS.stream().map(Statistic::fromValue).toList(); + AtomicReference> lastRead = new AtomicReference<>(List.of()); + if (present) { + TestFixtures.await(() -> { + lastRead.set(statistics(metric, minute, dimensions, statistics)); + return matchesSeries(lastRead.get(), expected, recorded); + }, metric + " at " + minute + " " + dimensions, "all recorded statistics " + expected, + Duration.ofSeconds(10).toNanos(), 50, () -> "; last read: " + lastRead.get()); + } else { + lastRead.set(statistics(metric, minute, dimensions, statistics)); + } + List points = lastRead.get(); + assertThat(points).as(metric + " at " + minute + " " + dimensions).hasSize(present ? 1 : 0); if (present) { Datapoint point = points.get(0); assertThat(point.timestamp()).isEqualTo(minute); @@ -406,6 +414,11 @@ class CloudWatchLogsMetricFilterTest { MetricFilterQueryAssertions.assertSeries(namespace, metric, minute, dimensions, expected); } + private static boolean matchesSeries(List points, JsonNode expected, List recorded) { + return points.size() == 1 && recorded.stream() + .allMatch(stat -> Double.compare(value(points.get(0), stat), expected.get(stat).asDouble()) == 0); + } + private static List statistics(String metric, Instant minute, List dimensions, List statistics) { return cloudWatch.getMetricStatistics(r -> r.namespace(namespace).metricName(metric).dimensions(dimensions)