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 d189252fa91b CAMEL-21438: Fix flaky file-based tests
d189252fa91b is described below

commit d189252fa91b64db23c1cd2f3351aaacd948500a
Author: Adriano Machado <[email protected]>
AuthorDate: Sat Jun 20 01:20:47 2026 -0400

    CAMEL-21438: Fix flaky file-based tests
    
    Replace Thread.sleep and add missing synchronization guards in file
    component tests to eliminate non-determinism:
    
    - PollEnrichFileCustomAggregationStrategyTest: replace Thread.sleep(500)
      with MockEndpoint.await on mock:start
    - FileConsumeHiddenFilesTest: add explicit Awaitility atMost timeout
    - FileProducerFileExistFailTest and GenericFileConverterTest: add
      MockEndpoint.await before assertMockEndpointsSatisfied
    - FileProducerCharset UTF tests: add Awaitility guard for file flush,
      assert matchesWaitTime return value
    - All MockEndpoint.await calls wrapped with assertTrue for explicit
      timeout failure reporting
    
    Closes #24123
    
    Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
---
 .../camel/component/file/FileConsumeHiddenFilesTest.java  |  3 ++-
 .../file/FileProducerCharsetUTFOptimizedTest.java         | 14 +++++++++-----
 .../file/FileProducerCharsetUTFtoISOConfiguredTest.java   | 12 ++++++++----
 .../component/file/FileProducerCharsetUTFtoUTFTest.java   | 15 ++++++++++-----
 .../component/file/FileProducerFileExistFailTest.java     |  3 +++
 .../camel/component/file/GenericFileConverterTest.java    | 10 ++++++++++
 .../PollEnrichFileCustomAggregationStrategyTest.java      |  8 ++++++--
 7 files changed, 48 insertions(+), 17 deletions(-)

diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeHiddenFilesTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeHiddenFilesTest.java
index c695432a8295..f7458c6a8684 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeHiddenFilesTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeHiddenFilesTest.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.file;
 
 import java.nio.file.Files;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
