You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/04/17 16:15:29 UTC

[camel] branch main updated: (chores) camel-core: replace a few Thread.sleep in tests

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

orpiske 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 e4cf4f2339b (chores) camel-core: replace a few Thread.sleep in tests
e4cf4f2339b is described below

commit e4cf4f2339b1ffcc1b92d5bb61a46df8aae91e8e
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Mon Apr 17 17:11:33 2023 +0200

    (chores) camel-core: replace a few Thread.sleep in tests
---
 .../FileConcurrentWriteAppendSameFileTest.java     | 42 +++++++++++++++++++---
 .../file/FileConsumeDoneFileIssueTest.java         | 20 +++++++----
 2 files changed, 50 insertions(+), 12 deletions(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConcurrentWriteAppendSameFileTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConcurrentWriteAppendSameFileTest.java
index 285b4caf81f..27deef4a3ae 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConcurrentWriteAppendSameFileTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConcurrentWriteAppendSameFileTest.java
@@ -16,33 +16,62 @@
  */
 package org.apache.camel.component.file;
 
+import java.io.IOException;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Arrays;
 import java.util.LinkedHashSet;
 import java.util.Set;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 public class FileConcurrentWriteAppendSameFileTest extends ContextTestSupport {
+    private static final Logger LOG = LoggerFactory.getLogger(FileConcurrentWriteAppendSameFileTest.class);
 
     private final int size = 100;
+    private String data;
 
-    @Test
-    public void testConcurrentAppend() throws Exception {
+    @BeforeEach
+    void setUpData() {
         // create file with many lines
         StringBuilder sb = new StringBuilder();
         for (int i = 0; i < size; i++) {
             sb.append("Line ").append(i).append(LS);
         }
 
-        template.sendBodyAndHeader(fileUri(), sb.toString(), Exchange.FILE_NAME, "input.txt");
+        data = sb.toString();
+    }
+
+    private boolean fileIsOk() {
+        final Path path = testFile("outbox/result.txt");
+        if (Files.exists(path)) {
+            try {
+                final long expectedSize = 1790;
+
+                return Files.size(path) == expectedSize;
+            } catch (IOException e) {
+                LOG.error("IOException: {}", e.getMessage(), e);
+            }
+        }
+
+        return false;
+    }
+
+    @Test
+    public void testConcurrentAppend() throws Exception {
+        template.sendBodyAndHeader(fileUri(), data, Exchange.FILE_NAME, "input.txt");
 
         // start route
         MockEndpoint mock = getMockEndpoint("mock:result");
@@ -50,10 +79,11 @@ public class FileConcurrentWriteAppendSameFileTest extends ContextTestSupport {
         mock.expectsNoDuplicates(body());
         mock.setResultWaitTime(30000);
 
+        context.getRouteController().startRoute("foo");
+
         // we need to wait a bit for our slow CI server to make sure the entire
         // file is written on disc
-        Thread.sleep(500);
-        context.getRouteController().startRoute("foo");
+        Awaitility.await().atMost(500, TimeUnit.MILLISECONDS).until(this::fileIsOk);
 
         assertMockEndpointsSatisfied();
 
@@ -71,6 +101,8 @@ public class FileConcurrentWriteAppendSameFileTest extends ContextTestSupport {
         log.info(txt);
     }
 
+
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeDoneFileIssueTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeDoneFileIssueTest.java
index 6ef3e4d01e6..81228413987 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeDoneFileIssueTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeDoneFileIssueTest.java
@@ -17,11 +17,13 @@
 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;
 import org.apache.camel.builder.NotifyBuilder;
 import org.apache.camel.builder.RouteBuilder;
+import org.awaitility.Awaitility;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -52,10 +54,11 @@ public class FileConsumeDoneFileIssueTest extends ContextTestSupport {
         assertMockEndpointsSatisfied();
         assertTrue(notify.matchesWaitTime());
 
-        Thread.sleep(50);
 
         // the done file should be deleted
-        assertFalse(Files.exists(testFile("done/foo.done")), "Done file should be deleted");
+        Awaitility.await().atLeast(50, TimeUnit.MILLISECONDS).untilAsserted(
+                () -> assertFalse(Files.exists(testFile("done/foo.done")), "Done file should be deleted")
+        );
     }
 
     @Test
@@ -80,10 +83,12 @@ public class FileConsumeDoneFileIssueTest extends ContextTestSupport {
         assertMockEndpointsSatisfied();
         assertTrue(notify.matchesWaitTime());
 
-        Thread.sleep(50);
+        // the done file should be deleted
+        Awaitility.await().atLeast(50, TimeUnit.MILLISECONDS).untilAsserted(
+                () -> assertFalse(Files.exists(testFile("done2/a.txt.done")), "Done file should be deleted")
+        );
 
         // the done file should be deleted
-        assertFalse(Files.exists(testFile("done2/a.txt.done")), "Done file should be deleted");
         assertFalse(Files.exists(testFile("done2/b.txt.done")), "Done file should be deleted");
         assertFalse(Files.exists(testFile("done2/c.txt.done")), "Done file should be deleted");
 
@@ -111,10 +116,11 @@ public class FileConsumeDoneFileIssueTest extends ContextTestSupport {
         assertMockEndpointsSatisfied();
         assertTrue(notify.matchesWaitTime());
 
-        Thread.sleep(50);
-
         // the done file should be deleted
-        assertFalse(Files.exists(testFile("done2/$a$.txt.done")), "Done file should be deleted");
+        Awaitility.await().atLeast(50, TimeUnit.MILLISECONDS).untilAsserted(
+                () -> assertFalse(Files.exists(testFile("done2/$a$.txt.done")), "Done file should be deleted")
+        );
+
         assertFalse(Files.exists(testFile("done2/$b.txt.done")), "Done file should be deleted");
         assertFalse(Files.exists(testFile("done2/c$.txt.done")), "Done file should be deleted");
     }