You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/08/02 14:46:32 UTC

[camel] 04/08: CAMEL-12003: Add failFast option to mock endpoint

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 61909c2199f708db50bc9999d7d8795d751b2b2c
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Aug 2 08:27:22 2019 +0200

    CAMEL-12003: Add failFast option to mock endpoint
---
 .../src/main/docs/dataset-test-component.adoc          |  3 ++-
 .../camel/component/dataset/DataSetEndpoint.java       |  7 ++++++-
 .../apache/camel/component/mock/AssertionClause.java   | 12 +++++++-----
 .../org/apache/camel/component/mock/MockEndpoint.java  | 18 +++++++++++++-----
 4 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/components/camel-dataset/src/main/docs/dataset-test-component.adoc b/components/camel-dataset/src/main/docs/dataset-test-component.adoc
index 78f9597..82cca82 100644
--- a/components/camel-dataset/src/main/docs/dataset-test-component.adoc
+++ b/components/camel-dataset/src/main/docs/dataset-test-component.adoc
@@ -73,7 +73,7 @@ with the following path and query parameters:
 |===
 
 
-=== Query Parameters (16 parameters):
+=== Query Parameters (17 parameters):
 
 
 [width="100%",cols="2,5,^1,2",options="header"]
@@ -85,6 +85,7 @@ with the following path and query parameters:
 | *timeout* (producer) | The timeout to use when polling for message bodies from the URI | 2000 | long
 | *assertPeriod* (producer) | Sets a grace period after which the mock endpoint will re-assert to ensure the preliminary assertion is still valid. This is used for example to assert that exactly a number of messages arrives. For example if expectedMessageCount(int) was set to 5, then the assertion is satisfied when 5 or more message arrives. To ensure that exactly 5 messages arrives, then you would need to wait a little period to ensure no further message arrives. This is what you can us [...]
 | *expectedCount* (producer) | Specifies the expected number of message exchanges that should be received by this endpoint. Beware: If you want to expect that 0 messages, then take extra care, as 0 matches when the tests starts, so you need to set a assert period time to let the test run for a while to make sure there are still no messages arrived; for that use setAssertPeriod(long). An alternative is to use NotifyBuilder, and use the notifier to know when Camel is done routing some mess [...]
+| *failFast* (producer) | Sets whether assertIsSatisfied() should fail fast at the first detected failed expectation while it may otherwise wait for all expected messages to arrive before performing expectations verifications. Is by default true. Set to false to use behavior as in Camel 2.x. | false | boolean
 | *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
 | *reportGroup* (producer) | A number that is used to turn on throughput logging based on groups of the size. |  | int
 | *resultMinimumWaitTime* (producer) | Sets the minimum expected amount of time (in millis) the assertIsSatisfied() will wait on a latch until it is satisfied | 0 | long
