Repository: camel
Updated Branches:
  refs/heads/master 7a9a6acb7 -> a757ea7d9


CAMEL-11446: Use awaitility in camel-core for testing where we otherwise use 
thred sleep which can be speeded up.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a757ea7d
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a757ea7d
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a757ea7d

Branch: refs/heads/master
Commit: a757ea7d97be8a27d834bcb9e4c93d9040622c24
Parents: 7a9a6ac
Author: Claus Ibsen <davscl...@apache.org>
Authored: Fri Jul 7 13:48:06 2017 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Fri Jul 7 13:48:06 2017 +0200

----------------------------------------------------------------------
 .../direct/DirectProducerBlockingTest.java      | 14 +++++++-----
 .../directvm/DirectVmProducerBlockingTest.java  | 12 +++++-----
 .../impl/DurationRoutePolicyFactoryTest.java    | 13 ++++++-----
 .../DurationRoutePolicyMaxMessagesTest.java     | 13 ++++++-----
 .../impl/FileWatcherReloadStrategyTest.java     | 24 +++++---------------
 .../EventNotifierExchangeSentTest.java          |  8 ++++---
 .../ManagedRouteLoadstatisticsTest.java         |  6 ++++-
 .../ManagedRoutePerformanceCounterTest.java     | 24 ++++++++++----------
 8 files changed, 56 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a757ea7d/camel-core/src/test/java/org/apache/camel/component/direct/DirectProducerBlockingTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/component/direct/DirectProducerBlockingTest.java
 
b/camel-core/src/test/java/org/apache/camel/component/direct/DirectProducerBlockingTest.java
index 5322202..371f683 100644
--- 
a/camel-core/src/test/java/org/apache/camel/component/direct/DirectProducerBlockingTest.java
+++ 
b/camel-core/src/test/java/org/apache/camel/component/direct/DirectProducerBlockingTest.java
@@ -16,8 +16,10 @@
  */
 package org.apache.camel.component.direct;
 
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.CamelExchangeException;
 import org.apache.camel.CamelExecutionException;
@@ -33,12 +35,12 @@ public class DirectProducerBlockingTest extends 
ContextTestSupport {
 
         StopWatch watch = new StopWatch();
         try {
-            template.sendBody("direct:suspended?block=true&timeout=2000", 
"hello world");
+            template.sendBody("direct:suspended?block=true&timeout=500", 
"hello world");
             fail("Expected CamelExecutionException");
         } catch (CamelExecutionException e) {
             DirectConsumerNotAvailableException cause = 
assertIsInstanceOf(DirectConsumerNotAvailableException.class, e.getCause());
             assertIsInstanceOf(CamelExchangeException.class, cause);
-            assertTrue(watch.taken() > 1500);
+            assertTrue(watch.taken() > 490);
         }
     }
 
