This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 1f26a2259724 chore(test): fix 10 flaky tests in camel-core and 
camel-management (#23175)
1f26a2259724 is described below

commit 1f26a2259724b3a680a620ce23f99ba84809e649
Author: Guillaume Nodet <[email protected]>
AuthorDate: Wed May 13 09:51:32 2026 +0200

    chore(test): fix 10 flaky tests in camel-core and camel-management (#23175)
    
    Replace Thread.sleep() with Awaitility, increase tight timeouts,
    and fix ordering assumptions on async routes.
    
    Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
---
 .../FileConsumeAlterFileNameHeaderIssueTest.java   | 24 +++++++++++------
 .../camel/impl/DurationRoutePolicyFactoryTest.java |  4 +--
 .../processor/MulticastParallelStreamingTest.java  |  2 +-
 .../OnCompletionBeforeChainedSedaRoutesTest.java   |  2 +-
 ...equenceStreamNotIgnoreInvalidExchangesTest.java |  2 +-
 .../apache/camel/processor/SagaTimeoutTest.java    |  4 +--
 ...xceptionRoutePolicyHalfOpenHandlerSedaTest.java |  6 ++---
 ...tlingExceptionRoutePolicyOpenViaConfigTest.java | 31 ++++++++++++----------
 .../support/task/task/ForegroundTimeTaskTest.java  |  2 +-
 .../management/AbstractManagedThrottlerTest.java   |  9 ++++---
 10 files changed, 50 insertions(+), 36 deletions(-)

diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeAlterFileNameHeaderIssueTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeAlterFileNameHeaderIssueTest.java
index 486cd05331f0..fc0c024e7cd6 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeAlterFileNameHeaderIssueTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeAlterFileNameHeaderIssueTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.file;
 
 import java.nio.file.Files;
 import java.util.UUID;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
@@ -25,6 +26,7 @@ import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.junit.jupiter.api.Test;
 
+import static org.awaitility.Awaitility.await;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
@@ -45,7 +47,7 @@ public class FileConsumeAlterFileNameHeaderIssueTest extends 
ContextTestSupport
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() {
-                from(fileUri() + "?initialDelay=0&delay=10&delete=true")
+                from(fileUri() + "?initialDelay=500&delay=10&delete=true")
                         // remove all headers
                         .removeHeaders("*").to("mock:result");
             }
@@ -65,7 +67,8 @@ public class FileConsumeAlterFileNameHeaderIssueTest extends 
ContextTestSupport
         // the original file should have been deleted, as the file consumer
         // should be resilient against
         // end users deleting headers
-        assertFalse(Files.exists(testFile(TEST_FILE_NAME)), "File should been 
deleted");
+        await().atMost(5, TimeUnit.SECONDS)
+                .untilAsserted(() -> 
assertFalse(Files.exists(testFile(TEST_FILE_NAME)), "File should been 
deleted"));
     }
 
     @Test
@@ -73,7 +76,7 @@ public class FileConsumeAlterFileNameHeaderIssueTest extends 
ContextTestSupport
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() {
-                from(fileUri() + "?initialDelay=0&delay=10&delete=true")
+                from(fileUri() + "?initialDelay=500&delay=10&delete=true")
                         // change file header
                         .setHeader(Exchange.FILE_NAME, 
constant(TEST_FILE_NAME_BYE)).to("mock:result");
             }
@@ -92,7 +95,8 @@ public class FileConsumeAlterFileNameHeaderIssueTest extends 
ContextTestSupport
         // the original file should have been deleted, as the file consumer
         // should be resilient against
         // end users changing headers
-        assertFalse(Files.exists(testFile(TEST_FILE_NAME)), "File should been 
deleted");
+        await().atMost(5, TimeUnit.SECONDS)
+                .untilAsserted(() -> 
assertFalse(Files.exists(testFile(TEST_FILE_NAME)), "File should been 
deleted"));
     }
 
     @Test
@@ -100,7 +104,7 @@ public class FileConsumeAlterFileNameHeaderIssueTest 
extends ContextTestSupport
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() {
-                from(fileUri() + "?initialDelay=0&delay=10")
+                from(fileUri() + "?initialDelay=500&delay=10")
                         // remove all headers
                         .removeHeaders("*").to("mock:result");
             }
@@ -120,7 +124,9 @@ public class FileConsumeAlterFileNameHeaderIssueTest 
extends ContextTestSupport
         // the original file should have been moved, as the file consumer 
should
         // be resilient against
         // end users deleting headers
-        assertTrue(Files.exists(testFile(".camel/" + TEST_FILE_NAME)), "File 
should been moved");
+        await().atMost(5, TimeUnit.SECONDS)
+                .untilAsserted(
+                        () -> assertTrue(Files.exists(testFile(".camel/" + 
TEST_FILE_NAME)), "File should been moved"));
     }
 
     @Test
@@ -128,7 +134,7 @@ public class FileConsumeAlterFileNameHeaderIssueTest 
extends ContextTestSupport
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() {
-                from(fileUri() + "?initialDelay=0&delay=10")
+                from(fileUri() + "?initialDelay=500&delay=10")
                         // change file header
                         .setHeader(Exchange.FILE_NAME, 
constant(TEST_FILE_NAME_BYE)).to("mock:result");
             }
@@ -147,7 +153,9 @@ public class FileConsumeAlterFileNameHeaderIssueTest 
extends ContextTestSupport
         // the original file should have been moved, as the file consumer 
should
         // be resilient against
         // end users changing headers
-        assertTrue(Files.exists(testFile(".camel/" + TEST_FILE_NAME)), "File 
should been moved");
+        await().atMost(5, TimeUnit.SECONDS)
+                .untilAsserted(
+                        () -> assertTrue(Files.exists(testFile(".camel/" + 
TEST_FILE_NAME)), "File should been moved"));
     }
 
 }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/impl/DurationRoutePolicyFactoryTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/impl/DurationRoutePolicyFactoryTest.java
index cc44c1900854..bccb2741e45d 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/impl/DurationRoutePolicyFactoryTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/impl/DurationRoutePolicyFactoryTest.java
@@ -37,7 +37,7 @@ public class DurationRoutePolicyFactoryTest extends 
ContextTestSupport {
         assertMockEndpointsSatisfied();
 
         // need some time to stop async
-        await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> {
+        await().atMost(15, TimeUnit.SECONDS).untilAsserted(() -> {
             
assertFalse(context.getRouteController().getRouteStatus("foo").isStarted());
             
assertTrue(context.getRouteController().getRouteStatus("foo").isStopped());
         });
@@ -49,7 +49,7 @@ public class DurationRoutePolicyFactoryTest extends 
ContextTestSupport {
             @Override
             public void configure() {
                 DurationRoutePolicyFactory factory = new 
DurationRoutePolicyFactory();
-                factory.setMaxSeconds(2);
+                factory.setMaxSeconds(4);
                 factory.setMaxMessages(25);
 
                 getContext().addRoutePolicyFactory(factory);
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/MulticastParallelStreamingTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/processor/MulticastParallelStreamingTest.java
index 0203d0967068..35fdff3b5b39 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/processor/MulticastParallelStreamingTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/MulticastParallelStreamingTest.java
@@ -78,7 +78,7 @@ public class MulticastParallelStreamingTest extends 
ContextTestSupport {
                         // use end to indicate end of multicast route
                         .end().to("mock:result");
 
-                
from("direct:a").delay(500).asyncDelayed().setBody(constant("A"));
+                
from("direct:a").delay(2000).asyncDelayed().setBody(constant("A"));
 
                 from("direct:b").setBody(constant("B"));
             }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/OnCompletionBeforeChainedSedaRoutesTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/processor/OnCompletionBeforeChainedSedaRoutesTest.java
index 6cab838f2ca1..83fb10558886 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/processor/OnCompletionBeforeChainedSedaRoutesTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/OnCompletionBeforeChainedSedaRoutesTest.java
@@ -29,7 +29,7 @@ public class OnCompletionBeforeChainedSedaRoutesTest extends 
ContextTestSupport
         final var completionMockEndpoint = getMockEndpoint("mock:completion");
 
         completionMockEndpoint.expectedMessageCount(5);
-        completionMockEndpoint.expectedBodiesReceived(
+        completionMockEndpoint.expectedBodiesReceivedInAnyOrder(
                 "completion:a", "completion:b", "completion:c", body, 
"completion:d");
 
         template.sendBody("direct:a", body);
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/ResequenceStreamNotIgnoreInvalidExchangesTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/processor/ResequenceStreamNotIgnoreInvalidExchangesTest.java
index c60f0546228d..a9389a97a4b8 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/processor/ResequenceStreamNotIgnoreInvalidExchangesTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/ResequenceStreamNotIgnoreInvalidExchangesTest.java
@@ -90,7 +90,7 @@ public class ResequenceStreamNotIgnoreInvalidExchangesTest 
extends ContextTestSu
         return new RouteBuilder() {
             @Override
             public void configure() {
-                
from("direct:start").resequence(header("seqno")).stream().timeout(150).deliveryAttemptInterval(10)
+                
from("direct:start").resequence(header("seqno")).stream().timeout(2000).deliveryAttemptInterval(100)
                         .to("mock:result");
             }
         };
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/SagaTimeoutTest.java 
b/core/camel-core/src/test/java/org/apache/camel/processor/SagaTimeoutTest.java
index 1bd6d28f8043..14b57842abe7 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/processor/SagaTimeoutTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/SagaTimeoutTest.java
@@ -52,7 +52,7 @@ public class SagaTimeoutTest extends ContextTestSupport {
     public void testTimeoutHasNoEffectIfCompleted() throws Exception {
         MockEndpoint compensate = getMockEndpoint("mock:compensate");
         compensate.expectedMessageCount(1);
-        compensate.setResultWaitTime(500);
+        compensate.setResultWaitTime(2000);
 
         MockEndpoint complete = getMockEndpoint("mock:complete");
         complete.expectedMessageCount(1);
@@ -105,7 +105,7 @@ public class SagaTimeoutTest extends ContextTestSupport {
                         .completionMode(SagaCompletionMode.MANUAL)
                         .compensation("mock:compensate").to("mock:end");
 
-                from("direct:saga-auto").saga().timeout(350, 
TimeUnit.MILLISECONDS).option("id", constant("myid"))
+                from("direct:saga-auto").saga().timeout(2000, 
TimeUnit.MILLISECONDS).option("id", constant("myid"))
                         
.compensation("mock:compensate").completion("mock:complete")
                         .to("mock:end");
 
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyHalfOpenHandlerSedaTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyHalfOpenHandlerSedaTest.java
index 14df8e401a5e..a6a712ed6f4b 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyHalfOpenHandlerSedaTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyHalfOpenHandlerSedaTest.java
@@ -75,7 +75,7 @@ public class 
ThrottlingExceptionRoutePolicyHalfOpenHandlerSedaTest extends Conte
         final ServiceSupport consumer = (ServiceSupport) 
context.getRoute("foo").getConsumer();
 
         // wait long enough to have the consumer suspended
-        await().atMost(2, TimeUnit.SECONDS).until(consumer::isSuspended);
+        await().atMost(5, TimeUnit.SECONDS).until(consumer::isSuspended);
 
         // send more messages
         // but should get there (yet)
@@ -92,7 +92,7 @@ public class 
ThrottlingExceptionRoutePolicyHalfOpenHandlerSedaTest extends Conte
         result.expectedBodiesReceivedInAnyOrder(bodies);
 
         // wait long enough to have the consumer resumed
-        await().atMost(2, TimeUnit.SECONDS).until(consumer::isStarted);
+        await().atMost(5, TimeUnit.SECONDS).until(consumer::isStarted);
 
         // send message
         // should get through
@@ -109,7 +109,7 @@ public class 
ThrottlingExceptionRoutePolicyHalfOpenHandlerSedaTest extends Conte
             public void configure() {
                 int threshold = 2;
                 long failureWindow = 30;
-                long halfOpenAfter = 250;
+                long halfOpenAfter = 1000;
                 ThrottlingExceptionRoutePolicy policy
                         = new ThrottlingExceptionRoutePolicy(threshold, 
failureWindow, halfOpenAfter, null);
                 policy.setHalfOpenHandler(new AlwaysCloseHandler());
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyOpenViaConfigTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyOpenViaConfigTest.java
index 55bfaeb8af9e..35769a3abade 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyOpenViaConfigTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyOpenViaConfigTest.java
@@ -16,13 +16,18 @@
  */
 package org.apache.camel.processor.throttle;
 
+import java.util.concurrent.TimeUnit;
+
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.support.service.ServiceSupport;
 import org.apache.camel.throttling.ThrottlingExceptionRoutePolicy;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
+import static org.awaitility.Awaitility.await;
+
 public class ThrottlingExceptionRoutePolicyOpenViaConfigTest extends 
ContextTestSupport {
 
     private final String url = "seda:foo?concurrentConsumers=20";
@@ -51,16 +56,16 @@ public class 
ThrottlingExceptionRoutePolicyOpenViaConfigTest extends ContextTest
 
     @Test
     public void testThrottlingRoutePolicyStartWithAlwaysOpenOffThenToggle() 
throws Exception {
+        final ServiceSupport consumer = (ServiceSupport) 
context.getRoute("foo").getConsumer();
 
         // send first set of messages
         // should go through b/c circuit is closed
         int size = 5;
         for (int i = 0; i < size; i++) {
             template.sendBody(url, "MessageRound1 " + i);
-            Thread.sleep(3);
         }
         result.expectedMessageCount(size);
-        result.setResultWaitTime(1000);
+        result.setResultWaitTime(5000);
         assertMockEndpointsSatisfied();
 
         // set keepOpen to true
@@ -70,31 +75,29 @@ public class 
ThrottlingExceptionRoutePolicyOpenViaConfigTest extends ContextTest
         // by sending another message
         template.sendBody(url, "MessageTrigger");
 
-        // give time for circuit to open
-        Thread.sleep(500);
+        // wait for the circuit to open (consumer suspended)
+        await().atMost(5, TimeUnit.SECONDS).until(consumer::isSuspended);
 
         // send next set of messages
         // should NOT go through b/c circuit is open
         for (int i = 0; i < size; i++) {
             template.sendBody(url, "MessageRound2 " + i);
-            Thread.sleep(3);
         }
 
-        // gives time for policy half open check to run every second
-        // and should not close b/c keepOpen is true
-        Thread.sleep(500);
-
+        // should not close b/c keepOpen is true
         result.expectedMessageCount(size + 1);
-        result.setResultWaitTime(1000);
+        result.setResultWaitTime(2000);
         assertMockEndpointsSatisfied();
 
         // set keepOpen to false
         policy.setKeepOpen(false);
 
-        // gives time for policy half open check to run every second
-        // and it should close b/c keepOpen is false
+        // wait for the consumer to resume since keepOpen is now false
+        await().atMost(5, TimeUnit.SECONDS).until(consumer::isStarted);
+
+        // it should close b/c keepOpen is false — queued messages should now 
arrive
         result.expectedMessageCount(size * 2 + 1);
-        result.setResultWaitTime(1000);
+        result.setResultWaitTime(5000);
         assertMockEndpointsSatisfied();
     }
 
@@ -103,7 +106,7 @@ public class 
ThrottlingExceptionRoutePolicyOpenViaConfigTest extends ContextTest
         return new RouteBuilder() {
             @Override
             public void configure() {
-                
from(url).routePolicy(policy).log("${body}").to("log:foo?groupSize=10").to("mock:result");
+                
from(url).routeId("foo").routePolicy(policy).log("${body}").to("log:foo?groupSize=10").to("mock:result");
             }
         };
     }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/support/task/task/ForegroundTimeTaskTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/support/task/task/ForegroundTimeTaskTest.java
index 09561838cbab..545117e21448 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/support/task/task/ForegroundTimeTaskTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/support/task/task/ForegroundTimeTaskTest.java
@@ -63,7 +63,7 @@ class ForegroundTimeTaskTest extends TaskTestSupport {
         // this should run 5 times in a total duration of 6 seconds (5s 
executing + 1s delay)
         ForegroundTask task = Tasks.foregroundTask()
                 .withBudget(Budgets.iterationTimeBudget()
-                        .withMaxDuration(Duration.ofMillis(6_500)) // Add 500 
ms delay to make the test more flexible
+                        .withMaxDuration(Duration.ofMillis(10_000))
                         .withMaxIterations(5)
                         .withInitialDelay(Duration.ofSeconds(1))
                         .withInterval(Duration.ofSeconds(1))
diff --git 
a/core/camel-management/src/test/java/org/apache/camel/management/AbstractManagedThrottlerTest.java
 
b/core/camel-management/src/test/java/org/apache/camel/management/AbstractManagedThrottlerTest.java
index a6c552655e6f..331ba4adff3d 100644
--- 
a/core/camel-management/src/test/java/org/apache/camel/management/AbstractManagedThrottlerTest.java
+++ 
b/core/camel-management/src/test/java/org/apache/camel/management/AbstractManagedThrottlerTest.java
@@ -119,11 +119,14 @@ public abstract class AbstractManagedThrottlerTest 
extends ManagementTestSupport
             template.sendBody("seda:throttleCountAsync", "Message " + i);
         }
 
-        assertTrue(notifier.matches(5, TimeUnit.SECONDS));
+        assertTrue(notifier.matches(10, TimeUnit.SECONDS));
         assertMockEndpointsSatisfied();
 
-        Long completed = (Long) mbeanServer.getAttribute(routeName, 
"ExchangesCompleted");
-        assertEquals(10, completed.longValue());
+        Awaitility.await().atMost(15, TimeUnit.SECONDS)
+                .untilAsserted(() -> {
+                    Long c = (Long) mbeanServer.getAttribute(routeName, 
"ExchangesCompleted");
+                    assertEquals(10, c.longValue());
+                });
     }
 
     @DisabledOnOs(OS.WINDOWS)

Reply via email to