james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [04/10] james-project git commit: JAMES-2334 Reuse rabbitmq cluster after each test
Date Wed, 24 Oct 2018 01:51:30 GMT
JAMES-2334 Reuse rabbitmq cluster after each test

- update extension to reuse rabbit cluster and make each rabbit
leave cluster & restart after each test
- move extension to nested class level to optimize test executing time
(each class makes RabbitMQ cluster start one time -> 3 class = three times)


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/614ebad3
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/614ebad3
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/614ebad3

Branch: refs/heads/master
Commit: 614ebad33a1ab846ccfede59c22c29f613949451
Parents: b1035f3
Author: duc <dtran@linagora.com>
Authored: Fri Oct 19 14:32:25 2018 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Wed Oct 24 08:48:16 2018 +0700

----------------------------------------------------------------------
 .../DockerClusterRabbitMQExtension.java         | 40 ++++++++++++++++----
 .../james/backend/rabbitmq/DockerRabbitMQ.java  | 27 +++++++++++--
 .../backend/rabbitmq/RabbitMQClusterTest.java   |  3 +-
 .../apache/james/util/docker/RateLimiters.java  |  5 ---
 4 files changed, 58 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/614ebad3/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerClusterRabbitMQExtension.java
----------------------------------------------------------------------
diff --git a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerClusterRabbitMQExtension.java
b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerClusterRabbitMQExtension.java
index aede7c6..81f62a5 100644
--- a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerClusterRabbitMQExtension.java
+++ b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerClusterRabbitMQExtension.java
@@ -21,7 +21,9 @@ package org.apache.james.backend.rabbitmq;
 import java.nio.charset.StandardCharsets;
 
 import org.apache.james.util.Runnables;
+import org.junit.jupiter.api.extension.AfterAllCallback;
 import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
 import org.junit.jupiter.api.extension.BeforeEachCallback;
 import org.junit.jupiter.api.extension.ExtensionContext;
 import org.junit.jupiter.api.extension.ParameterContext;
@@ -34,16 +36,22 @@ import com.google.common.collect.ImmutableList;
 import com.google.common.hash.Hashing;
 import com.rabbitmq.client.Address;
 
-public class DockerClusterRabbitMQExtension implements BeforeEachCallback, AfterEachCallback,
ParameterResolver {
+public class DockerClusterRabbitMQExtension implements BeforeAllCallback, BeforeEachCallback,
AfterEachCallback, AfterAllCallback, ParameterResolver {
 
-    public static final String RABBIT_1 = "rabbit1";
-    public static final String RABBIT_2 = "rabbit2";
-    public static final String RABBIT_3 = "rabbit3";
+    private static final String RABBIT_1_NODE_NAME = "rabbit@rabbit1";
+    private static final String RABBIT_2_NODE_NAME = "rabbit@rabbit2";
+    private static final String RABBIT_3_NODE_NAME = "rabbit@rabbit3";
+    static final String RABBIT_1 = "rabbit1";
+    static final String RABBIT_2 = "rabbit2";
+    static final String RABBIT_3 = "rabbit3";
     private DockerRabbitMQCluster cluster;
     private Network network;
+    private DockerRabbitMQ rabbitMQ1;
+    private DockerRabbitMQ rabbitMQ2;
+    private DockerRabbitMQ rabbitMQ3;
 
     @Override
-    public void beforeEach(ExtensionContext context) {
+    public void beforeAll(ExtensionContext extensionContext) throws Exception {
         String cookie = Hashing.sha256().hashString("secret cookie here", StandardCharsets.UTF_8).toString();
 
         network = Network.NetworkImpl.builder()
@@ -51,15 +59,18 @@ public class DockerClusterRabbitMQExtension implements BeforeEachCallback,
After
             .createNetworkCmdModifiers(ImmutableList.of())
             .build();
 
-        DockerRabbitMQ rabbitMQ1 = DockerRabbitMQ.withCookieAndNodeName(RABBIT_1, cookie,
"rabbit@rabbit1", network);
-        DockerRabbitMQ rabbitMQ2 = DockerRabbitMQ.withCookieAndNodeName(RABBIT_2, cookie,
"rabbit@rabbit2", network);
-        DockerRabbitMQ rabbitMQ3 = DockerRabbitMQ.withCookieAndNodeName(RABBIT_3, cookie,
"rabbit@rabbit3", network);
+        rabbitMQ1 = DockerRabbitMQ.withCookieAndNodeName(RABBIT_1, cookie, RABBIT_1_NODE_NAME,
network);
+        rabbitMQ2 = DockerRabbitMQ.withCookieAndNodeName(RABBIT_2, cookie, RABBIT_2_NODE_NAME,
network);
+        rabbitMQ3 = DockerRabbitMQ.withCookieAndNodeName(RABBIT_3, cookie, RABBIT_3_NODE_NAME,
network);
 
         Runnables.runParallel(
             rabbitMQ1::start,
             rabbitMQ2::start,
             rabbitMQ3::start);
+    }
 
+    @Override
+    public void beforeEach(ExtensionContext context) {
         Runnables.runParallel(
             Throwing.runnable(() -> rabbitMQ2.join(rabbitMQ1)),
             Throwing.runnable(() -> rabbitMQ3.join(rabbitMQ1)));
@@ -69,6 +80,11 @@ public class DockerClusterRabbitMQExtension implements BeforeEachCallback,
After
 
     @Override
     public void afterEach(ExtensionContext context) throws Exception {
+        cluster.detach();
+    }
+
+    @Override
+    public void afterAll(ExtensionContext extensionContext) throws Exception {
         cluster.stop();
         network.close();
     }
@@ -118,5 +134,13 @@ public class DockerClusterRabbitMQExtension implements BeforeEachCallback,
After
             return ImmutableList.of(
                 rabbitMQ1.address(), rabbitMQ2.address(), rabbitMQ3.address());
         }
+
+        public void detach() {
+            rabbitMQ3.performIfRunning(DockerRabbitMQ::reset);
+            rabbitMQ1.performIfRunning(rabbitMQ -> rabbitMQ.forgetNode(RABBIT_3_NODE_NAME));
+            rabbitMQ2.performIfRunning(DockerRabbitMQ::reset);
+            rabbitMQ1.performIfRunning(rabbitMQ -> rabbitMQ.forgetNode(RABBIT_2_NODE_NAME));
+            rabbitMQ1.performIfRunning(DockerRabbitMQ::reset);
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/614ebad3/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerRabbitMQ.java
----------------------------------------------------------------------
diff --git a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerRabbitMQ.java
b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerRabbitMQ.java
index e88c74d..9153f81 100644
--- a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerRabbitMQ.java
+++ b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerRabbitMQ.java
@@ -34,7 +34,9 @@ import org.testcontainers.containers.GenericContainer;
 import org.testcontainers.containers.Network;
 import org.testcontainers.containers.wait.strategy.Wait;
 import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
+import org.testcontainers.containers.wait.strategy.WaitStrategy;
 
+import com.github.fge.lambdas.consumers.ThrowingConsumer;
 import com.google.common.collect.ImmutableMap;
 import com.rabbitmq.client.Address;
 import com.rabbitmq.client.ConnectionFactory;
@@ -49,6 +51,7 @@ public class DockerRabbitMQ {
     private static final String DEFAULT_RABBITMQ_PASSWORD = "guest";
     private static final String RABBITMQ_ERLANG_COOKIE = "RABBITMQ_ERLANG_COOKIE";
     private static final String RABBITMQ_NODENAME = "RABBITMQ_NODENAME";
+    private static final Duration TEN_MINUTE_TIMEOUT = Duration.ofMinutes(10);
 
     private final GenericContainer<?> container;
     private final Optional<String> nodeName;
@@ -78,10 +81,13 @@ public class DockerRabbitMQ {
         this.nodeName = nodeName;
     }
 
-    private WaitAllStrategy waitStrategy() {
+    private WaitStrategy waitStrategy() {
         return new WaitAllStrategy()
-            .withStrategy(Wait.forHttp("").forPort(DEFAULT_RABBITMQ_ADMIN_PORT).withRateLimiter(RateLimiters.TWENTIES_PER_MINUTE))
-            .withStrategy(new RabbitMQWaitStrategy(this, Duration.ofMinutes(10)));
+            .withStrategy(Wait.forHttp("").forPort(DEFAULT_RABBITMQ_ADMIN_PORT)
+                .withRateLimiter(RateLimiters.DEFAULT)
+                .withStartupTimeout(TEN_MINUTE_TIMEOUT))
+            .withStrategy(new RabbitMQWaitStrategy(this, TEN_MINUTE_TIMEOUT))
+            .withStartupTimeout(TEN_MINUTE_TIMEOUT);
     }
 
     private String randomName() {
@@ -177,6 +183,15 @@ public class DockerRabbitMQ {
         startApp();
     }
 
+    public void forgetNode(String removalClusterNodeName) throws Exception {
+        String stdout = container()
+            .execInContainer("rabbitmqctl", "-n", this.nodeName.get(), "forget_cluster_node",
removalClusterNodeName)
+            .getStdout();
+        LOGGER.debug("forget_cluster_node: {}", stdout);
+
+        startApp();
+    }
+
     public void waitForReadyness() {
         waitStrategy().waitUntilReady(container);
     }
@@ -200,4 +215,10 @@ public class DockerRabbitMQ {
                 .setPort(getAdminPort())
                 .build();
     }
+
+    public void performIfRunning(ThrowingConsumer<DockerRabbitMQ> actionPerform) {
+        if (container.isRunning()) {
+            actionPerform.accept(this);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/614ebad3/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQClusterTest.java
----------------------------------------------------------------------
diff --git a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQClusterTest.java
b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQClusterTest.java
index b72b45b..bf9a875 100644
--- a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQClusterTest.java
+++ b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQClusterTest.java
@@ -57,13 +57,13 @@ import com.rabbitmq.client.Channel;
 import com.rabbitmq.client.Connection;
 import com.rabbitmq.client.ConnectionFactory;
 
-@ExtendWith(DockerClusterRabbitMQExtension.class)
 class RabbitMQClusterTest {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(RabbitMQClusterTest.class);
 
     private static final String QUEUE = "queue";
 
+    @ExtendWith(DockerClusterRabbitMQExtension.class)
     @Nested
     class ClusterSharing {
 
@@ -147,6 +147,7 @@ class RabbitMQClusterTest {
 
     }
 
+    @ExtendWith(DockerClusterRabbitMQExtension.class)
     @Nested
     class ClusterNodesFailure {
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/614ebad3/server/testing/src/main/java/org/apache/james/util/docker/RateLimiters.java
----------------------------------------------------------------------
diff --git a/server/testing/src/main/java/org/apache/james/util/docker/RateLimiters.java b/server/testing/src/main/java/org/apache/james/util/docker/RateLimiters.java
index de92ba1..d75730e 100644
--- a/server/testing/src/main/java/org/apache/james/util/docker/RateLimiters.java
+++ b/server/testing/src/main/java/org/apache/james/util/docker/RateLimiters.java
@@ -29,9 +29,4 @@ public interface RateLimiters {
         .withRate(20, TimeUnit.SECONDS)
         .withConstantThroughput()
         .build();
-
-    RateLimiter TWENTIES_PER_MINUTE = RateLimiterBuilder.newBuilder()
-        .withRate(20, TimeUnit.MINUTES)
-        .withConstantThroughput()
-        .build();
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message