@@ -48,13 +50,13 @@ public class DirectProducerBlockingTest extends 
ContextTestSupport {
 
         StopWatch watch = new StopWatch();
         try {
-            template.sendBody("direct:start?block=true&timeout=2000", "hello 
world");
+            template.sendBody("direct:start?block=true&timeout=500", "hello 
world");
             fail("Expected CamelExecutionException");
         } catch (CamelExecutionException e) {
             DirectConsumerNotAvailableException cause = 
assertIsInstanceOf(DirectConsumerNotAvailableException.class, e.getCause());
             assertIsInstanceOf(CamelExchangeException.class, cause);
 
-            assertTrue(watch.taken() > 1500);
+            assertTrue(watch.taken() > 490);
         }
     }
 
@@ -66,7 +68,7 @@ public class DirectProducerBlockingTest extends 
ContextTestSupport {
             @Override
             public void run() {
                 try {
-                    Thread.sleep(2000);
+                    Thread.sleep(200);
                     log.info("Resuming consumer");
                     context.resumeRoute("foo");
                 } catch (Exception e) {
@@ -77,7 +79,7 @@ public class DirectProducerBlockingTest extends 
ContextTestSupport {
 
         getMockEndpoint("mock:result").expectedMessageCount(1);
 
-        template.sendBody("direct:suspended?block=true&timeout=5000", "hello 
world");
+        template.sendBody("direct:suspended?block=true&timeout=1000", "hello 
world");
 
         assertMockEndpointsSatisfied();
 

http://git-wip-us.apache.org/repos/asf/camel/blob/a757ea7d/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmProducerBlockingTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmProducerBlockingTest.java
 
b/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmProducerBlockingTest.java
index 9c92785..d2f3482 100644
--- 
a/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmProducerBlockingTest.java
+++ 
b/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmProducerBlockingTest.java
@@ -33,12 +33,12 @@ public class DirectVmProducerBlockingTest extends 
ContextTestSupport {
 
         StopWatch watch = new StopWatch();
         try {
-            template.sendBody("direct-vm:suspended?block=true&timeout=2000", 
"hello world");
+            template.sendBody("direct-vm:suspended?block=true&timeout=500", 
"hello world");
             fail("Expected CamelExecutionException");
         } catch (CamelExecutionException e) {
             DirectVmConsumerNotAvailableException cause = 
assertIsInstanceOf(DirectVmConsumerNotAvailableException.class, e.getCause());
             assertIsInstanceOf(CamelExchangeException.class, cause);
-            assertTrue(watch.taken() > 1500);
+            assertTrue(watch.taken() > 490);
         }
     }
 
@@ -48,13 +48,13 @@ public class DirectVmProducerBlockingTest extends 
ContextTestSupport {
 
         StopWatch watch = new StopWatch();
         try {
-            template.sendBody("direct-vm:start?block=true&timeout=2000", 
"hello world");
+            template.sendBody("direct-vm:start?block=true&timeout=500", "hello 
world");
             fail("Expected CamelExecutionException");
         } catch (CamelExecutionException e) {
             DirectVmConsumerNotAvailableException cause = 
assertIsInstanceOf(DirectVmConsumerNotAvailableException.class, e.getCause());
             assertIsInstanceOf(CamelExchangeException.class, cause);
 
-            assertTrue(watch.taken() > 1500);
+            assertTrue(watch.taken() > 490);
         }
     }
 
@@ -66,7 +66,7 @@ public class DirectVmProducerBlockingTest extends 
ContextTestSupport {
             @Override
             public void run() {
                 try {
-                    Thread.sleep(2000);
+                    Thread.sleep(200);
                     log.info("Resuming consumer");
                     context.resumeRoute("foo");
                 } catch (Exception e) {
@@ -77,7 +77,7 @@ public class DirectVmProducerBlockingTest extends 
ContextTestSupport {
 
         getMockEndpoint("mock:result").expectedMessageCount(1);
 
-        template.sendBody("direct-vm:suspended?block=true&timeout=5000", 
"hello world");
+        template.sendBody("direct-vm:suspended?block=true&timeout=1000", 
"hello world");
 
         assertMockEndpointsSatisfied();
 

http://git-wip-us.apache.org/repos/asf/camel/blob/a757ea7d/camel-core/src/test/java/org/apache/camel/impl/DurationRoutePolicyFactoryTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/impl/DurationRoutePolicyFactoryTest.java
 
b/camel-core/src/test/java/org/apache/camel/impl/DurationRoutePolicyFactoryTest.java
index d20ac57..7a7649b 100644
--- 
a/camel-core/src/test/java/org/apache/camel/impl/DurationRoutePolicyFactoryTest.java
+++ 
b/camel-core/src/test/java/org/apache/camel/impl/DurationRoutePolicyFactoryTest.java
@@ -16,9 +16,13 @@
  */
 package org.apache.camel.impl;
 
+import java.util.concurrent.TimeUnit;
+
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
 
+import static org.awaitility.Awaitility.await;
+
 public class DurationRoutePolicyFactoryTest extends ContextTestSupport {
 
     public void testDurationRoutePolicyFactory() throws Exception {
@@ -30,14 +34,11 @@ public class DurationRoutePolicyFactoryTest extends 
ContextTestSupport {
         assertMockEndpointsSatisfied();
 
         // need a little time to stop async
-        for (int i = 0; i < 10; i++) {
-            Thread.sleep(100);
+        await().atMost(5, TimeUnit.SECONDS).until(() -> {
             boolean started = context.getRouteStatus("foo").isStarted();
             boolean stopped = context.getRouteStatus("foo").isStopped();
-            if (!started && stopped) {
-                break;
-            }
-        }
+            return !started && stopped;
+        });
         assertFalse(context.getRouteStatus("foo").isStarted());
         assertTrue(context.getRouteStatus("foo").isStopped());
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/a757ea7d/camel-core/src/test/java/org/apache/camel/impl/DurationRoutePolicyMaxMessagesTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/impl/DurationRoutePolicyMaxMessagesTest.java
 
b/camel-core/src/test/java/org/apache/camel/impl/DurationRoutePolicyMaxMessagesTest.java
index 11703096..2c2408e 100644
--- 
a/camel-core/src/test/java/org/apache/camel/impl/DurationRoutePolicyMaxMessagesTest.java
+++ 
b/camel-core/src/test/java/org/apache/camel/impl/DurationRoutePolicyMaxMessagesTest.java
@@ -16,9 +16,13 @@
  */
 package org.apache.camel.impl;
 
+import java.util.concurrent.TimeUnit;
+
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
 
+import static org.awaitility.Awaitility.await;
+
 public class DurationRoutePolicyMaxMessagesTest extends ContextTestSupport {
 
     public void testDurationRoutePolicy() throws Exception {
@@ -30,14 +34,11 @@ public class DurationRoutePolicyMaxMessagesTest extends 
ContextTestSupport {
         assertMockEndpointsSatisfied();
 
         // need a little time to stop async
-        for (int i = 0; i < 10; i++) {
-            Thread.sleep(100);
+        await().atMost(5, TimeUnit.SECONDS).until(() -> {
             boolean started = context.getRouteStatus("foo").isStarted();
             boolean stopped = context.getRouteStatus("foo").isStopped();
-            if (!started && stopped) {
-                break;
-            }
-        }
+            return !started && stopped;
+        });
         assertFalse(context.getRouteStatus("foo").isStarted());
         assertTrue(context.getRouteStatus("foo").isStopped());
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/a757ea7d/camel-core/src/test/java/org/apache/camel/impl/FileWatcherReloadStrategyTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/impl/FileWatcherReloadStrategyTest.java
 
b/camel-core/src/test/java/org/apache/camel/impl/FileWatcherReloadStrategyTest.java
index 2691c96..86b8e47 100644
--- 
a/camel-core/src/test/java/org/apache/camel/impl/FileWatcherReloadStrategyTest.java
+++ 
b/camel-core/src/test/java/org/apache/camel/impl/FileWatcherReloadStrategyTest.java
@@ -28,6 +28,8 @@ import org.apache.camel.management.event.RouteAddedEvent;
 import org.apache.camel.support.EventNotifierSupport;
 import org.apache.camel.util.FileUtil;
 
+import static org.awaitility.Awaitility.await;
+
 public class FileWatcherReloadStrategyTest extends ContextTestSupport {
 
     private FileWatcherReloadStrategy reloadStrategy;
@@ -55,7 +57,6 @@ public class FileWatcherReloadStrategyTest extends 
ContextTestSupport {
         // there are 0 routes to begin with
         assertEquals(0, context.getRoutes().size());
 
-        Thread.sleep(1000);
         log.info("Copying file to target/dummy");
 
         // create an xml file with some routes
@@ -63,17 +64,11 @@ public class FileWatcherReloadStrategyTest extends 
ContextTestSupport {
 
         // wait for that file to be processed
         // (is slow on osx, so wait up till 20 seconds)
-        for (int i = 0; i < 20; i++) {
-            if (context.getRoutes().size() > 0) {
-                break;
-            }
-            Thread.sleep(1000);
-        }
+        await().atMost(20, TimeUnit.SECONDS).until(() -> 
context.getRoutes().size() > 0);
 
         assertEquals(1, context.getRoutes().size());
 
         // and the route should work
-        Thread.sleep(1000);
         getMockEndpoint("mock:bar").expectedMessageCount(1);
         template.sendBody("direct:bar", "Hello World");
         assertMockEndpointsSatisfied();
@@ -116,7 +111,6 @@ public class FileWatcherReloadStrategyTest extends 
ContextTestSupport {
 
         resetMocks();
 
-        Thread.sleep(1000);
         log.info("Copying file to target/dummy");
 
         // create an xml file with some routes
@@ -128,7 +122,7 @@ public class FileWatcherReloadStrategyTest extends 
ContextTestSupport {
         assertTrue("Should reload file within 20 seconds", done);
 
         // and the route should be changed to route to mock:bar instead of 
mock:foo
-        Thread.sleep(1000);
+        Thread.sleep(500);
         getMockEndpoint("mock:bar").expectedMessageCount(1);
         getMockEndpoint("mock:foo").expectedMessageCount(0);
         template.sendBody("direct:bar", "Bye World");
@@ -158,7 +152,6 @@ public class FileWatcherReloadStrategyTest extends 
ContextTestSupport {
         // there are 0 routes to begin with
         assertEquals(0, context.getRoutes().size());
 
-        Thread.sleep(1000);
         log.info("Copying file to target/dummy");
 
         // create an xml file with some routes
@@ -166,12 +159,7 @@ public class FileWatcherReloadStrategyTest extends 
ContextTestSupport {
 
         // wait for that file to be processed
         // (is slow on osx, so wait up till 20 seconds)
-        for (int i = 0; i < 20; i++) {
-            if (context.getRoutes().size() > 0) {
-                break;
-            }
-            Thread.sleep(1000);
-        }
+        await().atMost(20, TimeUnit.SECONDS).until(() -> 
context.getRoutes().size() > 0);
 
         assertEquals(1, context.getRoutes().size());
 
@@ -194,7 +182,7 @@ public class FileWatcherReloadStrategyTest extends 
ContextTestSupport {
         assertTrue("Should reload file within 20 seconds", done);
 
         // and the route should work with the update
-        Thread.sleep(1000);
+        Thread.sleep(500);
         getMockEndpoint("mock:bar").expectedBodiesReceived("Bye Camel");
         template.sendBody("direct:bar", "Camel");
         assertMockEndpointsSatisfied();

http://git-wip-us.apache.org/repos/asf/camel/blob/a757ea7d/camel-core/src/test/java/org/apache/camel/management/EventNotifierExchangeSentTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/management/EventNotifierExchangeSentTest.java
 
b/camel-core/src/test/java/org/apache/camel/management/EventNotifierExchangeSentTest.java
index 8428258..239fb49 100644
--- 
a/camel-core/src/test/java/org/apache/camel/management/EventNotifierExchangeSentTest.java
+++ 
b/camel-core/src/test/java/org/apache/camel/management/EventNotifierExchangeSentTest.java
@@ -19,6 +19,7 @@ package org.apache.camel.management;
 import java.util.ArrayList;
 import java.util.EventObject;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.ContextTestSupport;
@@ -28,6 +29,8 @@ import org.apache.camel.management.event.ExchangeSendingEvent;
 import org.apache.camel.management.event.ExchangeSentEvent;
 import org.apache.camel.support.EventNotifierSupport;
 
+import static org.awaitility.Awaitility.await;
+
 /**
  * @version 
  */
@@ -114,8 +117,7 @@ public class EventNotifierExchangeSentTest extends 
ContextTestSupport {
 
         assertMockEndpointsSatisfied();
 
-        // give it time to complete
-        Thread.sleep(200);
+        assertTrue(oneExchangeDone.matchesMockWaitTime());
 
         assertEquals(12, events.size());
         ExchangeSendingEvent e0 = 
assertIsInstanceOf(ExchangeSendingEvent.class, events.get(0));
@@ -153,7 +155,7 @@ public class EventNotifierExchangeSentTest extends 
ContextTestSupport {
         assertMockEndpointsSatisfied();
 
         // give it time to complete
-        Thread.sleep(200);
+        await().atMost(1, TimeUnit.SECONDS).until(() -> events.size() == 6);
 
         assertEquals(6, events.size());
 

http://git-wip-us.apache.org/repos/asf/camel/blob/a757ea7d/camel-core/src/test/java/org/apache/camel/management/ManagedRouteLoadstatisticsTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/management/ManagedRouteLoadstatisticsTest.java
 
b/camel-core/src/test/java/org/apache/camel/management/ManagedRouteLoadstatisticsTest.java
index 5c39897..191fbe8 100644
--- 
a/camel-core/src/test/java/org/apache/camel/management/ManagedRouteLoadstatisticsTest.java
+++ 
b/camel-core/src/test/java/org/apache/camel/management/ManagedRouteLoadstatisticsTest.java
@@ -16,11 +16,14 @@
  */
 package org.apache.camel.management;
 
+import java.util.concurrent.TimeUnit;
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
 
 import org.apache.camel.builder.RouteBuilder;
 
+import static org.awaitility.Awaitility.await;
+
 /**
  * @version 
  */
@@ -91,7 +94,8 @@ public class ManagedRouteLoadstatisticsTest extends 
ManagementTestSupport {
 
         assertMockEndpointsSatisfied();
 
-        Thread.sleep(2000);
+        await().atMost(2, TimeUnit.SECONDS).until(() -> 
mbeanServer.getAttribute(on, "Load01") != null);
+
         String load01 = (String)mbeanServer.getAttribute(on, "Load01");
         String load05 = (String)mbeanServer.getAttribute(on, "Load05");
         String load15 = (String)mbeanServer.getAttribute(on, "Load15");

http://git-wip-us.apache.org/repos/asf/camel/blob/a757ea7d/camel-core/src/test/java/org/apache/camel/management/ManagedRoutePerformanceCounterTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/management/ManagedRoutePerformanceCounterTest.java
 
b/camel-core/src/test/java/org/apache/camel/management/ManagedRoutePerformanceCounterTest.java
index 5b09ad6..9191112 100644
--- 
a/camel-core/src/test/java/org/apache/camel/management/ManagedRoutePerformanceCounterTest.java
+++ 
b/camel-core/src/test/java/org/apache/camel/management/ManagedRoutePerformanceCounterTest.java
@@ -17,11 +17,14 @@
 package org.apache.camel.management;
 
 import java.util.Date;
+import java.util.concurrent.TimeUnit;
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
 
 import org.apache.camel.builder.RouteBuilder;
 
+import static org.awaitility.Awaitility.await;
+
 /**
  * @version 
  */
@@ -45,20 +48,17 @@ public class ManagedRoutePerformanceCounterTest extends 
ManagementTestSupport {
         template.asyncSendBody("direct:start", "Hello World");
 
         // cater for slow boxes
-        Integer inFlight = null;
-        for (int i = 0; i < 10; i++) {
-            Thread.sleep(500);
-            inFlight = (Integer) mbeanServer.getAttribute(on, 
"InflightExchanges");
-            if (inFlight.longValue() == 1) {
-                break;
-            }
-        }
-        assertNotNull("too slow server", inFlight);
-        assertEquals(1, inFlight.longValue());
+        await().atMost(5, TimeUnit.SECONDS).until(() -> {
+            Integer num = (Integer) mbeanServer.getAttribute(on, 
"InflightExchanges");
+            return num == 1;
+        });
 
         assertMockEndpointsSatisfied();
 
-        Thread.sleep(3000);
+        await().atMost(5, TimeUnit.SECONDS).until(() -> {
+            Long num = (Long) mbeanServer.getAttribute(on, 
"ExchangesCompleted");
+            return num == 1;
+        });
 
         Long completed = (Long) mbeanServer.getAttribute(on, 
"ExchangesCompleted");
         assertEquals(1, completed.longValue());
@@ -92,7 +92,7 @@ public class ManagedRoutePerformanceCounterTest extends 
ManagementTestSupport {
         assertNull(lastFailed);
         assertNull(firstFailed);
 
-        inFlight = (Integer) mbeanServer.getAttribute(on, "InflightExchanges");
+        Integer inFlight = (Integer) mbeanServer.getAttribute(on, 
"InflightExchanges");
         assertEquals(0, inFlight.longValue());
     }
 

Reply via email to