You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pp...@apache.org on 2021/07/29 06:08:45 UTC

[camel-quarkus] 04/10: Test sample() EIP #2628

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

ppalaga pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git

commit 56bd770c39387dadfeb4de3f6409bb5c6dc7d435
Author: Peter Palaga <pp...@redhat.com>
AuthorDate: Tue Jul 27 12:23:58 2021 +0200

    Test sample() EIP #2628
---
 integration-test-groups/foundation/eip/pom.xml     |  5 ++++
 .../apache/camel/quarkus/eip/it/EipResource.java   | 10 +++++--
 .../org/apache/camel/quarkus/eip/it/EipRoutes.java |  4 +++
 .../org/apache/camel/quarkus/eip/it/EipTest.java   | 35 ++++++++++++++++++++++
 integration-tests/foundation-grouped/pom.xml       |  5 ++++
 5 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/integration-test-groups/foundation/eip/pom.xml b/integration-test-groups/foundation/eip/pom.xml
index f6ea876..e8babb8 100644
--- a/integration-test-groups/foundation/eip/pom.xml
+++ b/integration-test-groups/foundation/eip/pom.xml
@@ -67,6 +67,11 @@
             <artifactId>rest-assured</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.assertj</groupId>
+            <artifactId>assertj-core</artifactId>
+            <scope>test</scope>
+        </dependency>
 
         <!-- The following dependencies guarantee that this module is built after them. You can update them by running `mvn process-resources -Pformat -N` from the source tree root directory -->
         <dependency>
diff --git a/integration-test-groups/foundation/eip/src/main/java/org/apache/camel/quarkus/eip/it/EipResource.java b/integration-test-groups/foundation/eip/src/main/java/org/apache/camel/quarkus/eip/it/EipResource.java
index fd0940f..0091b96 100644
--- a/integration-test-groups/foundation/eip/src/main/java/org/apache/camel/quarkus/eip/it/EipResource.java
+++ b/integration-test-groups/foundation/eip/src/main/java/org/apache/camel/quarkus/eip/it/EipResource.java
@@ -61,10 +61,16 @@ public class EipResource {
     @Path("/mock/{name}/{count}/{timeout}/{part}")
     @Produces(MediaType.TEXT_PLAIN)
     @GET
-    public String mockHeader(@PathParam("name") String name, @PathParam("count") int count, @PathParam("timeout") int timeout,
+    public String mockHeader(@PathParam("name") String name, @PathParam("count") String count,
+            @PathParam("timeout") int timeout,
             @PathParam("part") String part) {
         MockEndpoint mock = context.getEndpoint("mock:" + name, MockEndpoint.class);
-        mock.setExpectedMessageCount(count);
+
+        if (count.endsWith("+")) {
+            mock.setMinimumExpectedMessageCount(Integer.valueOf(count.substring(0, count.length() - 1)));
+        } else {
+            mock.setExpectedMessageCount(Integer.valueOf(count));
+        }
         try {
             mock.assertIsSatisfied(timeout);
         } catch (InterruptedException e1) {
diff --git a/integration-test-groups/foundation/eip/src/main/java/org/apache/camel/quarkus/eip/it/EipRoutes.java b/integration-test-groups/foundation/eip/src/main/java/org/apache/camel/quarkus/eip/it/EipRoutes.java
index 0fe86eb..a8e14ae 100644
--- a/integration-test-groups/foundation/eip/src/main/java/org/apache/camel/quarkus/eip/it/EipRoutes.java
+++ b/integration-test-groups/foundation/eip/src/main/java/org/apache/camel/quarkus/eip/it/EipRoutes.java
@@ -92,6 +92,10 @@ public class EipRoutes extends RouteBuilder {
         from("direct:routingSlip")
                 .routingSlip(header("routingSlipHeader"));
 
+        from("direct:sample")
+                .sample()
+                .to("mock:sample");
+
     }
 
     @Produces
diff --git a/integration-test-groups/foundation/eip/src/test/java/org/apache/camel/quarkus/eip/it/EipTest.java b/integration-test-groups/foundation/eip/src/test/java/org/apache/camel/quarkus/eip/it/EipTest.java
index 41fd986..85d720a 100644
--- a/integration-test-groups/foundation/eip/src/test/java/org/apache/camel/quarkus/eip/it/EipTest.java
+++ b/integration-test-groups/foundation/eip/src/test/java/org/apache/camel/quarkus/eip/it/EipTest.java
@@ -22,12 +22,16 @@ import java.util.List;
 import io.quarkus.test.junit.QuarkusTest;
 import io.restassured.RestAssured;
 import io.restassured.http.ContentType;
+import org.assertj.core.api.Assertions;
 import org.hamcrest.Matchers;
+import org.jboss.logging.Logger;
 import org.junit.jupiter.api.Test;
 
 @QuarkusTest
 class EipTest {
 
+    private static final Logger LOG = Logger.getLogger(EipTest.class);
+
     @Test
     public void claimCheckByHeader() {
         RestAssured.given()
@@ -314,4 +318,35 @@ class EipTest {
 
     }
 
+    @Test
+    public void sample() {
+        final int durationSec = 2;
+        LOG.infof("About to sent messages for %d seconds", durationSec);
+        final long deadline = System.currentTimeMillis() + (durationSec * 1000); // two seconds ahead
+        int i = 0;
+        while (System.currentTimeMillis() < deadline) {
+            /* Send messages for 2 seconds */
+            RestAssured.given()
+                    .contentType(ContentType.TEXT)
+                    .body("message-" + i++)
+                    .post("/eip/route/sample")
+                    .then()
+                    .statusCode(200);
+        }
+        LOG.infof("Sent %d messages", i);
+        /*
+         * We should normally get just 2 samples in 2 seconds using the default sample rate of 1 message per second
+         * But timing is hard in programming, let's allow one more
+         */
+        int overratedSampleUpperBound = durationSec + 1;
+        Assertions.assertThat(i).isGreaterThan(overratedSampleUpperBound);
+        String[] samples = RestAssured.get("/eip/mock/sample/1+/5000/body")
+                .then()
+                .statusCode(200)
+                .extract()
+                .body().asString().split(",");
+        LOG.infof("Got %d samples", samples.length);
+        Assertions.assertThat(samples.length).isBetween(1, overratedSampleUpperBound);
+    }
+
 }
diff --git a/integration-tests/foundation-grouped/pom.xml b/integration-tests/foundation-grouped/pom.xml
index 92712a2..5c3a46c 100644
--- a/integration-tests/foundation-grouped/pom.xml
+++ b/integration-tests/foundation-grouped/pom.xml
@@ -143,6 +143,11 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>org.assertj</groupId>
+            <artifactId>assertj-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.awaitility</groupId>
             <artifactId>awaitility</artifactId>
             <scope>test</scope>