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 aeb745cd5b9c Replace Thread.sleep with Awaitility in camel-core tests 
(#24370)
aeb745cd5b9c is described below

commit aeb745cd5b9c908094643792cef0a48885842464
Author: Guillaume Nodet <[email protected]>
AuthorDate: Thu Jul 2 09:39:03 2026 +0200

    Replace Thread.sleep with Awaitility in camel-core tests (#24370)
    
    Replace Thread.sleep() calls with Awaitility await() in two camel-core
    test files to address SonarCloud rule S2925. Only 2 of ~54 Thread.sleep
    calls in camel-core tests were eligible for replacement - the rest are
    legitimate (simulating I/O delays in processors, pacing message sends
    for time-based assertions, or in manual tests).
    
    Co-authored-by: Claude Opus 4.6 <[email protected]>
---
 ...llEnrichFileDefaultAggregationStrategyTest.java | 12 +++++++--
 ...lingExceptionRoutePolicyKeepOpenOnInitTest.java | 31 +++++++++++-----------
 2 files changed, 25 insertions(+), 18 deletions(-)

diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnrichFileDefaultAggregationStrategyTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnrichFileDefaultAggregationStrategyTest.java
index 6745ac920167..48811519d3a0 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnrichFileDefaultAggregationStrategyTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnrichFileDefaultAggregationStrategyTest.java
@@ -16,12 +16,17 @@
  */
 package org.apache.camel.processor.enricher;
 
+import java.util.concurrent.TimeUnit;
+
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
+import org.apache.camel.ServiceStatus;
 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;
+
 public class PollEnrichFileDefaultAggregationStrategyTest extends 
ContextTestSupport {
 
     @Test
@@ -38,8 +43,11 @@ public class PollEnrichFileDefaultAggregationStrategyTest 
extends ContextTestSup
 
         context.getRouteController().startAllRoutes();
 
-        log.info("Sleeping for 0.25 sec before writing enrichdata file");
-        Thread.sleep(250);
+        // wait for the route to be fully started before writing enrichdata 
file
+        await().atMost(5, TimeUnit.SECONDS)
+                .until(() -> context.getRoutes().stream()
+                        .allMatch(r -> context.getRouteController()
+                                .getRouteStatus(r.getRouteId()) == 
ServiceStatus.Started));
         template.sendBodyAndHeader(fileUri("enrichdata"), "Big file",
                 Exchange.FILE_NAME, "AAA.dat");
         log.info("... write done");
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyKeepOpenOnInitTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyKeepOpenOnInitTest.java
index f11ae11c1fa8..fe84be3adc22 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyKeepOpenOnInitTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyKeepOpenOnInitTest.java
@@ -16,6 +16,8 @@
  */
 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;
@@ -25,6 +27,9 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.condition.EnabledOnOs;
 import org.junit.jupiter.api.condition.OS;
 
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
 @EnabledOnOs(value = { OS.LINUX, OS.MAC, OS.FREEBSD, OS.OPENBSD },
              architectures = { "amd64", "aarch64", "ppc64le" },
              disabledReason = "This test does not run reliably multiple 
platforms (see CAMEL-21438)")
@@ -63,15 +68,11 @@ public class 
ThrottlingExceptionRoutePolicyKeepOpenOnInitTest extends ContextTes
             template.sendBody(url, "Message " + i);
         }
 
-        // gives time for policy half open check to run every second
-        // and should not close b/c keepOpen is true
-        Thread.sleep(500);
-
-        // gives time for policy half open check to run every second
-        // but it should never close b/c keepOpen is true
-        result.expectedMessageCount(0);
-        result.setResultWaitTime(1000);
-        assertMockEndpointsSatisfied();
+        // gives time for policy half open check to run
+        // and verifies it should not close b/c keepOpen is true
+        await().pollDelay(500, TimeUnit.MILLISECONDS)
+                .atMost(2, TimeUnit.SECONDS)
+                .untilAsserted(() -> assertEquals(0, 
result.getReceivedCounter()));
     }
 
     @Test
@@ -81,13 +82,11 @@ public class 
ThrottlingExceptionRoutePolicyKeepOpenOnInitTest extends ContextTes
             template.sendBody(url, "Message " + i);
         }
 
-        // gives time for policy half open check to run every second
-        // and should not close b/c keepOpen is true
-        Thread.sleep(500);
-
-        result.expectedMessageCount(0);
-        result.setResultWaitTime(1500);
-        assertMockEndpointsSatisfied();
+        // gives time for policy half open check to run
+        // and verifies it should not close b/c keepOpen is true
+        await().pollDelay(500, TimeUnit.MILLISECONDS)
+                .atMost(2, TimeUnit.SECONDS)
+                .untilAsserted(() -> assertEquals(0, 
result.getReceivedCounter()));
 
         // set keepOpen to false
         // now half open check will succeed

Reply via email to