提交 16f2bc37 authored 作者: noelgrandin's avatar noelgrandin

improve testFileLockSerialized - fails on slow machines.

Replace Thread#sleep() with CountDownLatch
Original patch by Davide Cavestro, with some modifications by myself
上级 50fe9419
...@@ -14,6 +14,8 @@ import java.sql.ResultSet; ...@@ -14,6 +14,8 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.List; import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.jdbc.JdbcConnection; import org.h2.jdbc.JdbcConnection;
import org.h2.store.fs.FileUtils; import org.h2.store.fs.FileUtils;
...@@ -541,18 +543,20 @@ public class TestFileLockSerialized extends TestBase { ...@@ -541,18 +543,20 @@ public class TestFileLockSerialized extends TestBase {
deleteDb("fileLockSerialized"); deleteDb("fileLockSerialized");
} }
private void testBigDatabase(boolean withCache) { private void testBigDatabase(boolean withCache) throws InterruptedException {
boolean longRun = false; boolean longRun = false;
final int howMuchRows = longRun ? 2000000 : 500000; final int howMuchRows = longRun ? 2000000 : 500000;
deleteDb("fileLockSerialized"); deleteDb("fileLockSerialized");
int cacheSizeKb = withCache ? 5000 : 0; int cacheSizeKb = withCache ? 5000 : 0;
final CountDownLatch importFinishedLatch = new CountDownLatch(1);
final CountDownLatch select1FinishedLatch = new CountDownLatch(1);
final String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized" + final String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized" +
";FILE_LOCK=SERIALIZED" + ";FILE_LOCK=SERIALIZED" +
";OPEN_NEW=TRUE" + ";OPEN_NEW=TRUE" +
";CACHE_SIZE=" + cacheSizeKb; ";CACHE_SIZE=" + cacheSizeKb;
final boolean[] importFinished = { false }; final Task importUpdateTask = new Task() {
final Task importUpdate = new Task() {
@Override @Override
public void call() throws Exception { public void call() throws Exception {
Connection conn = DriverManager.getConnection(url); Connection conn = DriverManager.getConnection(url);
...@@ -561,33 +565,32 @@ public class TestFileLockSerialized extends TestBase { ...@@ -561,33 +565,32 @@ public class TestFileLockSerialized extends TestBase {
for (int i = 0; i < howMuchRows; i++) { for (int i = 0; i < howMuchRows; i++) {
stat.execute("insert into test values(" + i + ", " + i + ")"); stat.execute("insert into test values(" + i + ", " + i + ")");
} }
importFinished[0] = true; importFinishedLatch.countDown();
Thread.sleep(5000);
select1FinishedLatch.await();
stat.execute("update test set id2=999 where id=500"); stat.execute("update test set id2=999 where id=500");
conn.close(); conn.close();
importFinished[0] = true;
} }
}; };
importUpdate.execute(); importUpdateTask.execute();
Task select = new Task() { Task selectTask = new Task() {
@Override @Override
public void call() throws Exception { public void call() throws Exception {
Connection conn = DriverManager.getConnection(url); Connection conn = DriverManager.getConnection(url);
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
while (!importFinished[0]) { importFinishedLatch.await();
Thread.sleep(100);
}
Thread.sleep(1000);
ResultSet rs = stat.executeQuery("select id2 from test where id=500"); ResultSet rs = stat.executeQuery("select id2 from test where id=500");
assertTrue(rs.next()); assertTrue(rs.next());
assertEquals(500, rs.getInt(1)); assertEquals(500, rs.getInt(1));
rs.close(); rs.close();
select1FinishedLatch.countDown();
// wait until the task finished // wait until the other task finished
importUpdate.get(); importUpdateTask.get();
Thread.sleep(1000);
// can't use the exact same query, otherwise it would use // can't use the exact same query, otherwise it would use
// the query cache // the query cache
rs = stat.executeQuery("select id2 from test where id=500+0"); rs = stat.executeQuery("select id2 from test where id=500+0");
...@@ -597,9 +600,10 @@ public class TestFileLockSerialized extends TestBase { ...@@ -597,9 +600,10 @@ public class TestFileLockSerialized extends TestBase {
conn.close(); conn.close();
} }
}; };
select.execute(); selectTask.execute();
importUpdate.get();
select.get(); importUpdateTask.get();
selectTask.get();
deleteDb("fileLockSerialized"); deleteDb("fileLockSerialized");
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论