You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by hx...@apache.org on 2021/05/05 16:41:11 UTC

[iotdb] 02/02: refactor testcontainer

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

hxd pushed a commit to branch test_container
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit 73eb4ead90cacc5b4d1a37e018c2a58128827fb3
Author: xiangdong huang <sa...@gmail.com>
AuthorDate: Thu May 6 00:38:43 2021 +0800

    refactor testcontainer
---
 .../java/org/apache/iotdb/cluster/ClusterMain.java |  2 +-
 .../server/clusterinfo/ClusterInfoServerTest.java  |  4 +-
 .../clusterinfo/ClusterInfoServiceImplTest.java    | 12 ++----
 .../test/java/org/apache/iotdb/db/sql/Cases.java   | 20 ++++++---
 .../java/org/apache/iotdb/db/sql/ClusterIT.java    | 50 ++--------------------
 .../java/org/apache/iotdb/db/sql/SingleNodeIT.java | 40 ++++-------------
 6 files changed, 32 insertions(+), 96 deletions(-)

diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/ClusterMain.java b/cluster/src/main/java/org/apache/iotdb/cluster/ClusterMain.java
index 3518a8b..139bcd6 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/ClusterMain.java
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/ClusterMain.java
@@ -36,8 +36,8 @@ import org.apache.iotdb.db.conf.IoTDBConstant;
 import org.apache.iotdb.db.conf.IoTDBDescriptor;
 import org.apache.iotdb.db.exception.StartupException;
 import org.apache.iotdb.db.exception.query.QueryProcessException;
-
 import org.apache.iotdb.db.utils.TestOnly;
+
 import org.apache.thrift.TException;
 import org.apache.thrift.async.TAsyncClientManager;
 import org.apache.thrift.protocol.TBinaryProtocol.Factory;
diff --git a/cluster/src/test/java/org/apache/iotdb/cluster/server/clusterinfo/ClusterInfoServerTest.java b/cluster/src/test/java/org/apache/iotdb/cluster/server/clusterinfo/ClusterInfoServerTest.java
index c0574ea..35818f8 100644
--- a/cluster/src/test/java/org/apache/iotdb/cluster/server/clusterinfo/ClusterInfoServerTest.java
+++ b/cluster/src/test/java/org/apache/iotdb/cluster/server/clusterinfo/ClusterInfoServerTest.java
@@ -23,11 +23,9 @@ import org.apache.iotdb.cluster.rpc.thrift.ClusterInfoService;
 import org.apache.iotdb.cluster.server.clusterInfo.ClusterInfoServer;
 import org.apache.iotdb.db.conf.IoTDBDescriptor;
 import org.apache.iotdb.db.exception.metadata.MetadataException;
-import org.apache.iotdb.rpc.IoTDBConnectionException;
 import org.apache.iotdb.rpc.RpcTransportFactory;
-import org.apache.iotdb.service.rpc.thrift.TSIService;
+
 import org.apache.thrift.protocol.TBinaryProtocol;
-import org.apache.thrift.protocol.TCompactProtocol;
 import org.apache.thrift.transport.TSocket;
 import org.apache.thrift.transport.TTransport;
 import org.apache.thrift.transport.TTransportException;
diff --git a/cluster/src/test/java/org/apache/iotdb/cluster/server/clusterinfo/ClusterInfoServiceImplTest.java b/cluster/src/test/java/org/apache/iotdb/cluster/server/clusterinfo/ClusterInfoServiceImplTest.java
index 63eacc3..544bfcd 100644
--- a/cluster/src/test/java/org/apache/iotdb/cluster/server/clusterinfo/ClusterInfoServiceImplTest.java
+++ b/cluster/src/test/java/org/apache/iotdb/cluster/server/clusterinfo/ClusterInfoServiceImplTest.java
@@ -19,28 +19,24 @@
 
 package org.apache.iotdb.cluster.server.clusterinfo;
 
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
 import org.apache.iotdb.cluster.ClusterMain;
