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

davsclaus 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 872d750c8fa6 CAMEL-19545: Replace sleep-based synchronization in 
camel-stream tests
872d750c8fa6 is described below

commit 872d750c8fa6bddf5607df49351d49bdc37f1d5e
Author: Ravi <[email protected]>
AuthorDate: Sat Jul 11 23:58:35 2026 +0530

    CAMEL-19545: Replace sleep-based synchronization in camel-stream tests
    
    Replace Thread.sleep() calls with fos.flush() for write synchronization
    and Awaitility/MockEndpoint timed assertions for read synchronization in
    ScanStreamFileTest and ScanStreamFileWithFilterTest. This removes flaky
    timing assumptions from the camel-stream test suite.
---
 .../camel/component/stream/ScanStreamFileTest.java | 26 +++++++++++-----------
 .../stream/ScanStreamFileWithFilterTest.java       | 14 ++++--------
 2 files changed, 17 insertions(+), 23 deletions(-)

diff --git 
a/components/camel-stream/src/test/java/org/apache/camel/component/stream/ScanStreamFileTest.java
 
b/components/camel-stream/src/test/java/org/apache/camel/component/stream/ScanStreamFileTest.java
index 9f58b0c8c114..0e9c85329c69 100644
--- 
a/components/camel-stream/src/test/java/org/apache/camel/component/stream/ScanStreamFileTest.java
+++ 
b/components/camel-stream/src/test/java/org/apache/camel/component/stream/ScanStreamFileTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.stream;
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
@@ -26,6 +27,7 @@ import org.junit.jupiter.api.Test;
 
 import static org.apache.camel.test.junit6.TestSupport.createDirectory;
 import static org.apache.camel.test.junit6.TestSupport.deleteDirectory;
+import static org.awaitility.Awaitility.await;
 
 /**
  * Unit test for scan stream file
@@ -58,15 +60,14 @@ public class ScanStreamFileTest extends CamelTestSupport {
         FileOutputStream fos = new FileOutputStream(file);
         try {
             fos.write("Hello\n".getBytes());
-            Thread.sleep(150);
+            fos.flush();
             fos.write("World\n".getBytes());
-            // ensure it does not read the file again
-            Thread.sleep(1000);
+            fos.flush();
         } finally {
             fos.close();
         }
 
-        MockEndpoint.assertIsSatisfied(context);
+        MockEndpoint.assertIsSatisfied(context, 10, TimeUnit.SECONDS);
     }
 
     @Test
@@ -79,22 +80,22 @@ public class ScanStreamFileTest extends CamelTestSupport {
         FileOutputStream fos = refreshFile(null);
         try {
             fos.write("Hello\nWorld\n".getBytes());
-            Thread.sleep(150);
+            fos.flush();
 
             context.getRouteController().startAllRoutes();
 
-            // roll-over file
-            Thread.sleep(1500);
+            // wait for the initial 2 messages before rolling over the file
+            await().atMost(10, TimeUnit.SECONDS).until(() -> 
mock.getReceivedCounter() >= 2);
+
             fos = refreshFile(fos);
             fos.write("Bye\nWorld\n".getBytes());
             fos.write("!\n".getBytes());
-            // ensure it does not read the file again
-            Thread.sleep(1500);
+            fos.flush();
         } finally {
             fos.close();
         }
 
-        MockEndpoint.assertIsSatisfied(context);
+        MockEndpoint.assertIsSatisfied(context, 10, TimeUnit.SECONDS);
     }
 
     @Test
@@ -105,14 +106,13 @@ public class ScanStreamFileTest extends CamelTestSupport {
         FileOutputStream fos = refreshFile(null);
         try {
             fos.write("Hello\nthere\nWorld\n!\n".getBytes());
+            fos.flush();
             context.getRouteController().startAllRoutes();
-            // ensure it does not read the file again
-            Thread.sleep(1000);
         } finally {
             fos.close();
         }
 
-        MockEndpoint.assertIsSatisfied(context);
+        MockEndpoint.assertIsSatisfied(context, 10, TimeUnit.SECONDS);
     }
 
     private FileOutputStream refreshFile(FileOutputStream fos) throws 
Exception {
diff --git 
a/components/camel-stream/src/test/java/org/apache/camel/component/stream/ScanStreamFileWithFilterTest.java
 
b/components/camel-stream/src/test/java/org/apache/camel/component/stream/ScanStreamFileWithFilterTest.java
index 0499f3b776a0..ba0b251f9963 100644
--- 
a/components/camel-stream/src/test/java/org/apache/camel/component/stream/ScanStreamFileWithFilterTest.java
+++ 
b/components/camel-stream/src/test/java/org/apache/camel/component/stream/ScanStreamFileWithFilterTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.stream;
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
@@ -50,24 +51,17 @@ public class ScanStreamFileWithFilterTest extends 
CamelTestSupport {
 
         FileOutputStream fos = new FileOutputStream(file);
         fos.write("Hello\n".getBytes());
-        Thread.sleep(150);
         fos.write("World\n".getBytes());
-        Thread.sleep(150);
         fos.write("Hello\n".getBytes());
-        Thread.sleep(150);
         fos.write("World\n".getBytes());
-        Thread.sleep(150);
         fos.write("Hello\n".getBytes());
-        Thread.sleep(150);
         fos.write("World\n".getBytes());
-        Thread.sleep(150);
         fos.write("Hello Boy\n".getBytes());
-        Thread.sleep(150);
         fos.write("World\n".getBytes());
-
-        MockEndpoint.assertIsSatisfied(context);
-
+        fos.flush();
         fos.close();
+
+        MockEndpoint.assertIsSatisfied(context, 10, TimeUnit.SECONDS);
     }
 
     @Override

Reply via email to