提交 86af1cc6 authored 作者: Thomas Mueller's avatar Thomas Mueller

MVStore: additional tests

上级 5631940a
......@@ -114,11 +114,13 @@ import org.h2.test.store.TestCacheLongKeyLIRS;
import org.h2.test.store.TestConcurrent;
import org.h2.test.store.TestDataUtils;
import org.h2.test.store.TestFreeSpace;
import org.h2.test.store.TestKillProcessWhileWriting;
import org.h2.test.store.TestMVRTree;
import org.h2.test.store.TestMVStore;
import org.h2.test.store.TestMVStoreBenchmark;
import org.h2.test.store.TestMVTableEngine;
import org.h2.test.store.TestObjectDataType;
import org.h2.test.store.TestRandomMapOps;
import org.h2.test.store.TestSpinLock;
import org.h2.test.store.TestStreamStore;
import org.h2.test.store.TestTransactionStore;
......@@ -734,11 +736,13 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
new TestConcurrent().runTest(this);
new TestDataUtils().runTest(this);
new TestFreeSpace().runTest(this);
new TestKillProcessWhileWriting().runTest(this);
new TestMVRTree().runTest(this);
new TestMVStore().runTest(this);
new TestMVStoreBenchmark().runTest(this);
new TestMVTableEngine().runTest(this);
new TestObjectDataType().runTest(this);
new TestRandomMapOps().runTest(this);
new TestSpinLock().runTest(this);
new TestStreamStore().runTest(this);
new TestTransactionStore().runTest(this);
......
/*
* Copyright 2004-2013 H2 Group. Multiple-Licensed under the H2 License, Version
* 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). Initial Developer: H2 Group
*/
package org.h2.test.store;
import java.util.Random;
import org.h2.mvstore.MVMap;
import org.h2.mvstore.MVStore;
import org.h2.store.fs.FilePathCrypt;
import org.h2.store.fs.FileUtils;
import org.h2.test.TestBase;
import org.h2.test.utils.FilePathUnstable;
/**
* Tests the MVStore.
*/
public class TestKillProcessWhileWriting extends TestBase {
private String fileName;
private int seed;
private FilePathUnstable fs;
/**
* Run just this test.
*
* @param a ignored
*/
public static void main(String... a) throws Exception {
TestBase.createCaller().init().test();
}
@Override
public void test() throws Exception {
fs = FilePathUnstable.register();
test("unstable:memFS:killProcess.h3");
int todo;
// need to test with a file system splits writes into blocks of 4 KB
FilePathCrypt.register();
test("unstable:crypt:0007:memFS:killProcess.h3");
}
public void test(String fileName) throws Exception {
for (seed = 0; seed < 10; seed++) {
this.fileName = fileName;
FileUtils.delete(fileName);
test(Integer.MAX_VALUE);
int max = Integer.MAX_VALUE - fs.getDiskFullCount() + 10;
assertTrue("" + (max - 10), max > 0);
for (int i = 0; i < max; i++) {
test(i);
}
}
}
private void test(int x) throws Exception {
FileUtils.delete(fileName);
fs.setDiskFullCount(x);
try {
write();
verify();
} catch (Exception e) {
if (x == Integer.MAX_VALUE) {
throw e;
}
fs.setDiskFullCount(0);
verify();
}
}
private int write() {
MVStore s;
MVMap<Integer, byte[]> m;
s = new MVStore.Builder().
fileName(fileName).
pageSplitSize(50).
writeDelay(0).
open();
s.setWriteDelay(0);
m = s.openMap("data");
Random r = new Random(seed);
int op = 0;
try {
for (; op < 50; op++) {
int k = r.nextInt(100);
byte[] v = new byte[r.nextInt(100) * 100];
int type = r.nextInt(10);
switch (type) {
case 0:
case 1:
case 2:
case 3:
m.put(k, v);
break;
case 4:
case 5:
m.remove(k);
break;
case 6:
s.store();
break;
case 7:
s.compact(80);
break;
case 8:
m.clear();
break;
case 9:
s.close();
s = new MVStore.Builder().
fileName(fileName).
pageSplitSize(50).
writeDelay(0).open();
m = s.openMap("data");
break;
}
}
s.store();
s.close();
return 0;
} catch (Exception e) {
s.closeImmediately();
return op;
}
}
private void verify() {
MVStore s;
MVMap<Integer, byte[]> m;
FileUtils.delete(fileName);
s = new MVStore.Builder().
fileName(fileName).open();
m = s.openMap("data");
for (int i = 0; i < 100; i++) {
byte[] x = m.get(i);
if (x == null) {
break;
}
assertEquals(i * 100, x.length);
}
s.close();
}
}
/*
* Copyright 2004-2013 H2 Group. Multiple-Licensed under the H2 License, Version
* 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). Initial Developer: H2 Group
*/
package org.h2.test.store;
import java.util.Random;
import java.util.TreeMap;
import org.h2.mvstore.MVMap;
import org.h2.mvstore.MVStore;
import org.h2.store.fs.FileUtils;
import org.h2.test.TestBase;
/**
* Tests the MVStore.
*/
public class TestRandomMapOps extends TestBase {
private String fileName;
private int seed;
private int op;
/**
* Run just this test.
*
* @param a ignored
*/
public static void main(String... a) throws Exception {
TestBase.createCaller().init().test();
}
@Override
public void test() throws Exception {
test("memFS:randomOps.h3");
int todoTestConcurrentMap;
int todoTestMVRTreeMap;
}
public void test(String fileName) {
this.fileName = fileName;
int best = Integer.MAX_VALUE;
int bestSeed = 0;
Throwable failException = null;
for (seed = 0; seed < 1000; seed++) {
FileUtils.delete(fileName);
Throwable ex = null;
try {
testCase();
continue;
} catch (Exception e) {
ex = e;
} catch (AssertionError e) {
ex = e;
}
if (op < best) {
trace(seed);
bestSeed = seed;
best = op;
failException = ex;
}
}
if (failException != null) {
throw (AssertionError) new AssertionError("seed = " + bestSeed
+ " op = " + best).initCause(failException);
}
}
private void testCase() throws Exception {
FileUtils.delete(fileName);
MVStore s;
MVMap<Integer, byte[]> m;
s = new MVStore.Builder().fileName(fileName).
pageSplitSize(50).writeDelay(0).open();
m = s.openMap("data");
Random r = new Random(seed);
op = 0;
int size = getSize(10, 100);
TreeMap<Integer, byte[]> map = new TreeMap<Integer, byte[]>();
for (; op < size; op++) {
int k = r.nextInt(100);
byte[] v = new byte[r.nextInt(10) * 10];
int type = r.nextInt(11);
switch (type) {
case 0:
case 1:
case 2:
case 3:
log(op, k, v, "put");
m.put(k, v);
map.put(k, v);
break;
case 4:
case 5:
log(op, k, v, "remove");
m.remove(k);
map.remove(k);
break;
case 6:
log(op, k, v, "store");
s.store();
break;
case 7:
log(op, k, v, "compact");
s.compact(80);
break;
case 8:
log(op, k, v, "clear");
m.clear();
map.clear();
break;
case 9:
log(op, k, v, "commit");
s.commit();
break;
case 10:
log(op, k, v, "reopen");
s.commit();
s.close();
s = new MVStore.Builder().fileName(fileName).
pageSplitSize(50).writeDelay(0).open();
m = s.openMap("data");
break;
}
assertEqualsMapValues(map.get(k), m.get(k));
assertEquals(map.ceilingKey(k), m.ceilingKey(k));
assertEquals(map.floorKey(k), m.floorKey(k));
assertEquals(map.higherKey(k), m.higherKey(k));
assertEquals(map.lowerKey(k), m.lowerKey(k));
assertEquals(map.isEmpty(), m.isEmpty());
if (map.size() != m.size()) {
assertEquals(map.size(), m.size());
}
}
s.store();
s.close();
}
private void assertEqualsMapValues(byte[] x, byte[] y) {
if (x == null || y == null) {
if (x != y) {
assertTrue(x == y);
}
} else {
assertEquals(x.length, y.length);
}
}
private static void log(int op, int k, byte[] v, String msg) {
// System.out.println(op + ": " + msg + " key: " + k + " value: " + v);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论