-import org.apache.iotdb.cluster.partition.PartitionTable;
 import org.apache.iotdb.cluster.rpc.thrift.DataPartitionEntry;
 import org.apache.iotdb.cluster.rpc.thrift.Node;
 import org.apache.iotdb.cluster.server.MetaClusterServer;
 import org.apache.iotdb.cluster.server.clusterInfo.ClusterInfoServiceImpl;
 import org.apache.iotdb.cluster.server.member.MetaGroupMember;
-
 import org.apache.iotdb.cluster.server.member.MetaGroupMemberTest;
-import org.apache.iotdb.db.engine.StorageEngine;
-import org.apache.iotdb.db.exception.metadata.IllegalPathException;
 import org.apache.iotdb.db.exception.metadata.MetadataException;
 import org.apache.iotdb.db.metadata.PartialPath;
+
 import org.apache.thrift.TException;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
-import org.powermock.reflect.Whitebox;
+
+import java.util.Collections;
+import java.util.List;
 
 public class ClusterInfoServiceImplTest {
 
diff --git a/testcontainer/src/test/java/org/apache/iotdb/db/sql/Cases.java b/testcontainer/src/test/java/org/apache/iotdb/db/sql/Cases.java
index 4cae584..bbb9e6a 100644
--- a/testcontainer/src/test/java/org/apache/iotdb/db/sql/Cases.java
+++ b/testcontainer/src/test/java/org/apache/iotdb/db/sql/Cases.java
@@ -18,21 +18,29 @@
  */
 package org.apache.iotdb.db.sql;
 
+import org.junit.Assert;
+import org.junit.Test;
+
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.HashSet;
 import java.util.Set;
-import org.junit.Assert;
-import org.junit.Test;
 
 public abstract class Cases {
 
-  private Statement writeStatement;
-  private Connection writeConnection;
-  private Statement readStatement;
-  private Connection readConnection;
+  protected Statement writeStatement;
+  protected Connection writeConnection;
+  protected Statement readStatement;
+  protected Connection readConnection;
+
+  public void tearDown() throws Exception {
+    writeStatement.close();
+    writeConnection.close();
+    readStatement.close();
+    readConnection.close();
+  }
 
   @Test
   public void testSimplePutAndGet() throws SQLException {
diff --git a/testcontainer/src/test/java/org/apache/iotdb/db/sql/ClusterIT.java b/testcontainer/src/test/java/org/apache/iotdb/db/sql/ClusterIT.java
index 6dc6c76..c1ee5a7 100644
--- a/testcontainer/src/test/java/org/apache/iotdb/db/sql/ClusterIT.java
+++ b/testcontainer/src/test/java/org/apache/iotdb/db/sql/ClusterIT.java
@@ -22,22 +22,18 @@ package org.apache.iotdb.db.sql;
 import org.apache.iotdb.jdbc.Config;
 
 import org.junit.After;
-import org.junit.Assert;
 import org.junit.Before;
-import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testcontainers.containers.DockerComposeContainer;
 
 import java.sql.Connection;
 import java.sql.DriverManager;
-import java.sql.ResultSet;
-import java.sql.SQLException;
 import java.sql.Statement;
-import java.util.HashSet;
-import java.util.Set;
 
-public abstract class ClusterIT {
+// do not add tests here.
+// add tests into Cases.java instead.
+public abstract class ClusterIT extends Cases {
 
   private static Logger logger = LoggerFactory.getLogger(ClusterIT.class);
 
@@ -87,44 +83,6 @@ public abstract class ClusterIT {
 
   @After
   public void tearDown() throws Exception {
-    writeStatement.close();
-    writeConnection.close();
-    readStatement.close();
-    readConnection.close();
-  }
-
-  @Test
-  public void testSimplePutAndGet() throws SQLException {
-
-    String[] timeSeriesArray = {"root.sg1.aa.bb", "root.sg1.aa.bb.cc", "root.sg1.aa"};
-
-    for (String timeSeries : timeSeriesArray) {
-      writeStatement.execute(
-          String.format(
-              "create timeseries %s with datatype=INT64, encoding=PLAIN, compression=SNAPPY",
-              timeSeries));
-    }
-    ResultSet resultSet = null;
-    resultSet = readStatement.executeQuery("show timeseries");
-    Set<String> result = new HashSet<>();
-    while (resultSet.next()) {
-      result.add(resultSet.getString(1));
-    }
-    Assert.assertEquals(3, result.size());
-    for (String timeseries : timeSeriesArray) {
-      Assert.assertTrue(result.contains(timeseries));
-    }
-    resultSet.close();
-
-    // test https://issues.apache.org/jira/browse/IOTDB-1331
-    writeStatement.execute("insert into root.ln.wf01.wt01(time, temperature) values(10, 1.0)");
-    resultSet = readStatement.executeQuery("select avg(temperature) from root.ln.wf01.wt01");
-    if (resultSet.next()) {
-      Assert.assertEquals(1.0, resultSet.getDouble(1), 0.01);
-    } else {
-      Assert.fail("expect 1 result, but get an empty resultSet.");
-    }
-    Assert.assertFalse(resultSet.next());
-    resultSet.close();
+    super.tearDown();
   }
 }
diff --git a/testcontainer/src/test/java/org/apache/iotdb/db/sql/SingleNodeIT.java b/testcontainer/src/test/java/org/apache/iotdb/db/sql/SingleNodeIT.java
index 65b32ec..3a042e9 100644
--- a/testcontainer/src/test/java/org/apache/iotdb/db/sql/SingleNodeIT.java
+++ b/testcontainer/src/test/java/org/apache/iotdb/db/sql/SingleNodeIT.java
@@ -32,13 +32,11 @@ import org.testcontainers.utility.DockerImageName;
 
 import java.io.File;
 import java.sql.*;
-import java.util.HashSet;
-import java.util.Set;
 
-public class SingleNodeIT {
+// do not add tests here.
+// add tests into Cases.java instead.
+public class SingleNodeIT extends Cases {
   private static Logger logger = LoggerFactory.getLogger(SingleNodeIT.class);
-  private Statement statement;
-  private Connection connection;
 
   @Rule
   public GenericContainer dslContainer =
@@ -63,38 +61,16 @@ public class SingleNodeIT {
   @Before
   public void setUp() throws Exception {
     rpcPort = dslContainer.getMappedPort(6667);
-
     syncPort = dslContainer.getMappedPort(5555);
     Class.forName(Config.JDBC_DRIVER_NAME);
-    connection = DriverManager.getConnection("jdbc:iotdb://127.0.0.1:" + rpcPort, "root", "root");
-    statement = connection.createStatement();
+    writeConnection =
+        readConnection =
+            DriverManager.getConnection("jdbc:iotdb://127.0.0.1:" + rpcPort, "root", "root");
+    writeStatement = readStatement = writeConnection.createStatement();
   }
 
   @After
   public void tearDown() throws Exception {
-    statement.close();
-    connection.close();
-  }
-
-  @Test
-  public void testSimplePutAndGet() throws SQLException {
-    String[] timeSeriesArray = {"root.sg1.aa.bb", "root.sg1.aa.bb.cc", "root.sg1.aa"};
-
-    for (String timeSeries : timeSeriesArray) {
-      statement.execute(
-          String.format(
-              "create timeseries %s with datatype=INT64, encoding=PLAIN, compression=SNAPPY",
-              timeSeries));
-    }
-    ResultSet resultSet = null;
-    resultSet = statement.executeQuery("show timeseries");
-    Set<String> result = new HashSet<>();
-    while (resultSet.next()) {
-      result.add(resultSet.getString(1));
-    }
-    Assert.assertEquals(3, result.size());
-    for (String timeseries : timeSeriesArray) {
-      Assert.assertTrue(result.contains(timeseries));
-    }
+    super.tearDown();
   }
 }