@@ -42,7 +43,7 @@ public class FileConsumeHiddenFilesTest extends 
ContextTestSupport {
 
         assertMockEndpointsSatisfied();
 
-        Awaitility.await().untilAsserted(() -> {
+        Awaitility.await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> {
             // file should be deleted
             assertFalse(Files.exists(testFile("report.txt")), "File should 
been deleted");
             assertFalse(Files.exists(testFile(".report.hidden")), "File should 
been deleted");
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFOptimizedTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFOptimizedTest.java
index 1c2b4f63d38a..5d2ac15dda0e 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFOptimizedTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFOptimizedTest.java
@@ -20,9 +20,11 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
+import org.awaitility.Awaitility;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
@@ -53,12 +55,14 @@ public class FileProducerCharsetUTFOptimizedTest extends 
ContextTestSupport {
 
     @Test
     public void testFileProducerCharsetUTFOptimized() throws Exception {
-        oneExchangeDone.matchesWaitTime();
+        assertTrue(oneExchangeDone.matchesWaitTime());
 
-        assertTrue(Files.exists(testFile("output.txt")), "File should exist");
-
-        byte[] data = Files.readAllBytes(testFile("output.txt"));
-        assertArrayEquals(utf, data);
+        // The file may have been created but not yet fully flushed to disk
+        Awaitility.await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> {
+            assertTrue(Files.exists(testFile("output.txt")), "File should 
exist");
+            byte[] data = Files.readAllBytes(testFile("output.txt"));
+            assertArrayEquals(utf, data);
+        });
     }
 
     @Override
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConfiguredTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConfiguredTest.java
index 693b1d3accad..62b60748aa73 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConfiguredTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConfiguredTest.java
@@ -19,9 +19,11 @@ package org.apache.camel.component.file;
 import java.io.OutputStream;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
+import org.awaitility.Awaitility;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -43,10 +45,12 @@ class FileProducerCharsetUTFtoISOConfiguredTest extends 
ContextTestSupport {
 
         assertTrue(oneExchangeDone.matchesWaitTime());
 
-        assertFileExists(testFile(OUTPUT_FILE));
-        byte[] data = Files.readAllBytes(testFile(OUTPUT_FILE));
-
-        assertEquals(DATA, new String(data, StandardCharsets.ISO_8859_1));
+        // The file may have been created but not yet fully flushed to disk
+        Awaitility.await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> {
+            assertFileExists(testFile(OUTPUT_FILE));
+            byte[] data = Files.readAllBytes(testFile(OUTPUT_FILE));
+            assertEquals(DATA, new String(data, StandardCharsets.ISO_8859_1));
+        });
     }
 
     @Override
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoUTFTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoUTFTest.java
index 40466e0a9df7..e78881327bd2 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoUTFTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoUTFTest.java
@@ -19,9 +19,11 @@ package org.apache.camel.component.file;
 import java.io.OutputStream;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
+import org.awaitility.Awaitility;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
@@ -43,11 +45,14 @@ class FileProducerCharsetUTFtoUTFTest extends 
ContextTestSupport {
 
         assertTrue(oneExchangeDone.matchesWaitTime());
 
-        assertFileExists(testFile(OUTPUT_FILE));
-        byte[] target = Files.readAllBytes(testFile(OUTPUT_FILE));
-
-        assertArrayEquals(source, target, "The byte arrays should be equals 
but they are not.\n Source:\n" + new String(source)
-                                          + "\nTarget:\n" + new 
String(target));
+        // The file may have been created but not yet fully flushed to disk
+        Awaitility.await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> {
+            assertFileExists(testFile(OUTPUT_FILE));
+            byte[] target = Files.readAllBytes(testFile(OUTPUT_FILE));
+            assertArrayEquals(source, target,
+                    "The byte arrays should be equals but they are not.\n 
Source:\n" + new String(source)
+                                              + "\nTarget:\n" + new 
String(target));
+        });
     }
 
     @Override
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerFileExistFailTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerFileExistFailTest.java
index 0e8d02793459..e4c68a0f72e1 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerFileExistFailTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerFileExistFailTest.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.file;
 
 import java.util.UUID;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.CamelExecutionException;
 import org.apache.camel.ContextTestSupport;
@@ -28,6 +29,7 @@ import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 public class FileProducerFileExistFailTest extends ContextTestSupport {
     private static final String TEST_FILE_NAME = "hello." + UUID.randomUUID() 
+ ".txt";
@@ -51,6 +53,7 @@ public class FileProducerFileExistFailTest extends 
ContextTestSupport {
                         "File already exist: " + 
testFile(TEST_FILE_NAME).toString() + ". Cannot write new file."),
                 cause.getMessage());
 
+        assertTrue(mock.await(10, TimeUnit.SECONDS), "Timed out waiting for 
mock endpoint");
         assertMockEndpointsSatisfied();
     }
 
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/file/GenericFileConverterTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/file/GenericFileConverterTest.java
index 1a57c64c062a..991f5fca5a10 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/file/GenericFileConverterTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/file/GenericFileConverterTest.java
@@ -21,6 +21,7 @@ import java.io.File;
 import java.io.InputStream;
 import java.io.Serializable;
 import java.util.UUID;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
@@ -30,6 +31,8 @@ import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.converter.stream.InputStreamCache;
 import org.junit.jupiter.api.Test;
 
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
 public class GenericFileConverterTest extends ContextTestSupport {
 
     public static final String TEST_FILE_NAME = "hello." + UUID.randomUUID() + 
".txt";
@@ -55,6 +58,7 @@ public class GenericFileConverterTest extends 
ContextTestSupport {
 
         template.sendBodyAndHeader(fileUri(), "Hello World", 
Exchange.FILE_NAME, TEST_FILE_NAME);
 
+        assertTrue(mock.await(10, TimeUnit.SECONDS), "Timed out waiting for 
mock endpoint");
         assertMockEndpointsSatisfied();
     }
 
@@ -75,6 +79,7 @@ public class GenericFileConverterTest extends 
ContextTestSupport {
 
         template.sendBodyAndHeader(fileUri(), "Hello World", 
Exchange.FILE_NAME, TEST_FILE_NAME);
 
+        assertTrue(mock.await(10, TimeUnit.SECONDS), "Timed out waiting for 
mock endpoint");
         assertMockEndpointsSatisfied();
     }
 
@@ -95,6 +100,7 @@ public class GenericFileConverterTest extends 
ContextTestSupport {
 
         template.sendBodyAndHeader(fileUri(), "Hello World", 
Exchange.FILE_NAME, TEST_FILE_NAME);
 
+        assertTrue(mock.await(10, TimeUnit.SECONDS), "Timed out waiting for 
mock endpoint");
         assertMockEndpointsSatisfied();
     }
 
@@ -115,6 +121,7 @@ public class GenericFileConverterTest extends 
ContextTestSupport {
 
         template.sendBodyAndHeader(fileUri(), "Hello World", 
Exchange.FILE_NAME, TEST_FILE_NAME);
 
+        assertTrue(mock.await(10, TimeUnit.SECONDS), "Timed out waiting for 
mock endpoint");
         assertMockEndpointsSatisfied();
     }
 
@@ -135,6 +142,7 @@ public class GenericFileConverterTest extends 
ContextTestSupport {
 
         template.sendBodyAndHeader(fileUri(), "Hello World", 
Exchange.FILE_NAME, TEST_FILE_NAME);
 
+        assertTrue(mock.await(10, TimeUnit.SECONDS), "Timed out waiting for 
mock endpoint");
         assertMockEndpointsSatisfied();
     }
 
@@ -161,6 +169,7 @@ public class GenericFileConverterTest extends 
ContextTestSupport {
 
         template.sendBodyAndHeader(fileUri(), "Hello World", 
Exchange.FILE_NAME, TEST_FILE_NAME);
 
+        assertTrue(mock.await(10, TimeUnit.SECONDS), "Timed out waiting for 
mock endpoint");
         assertMockEndpointsSatisfied();
     }
 
@@ -192,6 +201,7 @@ public class GenericFileConverterTest extends 
ContextTestSupport {
 
         template.sendBodyAndHeader(fileUri(), "Hello World", 
Exchange.FILE_NAME, TEST_FILE_NAME);
 
+        assertTrue(mock.await(10, TimeUnit.SECONDS), "Timed out waiting for 
mock endpoint");
         assertMockEndpointsSatisfied();
     }
 
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnrichFileCustomAggregationStrategyTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnrichFileCustomAggregationStrategyTest.java
index 0defda6f021d..274fa48cca8d 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnrichFileCustomAggregationStrategyTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/enricher/PollEnrichFileCustomAggregationStrategyTest.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.processor.enricher;
 
+import java.util.concurrent.TimeUnit;
+
 import org.apache.camel.AggregationStrategy;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
@@ -23,6 +25,8 @@ import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.junit.jupiter.api.Test;
 
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
 public class PollEnrichFileCustomAggregationStrategyTest extends 
ContextTestSupport {
 
     @Test
@@ -39,8 +43,8 @@ public class PollEnrichFileCustomAggregationStrategyTest 
extends ContextTestSupp
 
         context.getRouteController().startAllRoutes();
 
-        log.info("Sleeping for 0.5 sec before writing enrichdata file");
-        Thread.sleep(500);
+        assertTrue(getMockEndpoint("mock:start").await(10, TimeUnit.SECONDS), 
"Timed out waiting for mock:start");
+        log.info("mock:start satisfied, writing enrichdata file");
         template.sendBodyAndHeader(fileUri("enrichdata"), "Big file",
                 Exchange.FILE_NAME, "AAA.dat");
         log.info("... write done");

Reply via email to