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

ctubbsii pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/accumulo.git


The following commit(s) were added to refs/heads/main by this push:
     new f4a1123  Make MiniAccumuloCluster AutoCloseable (#1959)
f4a1123 is described below

commit f4a11230bbc881b6eeede73f1711143b08cb0949
Author: Dom G <47725857+domgargu...@users.noreply.github.com>
AuthorDate: Mon Mar 8 13:20:21 2021 -0500

    Make MiniAccumuloCluster AutoCloseable (#1959)
    
    * Make MAC auto-closable
    * Handle InterruptedException
---
 .../accumulo/minicluster/MiniAccumuloCluster.java  | 15 ++++-
 .../accumulo/minicluster/MiniAccumuloConfig.java   | 15 +++++
 .../MiniAccumuloClusterExistingZooKeepersTest.java | 72 +++++++---------------
 3 files changed, 52 insertions(+), 50 deletions(-)

diff --git 
a/minicluster/src/main/java/org/apache/accumulo/minicluster/MiniAccumuloCluster.java
 
b/minicluster/src/main/java/org/apache/accumulo/minicluster/MiniAccumuloCluster.java
index bc9fa92..7b968ab 100644
--- 
a/minicluster/src/main/java/org/apache/accumulo/minicluster/MiniAccumuloCluster.java
+++ 
b/minicluster/src/main/java/org/apache/accumulo/minicluster/MiniAccumuloCluster.java
@@ -42,7 +42,7 @@ import com.google.common.base.Preconditions;
  *
  * @since 1.5.0
  */
-public class MiniAccumuloCluster {
+public class MiniAccumuloCluster implements AutoCloseable {
 
   private MiniAccumuloClusterImpl impl;
 
@@ -110,6 +110,19 @@ public class MiniAccumuloCluster {
   }
 
   /**
+   * @since 2.0.1
+   */
+  @Override
+  public void close() throws IOException {
+    try {
+      this.stop();
+    } catch (InterruptedException e) {
+      Thread.currentThread().interrupt();
+      throw new RuntimeException(e);
+    }
+  }
+
+  /**
    * @since 1.6.0
    */
   public MiniAccumuloConfig getConfig() {
diff --git 
a/minicluster/src/main/java/org/apache/accumulo/minicluster/MiniAccumuloConfig.java
 
b/minicluster/src/main/java/org/apache/accumulo/minicluster/MiniAccumuloConfig.java
index c8f6e7b..42ba345 100644
--- 
a/minicluster/src/main/java/org/apache/accumulo/minicluster/MiniAccumuloConfig.java
+++ 
b/minicluster/src/main/java/org/apache/accumulo/minicluster/MiniAccumuloConfig.java
@@ -99,6 +99,21 @@ public class MiniAccumuloConfig {
   }
 
   /**
+   * Configure an existing ZooKeeper instance to use. Calling this method is 
optional. If not set, a
+   * new ZooKeeper instance is created.
+   *
+   * @param existingZooKeepers
+   *          Connection string for a already-running ZooKeeper instance. A 
null value will turn off
+   *          this feature.
+   *
+   * @since 2.1.0
+   */
+  public MiniAccumuloConfig setExistingZooKeepers(String existingZooKeepers) {
+    impl.setExistingZooKeepers(existingZooKeepers);
+    return this;
+  }
+
+  /**
    * Configure the time to wait for ZooKeeper to startup. Calling this method 
is optional. The
    * default is 20000 milliseconds
    *
diff --git 
a/minicluster/src/test/java/org/apache/accumulo/minicluster/MiniAccumuloClusterExistingZooKeepersTest.java
 
b/minicluster/src/test/java/org/apache/accumulo/minicluster/MiniAccumuloClusterExistingZooKeepersTest.java
index 609dba0..f1b1943 100644
--- 
a/minicluster/src/test/java/org/apache/accumulo/minicluster/MiniAccumuloClusterExistingZooKeepersTest.java
+++ 
b/minicluster/src/test/java/org/apache/accumulo/minicluster/MiniAccumuloClusterExistingZooKeepersTest.java
@@ -23,7 +23,6 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.io.File;
-import java.io.IOException;
 import java.util.Map;
 
 import org.apache.commons.io.FileUtils;
@@ -31,13 +30,10 @@ import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.framework.CuratorFrameworkFactory;
 import org.apache.curator.retry.RetryOneTime;
 import org.apache.curator.test.TestingServer;
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TestName;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 
@@ -48,16 +44,13 @@ public class MiniAccumuloClusterExistingZooKeepersTest {
 
   private static final String SECRET = "superSecret";
 
-  private static final Logger log =
-      LoggerFactory.getLogger(MiniAccumuloClusterExistingZooKeepersTest.class);
-  private TestingServer zooKeeper;
-  private MiniAccumuloCluster accumulo;
+  private MiniAccumuloConfig config;
 
   @Rule
   public TestName testName = new TestName();
 
   @Before
-  public void setupTestCluster() throws Exception {
+  public void setupTestCluster() {
     assertTrue(BASE_DIR.mkdirs() || BASE_DIR.isDirectory());
     File testDir = new File(BASE_DIR, testName.getMethodName());
     FileUtils.deleteQuietly(testDir);
@@ -65,51 +58,32 @@ public class MiniAccumuloClusterExistingZooKeepersTest {
 
     // disable adminServer, which runs on port 8080 by default and we don't 
need
     System.setProperty("zookeeper.admin.enableServer", "false");
-    zooKeeper = new TestingServer();
-
-    MiniAccumuloConfig config = new MiniAccumuloConfig(testDir, SECRET);
-    config.getImpl().setExistingZooKeepers(zooKeeper.getConnectString());
-    accumulo = new MiniAccumuloCluster(config);
-    accumulo.start();
-  }
-
-  @After
-  public void teardownTestCluster() {
-    if (accumulo != null) {
-      try {
-        accumulo.stop();
-      } catch (IOException | InterruptedException e) {
-        log.warn("Failure during tear down", e);
-      }
-    }
-
-    if (zooKeeper != null) {
-      try {
-        zooKeeper.close();
-      } catch (IOException e) {
-        log.warn("Failure stopping test ZooKeeper server");
-      }
-    }
+    config = new MiniAccumuloConfig(testDir, SECRET);
   }
 
   @SuppressWarnings("deprecation")
   @Test
   public void canConnectViaExistingZooKeeper() throws Exception {
-    org.apache.accumulo.core.client.Connector conn = 
accumulo.getConnector("root", SECRET);
-    assertEquals(zooKeeper.getConnectString(), 
conn.getInstance().getZooKeepers());
-
-    String tableName = "foo";
-    conn.tableOperations().create(tableName);
-    Map<String,String> tableIds = conn.tableOperations().tableIdMap();
-    assertTrue(tableIds.containsKey(tableName));
-
-    String zkTablePath = String.format("/accumulo/%s/tables/%s/name",
-        conn.getInstance().getInstanceID(), tableIds.get(tableName));
-    try (CuratorFramework client =
-        CuratorFrameworkFactory.newClient(zooKeeper.getConnectString(), new 
RetryOneTime(1))) {
-      client.start();
-      assertNotNull(client.checkExists().forPath(zkTablePath));
-      assertEquals(tableName, new 
String(client.getData().forPath(zkTablePath)));
+    try (TestingServer zooKeeper = new TestingServer(); MiniAccumuloCluster 
accumulo =
+        new 
MiniAccumuloCluster(config.setExistingZooKeepers(zooKeeper.getConnectString())))
 {
+      accumulo.start();
+
+      org.apache.accumulo.core.client.Connector conn = 
accumulo.getConnector("root", SECRET);
+      assertEquals(zooKeeper.getConnectString(), 
conn.getInstance().getZooKeepers());
+
+      String tableName = "foo";
+      conn.tableOperations().create(tableName);
+      Map<String,String> tableIds = conn.tableOperations().tableIdMap();
+      assertTrue(tableIds.containsKey(tableName));
+
+      String zkTablePath = String.format("/accumulo/%s/tables/%s/name",
+          conn.getInstance().getInstanceID(), tableIds.get(tableName));
+      try (CuratorFramework client =
+          CuratorFrameworkFactory.newClient(zooKeeper.getConnectString(), new 
RetryOneTime(1))) {
+        client.start();
+        assertNotNull(client.checkExists().forPath(zkTablePath));
+        assertEquals(tableName, new 
String(client.getData().forPath(zkTablePath)));
+      }
     }
   }
 }

Reply via email to