提交 fe722349 authored 作者: Tomas Pospichal's avatar Tomas Pospichal

Ensure tool tests do not leave running servers after failure

上级 ddd4d28c
......@@ -33,6 +33,7 @@ import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.h2.api.ErrorCode;
......@@ -64,6 +65,7 @@ public class TestTools extends TestBase {
private static String lastUrl;
private Server server;
private List<Server> remainingServers = new ArrayList<Server>(3);
/**
* Run just this test.
......@@ -171,7 +173,6 @@ public class TestTools extends TestBase {
assertThrows(ErrorCode.EXCEPTION_OPENING_PORT_2, c).runTool("-web",
"-webPort", "9002", "-tcp", "-tcpPort", "9002");
c.runTool("-web", "-webPort", "9002");
c.shutdown();
} finally {
if (old != null) {
......@@ -179,6 +180,7 @@ public class TestTools extends TestBase {
} else {
System.clearProperty(SysProperties.H2_BROWSER);
}
c.shutdown();
}
}
......@@ -486,6 +488,7 @@ public class TestTools extends TestBase {
}
}
};
try {
task.execute();
Thread.sleep(100);
try {
......@@ -494,9 +497,11 @@ public class TestTools extends TestBase {
} catch (SQLException e) {
assertEquals(ErrorCode.CONNECTION_BROKEN_1, e.getErrorCode());
}
} finally {
serverSocket.close();
task.getException();
}
}
private void testDeleteFiles() throws SQLException {
deleteDb("testDeleteFiles");
......@@ -527,6 +532,7 @@ public class TestTools extends TestBase {
String result;
Connection conn;
try {
result = runServer(0, new String[]{"-?"});
assertTrue(result.contains("Starts the H2 Console"));
assertTrue(result.indexOf("Unknown option") < 0);
......@@ -545,12 +551,16 @@ public class TestTools extends TestBase {
result = runServer(0, new String[]{"-tcpShutdown",
"tcp://localhost:9001", "-tcpPassword", "abc", "-tcpShutdownForce"});
assertTrue(result.contains("Shutting down"));
} finally {
shutdownServers();
}
}
private void testSSL() throws SQLException {
String result;
Connection conn;
try {
result = runServer(0, new String[]{"-tcp",
"-tcpAllowOthers", "-tcpPort", "9001", "-tcpPassword", "abcdef", "-tcpSSL"});
assertTrue(result.contains("ssl://"));
......@@ -589,11 +599,17 @@ public class TestTools extends TestBase {
stop.shutdown();
assertThrows(ErrorCode.CONNECTION_BROKEN_1, this).
getConnection("jdbc:h2:tcp://localhost:9006/mem:", "sa", "sa");
} finally {
shutdownServers();
}
}
private String runServer(int exitCode, String... args) {
ByteArrayOutputStream buff = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(buff);
if (server != null) {
remainingServers.add(server);
}
server = new Server();
server.setOut(ps);
int result = 0;
......@@ -609,6 +625,18 @@ public class TestTools extends TestBase {
return s;
}
private void shutdownServers() {
for (Server remainingServer : remainingServers) {
if (remainingServer != null) {
remainingServer.shutdown();
}
}
remainingServers.clear();
if (server != null) {
server.shutdown();
}
}
private void testConvertTraceFile() throws Exception {
deleteDb("toolsConvertTraceFile");
org.h2.Driver.load();
......@@ -1018,11 +1046,13 @@ public class TestTools extends TestBase {
private void testServer() throws SQLException {
Connection conn;
try {
deleteDb("test");
Server tcpServer = Server.createTcpServer(
"-baseDir", getBaseDir(),
"-tcpPort", "9192",
"-tcpAllowOthers").start();
remainingServers.add(tcpServer);
conn = getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", "");
conn.close();
// must not be able to use a different base dir
......@@ -1037,11 +1067,12 @@ public class TestTools extends TestBase {
getConnection("jdbc:h2:tcp://localhost:9192/../test2/test", "sa", "");
}};
tcpServer.stop();
Server.createTcpServer(
Server tcpServerWithPassword = Server.createTcpServer(
"-ifExists",
"-tcpPassword", "abc",
"-baseDir", getBaseDir(),
"-tcpPort", "9192").start();
remainingServers.add(tcpServerWithPassword);
// must not be able to create new db
new AssertThrows(ErrorCode.DATABASE_NOT_FOUND_1) {
@Override
......@@ -1083,6 +1114,9 @@ public class TestTools extends TestBase {
server.stop();
deleteDb("testSplit");
} finally {
shutdownServers();
}
}
/**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论