diff --git a/components/camel-dataset/src/main/java/org/apache/camel/component/dataset/DataSetEndpoint.java b/components/camel-dataset/src/main/java/org/apache/camel/component/dataset/DataSetEndpoint.java
index 49074ed..c72ab14 100644
--- a/components/camel-dataset/src/main/java/org/apache/camel/component/dataset/DataSetEndpoint.java
+++ b/components/camel-dataset/src/main/java/org/apache/camel/component/dataset/DataSetEndpoint.java
@@ -45,7 +45,7 @@ import org.slf4j.LoggerFactory;
  * Camel will use the throughput logger when sending dataset's.
  */
 @UriEndpoint(firstVersion = "1.3.0", scheme = "dataset", title = "Dataset", syntax = "dataset:name",
-    label = "core,testing", lenientProperties = true)
+    label = "core,testing", excludeProperties = "failFast", lenientProperties = true)
 public class DataSetEndpoint extends MockEndpoint implements Service {
     private final transient Logger log;
     private final AtomicInteger receivedCounter = new AtomicInteger();
@@ -70,6 +70,8 @@ public class DataSetEndpoint extends MockEndpoint implements Service {
         this.log = LoggerFactory.getLogger(endpointUri);
         // optimize as we dont need to copy the exchange
         setCopyOnExchange(false);
+        // fail fast mode is not possible with dataset endpoints
+        setFailFast(false);
     }
 
     public static void assertEquals(String description, Object expected, Object actual, Exchange exchange) {
@@ -297,6 +299,9 @@ public class DataSetEndpoint extends MockEndpoint implements Service {
             reporter = createReporter();
         }
 
+        // fail fast mode is not possible with dataset endpoints
+        setFailFast(false);
+
         log.info(this + " expecting " + getExpectedCount() + " messages");
     }
 
diff --git a/components/camel-mock/src/main/java/org/apache/camel/component/mock/AssertionClause.java b/components/camel-mock/src/main/java/org/apache/camel/component/mock/AssertionClause.java
index 3009090..16b9503 100644
--- a/components/camel-mock/src/main/java/org/apache/camel/component/mock/AssertionClause.java
+++ b/components/camel-mock/src/main/java/org/apache/camel/component/mock/AssertionClause.java
@@ -100,11 +100,13 @@ public abstract class AssertionClause extends MockExpressionClauseSupport<MockVa
         for (Predicate predicate : predicates) {
             currentIndex = index;
 
-            Object value = exchange.getMessage().getBody();
-            // if the value is StreamCache then ensure its readable before evaluating any predicates
-            // by resetting it (this is also what StreamCachingAdvice does)
-            if (value instanceof StreamCache) {
-                ((StreamCache) value).reset();
+            if (exchange != null) {
+                Object value = exchange.getMessage().getBody();
+                // if the value is StreamCache then ensure its readable before evaluating any predicates
+                // by resetting it (this is also what StreamCachingAdvice does)
+                if (value instanceof StreamCache) {
+                    ((StreamCache) value).reset();
+                }
             }
 
             PredicateAssertHelper.assertMatches(predicate, "Assertion error at index " + index + " on mock " + endpoint.getEndpointUri() + " with predicate: ", exchange);
diff --git a/components/camel-mock/src/main/java/org/apache/camel/component/mock/MockEndpoint.java b/components/camel-mock/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
index 3465f1c..45b2f19 100644
--- a/components/camel-mock/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
+++ b/components/camel-mock/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
@@ -426,8 +426,8 @@ public class MockEndpoint extends DefaultEndpoint implements BrowsableEndpoint,
 
         for (Throwable failure : failures) {
             if (failure != null) {
-                log.error("Caught on " + getEndpointUri() + " Exception: " + failure, failure);
-                fail("Failed due to caught exception: " + failure);
+                log.error("Caught exception on " + getEndpointUri() + " due to: " + failure.getMessage(), failure);
+                fail(failure);
             }
         }
     }
@@ -1114,11 +1114,13 @@ public class MockEndpoint extends DefaultEndpoint implements BrowsableEndpoint,
         final AssertionClause clause = new AssertionClauseTask(this) {
             @Override
             public void assertOnIndex(int index) {
-                applyAssertionOn(MockEndpoint.this, index, assertExchangeReceived(index));
+                if (index < getReceivedExchanges().size()) {
+                    applyAssertionOn(MockEndpoint.this, index, assertExchangeReceived(index));
+                }
             }
 
             public void run() {
-                for (int i = 0; i < getReceivedCounter(); i++) {
+                for (int i = 0; i < getReceivedExchanges().size(); i++) {
                     assertOnIndex(i);
                 }
             }
@@ -1603,7 +1605,13 @@ public class MockEndpoint extends DefaultEndpoint implements BrowsableEndpoint,
                 log.debug("{} failed and received[{}]: {}", getEndpointUri(), ++index, exchange);
             }
         }
-        throw new AssertionError(getEndpointUri() + " " + message);
+        if (message instanceof Throwable) {
+            Throwable cause = (Throwable) message;
+            String msg = "Caught exception on " + getEndpointUri() + " due to: " + cause.getMessage();
+            throw new AssertionError(msg, cause);
+        } else {
+            throw new AssertionError(getEndpointUri() + " " + message);
+        }
     }
 
     public int getExpectedMinimumCount() {