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

Ensure threads stop running if the test fails

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