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 ff1ad0ae9c2 CAMEL-19533 - camel-mail: replace Thread.sleep in tests.
ff1ad0ae9c2 is described below

commit ff1ad0ae9c28f667510a70ee7b1c1a0917bbe332
Author: Vaishnavi R <v...@var-thinkpadp1gen4i.remote.csb>
AuthorDate: Wed Apr 3 12:39:42 2024 +0530

    CAMEL-19533 - camel-mail: replace Thread.sleep in tests.
    
    Updated the code.
    
    Updated the code.
    
    Updated the code.
---
 .../mail/MailAttachmentsUmlautIssueTest.java         |  9 ++++++---
 .../camel/component/mail/MailDisconnectTest.java     | 20 +++++++++++++-------
 .../component/mail/MailPostProcessActionTest.java    | 12 ++++--------
 3 files changed, 23 insertions(+), 18 deletions(-)

diff --git 
a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentsUmlautIssueTest.java
 
b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentsUmlautIssueTest.java
index 73c14c9601f..a739e5a2590 100644
--- 
a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentsUmlautIssueTest.java
+++ 
b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentsUmlautIssueTest.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.mail;
 import java.net.URLEncoder;
 import java.nio.charset.Charset;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 import jakarta.activation.DataHandler;
 import jakarta.activation.FileDataSource;
@@ -32,6 +33,7 @@ import org.apache.camel.component.mail.Mailbox.MailboxUser;
 import org.apache.camel.component.mail.Mailbox.Protocol;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.junit5.CamelTestSupport;
+import org.awaitility.Awaitility;
 import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 
@@ -71,12 +73,13 @@ public class MailAttachmentsUmlautIssueTest extends 
CamelTestSupport {
         producer.process(exchange);
 
         // need some time for the mail to arrive on the inbox (consumed and 
sent to the mock)
-        Thread.sleep(2000);
-
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedMessageCount(1);
         Exchange out = mock.assertExchangeReceived(0);
-        mock.assertIsSatisfied();
+
+        Awaitility.await().pollDelay(2, TimeUnit.SECONDS).untilAsserted(() -> {
+            mock.assertIsSatisfied();
+        });
 
         // plain text
         assertEquals("Hello World", out.getIn().getBody(String.class));
diff --git 
a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailDisconnectTest.java
 
b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailDisconnectTest.java
index 8f01f520a62..46a23116126 100644
--- 
a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailDisconnectTest.java
+++ 
b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailDisconnectTest.java
@@ -16,11 +16,15 @@
  */
 package org.apache.camel.component.mail;
 
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mail.Mailbox.MailboxUser;
 import org.apache.camel.component.mail.Mailbox.Protocol;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -29,6 +33,8 @@ import org.junit.jupiter.api.Test;
 public class MailDisconnectTest extends CamelTestSupport {
     private static final MailboxUser jones = Mailbox.getOrCreateUser("jones", 
"secret");
 
+    private CountDownLatch latch = new CountDownLatch(5);
+
     @Test
     public void testDisconnect() throws Exception {
         Mailbox.clearAll();
@@ -38,16 +44,14 @@ public class MailDisconnectTest extends CamelTestSupport {
         // send 5 mails with some delay so we do multiple polls with 
disconnect between
         template.sendBodyAndHeader(jones.uriPrefix(Protocol.smtp), "A Bla 
bla", "Subject", "Hello A");
         template.sendBodyAndHeader(jones.uriPrefix(Protocol.smtp), "B Bla 
bla", "Subject", "Hello B");
-
-        Thread.sleep(500);
         template.sendBodyAndHeader(jones.uriPrefix(Protocol.smtp), "C Bla 
bla", "Subject", "Hello C");
-
-        Thread.sleep(500);
         template.sendBodyAndHeader(jones.uriPrefix(Protocol.smtp), "D Bla 
bla", "Subject", "Hello D");
-
-        Thread.sleep(500);
         template.sendBodyAndHeader(jones.uriPrefix(Protocol.smtp), "E Bla 
bla", "Subject", "Hello E");
 
+        if (!latch.await(2500, TimeUnit.MILLISECONDS)) {
+            Assertions.fail("Not all messages were received as expected");
+        }
+
         MockEndpoint.assertIsSatisfied(context);
     }
 
@@ -55,7 +59,9 @@ public class MailDisconnectTest extends CamelTestSupport {
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
             public void configure() {
-                from(jones.uriPrefix(Protocol.imap) + 
"&disconnect=true&initialDelay=100&delay=100").to("mock:result");
+                from(jones.uriPrefix(Protocol.imap) + 
"&disconnect=true&initialDelay=100&delay=100")
+                        .process(e -> latch.countDown())
+                        .to("mock:result");
             }
         };
     }
diff --git 
a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailPostProcessActionTest.java
 
b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailPostProcessActionTest.java
index 9339c2537ab..0dbea0adf6e 100644
--- 
a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailPostProcessActionTest.java
+++ 
b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailPostProcessActionTest.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.mail;
 
+import java.util.concurrent.TimeUnit;
+
 import jakarta.mail.Folder;
 import jakarta.mail.Message;
 import jakarta.mail.Store;
@@ -27,6 +29,7 @@ import org.apache.camel.component.mail.Mailbox.MailboxUser;
 import org.apache.camel.component.mail.Mailbox.Protocol;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.junit5.CamelTestSupport;
+import org.awaitility.Awaitility;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.slf4j.Logger;
@@ -65,14 +68,7 @@ public class MailPostProcessActionTest extends 
CamelTestSupport {
 
     private void waitForActionCalled() throws InterruptedException {
         // Wait for a maximum of 500 ms for the action to be called
-        for (int i = 0; i < 50; i++) {
-            if (action.hasBeenCalled()) {
-                break;
-            }
-            LOG.debug("Sleeping for 10 millis to wait for action call");
-            Thread.sleep(10);
-        }
-        assertEquals(true, action.hasBeenCalled());
+        Awaitility.await().atMost(500, TimeUnit.MILLISECONDS).untilAsserted(() 
-> assertEquals(true, action.hasBeenCalled()));
     }
 
     private void prepareMailbox() throws Exception {

Reply via email to