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 ro...@apache.org on 2019/07/26 07:51:30 UTC
[james-project] 11/32: JAMES-2848 Ensure that only one
CassandraCluster is open at a given time
This is an automated email from the ASF dual-hosted git repository.
rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit b18c8f72c396e23cec8be744ba6012b8f796de2f
Author: Matthieu Baechler <ma...@apache.org>
AuthorDate: Tue Jul 23 15:17:44 2019 +0200
JAMES-2848 Ensure that only one CassandraCluster is open at a given time
---
.../james/backends/cassandra/CassandraCluster.java | 15 +++-
.../backends/cassandra/CassandraClusterTest.java | 82 ++++++++++++++++++++++
2 files changed, 96 insertions(+), 1 deletion(-)
diff --git a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraCluster.java b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraCluster.java
index 44df2f6..1517e00 100644
--- a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraCluster.java
+++ b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraCluster.java
@@ -18,6 +18,8 @@
****************************************************************/
package org.apache.james.backends.cassandra;
+import java.util.Optional;
+
import org.apache.james.backends.cassandra.components.CassandraModule;
import org.apache.james.backends.cassandra.init.CassandraTableManager;
import org.apache.james.backends.cassandra.init.CassandraTypesProvider;
@@ -33,13 +35,23 @@ import com.datastax.driver.core.Session;
public final class CassandraCluster implements AutoCloseable {
public static final String KEYSPACE = "testing";
+ private static Optional<Exception> startStackTrace = Optional.empty();
private final CassandraModule module;
private Session session;
private CassandraTypesProvider typesProvider;
private Cluster cluster;
public static CassandraCluster create(CassandraModule module, Host host) {
- return new CassandraCluster(module, host);
+ assertClusterNotRunning();
+ CassandraCluster cassandraCluster = new CassandraCluster(module, host);
+ startStackTrace = Optional.of(new Exception("initial connection call trace"));
+ return cassandraCluster;
+ }
+
+ private static void assertClusterNotRunning() {
+ startStackTrace.ifPresent( e -> {
+ throw new IllegalStateException("Cluster already running, look at the cause for the initial connection creation call trace", e);
+ });
}
private CassandraCluster(CassandraModule module, Host host) throws RuntimeException {
@@ -86,6 +98,7 @@ public final class CassandraCluster implements AutoCloseable {
public void closeCluster() {
cluster.closeAsync().force();
+ startStackTrace = Optional.empty();
}
public void clearTables() {
diff --git a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraClusterTest.java b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraClusterTest.java
new file mode 100644
index 0000000..6046770
--- /dev/null
+++ b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraClusterTest.java
@@ -0,0 +1,82 @@
+/****************************************************************
+ * 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 static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode;
+
+import org.apache.james.backends.cassandra.components.CassandraModule;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+class CassandraClusterTest {
+
+ @RegisterExtension
+ static DockerCassandraExtension cassandraExtension = new DockerCassandraExtension();
+ CassandraCluster connection;
+
+ @BeforeEach
+ void setUp() {
+ connection = methodToDetectInStackTrace();
+ }
+
+ CassandraCluster methodToDetectInStackTrace() {
+ return createCluster();
+ }
+
+ @AfterEach
+ void tearDown() {
+ connection.close();
+ }
+
+ private CassandraCluster createCluster() {
+ return CassandraCluster.create(CassandraModule.builder().build(), cassandraExtension.getDockerCassandra().getHost());
+ }
+
+ @Test
+ void creatingTwoClustersShouldThrow() {
+ assertThatThrownBy(this::createCluster).isInstanceOf(IllegalStateException.class);
+ }
+
+ @Test
+ void creatingTwoClustersSequentiallyShouldNotThrow() {
+ connection.close();
+ assertThatCode(() -> {
+ try (CassandraCluster cluster = createCluster()) {
+
+ }
+ }).doesNotThrowAnyException();
+ }
+
+ @Test
+ void closingAnAlreadyClosedConnectionShouldNotCloseANewOne() {
+ connection.close();
+ try (CassandraCluster cnx2 = createCluster()) {
+ connection.close();
+ assertThatThrownBy(this::createCluster).isInstanceOf(IllegalStateException.class);
+ }
+ }
+
+ @Test
+ void creatingTwoClustersShouldProvideFirstCreationStacktrace() {
+ assertThatThrownBy(this::createCluster).hasStackTraceContaining("methodToDetectInStackTrace");
+ }
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org