|
32 | 32 | import java.util.function.Consumer; |
33 | 33 | import java.util.stream.Collectors; |
34 | 34 | import org.assertj.core.api.MapAssert; |
| 35 | +import org.awaitility.core.ConditionTimeoutException; |
35 | 36 | import org.junit.jupiter.api.AfterAll; |
36 | 37 | import org.junit.jupiter.api.BeforeAll; |
37 | 38 | import org.junit.jupiter.api.BeforeEach; |
@@ -134,28 +135,41 @@ protected static GenericContainer<?> cassandraContainer() { |
134 | 135 | } |
135 | 136 |
|
136 | 137 | protected final void waitAndAssertMetrics(Iterable<Consumer<Metric>> assertions) { |
137 | | - await() |
138 | | - .atMost(Duration.ofSeconds(30)) |
139 | | - .untilAsserted( |
140 | | - () -> { |
141 | | - List<Metric> metrics = |
142 | | - otlpServer.getMetrics().stream() |
143 | | - .map(ExportMetricsServiceRequest::getResourceMetricsList) |
144 | | - .flatMap(rm -> rm.stream().map(ResourceMetrics::getScopeMetricsList)) |
145 | | - .flatMap(Collection::stream) |
146 | | - .filter( |
147 | | - sm -> |
148 | | - sm.getScope().getName().equals("io.opentelemetry.contrib.jmxmetrics") |
149 | | - && sm.getScope().getVersion().equals(expectedMeterVersion())) |
150 | | - .flatMap(sm -> sm.getMetricsList().stream()) |
151 | | - .collect(Collectors.toList()); |
152 | | - |
153 | | - assertThat(metrics).isNotEmpty(); |
154 | | - |
155 | | - for (Consumer<Metric> assertion : assertions) { |
156 | | - assertThat(metrics).anySatisfy(assertion); |
157 | | - } |
158 | | - }); |
| 138 | + waitAndAssertMetrics( |
| 139 | + () -> { |
| 140 | + List<Metric> metrics = |
| 141 | + otlpServer.getMetrics().stream() |
| 142 | + .map(ExportMetricsServiceRequest::getResourceMetricsList) |
| 143 | + .flatMap(rm -> rm.stream().map(ResourceMetrics::getScopeMetricsList)) |
| 144 | + .flatMap(Collection::stream) |
| 145 | + .filter( |
| 146 | + sm -> |
| 147 | + sm.getScope().getName().equals("io.opentelemetry.contrib.jmxmetrics") |
| 148 | + && sm.getScope().getVersion().equals(expectedMeterVersion())) |
| 149 | + .flatMap(sm -> sm.getMetricsList().stream()) |
| 150 | + .collect(Collectors.toList()); |
| 151 | + |
| 152 | + assertThat(metrics).isNotEmpty(); |
| 153 | + |
| 154 | + for (Consumer<Metric> assertion : assertions) { |
| 155 | + assertThat(metrics).anySatisfy(assertion); |
| 156 | + } |
| 157 | + }); |
| 158 | + } |
| 159 | + |
| 160 | + private static void waitAndAssertMetrics(Runnable assertion) { |
| 161 | + try { |
| 162 | + await().atMost(Duration.ofSeconds(30)).untilAsserted(assertion::run); |
| 163 | + } catch (Throwable t) { |
| 164 | + if (t instanceof ConditionTimeoutException) { |
| 165 | + // Don't throw this failure since the stack is the awaitility thread, causing confusion. |
| 166 | + // Instead, just assert one more time on the test thread, which will fail with a better |
| 167 | + // stack trace - that is on the same thread as the test. |
| 168 | + assertion.run(); |
| 169 | + } else { |
| 170 | + throw t; |
| 171 | + } |
| 172 | + } |
159 | 173 | } |
160 | 174 |
|
161 | 175 | @SafeVarargs |
|
0 commit comments