提交 555af2d5 authored 作者: Thomas Mueller's avatar Thomas Mueller

Ensure threads stop running if the test fails

上级 0aae334e
...@@ -6,9 +6,10 @@ ...@@ -6,9 +6,10 @@
package org.h2.test.store; package org.h2.test.store;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream; import java.io.BufferedOutputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
...@@ -178,11 +179,14 @@ public class TestConcurrent extends TestMVStore { ...@@ -178,11 +179,14 @@ public class TestConcurrent extends TestMVStore {
}; };
tasks[i].execute(); tasks[i].execute();
} }
try {
Thread.sleep(100); Thread.sleep(100);
} finally {
for (Task t : tasks) { for (Task t : tasks) {
t.get(); t.get();
} }
} }
}
private void testConcurrentAutoCommitAndChange() throws InterruptedException { private void testConcurrentAutoCommitAndChange() throws InterruptedException {
String fileName = "memFS:" + getTestName(); String fileName = "memFS:" + getTestName();
...@@ -256,11 +260,14 @@ public class TestConcurrent extends TestMVStore { ...@@ -256,11 +260,14 @@ public class TestConcurrent extends TestMVStore {
} }
}; };
task.execute(); task.execute();
try {
Thread.sleep(1); Thread.sleep(1);
for (int i = 0; !task.isFinished() && i < 1000000; i++) { for (int i = 0; !task.isFinished() && i < 1000000; i++) {
assertEquals(i % 100, map.get(i % 100).intValue()); assertEquals(i % 100, map.get(i % 100).intValue());
} }
} finally {
task.get(); task.get();
}
s.close(); s.close();
} }
...@@ -272,6 +279,7 @@ public class TestConcurrent extends TestMVStore { ...@@ -272,6 +279,7 @@ public class TestConcurrent extends TestMVStore {
pageSplitSize(10). pageSplitSize(10).
autoCommitDisabled().open(); autoCommitDisabled().open();
s.setRetentionTime(10000); s.setRetentionTime(10000);
try {
Task task = new Task() { Task task = new Task() {
@Override @Override
public void call() throws Exception { public void call() throws Exception {
...@@ -301,13 +309,16 @@ public class TestConcurrent extends TestMVStore { ...@@ -301,13 +309,16 @@ public class TestConcurrent extends TestMVStore {
} }
task.get(); task.get();
task2.get(); task2.get();
} finally {
s.close(); s.close();
} }
}
private void testConcurrentChangeAndGetVersion() throws InterruptedException { private void testConcurrentChangeAndGetVersion() throws InterruptedException {
for (int test = 0; test < 10; test++) { for (int test = 0; test < 10; test++) {
final MVStore s = new MVStore.Builder(). final MVStore s = new MVStore.Builder().
autoCommitDisabled().open(); autoCommitDisabled().open();
try {
s.setVersionsToKeep(10); s.setVersionsToKeep(10);
final MVMap<Integer, Integer> m = s.openMap("data"); final MVMap<Integer, Integer> m = s.openMap("data");
m.put(1, 1); m.put(1, 1);
...@@ -341,9 +352,11 @@ public class TestConcurrent extends TestMVStore { ...@@ -341,9 +352,11 @@ public class TestConcurrent extends TestMVStore {
} }
task.get(); task.get();
s.commit(); s.commit();
} finally {
s.close(); s.close();
} }
} }
}
private void testConcurrentFree() throws InterruptedException { private void testConcurrentFree() throws InterruptedException {
String fileName = "memFS:" + getTestName(); String fileName = "memFS:" + getTestName();
...@@ -363,6 +376,7 @@ public class TestConcurrent extends TestMVStore { ...@@ -363,6 +376,7 @@ public class TestConcurrent extends TestMVStore {
s1.close(); s1.close();
final MVStore s = new MVStore.Builder(). final MVStore s = new MVStore.Builder().
fileName(fileName).autoCommitDisabled().open(); fileName(fileName).autoCommitDisabled().open();
try {
s.setRetentionTime(0); s.setRetentionTime(0);
final ArrayList<MVMap<Integer, Integer>> list = New.arrayList(); final ArrayList<MVMap<Integer, Integer>> list = New.arrayList();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
...@@ -419,14 +433,17 @@ public class TestConcurrent extends TestMVStore { ...@@ -419,14 +433,17 @@ public class TestConcurrent extends TestMVStore {
} }
} }
assertTrue("" + chunkCount, chunkCount < 3); assertTrue("" + chunkCount, chunkCount < 3);
} finally {
s.close(); s.close();
} }
} }
}
private void testConcurrentStoreAndRemoveMap() throws InterruptedException { private void testConcurrentStoreAndRemoveMap() throws InterruptedException {
String fileName = "memFS:" + getTestName(); String fileName = "memFS:" + getTestName();
FileUtils.delete(fileName); FileUtils.delete(fileName);
final MVStore s = openStore(fileName); final MVStore s = openStore(fileName);
try {
int count = 200; int count = 200;
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
MVMap<Integer, Integer> m = s.openMap("d" + i); MVMap<Integer, Integer> m = s.openMap("d" + i);
...@@ -453,14 +470,17 @@ public class TestConcurrent extends TestMVStore { ...@@ -453,14 +470,17 @@ public class TestConcurrent extends TestMVStore {
} }
} }
task.get(); task.get();
} finally {
s.close(); s.close();
} }
}
private void testConcurrentStoreAndClose() throws InterruptedException { private void testConcurrentStoreAndClose() throws InterruptedException {
String fileName = "memFS:" + getTestName(); String fileName = "memFS:" + getTestName();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
FileUtils.delete(fileName); FileUtils.delete(fileName);
final MVStore s = openStore(fileName); final MVStore s = openStore(fileName);
try {
final AtomicInteger counter = new AtomicInteger(); final AtomicInteger counter = new AtomicInteger();
Task task = new Task() { Task task = new Task() {
@Override @Override
...@@ -493,9 +513,11 @@ public class TestConcurrent extends TestMVStore { ...@@ -493,9 +513,11 @@ public class TestConcurrent extends TestMVStore {
DataUtils.getErrorCode(e.getMessage())); DataUtils.getErrorCode(e.getMessage()));
task.get(); task.get();
} }
} finally {
s.close(); s.close();
} }
} }
}
/** /**
* Test the concurrent map implementation. * Test the concurrent map implementation.
...@@ -503,6 +525,7 @@ public class TestConcurrent extends TestMVStore { ...@@ -503,6 +525,7 @@ public class TestConcurrent extends TestMVStore {
private void testConcurrentMap() throws InterruptedException { private void testConcurrentMap() throws InterruptedException {
final MVStore s = openStore(null); final MVStore s = openStore(null);
final MVMap<Integer, Integer> m = s.openMap("data"); final MVMap<Integer, Integer> m = s.openMap("data");
try {
final int size = 20; final int size = 20;
final Random rand = new Random(1); final Random rand = new Random(1);
Task task = new Task() { Task task = new Task() {
...@@ -547,8 +570,10 @@ public class TestConcurrent extends TestMVStore { ...@@ -547,8 +570,10 @@ public class TestConcurrent extends TestMVStore {
Thread.sleep(1); Thread.sleep(1);
} }
task.get(); task.get();
} finally {
s.close(); s.close();
} }
}
private void testConcurrentOnlineBackup() throws Exception { private void testConcurrentOnlineBackup() throws Exception {
String fileName = getBaseDir() + "/" + getTestName(); String fileName = getBaseDir() + "/" + getTestName();
...@@ -556,7 +581,7 @@ public class TestConcurrent extends TestMVStore { ...@@ -556,7 +581,7 @@ public class TestConcurrent extends TestMVStore {
final MVStore s = openStore(fileName); final MVStore s = openStore(fileName);
final MVMap<Integer, byte[]> map = s.openMap("test"); final MVMap<Integer, byte[]> map = s.openMap("test");
final Random r = new Random(); final Random r = new Random();
Task t = new Task() { Task task = new Task() {
@Override @Override
public void call() throws Exception { public void call() throws Exception {
while (!stop) { while (!stop) {
...@@ -577,16 +602,18 @@ public class TestConcurrent extends TestMVStore { ...@@ -577,16 +602,18 @@ public class TestConcurrent extends TestMVStore {
} }
} }
}; };
t.execute(); task.execute();
try {
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
// System.out.println("test " + i); // System.out.println("test " + i);
s.setReuseSpace(false); s.setReuseSpace(false);
byte[] buff = readFileSlowly(s.getFileStore().getFile(), OutputStream out = new BufferedOutputStream(
s.getFileStore().size()); new FileOutputStream(fileNameRestore));
s.setReuseSpace(true); long len = s.getFileStore().size();
FileOutputStream out = new FileOutputStream(fileNameRestore); copyFileSlowly(s.getFileStore().getFile(),
out.write(buff); len, out);
out.close(); out.close();
s.setReuseSpace(true);
MVStore s2 = openStore(fileNameRestore); MVStore s2 = openStore(fileNameRestore);
MVMap<Integer, byte[]> test = s2.openMap("test"); MVMap<Integer, byte[]> test = s2.openMap("test");
for (Integer k : test.keySet()) { for (Integer k : test.keySet()) {
...@@ -596,25 +623,25 @@ public class TestConcurrent extends TestMVStore { ...@@ -596,25 +623,25 @@ public class TestConcurrent extends TestMVStore {
// let it compact // let it compact
Thread.sleep(10); Thread.sleep(10);
} }
t.get(); } finally {
task.get();
}
s.close(); s.close();
} }
private static byte[] readFileSlowly(FileChannel file, long length) private static void copyFileSlowly(FileChannel file, long length, OutputStream out)
throws Exception { throws Exception {
file.position(0); file.position(0);
InputStream in = new BufferedInputStream(new FileChannelInputStream( InputStream in = new BufferedInputStream(new FileChannelInputStream(
file, false)); file, false));
ByteArrayOutputStream buff = new ByteArrayOutputStream();
for (int j = 0; j < length; j++) { for (int j = 0; j < length; j++) {
int x = in.read(); int x = in.read();
if (x < 0) { if (x < 0) {
break; break;
} }
buff.write(x); out.write(x);
} }
in.close(); in.close();
return buff.toByteArray();
} }
private void testConcurrentIterate() { private void testConcurrentIterate() {
...@@ -623,7 +650,7 @@ public class TestConcurrent extends TestMVStore { ...@@ -623,7 +650,7 @@ public class TestConcurrent extends TestMVStore {
final MVMap<Integer, Integer> map = s.openMap("test"); final MVMap<Integer, Integer> map = s.openMap("test");
final int len = 10; final int len = 10;
final Random r = new Random(); final Random r = new Random();
Task t = new Task() { Task task = new Task() {
@Override @Override
public void call() throws Exception { public void call() throws Exception {
while (!stop) { while (!stop) {
...@@ -636,7 +663,8 @@ public class TestConcurrent extends TestMVStore { ...@@ -636,7 +663,8 @@ public class TestConcurrent extends TestMVStore {
} }
} }
}; };
t.execute(); task.execute();
try {
for (int k = 0; k < 10000; k++) { for (int k = 0; k < 10000; k++) {
Iterator<Integer> it = map.keyIterator(r.nextInt(len)); Iterator<Integer> it = map.keyIterator(r.nextInt(len));
long old = s.getCurrentVersion(); long old = s.getCurrentVersion();
...@@ -648,7 +676,9 @@ public class TestConcurrent extends TestMVStore { ...@@ -648,7 +676,9 @@ public class TestConcurrent extends TestMVStore {
it.next(); it.next();
} }
} }
t.get(); } finally {
task.get();
}
s.close(); s.close();
} }
...@@ -699,6 +729,7 @@ public class TestConcurrent extends TestMVStore { ...@@ -699,6 +729,7 @@ public class TestConcurrent extends TestMVStore {
} }
}; };
task.execute(); task.execute();
try {
Thread.sleep(1); Thread.sleep(1);
for (int j = 0; j < 10; j++) { for (int j = 0; j < 10; j++) {
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
...@@ -724,7 +755,9 @@ public class TestConcurrent extends TestMVStore { ...@@ -724,7 +755,9 @@ public class TestConcurrent extends TestMVStore {
s.commit(); s.commit();
Thread.sleep(1); Thread.sleep(1);
} }
} finally {
task.get(); task.get();
}
s.close(); s.close();
} }
...@@ -754,6 +787,7 @@ public class TestConcurrent extends TestMVStore { ...@@ -754,6 +787,7 @@ public class TestConcurrent extends TestMVStore {
} }
}; };
task.execute(); task.execute();
try {
Thread.sleep(1); Thread.sleep(1);
for (int j = 0; j < 100; j++) { for (int j = 0; j < 100; j++) {
x = (int) s.getCurrentVersion(); x = (int) s.getCurrentVersion();
...@@ -763,7 +797,9 @@ public class TestConcurrent extends TestMVStore { ...@@ -763,7 +797,9 @@ public class TestConcurrent extends TestMVStore {
s.commit(); s.commit();
Thread.sleep(1); Thread.sleep(1);
} }
} finally {
task.get(); task.get();
}
s.close(); s.close();
} }
......
...@@ -597,6 +597,8 @@ public class TestMVTableEngine extends TestBase { ...@@ -597,6 +597,8 @@ public class TestMVTableEngine extends TestBase {
private void testTransactionLogUsuallyNotStored() throws Exception { private void testTransactionLogUsuallyNotStored() throws Exception {
Connection conn; Connection conn;
Statement stat; Statement stat;
// we expect the transaction log is empty in at least some of the cases
for (int test = 0; test < 5; test++) {
deleteDb(getTestName()); deleteDb(getTestName());
String url = getTestName() + ";MV_STORE=TRUE"; String url = getTestName() + ";MV_STORE=TRUE";
url = getURL(url, true); url = getURL(url, true);
...@@ -615,13 +617,19 @@ public class TestMVTableEngine extends TestBase { ...@@ -615,13 +617,19 @@ public class TestMVTableEngine extends TestBase {
stat.execute("shutdown immediately"); stat.execute("shutdown immediately");
JdbcUtils.closeSilently(conn); JdbcUtils.closeSilently(conn);
String file = getBaseDir() + "/" + getTestName() + Constants.SUFFIX_MV_FILE; String file = getBaseDir() + "/" + getTestName() +
Constants.SUFFIX_MV_FILE;
MVStore store = MVStore.open(file); MVStore store = MVStore.open(file);
TransactionStore t = new TransactionStore(store); TransactionStore t = new TransactionStore(store);
t.init(); t.init();
assertEquals(0, t.getOpenTransactions().size()); int openTransactions = t.getOpenTransactions().size();
store.close(); store.close();
if (openTransactions == 0) {
return;
}
}
fail("transaction log was never empty");
} }
private void testShrinkDatabaseFile() throws Exception { private void testShrinkDatabaseFile() throws Exception {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论