This is an automated email from the ASF dual-hosted git repository. apupier pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 36503c9233604fc98b0011bc76b15bf4bb96d463 Author: Torsten Mielke <[email protected]> AuthorDate: Thu Sep 24 13:49:03 2026 +0200 CAMEL-24957: Fix flaky test MessageHistoryExceptionRouteTest Two distinct failure modes were addressed: 1. Race condition (Failure 1 - "expected: <5> but was: <4>"): Metric recording via nodeProcessingDone() happens asynchronously on seda threads. MockEndpoint.assertIsSatisfied() only waits for mock endpoints to receive messages, not for metrics to be flushed. The bare assertEquals() calls could run before all nodeProcessingDone() callbacks had fired. Fix: wrap all metric count assertions in a single await().untilAsserted() block. 2. Cleanup failure on rerun (Failure 2 - ConditionTimeoutException): CamelOpenTelemetryExtension implements BeforeEachCallback and AfterEachCallback, but the otelExtension field in AbstractOpenTelemetryTestSupport was missing @RegisterExtension, so JUnit never invoked those lifecycle methods. On a test retry in the same JVM the SDK was neither reset nor reinitialized, causing metrics to be undetectable. Fix: add @RegisterExtension to otelExtension in AbstractOpenTelemetryTestSupport. Co-authored-by: Claude Sonnet 4.5 <[email protected]> --- .../metrics/AbstractOpenTelemetryTestSupport.java | 2 ++ .../MessageHistoryExceptionRouteTest.java | 17 ++++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/components/camel-opentelemetry-metrics/src/test/java/org/apache/camel/opentelemetry/metrics/AbstractOpenTelemetryTestSupport.java b/components/camel-opentelemetry-metrics/src/test/java/org/apache/camel/opentelemetry/metrics/AbstractOpenTelemetryTestSupport.java index 0cd1b0060392..632284b3c5b3 100644 --- a/components/camel-opentelemetry-metrics/src/test/java/org/apache/camel/opentelemetry/metrics/AbstractOpenTelemetryTestSupport.java +++ b/components/camel-opentelemetry-metrics/src/test/java/org/apache/camel/opentelemetry/metrics/AbstractOpenTelemetryTestSupport.java @@ -26,6 +26,7 @@ import io.opentelemetry.sdk.metrics.data.LongPointData; import io.opentelemetry.sdk.metrics.data.MetricData; import io.opentelemetry.sdk.metrics.data.PointData; import org.apache.camel.test.junit6.CamelTestSupport; +import org.junit.jupiter.api.extension.RegisterExtension; import static org.apache.camel.opentelemetry.metrics.OpenTelemetryConstants.ROUTE_ID_ATTRIBUTE; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -34,6 +35,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public class AbstractOpenTelemetryTestSupport extends CamelTestSupport { + @RegisterExtension public final CamelOpenTelemetryExtension otelExtension = CamelOpenTelemetryExtension.create(); protected List<MetricData> getMetricData(String metricName) { diff --git a/components/camel-opentelemetry-metrics/src/test/java/org/apache/camel/opentelemetry/metrics/messagehistory/MessageHistoryExceptionRouteTest.java b/components/camel-opentelemetry-metrics/src/test/java/org/apache/camel/opentelemetry/metrics/messagehistory/MessageHistoryExceptionRouteTest.java index b58bcbeb45c0..fde1f5982a62 100644 --- a/components/camel-opentelemetry-metrics/src/test/java/org/apache/camel/opentelemetry/metrics/messagehistory/MessageHistoryExceptionRouteTest.java +++ b/components/camel-opentelemetry-metrics/src/test/java/org/apache/camel/opentelemetry/metrics/messagehistory/MessageHistoryExceptionRouteTest.java @@ -61,13 +61,16 @@ public class MessageHistoryExceptionRouteTest extends AbstractOpenTelemetryTestS MockEndpoint.assertIsSatisfied(context); - // there should be 3 names for the message history (foo, bar, exception) - assertEquals(3, getAllPointData(DEFAULT_CAMEL_MESSAGE_HISTORY_METER_NAME).size()); - assertEquals(5, getPointData("route1", "foo").getCount()); - assertEquals(5, getPointData("route2", "bar").getCount()); - // exception process node - await().atMost(5, TimeUnit.SECONDS) - .untilAsserted(() -> assertEquals(5, getPointData("route2", "process1").getCount())); + // Metric recording via nodeProcessingDone() happens asynchronously on seda threads, + // so all metric assertions need to be wrapped in await() to avoid a race with mock receipt. + await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> { + // there should be 3 names for the message history (foo, bar, process1) + assertEquals(3, getAllPointData(DEFAULT_CAMEL_MESSAGE_HISTORY_METER_NAME).size()); + assertEquals(5, getPointData("route1", "foo").getCount()); + assertEquals(5, getPointData("route2", "bar").getCount()); + // exception process node + assertEquals(5, getPointData("route2", "process1").getCount()); + }); } private HistogramPointData getPointData(String routeId, String nodeId) {
