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
commit fc4c39d13cf51713dfe4da9d7c62975a865b6634 Author: Nikita Konovalov <[email protected]> AuthorDate: Thu Jul 25 14:46:26 2024 +0200 CAMEL-19538: Replaced Thread.sleep() in camel-mllp --- .../MllpTcpServerConsumerLenientBindTest.java | 8 ++-- .../mllp/MllpIdleTimeoutStrategyTest.java | 9 +++-- ...TcpClientProducerIdleConnectionTimeoutTest.java | 46 ++++++++++++---------- .../mllp/MllpTcpServerConsumerBindTimeoutTest.java | 8 +++- .../mllp/MllpTcpServerConsumerConnectionTest.java | 26 ++++++------ ...rConsumerEndOfDataAndValidationTestSupport.java | 31 +++++++-------- .../camel/test/executor/PooledExecutorTest.java | 5 +-- .../camel/test/tcp/JavaSocketManualTests.java | 23 ++++++----- 8 files changed, 84 insertions(+), 72 deletions(-) diff --git a/components/camel-mllp/src/test/java/org/apache/camel/MllpTcpServerConsumerLenientBindTest.java b/components/camel-mllp/src/test/java/org/apache/camel/MllpTcpServerConsumerLenientBindTest.java index 6e007d69885..e885463f5e8 100644 --- a/components/camel-mllp/src/test/java/org/apache/camel/MllpTcpServerConsumerLenientBindTest.java +++ b/components/camel-mllp/src/test/java/org/apache/camel/MllpTcpServerConsumerLenientBindTest.java @@ -18,6 +18,7 @@ package org.apache.camel; import java.net.ServerSocket; import java.net.SocketTimeoutException; +import java.util.concurrent.TimeUnit; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; @@ -28,6 +29,7 @@ import org.apache.camel.test.junit5.CamelTestSupport; import org.apache.camel.test.mllp.Hl7TestMessageGenerator; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import org.testcontainers.shaded.org.awaitility.Awaitility; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.apache.camel.test.junit5.TestSupport.assertStringContains; @@ -86,10 +88,10 @@ public class MllpTcpServerConsumerLenientBindTest extends CamelTestSupport { assertIsInstanceOf(SocketTimeoutException.class, expectedEx.getCause()); } mllpClient.reset(); - portBlocker.close(); - Thread.sleep(2000); - assertEquals(ServiceStatus.Started, context.getStatus()); + + Awaitility.await().pollDelay(2000, TimeUnit.MILLISECONDS) + .untilAsserted(() -> assertEquals(ServiceStatus.Started, context.getStatus())); mllpClient.connect(); String acknowledgement diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpIdleTimeoutStrategyTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpIdleTimeoutStrategyTest.java index 699e1cd56fb..de9c31457c0 100644 --- a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpIdleTimeoutStrategyTest.java +++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpIdleTimeoutStrategyTest.java @@ -37,6 +37,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import org.mockito.Mockito; +import org.testcontainers.shaded.org.awaitility.Awaitility; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.never; @@ -146,10 +147,12 @@ public class MllpIdleTimeoutStrategyTest extends CamelTestSupport { private void sendHl7Message(ProducerTemplate template) throws Exception { target.expectedMessageCount(1); - // Need to send one message to get the connection established + template.sendBody(Hl7TestMessageGenerator.generateMessage()); - Thread.sleep(IDLE_TIMEOUT * 3); - MockEndpoint.assertIsSatisfied(context, 5, TimeUnit.SECONDS); + + // Need to send one message to get the connection established + Awaitility.await().pollDelay(IDLE_TIMEOUT * 3, TimeUnit.MILLISECONDS) + .untilAsserted(() -> MockEndpoint.assertIsSatisfied(context, 5, TimeUnit.SECONDS)); } @Test diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpClientProducerIdleConnectionTimeoutTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpClientProducerIdleConnectionTimeoutTest.java index 970e17acccc..9927885d52a 100644 --- a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpClientProducerIdleConnectionTimeoutTest.java +++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpClientProducerIdleConnectionTimeoutTest.java @@ -35,6 +35,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.testcontainers.shaded.org.awaitility.Awaitility; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -114,17 +115,18 @@ public class MllpTcpClientProducerIdleConnectionTimeoutTest extends CamelTestSup // Need to send one message to get the connection established source.sendBody(Hl7TestMessageGenerator.generateMessage()); - Thread.sleep(IDLE_TIMEOUT / 2); - source.sendBody(Hl7TestMessageGenerator.generateMessage()); - - assertTrue(done.matches(5, TimeUnit.SECONDS), "Should have completed two exchanges"); + Awaitility.await().untilAsserted(() -> { + source.sendBody(Hl7TestMessageGenerator.generateMessage()); - MockEndpoint.assertIsSatisfied(context, 5, TimeUnit.SECONDS); + assertTrue(done.matches(5, TimeUnit.SECONDS), "Should have completed two exchanges"); - Thread.sleep((long) (IDLE_TIMEOUT * 1.1)); + MockEndpoint.assertIsSatisfied(context, 5, TimeUnit.SECONDS); + }); - assertThrows(MllpJUnitResourceException.class, - () -> mllpServer.checkClientConnections()); + Awaitility.await().untilAsserted(() -> { + assertThrows(MllpJUnitResourceException.class, + () -> mllpServer.checkClientConnections()); + }); } @Test @@ -138,23 +140,25 @@ public class MllpTcpClientProducerIdleConnectionTimeoutTest extends CamelTestSup // Need to send one message to get the connection established source.sendBody(Hl7TestMessageGenerator.generateMessage()); - Thread.sleep(IDLE_TIMEOUT / 2); - source.sendBody(Hl7TestMessageGenerator.generateMessage()); - - assertTrue(done.matches(5, TimeUnit.SECONDS), "Should have completed two exchanges"); + Awaitility.await().untilAsserted(() -> { + Thread.sleep(IDLE_TIMEOUT / 2); + source.sendBody(Hl7TestMessageGenerator.generateMessage()); - Thread.sleep((long) (IDLE_TIMEOUT * 1.1)); + assertTrue(done.matches(5, TimeUnit.SECONDS), "Should have completed two exchanges"); + }); - try { - mllpServer.checkClientConnections(); - fail("Should receive and exception for the closed connection"); - } catch (MllpJUnitResourceException expectedEx) { - // Eat this - } + Awaitility.await().untilAsserted(() -> { + try { + mllpServer.checkClientConnections(); + fail("Should receive and exception for the closed connection"); + } catch (MllpJUnitResourceException expectedEx) { + // Eat this + } - source.sendBody(Hl7TestMessageGenerator.generateMessage()); + source.sendBody(Hl7TestMessageGenerator.generateMessage()); - MockEndpoint.assertIsSatisfied(context, 5, TimeUnit.SECONDS); + MockEndpoint.assertIsSatisfied(context, 5, TimeUnit.SECONDS); + }); log.debug("Breakpoint"); } diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerBindTimeoutTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerBindTimeoutTest.java index 7a5519d629c..2419818a579 100644 --- a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerBindTimeoutTest.java +++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerBindTimeoutTest.java @@ -30,8 +30,10 @@ import org.apache.camel.test.AvailablePortFinder; import org.apache.camel.test.junit.rule.mllp.MllpClientResource; import org.apache.camel.test.junit5.CamelTestSupport; import org.apache.camel.test.mllp.Hl7TestMessageGenerator; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import org.testcontainers.shaded.org.awaitility.Awaitility; public class MllpTcpServerConsumerBindTimeoutTest extends CamelTestSupport { @@ -92,8 +94,10 @@ public class MllpTcpServerConsumerBindTimeoutTest extends CamelTestSupport { public void run() { try { ServerSocket tmpSocket = new ServerSocket(mllpClient.getMllpPort()); - Thread.sleep(15000); - tmpSocket.close(); + Awaitility.await().untilAsserted(() -> { + tmpSocket.close(); + Assertions.assertTrue(tmpSocket.isClosed()); + }); } catch (Exception ex) { throw new RuntimeCamelException("Exception caught in dummy listener", ex); } diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerConnectionTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerConnectionTest.java index 4fc5c437b25..a4ebc6cc00f 100644 --- a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerConnectionTest.java +++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerConnectionTest.java @@ -28,6 +28,7 @@ import org.apache.camel.test.junit.rule.mllp.MllpJUnitResourceException; import org.apache.camel.test.junit5.CamelTestSupport; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import org.testcontainers.shaded.org.awaitility.Awaitility; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; @@ -83,13 +84,11 @@ public class MllpTcpServerConsumerConnectionTest extends CamelTestSupport { for (int i = 1; i <= connectionCount; ++i) { mllpClient.connect(); - Thread.sleep(connectionMillis); mllpClient.close(); } // Connect one more time and allow a client thread to start mllpClient.connect(); - Thread.sleep(1000); mllpClient.close(); MockEndpoint.assertIsSatisfied(context, 15, TimeUnit.SECONDS); @@ -107,13 +106,11 @@ public class MllpTcpServerConsumerConnectionTest extends CamelTestSupport { for (int i = 1; i <= connectionCount; ++i) { mllpClient.connect(); - Thread.sleep(connectionMillis); mllpClient.reset(); } // Connect one more time and allow a client thread to start mllpClient.connect(); - Thread.sleep(1000); mllpClient.reset(); MockEndpoint.assertIsSatisfied(context, 15, TimeUnit.SECONDS); @@ -136,17 +133,18 @@ public class MllpTcpServerConsumerConnectionTest extends CamelTestSupport { mllpClient.connect(); mllpClient.sendMessageAndWaitForAcknowledgement(testMessage); - Thread.sleep(idleTimeout + RECEIVE_TIMEOUT); - - try { - mllpClient.checkConnection(); - fail("The MllpClientResource should have thrown an exception when writing to the reset socket"); - } catch (MllpJUnitResourceException expectedEx) { - assertEquals("checkConnection failed - read() returned END_OF_STREAM", expectedEx.getMessage()); - assertNull(expectedEx.getCause()); - } - MockEndpoint.assertIsSatisfied(context, 15, TimeUnit.SECONDS); + Awaitility.await().untilAsserted(() -> { + try { + mllpClient.checkConnection(); + fail("The MllpClientResource should have thrown an exception when writing to the reset socket"); + } catch (MllpJUnitResourceException expectedEx) { + assertEquals("checkConnection failed - read() returned END_OF_STREAM", expectedEx.getMessage()); + assertNull(expectedEx.getCause()); + } + + MockEndpoint.assertIsSatisfied(context, 15, TimeUnit.SECONDS); + }); } void addTestRouteWithIdleTimeout(final int idleTimeout) throws Exception { diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/TcpServerConsumerEndOfDataAndValidationTestSupport.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/TcpServerConsumerEndOfDataAndValidationTestSupport.java index e1ded8a621e..4b947ae8075 100644 --- a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/TcpServerConsumerEndOfDataAndValidationTestSupport.java +++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/TcpServerConsumerEndOfDataAndValidationTestSupport.java @@ -36,6 +36,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.testcontainers.shaded.org.awaitility.Awaitility; import static org.apache.camel.component.mllp.MllpExceptionTestSupport.LOG_PHI_TRUE; import static org.hamcrest.CoreMatchers.instanceOf; @@ -138,8 +139,6 @@ public abstract class TcpServerConsumerEndOfDataAndValidationTestSupport extends setExpectedCounts(); mllpClient.connect(); - - Thread.sleep(5000); mllpClient.sendMessageAndWaitForAcknowledgement(Hl7TestMessageGenerator.generateMessage(), 10000); } @@ -187,7 +186,6 @@ public abstract class TcpServerConsumerEndOfDataAndValidationTestSupport extends log.info("Expected exception reading response"); } mllpClient.disconnect(); - Thread.sleep(1000); mllpClient.connect(); log.info("Sending TEST_MESSAGE_4"); @@ -243,20 +241,21 @@ public abstract class TcpServerConsumerEndOfDataAndValidationTestSupport extends log.info("Sending first message"); mllpClient.sendFramedData(Hl7TestMessageGenerator.generateMessage(10001)); - Thread.sleep(RECEIVE_TIMEOUT * 5); - - mllpClient.setSendEndOfBlock(true); - mllpClient.setSendEndOfData(true); + Awaitility.await().pollDelay(RECEIVE_TIMEOUT * 5, TimeUnit.MILLISECONDS).untilAsserted(() -> { + mllpClient.setSendEndOfBlock(true); + mllpClient.setSendEndOfData(true); - try { - log.info("Attempting to send second message"); - String acknowledgement - = mllpClient.sendMessageAndWaitForAcknowledgement(Hl7TestMessageGenerator.generateMessage(10002)); - assertEquals("", acknowledgement, "If the send doesn't throw an exception, the acknowledgement should be empty"); - } catch (MllpJUnitResourceException expected) { - assertThat("If the send throws an exception, the cause should be a SocketException", expected.getCause(), - instanceOf(SocketException.class)); - } + try { + log.info("Attempting to send second message"); + String acknowledgement + = mllpClient.sendMessageAndWaitForAcknowledgement(Hl7TestMessageGenerator.generateMessage(10002)); + assertEquals("", acknowledgement, + "If the send doesn't throw an exception, the acknowledgement should be empty"); + } catch (MllpJUnitResourceException expected) { + assertThat("If the send throws an exception, the cause should be a SocketException", expected.getCause(), + instanceOf(SocketException.class)); + } + }); mllpClient.disconnect(); mllpClient.connect(); diff --git a/components/camel-mllp/src/test/java/org/apache/camel/test/executor/PooledExecutorTest.java b/components/camel-mllp/src/test/java/org/apache/camel/test/executor/PooledExecutorTest.java index 40c9898d69e..8ecf7344e53 100644 --- a/components/camel-mllp/src/test/java/org/apache/camel/test/executor/PooledExecutorTest.java +++ b/components/camel-mllp/src/test/java/org/apache/camel/test/executor/PooledExecutorTest.java @@ -52,15 +52,12 @@ public class PooledExecutorTest { log.info("Starting first set of runnables"); startRunnables(runnableCount, runCount); - Thread.sleep(THREAD_COUNT * TestRunnable.SLEEP_MILLIS); - log.info("Starting second set of runnables"); startRunnables(runnableCount, runCount); } - void startRunnables(int runnableCount, int runCount) throws InterruptedException { + void startRunnables(int runnableCount, int runCount) { for (int id = 1; id <= runnableCount; ++id) { - Thread.sleep(TestRunnable.SLEEP_MILLIS / 2); try { instance.addRunnable(new TestRunnable(id, runCount)); } catch (RejectedExecutionException rejectedEx) { diff --git a/components/camel-mllp/src/test/java/org/apache/camel/test/tcp/JavaSocketManualTests.java b/components/camel-mllp/src/test/java/org/apache/camel/test/tcp/JavaSocketManualTests.java index 16a8c0c0175..30b8623f0c1 100644 --- a/components/camel-mllp/src/test/java/org/apache/camel/test/tcp/JavaSocketManualTests.java +++ b/components/camel-mllp/src/test/java/org/apache/camel/test/tcp/JavaSocketManualTests.java @@ -31,7 +31,9 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.testcontainers.shaded.org.awaitility.Awaitility; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; /** @@ -283,15 +285,18 @@ public class JavaSocketManualTests { log.info("Client Socket read() timed-out before close"); } clientSocket.getOutputStream().write(27); - Thread.sleep(1000); - log.info("Client Socket available() returned {} after close", clientSocket.getInputStream().available()); - log.info("Client Socket read() returned {} after close", clientSocket.getInputStream().read()); - // Javadoc for Socket says closing the InputStream will close the connection - clientSocket.getInputStream().close(); - if (!clientSocket.isClosed()) { - log.warn("Closing input stream didn't close socket"); - clientSocket.close(); - } + Awaitility.await().untilAsserted(() -> { + log.info("Client Socket available() returned {} after close", clientSocket.getInputStream().available()); + log.info("Client Socket read() returned {} after close", clientSocket.getInputStream().read()); + // Javadoc for Socket says closing the InputStream will close the connection + clientSocket.getInputStream().close(); + if (!clientSocket.isClosed()) { + log.warn("Closing input stream didn't close socket"); + clientSocket.close(); + } + assertTrue(clientSocket.isClosed()); + }); + log.info("Sleeping ..."); Thread.sleep(5000);
