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 2021/11/24 17:09:24 UTC
[camel] 01/02: (chores) core/camel-management: replace Thread.sleep with Awaitility 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
commit d2e1d303c5942e25f686918f70de87362127397d
Author: Otavio Rodolfo Piske <op...@redhat.com>
AuthorDate: Wed Nov 24 16:19:16 2021 +0100
(chores) core/camel-management: replace Thread.sleep with Awaitility in tests
---
.../ManagedRouteStopWithAbortAfterTimeoutTest.java | 8 +++++---
.../camel/management/ManagedThreadPoolTest.java | 20 ++++++++++++++++----
2 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/core/camel-management/src/test/java/org/apache/camel/management/ManagedRouteStopWithAbortAfterTimeoutTest.java b/core/camel-management/src/test/java/org/apache/camel/management/ManagedRouteStopWithAbortAfterTimeoutTest.java
index ea8d92e..e4c731e 100644
--- a/core/camel-management/src/test/java/org/apache/camel/management/ManagedRouteStopWithAbortAfterTimeoutTest.java
+++ b/core/camel-management/src/test/java/org/apache/camel/management/ManagedRouteStopWithAbortAfterTimeoutTest.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.management;
+import java.time.Duration;
import java.util.Set;
import javax.management.MBeanServer;
@@ -24,6 +25,7 @@ import javax.management.ObjectName;
import org.apache.camel.ServiceStatus;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
+import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
@@ -115,9 +117,9 @@ public class ManagedRouteStopWithAbortAfterTimeoutTest extends ManagementTestSup
template.sendBody("seda:start", "message-" + i);
}
- Thread.sleep(1000);
-
- assertTrue(mockEP.getExchanges().size() <= 5, "Should not have received more than 5 messages");
+ Awaitility.await().atMost(Duration.ofSeconds(1))
+ .untilAsserted(
+ () -> assertTrue(mockEP.getExchanges().size() <= 5, "Should not have received more than 5 messages"));
}
static ObjectName getRouteObjectName(MBeanServer mbeanServer) throws Exception {
diff --git a/core/camel-management/src/test/java/org/apache/camel/management/ManagedThreadPoolTest.java b/core/camel-management/src/test/java/org/apache/camel/management/ManagedThreadPoolTest.java
index bd0f789..83cfc32 100644
--- a/core/camel-management/src/test/java/org/apache/camel/management/ManagedThreadPoolTest.java
+++ b/core/camel-management/src/test/java/org/apache/camel/management/ManagedThreadPoolTest.java
@@ -16,10 +16,17 @@
*/
package org.apache.camel.management;
+import java.time.Duration;
+
+import javax.management.AttributeNotFoundException;
+import javax.management.InstanceNotFoundException;
+import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
+import javax.management.ReflectionException;
import org.apache.camel.builder.RouteBuilder;
+import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
@@ -59,10 +66,8 @@ public class ManagedThreadPoolTest extends ManagementTestSupport {
assertMockEndpointsSatisfied();
// wait a bit to ensure JMX have updated values
- Thread.sleep(2000);
-
- poolSize = (Integer) mbeanServer.getAttribute(on, "PoolSize");
- assertEquals(1, poolSize.intValue());
+ Awaitility.await().atMost(Duration.ofSeconds(2))
+ .untilAsserted(() -> assertPoolSize(mbeanServer, on));
Integer largest = (Integer) mbeanServer.getAttribute(on, "LargestPoolSize");
assertEquals(1, largest.intValue());
@@ -80,6 +85,13 @@ public class ManagedThreadPoolTest extends ManagementTestSupport {
assertEquals(200, remainingCapacity, "remainingCapacity");
}
+ private void assertPoolSize(MBeanServer mbeanServer, ObjectName on)
+ throws MBeanException, AttributeNotFoundException, InstanceNotFoundException, ReflectionException {
+ Integer poolSize;
+ poolSize = (Integer) mbeanServer.getAttribute(on, "PoolSize");
+ assertEquals(1, poolSize.intValue());
+ }
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {