You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ma...@apache.org on 2017/09/02 06:00:04 UTC

[02/13] james-project git commit: JAMES-2133 Make it possible to restart cassandra container every X tests

JAMES-2133 Make it possible to restart cassandra container every X tests

        When running cassandra in a container, it may need a lot
        of RAM to keep up with big testsuites. Running a container
        every once in a while help keeping everything fast.


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

Branch: refs/heads/master
Commit: 6c6cb53bb48743ea3343dd49204cf356d9171758
Parents: a18a448
Author: Matthieu Baechler <ma...@apache.org>
Authored: Tue Aug 29 17:07:36 2017 +0200
Committer: Matthieu Baechler <ma...@apache.org>
Committed: Sat Sep 2 07:58:48 2017 +0200

----------------------------------------------------------------------
 .../ContainerLifecycleConfiguration.java        | 95 ++++++++++++++++++++
 .../CassandraGetMessageListMethodTest.java      | 11 ++-
 .../CassandraSetMessagesMethodTest.java         | 11 ++-
 3 files changed, 113 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/6c6cb53b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/ContainerLifecycleConfiguration.java
----------------------------------------------------------------------
diff --git a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/ContainerLifecycleConfiguration.java b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/ContainerLifecycleConfiguration.java
new file mode 100644
index 0000000..cabda1f
--- /dev/null
+++ b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/ContainerLifecycleConfiguration.java
@@ -0,0 +1,95 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.backends.cassandra;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.rules.TestRule;
+import org.junit.runners.model.Statement;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.shaded.com.google.common.base.Preconditions;
+
+public class ContainerLifecycleConfiguration {
+
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    public static class Builder {
+        private static int DEFAULT_ITERATIONS_BETWEEN_RESTART = 20;
+
+        private GenericContainer<?> container;
+        private int iterationsBetweenRestart = DEFAULT_ITERATIONS_BETWEEN_RESTART;
+
+        private Builder() {}
+
+        public Builder container(GenericContainer<?> container) {
+            this.container = container;
+            return this;
+        }
+
+        public Builder iterationsBetweenRestart(int iterationsBetweenRestart) {
+            this.iterationsBetweenRestart = iterationsBetweenRestart;
+            return this;
+        }
+
+        public ContainerLifecycleConfiguration build() {
+            Preconditions.checkState(container != null);
+            return new ContainerLifecycleConfiguration(container, iterationsBetweenRestart);
+        }
+    }
+
+    private final GenericContainer<?> container;
+    private final int iterationsBetweenRestart;
+    private AtomicInteger iterationsBeforeRestart;
+
+    public ContainerLifecycleConfiguration(GenericContainer<?> container, int iterationsBetweenRestart) {
+        this.container = container;
+        this.iterationsBetweenRestart = iterationsBetweenRestart;
+        this.iterationsBeforeRestart = new AtomicInteger(iterationsBetweenRestart);
+    }
+
+    private void restartContainer() {
+        iterationsBeforeRestart.set(iterationsBetweenRestart);
+        container.stop();
+        container.start();
+    }
+
+    private boolean needsRestart() {
+        return iterationsBeforeRestart.decrementAndGet() <= 0;
+    }
+
+    private void restartContainerIfNeeded() {
+        if (needsRestart()) {
+            restartContainer();
+        }
+    }
+
+    public TestRule asTestRule() {
+        return (base, description) -> new Statement() {
+            @Override
+            public void evaluate() throws Throwable {
+                restartContainerIfNeeded();
+                base.evaluate();
+            }
+        };
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/6c6cb53b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraGetMessageListMethodTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraGetMessageListMethodTest.java b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraGetMessageListMethodTest.java
index 7e4abe5..1d8857b 100644
--- a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraGetMessageListMethodTest.java
+++ b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraGetMessageListMethodTest.java
@@ -22,18 +22,25 @@ package org.apache.james.jmap.cassandra;
 import org.apache.james.CassandraJmapTestRule;
 import org.apache.james.DockerCassandraRule;
 import org.apache.james.GuiceJamesServer;
+import org.apache.james.backends.cassandra.ContainerLifecycleConfiguration;
 import org.apache.james.jmap.methods.integration.GetMessageListMethodTest;
 import org.junit.ClassRule;
 import org.junit.Rule;
+import org.junit.rules.TestRule;
 
 public class CassandraGetMessageListMethodTest extends GetMessageListMethodTest {
 
     @ClassRule
     public static DockerCassandraRule cassandra = new DockerCassandraRule();
-    
+
+    public static ContainerLifecycleConfiguration cassandraLifecycleConfiguration = ContainerLifecycleConfiguration.builder().iterationsBetweenRestart(20).container(cassandra.getRawContainer()).build();
+
     @Rule 
     public CassandraJmapTestRule rule = CassandraJmapTestRule.defaultTestRule();
-    
+
+    @Rule
+    public TestRule cassandraLifecycleTestRule = cassandraLifecycleConfiguration.asTestRule();
+
     @Override
     protected GuiceJamesServer createJmapServer() {
         return rule.jmapServer(cassandra.getModule());

http://git-wip-us.apache.org/repos/asf/james-project/blob/6c6cb53b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraSetMessagesMethodTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraSetMessagesMethodTest.java b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraSetMessagesMethodTest.java
index 2691aaf..b58e8d1 100644
--- a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraSetMessagesMethodTest.java
+++ b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraSetMessagesMethodTest.java
@@ -22,20 +22,27 @@ package org.apache.james.jmap.cassandra;
 import org.apache.james.CassandraJmapTestRule;
 import org.apache.james.DockerCassandraRule;
 import org.apache.james.GuiceJamesServer;
+import org.apache.james.backends.cassandra.ContainerLifecycleConfiguration;
 import org.apache.james.jmap.methods.integration.SetMessagesMethodTest;
 import org.apache.james.mailbox.cassandra.ids.CassandraMessageId;
 import org.apache.james.mailbox.model.MessageId;
 import org.junit.ClassRule;
 import org.junit.Rule;
+import org.junit.rules.TestRule;
 
 public class CassandraSetMessagesMethodTest extends SetMessagesMethodTest {
 
     @ClassRule
     public static DockerCassandraRule cassandra = new DockerCassandraRule();
-    
-    @Rule 
+
+    public static ContainerLifecycleConfiguration cassandraLifecycleConfiguration = ContainerLifecycleConfiguration.builder().iterationsBetweenRestart(20).container(cassandra.getRawContainer()).build();
+
+    @Rule
     public CassandraJmapTestRule rule = CassandraJmapTestRule.defaultTestRule();
 
+    @Rule
+    public TestRule cassandraLifecycleTestRule = cassandraLifecycleConfiguration.asTestRule();
+
     @Override
     protected GuiceJamesServer createJmapServer() {
         return rule.jmapServer(cassandra.getModule());


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