From 88fa2ec28d5b9eb6f1595bffe70e9e384ff635fa Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Mon, 5 Jun 2023 13:51:18 +0300 Subject: [PATCH] Fix flaky runtime metrics close test --- .../runtimemetrics/java17/RuntimeMetricsTest.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/instrumentation/runtime-metrics/runtime-metrics-java17/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/java17/RuntimeMetricsTest.java b/instrumentation/runtime-metrics/runtime-metrics-java17/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/java17/RuntimeMetricsTest.java index a81d62c82882..d74f40775d6a 100644 --- a/instrumentation/runtime-metrics/runtime-metrics-java17/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/java17/RuntimeMetricsTest.java +++ b/instrumentation/runtime-metrics/runtime-metrics-java17/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/java17/RuntimeMetricsTest.java @@ -78,7 +78,7 @@ void builder() { } @Test - void close() { + void close() throws InterruptedException { try (RuntimeMetrics jfrTelemetry = RuntimeMetrics.builder(sdk).build()) { // Track whether RecordingStream has been closed AtomicBoolean recordingStreamClosed = new AtomicBoolean(false); @@ -92,11 +92,13 @@ void close() { jfrTelemetry.close(); logs.assertDoesNotContain("RuntimeMetrics is already closed"); assertThat(recordingStreamClosed.get()).isTrue(); - assertThat(reader.collectAllMetrics()) - .allSatisfy( - metric -> { - assertThat(metric.getName()).isEqualTo("process.runtime.jvm.gc.duration"); - }); + + // clear all metrics that might have arrived after close + Thread.sleep(100); // give time for any inflight metric export to be received + reader.collectAllMetrics(); + + Thread.sleep(100); + assertThat(reader.collectAllMetrics()).isEmpty(); jfrTelemetry.close(); logs.assertContains("RuntimeMetrics is already closed");