diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/TDNode.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/TDNode.java index df25076a95..800265868d 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/TDNode.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/TDNode.java @@ -1,6 +1,8 @@ package com.taosdata.jdbc.utils; +import java.io.BufferedReader; import java.io.File; +import java.io.InputStreamReader; import java.util.*; import java.util.concurrent.TimeUnit; @@ -31,6 +33,10 @@ public class TDNode { this.testCluster = testCluster; } + public void setRunning(int running) { + this.running = running; + } + public void searchTaosd(File dir, ArrayList taosdPath) { File[] fileList = dir.listFiles(); @@ -102,15 +108,46 @@ public class TDNode { this.running = 1; } - public void stop() { - String toBeKilled = "taosd"; + public Integer getTaosdPid() { + String cmd = "ps -ef|grep -w taosd| grep -v grep | awk '{print $2}'"; + String[] cmds = {"sh", "-c", cmd}; + try { + Process process = Runtime.getRuntime().exec(cmds); + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line = null; + Integer res = null; + while((line = reader.readLine()) != null) { + if(!line.isEmpty()) { + res = Integer.valueOf(line); + break; + } + } + + return res; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public void stop() { if (this.running != 0) { - String killCmd = "pkill -kill -x " + toBeKilled; - String[] killCmds = {"sh", "-c", killCmd}; - try { - Runtime.getRuntime().exec(killCmds).waitFor(); + Integer pid = null; + while((pid = getTaosdPid()) != null) { + + String killCmd = "kill -term " + pid; + String[] killCmds = {"sh", "-c", killCmd}; + try { + Runtime.getRuntime().exec(killCmds).waitFor(); + TimeUnit.SECONDS.sleep(2); + } catch (Exception e) { + e.printStackTrace(); + } + } + + try { for(int port = 6030; port < 6041; port ++) { String fuserCmd = "fuser -k -n tcp " + port; Runtime.getRuntime().exec(fuserCmd).waitFor(); @@ -120,7 +157,7 @@ public class TDNode { } this.running = 0; - System.out.println("dnode:" + this.index + " is stopped by pkill"); + System.out.println("dnode:" + this.index + " is stopped by kill -term"); } } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/TDNodes.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/TDNodes.java index ea15ae9863..efc4c53e28 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/TDNodes.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/TDNodes.java @@ -14,33 +14,6 @@ public class TDNodes { } } - public void setPath(String path) { - try { - String killCmd = "pkill -kill -x taosd"; - String[] killCmds = {"sh", "-c", killCmd}; - Runtime.getRuntime().exec(killCmds).waitFor(); - - String binPath = System.getProperty("user.dir"); - binPath += "/../../../debug"; - System.out.println("binPath: " + binPath); - - File file = new File(path); - binPath = file.getCanonicalPath(); - System.out.println("binPath real path: " + binPath); - - if(path.isEmpty()){ - file = new File(path + "/../../"); - path = file.getCanonicalPath(); - } - - for(int i = 0; i < tdNodes.size(); i++) { - tdNodes.get(i).setPath(path); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - public void setTestCluster(boolean testCluster) { this.testCluster = testCluster; } @@ -70,6 +43,11 @@ public class TDNodes { check(index); tdNodes.get(index - 1).setCfgConfig(option, value); } + + public TDNode getTDNode(int index) { + check(index); + return tdNodes.get(index - 1); + } public void start(int index) { check(index); diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/BaseTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/BaseTest.java index 5f105fb782..6c3437186f 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/BaseTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/BaseTest.java @@ -1,6 +1,5 @@ package com.taosdata.jdbc; -import java.io.File; import com.taosdata.jdbc.utils.TDNodes; import org.junit.AfterClass; @@ -8,31 +7,30 @@ import org.junit.BeforeClass; public class BaseTest { - private static boolean testCluster = false; - private static String deployPath = System.getProperty("user.dir"); - private static TDNodes tdNodes = new TDNodes(); + private static boolean testCluster = false; + private static TDNodes nodes = new TDNodes(); @BeforeClass public static void setupEnv() { - try{ - File file = new File(deployPath + "/../../../"); - String rootPath = file.getCanonicalPath(); - - tdNodes.setPath(rootPath); - tdNodes.setTestCluster(testCluster); + try{ + if (nodes.getTDNode(1).getTaosdPid() != null) { + System.out.println("Kill taosd before running JDBC test"); + nodes.getTDNode(1).setRunning(1); + nodes.stop(1); + } - tdNodes.deploy(1); - tdNodes.start(1); + nodes.setTestCluster(testCluster); + nodes.deploy(1); + nodes.start(1); } catch (Exception e) { - e.printStackTrace(); - System.out.println("Base Test Exception"); + e.printStackTrace(); } } @AfterClass public static void cleanUpEnv() { - tdNodes.stop(1); + nodes.stop(1); } } \ No newline at end of file