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 ab41f801734 CAMEL-19526: replaced the Thread.sleep with the best option or removed it. (#11945) ab41f801734 is described below commit ab41f8017343b308353751fe0fa816b5e89630fc Author: Rafael Marques <rafam...@gmail.com> AuthorDate: Fri Nov 10 06:25:36 2023 -0300 CAMEL-19526: replaced the Thread.sleep with the best option or removed it. (#11945) * Replaced the thread.sleep with the best option or removed. --- .../camel/component/jms/AggregatedJmsRouteTest.java | 5 +++-- .../component/jms/JmsInOnlyDisableTimeToLiveTest.java | 8 ++++++-- .../component/jms/JmsProducerWithJMSHeaderTest.java | 12 +++--------- .../jms/JmsSimpleRequestCustomReplyToTest.java | 5 ++--- .../component/jms/JmsSimpleRequestLateReplyTest.java | 2 -- .../camel/component/jms/ManagedJmsSelectorTest.java | 3 --- .../camel/component/jms/async/MyAsyncProducer.java | 2 +- .../JmsInOutExclusiveTopicRecipientListTest.java | 18 +++++++++--------- .../jms/issues/JmsInOutExclusiveTopicTest.java | 2 -- ...JmsSendToAlotOfDestinationWithSameEndpointTest.java | 3 --- .../component/jms/issues/TempReplyToIssueTest.java | 2 +- ...questReplyTemporaryRefreshFailureOnStartupTest.java | 10 ++++++---- 12 files changed, 31 insertions(+), 41 deletions(-) diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/AggregatedJmsRouteTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/AggregatedJmsRouteTest.java index a41dd1bad21..f01573faee2 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/AggregatedJmsRouteTest.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/AggregatedJmsRouteTest.java @@ -16,6 +16,8 @@ */ package org.apache.camel.component.jms; +import static org.junit.jupiter.api.Assertions.fail; + import org.apache.camel.CamelContext; import org.apache.camel.ConsumerTemplate; import org.apache.camel.Exchange; @@ -33,8 +35,6 @@ import org.junit.jupiter.api.extension.RegisterExtension; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static org.junit.jupiter.api.Assertions.fail; - public class AggregatedJmsRouteTest extends AbstractJMSTest { @Order(2) @@ -101,6 +101,7 @@ public class AggregatedJmsRouteTest extends AbstractJMSTest { return newExchange; }).completionTimeout(2000L).to("mock:result"); + from(multicastEndpointUri).to("jms:queue:AggregatedJmsRouteTestQueuePoint1", "jms:queue:AggregatedJmsRouteTestQueuePoint2", "jms:queue:AggregatedJmsRouteTestQueuePoint3"); from("jms:queue:AggregatedJmsRouteTestQueuePoint1").process(new MyProcessor()) diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsInOnlyDisableTimeToLiveTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsInOnlyDisableTimeToLiveTest.java index 7f92cd54438..b9b4d7eee7f 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsInOnlyDisableTimeToLiveTest.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsInOnlyDisableTimeToLiveTest.java @@ -16,6 +16,9 @@ */ package org.apache.camel.component.jms; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + import org.apache.camel.CamelContext; import org.apache.camel.ConsumerTemplate; import org.apache.camel.ProducerTemplate; @@ -43,6 +46,7 @@ public class JmsInOnlyDisableTimeToLiveTest extends AbstractJMSTest { private final String urlTimeout = "activemq:JmsInOnlyDisableTimeToLiveTest.in?timeToLive=2000"; private final String urlTimeToLiveDisabled = "activemq:JmsInOnlyDisableTimeToLiveTest.in?timeToLive=2000&disableTimeToLive=true"; + private CountDownLatch messageWasExpiredCountDownLatch = new CountDownLatch(2); @Test public void testInOnlyExpired() throws Exception { @@ -57,7 +61,7 @@ public class JmsInOnlyDisableTimeToLiveTest extends AbstractJMSTest { MockEndpoint.assertIsSatisfied(context); // wait after the msg has expired - Thread.sleep(2500); + messageWasExpiredCountDownLatch.await(2000, TimeUnit.MILLISECONDS); MockEndpoint.resetMocks(context); getMockEndpoint("mock:end").expectedMessageCount(0); @@ -82,7 +86,7 @@ public class JmsInOnlyDisableTimeToLiveTest extends AbstractJMSTest { MockEndpoint.assertIsSatisfied(context); // wait after the msg has expired - Thread.sleep(2500); + messageWasExpiredCountDownLatch.await(2000, TimeUnit.MILLISECONDS); MockEndpoint.resetMocks(context); getMockEndpoint("mock:end").expectedBodiesReceived("Hello World 2"); diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsProducerWithJMSHeaderTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsProducerWithJMSHeaderTest.java index c7142e36786..4e59f884fb7 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsProducerWithJMSHeaderTest.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsProducerWithJMSHeaderTest.java @@ -31,6 +31,7 @@ import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.test.infra.core.CamelContextExtension; import org.apache.camel.test.infra.core.DefaultCamelContextExtension; +import org.awaitility.Awaitility; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Tag; @@ -140,9 +141,6 @@ public class JmsProducerWithJMSHeaderTest extends AbstractJMSTest { template.sendBodyAndHeader("activemq:queue:barJmsProducerWithJMSHeaderTest?preserveMessageQos=true", "Hello World", "JMSExpiration", ttl); - // sleep just a little - Thread.sleep(2000); - // use timeout in case running on slow box Exchange bar = consumer.receive("activemq:queue:barJmsProducerWithJMSHeaderTest", 10000); assertNotNull(bar, "Should be a message on queue"); @@ -201,16 +199,12 @@ public class JmsProducerWithJMSHeaderTest extends AbstractJMSTest { template.sendBodyAndHeaders("activemq:queue:barJmsProducerWithJMSHeaderTest?preserveMessageQos=true", "Hello World", headers); - // sleep just a little - Thread.sleep(50); - Exchange bar = consumer.receive("activemq:queue:barJmsProducerWithJMSHeaderTest", 5000); assertNotNull(bar, "Should be a message on queue"); template.send("activemq:queue:fooJmsProducerWithJMSHeaderTest?preserveMessageQos=true", bar); - Thread.sleep(1000); - - MockEndpoint.assertIsSatisfied(context); + Awaitility.await().atMost(1, TimeUnit.SECONDS) + .untilAsserted(() -> MockEndpoint.assertIsSatisfied(context)); } @Test diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsSimpleRequestCustomReplyToTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsSimpleRequestCustomReplyToTest.java index 59294114204..453b818da42 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsSimpleRequestCustomReplyToTest.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsSimpleRequestCustomReplyToTest.java @@ -17,6 +17,7 @@ package org.apache.camel.component.jms; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import org.apache.camel.CamelContext; import org.apache.camel.Consumer; @@ -113,10 +114,8 @@ public class JmsSimpleRequestCustomReplyToTest extends AbstractJMSTest { public void run() { try { LOG.debug("Waiting for latch"); - latch.await(); - // wait 1 sec after latch before sending he late replay - Thread.sleep(1000); + latch.await(1, TimeUnit.SECONDS); } catch (Exception e) { // ignore } diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsSimpleRequestLateReplyTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsSimpleRequestLateReplyTest.java index 000c41bd842..149634fbfe0 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsSimpleRequestLateReplyTest.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsSimpleRequestLateReplyTest.java @@ -99,8 +99,6 @@ public class JmsSimpleRequestLateReplyTest extends AbstractJMSTest { LOG.info("Waiting for latch"); latch.await(30, TimeUnit.SECONDS); - // wait 1 sec after latch before sending he late replay - Thread.sleep(1000); } catch (Exception e) { // ignore } diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/ManagedJmsSelectorTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/ManagedJmsSelectorTest.java index 6dc9013f98d..00e35e96208 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/ManagedJmsSelectorTest.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/ManagedJmsSelectorTest.java @@ -116,9 +116,6 @@ public class ManagedJmsSelectorTest implements CamelTestSupportHelper { mbeanServer.setAttribute(on, new Attribute("MessageSelector", "brand='softdrink'")); - // give it a little time to adjust - Thread.sleep(100); - mock.expectedBodiesReceived("Pepsi"); mock.reset(); diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/async/MyAsyncProducer.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/async/MyAsyncProducer.java index fce1286728c..1f5b90fa0de 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/async/MyAsyncProducer.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/async/MyAsyncProducer.java @@ -51,8 +51,8 @@ public class MyAsyncProducer extends DefaultAsyncProducer { executor.submit(() -> { LOG.info("Simulating a task which takes {} millis to reply", getEndpoint().getDelay()); - Thread.sleep(getEndpoint().getDelay()); + Thread.sleep(getEndpoint().getDelay()); int count = counter.incrementAndGet(); if (getEndpoint().getFailFirstAttempts() >= count) { LOG.info("Simulating a failure at attempt {}", count); diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsInOutExclusiveTopicRecipientListTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsInOutExclusiveTopicRecipientListTest.java index 5656213ed85..d18ea557918 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsInOutExclusiveTopicRecipientListTest.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsInOutExclusiveTopicRecipientListTest.java @@ -24,6 +24,7 @@ import org.apache.camel.component.jms.AbstractJMSTest; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.test.infra.core.CamelContextExtension; import org.apache.camel.test.infra.core.DefaultCamelContextExtension; +import org.awaitility.Awaitility; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; @@ -44,14 +45,15 @@ public class JmsInOutExclusiveTopicRecipientListTest extends AbstractJMSTest { public void testJmsInOutExclusiveTopicTest() throws Exception { getMockEndpoint("mock:result").expectedBodiesReceived("Bye Camel"); - Thread.sleep(1000); // instantiate JmsInOutExclusiveTopicRecipientListTest.reply queue + // instantiate JmsInOutExclusiveTopicRecipientListTest.reply queue + Awaitility.await().untilAsserted(() -> { + String out = template.requestBodyAndHeader("direct:start", "Camel", "whereTo", + "activemq:topic:JmsInOutExclusiveTopicRecipientListTest.news?replyToType=Exclusive&replyTo=queue:JmsInOutExclusiveTopicRecipientListTest.reply", + String.class); + assertEquals("Bye Camel", out); - String out = template.requestBodyAndHeader("direct:start", "Camel", "whereTo", - "activemq:topic:JmsInOutExclusiveTopicRecipientListTest.news?replyToType=Exclusive&replyTo=queue:JmsInOutExclusiveTopicRecipientListTest.reply", - String.class); - assertEquals("Bye Camel", out); - - MockEndpoint.assertIsSatisfied(context); + MockEndpoint.assertIsSatisfied(context); + }); } @Override @@ -76,8 +78,6 @@ public class JmsInOutExclusiveTopicRecipientListTest extends AbstractJMSTest { log.info("ReplyTo: {}", replyTo); log.info("CorrelationID: {}", cid); if (replyTo != null && cid != null) { - // wait a bit before sending back - Thread.sleep(1000); log.info("Sending back reply message on {}", replyTo); template.sendBodyAndHeader("activemq:" + replyTo, exchange.getIn().getBody(), "JMSCorrelationID", cid); diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsInOutExclusiveTopicTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsInOutExclusiveTopicTest.java index 01601aa6058..56c7c407a22 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsInOutExclusiveTopicTest.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsInOutExclusiveTopicTest.java @@ -82,8 +82,6 @@ public class JmsInOutExclusiveTopicTest extends AbstractJMSTest { log.info("ReplyTo: {}", replyTo); log.info("CorrelationID: {}", cid); if (replyTo != null && cid != null) { - // wait a bit before sending back - Thread.sleep(1000); log.info("Sending back reply message on {}", replyTo); template.sendBodyAndHeader("activemq:" + replyTo, exchange.getIn().getBody(), "JMSCorrelationID", cid); diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsSendToAlotOfDestinationWithSameEndpointTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsSendToAlotOfDestinationWithSameEndpointTest.java index 9f85ab1b204..39b044a5512 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsSendToAlotOfDestinationWithSameEndpointTest.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsSendToAlotOfDestinationWithSameEndpointTest.java @@ -36,9 +36,6 @@ public class JmsSendToAlotOfDestinationWithSameEndpointTest extends CamelBrokerC @Test public void testSendToAlotOfMessageToQueues() { assertDoesNotThrow(this::sendToAlotOfMessagesToQueue); - - // now we should be able to poll a message from each queue - // Thread.sleep(99999999); } private void sendToAlotOfMessagesToQueue() { diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/TempReplyToIssueTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/TempReplyToIssueTest.java index 1dd08ce9351..4b2e1c13dee 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/TempReplyToIssueTest.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/TempReplyToIssueTest.java @@ -16,8 +16,8 @@ */ package org.apache.camel.component.jms.issues; -import jakarta.jms.Destination; +import jakarta.jms.Destination; import org.apache.camel.Body; import org.apache.camel.CamelContext; import org.apache.camel.ConsumerTemplate; diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/temp/JmsRequestReplyTemporaryRefreshFailureOnStartupTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/temp/JmsRequestReplyTemporaryRefreshFailureOnStartupTest.java index fa188a01da5..cd3c32347f3 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/temp/JmsRequestReplyTemporaryRefreshFailureOnStartupTest.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/temp/JmsRequestReplyTemporaryRefreshFailureOnStartupTest.java @@ -28,6 +28,7 @@ import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.test.infra.artemis.services.ArtemisService; import org.apache.camel.test.infra.artemis.services.ArtemisServiceFactory; import org.apache.camel.test.junit5.CamelTestSupport; +import org.awaitility.Awaitility; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tags; import org.junit.jupiter.api.Test; @@ -86,11 +87,12 @@ public class JmsRequestReplyTemporaryRefreshFailureOnStartupTest extends CamelTe } catch (Exception exception) { } - //wait for connection recovery before starting the broker - Thread.sleep(recoveryInterval + 500L); - template.asyncRequestBody("direct:start", "ping"); + Awaitility.await().untilAsserted(() -> { + template.asyncRequestBody("direct:start", "ping"); + + MockEndpoint.assertIsSatisfied(context, 10, TimeUnit.SECONDS); + }); - MockEndpoint.assertIsSatisfied(context, 10, TimeUnit.SECONDS); } }