提交 baf1b54b authored 作者: Thomas Mueller's avatar Thomas Mueller

Auto-stop long running tests.

上级 ec704d09
...@@ -50,7 +50,7 @@ import org.h2.util.New; ...@@ -50,7 +50,7 @@ import org.h2.util.New;
* A test that calls random methods with random parameters from JDBC objects. * A test that calls random methods with random parameters from JDBC objects.
* This is sometimes called 'Fuzz Testing'. * This is sometimes called 'Fuzz Testing'.
*/ */
public class TestCrashAPI extends TestBase { public class TestCrashAPI extends TestBase implements Runnable {
private static final boolean RECOVER_ALL = false; private static final boolean RECOVER_ALL = false;
...@@ -66,6 +66,10 @@ public class TestCrashAPI extends TestBase { ...@@ -66,6 +66,10 @@ public class TestCrashAPI extends TestBase {
private ArrayList<String> statements = New.arrayList(); private ArrayList<String> statements = New.arrayList();
private int openCount; private int openCount;
private long callCount; private long callCount;
private volatile long maxWait = 5 * 60;
private volatile boolean stopped;
private volatile boolean running;
private Thread mainThread;
/** /**
* Run just this test. * Run just this test.
...@@ -75,6 +79,25 @@ public class TestCrashAPI extends TestBase { ...@@ -75,6 +79,25 @@ public class TestCrashAPI extends TestBase {
public static void main(String... a) throws Exception { public static void main(String... a) throws Exception {
TestBase.createCaller().init().test(); TestBase.createCaller().init().test();
} }
@SuppressWarnings("deprecation")
public void run() {
while (--maxWait > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// ignore
}
}
if (maxWait == 0) {
println("stopping...");
stopped = true;
objects.clear();
if (running) {
mainThread.stop(new SQLException("stop"));
}
}
}
private void recoverAll() throws SQLException { private void recoverAll() throws SQLException {
org.h2.Driver.load(); org.h2.Driver.load();
...@@ -111,7 +134,7 @@ public class TestCrashAPI extends TestBase { ...@@ -111,7 +134,7 @@ public class TestCrashAPI extends TestBase {
} }
} }
public void test() throws SQLException { public void test() throws Exception {
if (RECOVER_ALL) { if (RECOVER_ALL) {
recoverAll(); recoverAll();
return; return;
...@@ -120,10 +143,21 @@ public class TestCrashAPI extends TestBase { ...@@ -120,10 +143,21 @@ public class TestCrashAPI extends TestBase {
return; return;
} }
int len = getSize(2, 6); int len = getSize(2, 6);
for (int i = 0; i < len; i++) { Thread t = new Thread(this);
int seed = MathUtils.randomInt(Integer.MAX_VALUE); try {
testCase(seed); mainThread = Thread.currentThread();
deleteDb(); t.start();
running = true;
for (int i = 0; i < len && !stopped; i++) {
int seed = MathUtils.randomInt(Integer.MAX_VALUE);
testCase(seed);
deleteDb();
}
} finally {
running = false;
deleteDb();
maxWait = -1;
t.join();
} }
} }
...@@ -196,7 +230,7 @@ public class TestCrashAPI extends TestBase { ...@@ -196,7 +230,7 @@ public class TestCrashAPI extends TestBase {
} }
} }
stat.execute("SCRIPT NOPASSWORDS NOSETTINGS"); stat.execute("SCRIPT NOPASSWORDS NOSETTINGS");
for (int i = first; i < end && i < statements.size(); i++) { for (int i = first; i < end && i < statements.size() && !stopped; i++) {
try { try {
stat.execute("SELECT * FROM TEST WHERE ID=1"); stat.execute("SELECT * FROM TEST WHERE ID=1");
} catch (Throwable t) { } catch (Throwable t) {
...@@ -230,7 +264,6 @@ public class TestCrashAPI extends TestBase { ...@@ -230,7 +264,6 @@ public class TestCrashAPI extends TestBase {
} }
private void testOne(int seed) throws SQLException { private void testOne(int seed) throws SQLException {
long start = System.currentTimeMillis();
printTime("seed: " + seed); printTime("seed: " + seed);
callCount = 0; callCount = 0;
openCount = 0; openCount = 0;
...@@ -238,12 +271,7 @@ public class TestCrashAPI extends TestBase { ...@@ -238,12 +271,7 @@ public class TestCrashAPI extends TestBase {
random.setSeed(seed); random.setSeed(seed);
Connection c1 = getConnection(seed, true); Connection c1 = getConnection(seed, true);
Connection conn = null; Connection conn = null;
for (int i = 0; i < 2000; i++) { for (int i = 0; i < 2000 && !stopped; i++) {
long time = System.currentTimeMillis() - start;
if (time > 30000) {
// at most 30 seconds per test
break;
}
// if(i % 10 == 0) { // if(i % 10 == 0) {
// for(int j=0; j<objects.size(); j++) { // for(int j=0; j<objects.size(); j++) {
// System.out.print(objects.get(j)); // System.out.print(objects.get(j));
...@@ -253,11 +281,11 @@ public class TestCrashAPI extends TestBase { ...@@ -253,11 +281,11 @@ public class TestCrashAPI extends TestBase {
// Thread.sleep(1); // Thread.sleep(1);
// } // }
if (objects.size() == 0) { if (objects.size() == 0 && !stopped) {
try { try {
conn = getConnection(seed, false); conn = getConnection(seed, false);
} catch (SQLException e) { } catch (SQLException e) {
if (e.getSQLState().equals("08004")) { if ("08004".equals(e.getSQLState())) {
// Wrong user/password [08004] // Wrong user/password [08004]
try { try {
c1.createStatement().execute("SET PASSWORD ''"); c1.createStatement().execute("SET PASSWORD ''");
...@@ -270,7 +298,7 @@ public class TestCrashAPI extends TestBase { ...@@ -270,7 +298,7 @@ public class TestCrashAPI extends TestBase {
} catch (Throwable t) { } catch (Throwable t) {
printIfBad(seed, -i, -1, t); printIfBad(seed, -i, -1, t);
} }
} else if (e.getSQLState().equals("90098")) { } else if ("90098".equals(e.getSQLState())) {
// The database has been closed // The database has been closed
break; break;
} else { } else {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论