use of org.h2.util.Task in project h2database by h2database.
the class TestConcurrent method testConcurrentStoreAndClose.
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
public void call() throws Exception {
while (!stop) {
s.setStoreVersion(counter.incrementAndGet());
s.commit();
}
}
};
task.execute();
while (counter.get() < 5) {
Thread.sleep(1);
}
try {
s.close();
// immediately)
for (int x = counter.get(), y = x; x <= y + 2; x++) {
Thread.sleep(1);
}
Exception e = task.getException();
assertEquals(DataUtils.ERROR_CLOSED, DataUtils.getErrorCode(e.getMessage()));
} catch (IllegalStateException e) {
// sometimes storing works, in which case
// closing must fail
assertEquals(DataUtils.ERROR_WRITING_FAILED, DataUtils.getErrorCode(e.getMessage()));
task.get();
}
} finally {
s.close();
}
}
}
use of org.h2.util.Task in project h2database by h2database.
the class TestConcurrent method testConcurrentWrite.
private static void testConcurrentWrite(final AtomicInteger detected, final AtomicInteger notDetected) throws InterruptedException {
final MVStore s = openStore(null);
final MVMap<Integer, Integer> m = s.openMap("data");
final int size = 20;
final Random rand = new Random(1);
Task task = new Task() {
@Override
public void call() throws Exception {
while (!stop) {
try {
if (rand.nextBoolean()) {
m.put(rand.nextInt(size), 1);
} else {
m.remove(rand.nextInt(size));
}
m.get(rand.nextInt(size));
} catch (ConcurrentModificationException e) {
detected.incrementAndGet();
} catch (NegativeArraySizeException e) {
notDetected.incrementAndGet();
} catch (ArrayIndexOutOfBoundsException e) {
notDetected.incrementAndGet();
} catch (IllegalArgumentException e) {
notDetected.incrementAndGet();
} catch (NullPointerException e) {
notDetected.incrementAndGet();
}
}
}
};
task.execute();
try {
Thread.sleep(1);
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 10; i++) {
try {
if (rand.nextBoolean()) {
m.put(rand.nextInt(size), 2);
} else {
m.remove(rand.nextInt(size));
}
m.get(rand.nextInt(size));
} catch (ConcurrentModificationException e) {
detected.incrementAndGet();
} catch (NegativeArraySizeException e) {
notDetected.incrementAndGet();
} catch (ArrayIndexOutOfBoundsException e) {
notDetected.incrementAndGet();
} catch (IllegalArgumentException e) {
notDetected.incrementAndGet();
} catch (NullPointerException e) {
notDetected.incrementAndGet();
}
}
s.commit();
Thread.sleep(1);
}
} finally {
task.get();
}
s.close();
}
use of org.h2.util.Task in project h2database by h2database.
the class TestMvccMultiThreaded method testConcurrentMerge.
private void testConcurrentMerge() throws Exception {
deleteDb(getTestName());
int len = 3;
final Connection[] connList = new Connection[len];
for (int i = 0; i < len; i++) {
Connection conn = getConnection(getTestName() + ";MVCC=TRUE;LOCK_TIMEOUT=500");
connList[i] = conn;
}
Connection conn = connList[0];
conn.createStatement().execute("create table test(id int primary key, name varchar)");
Task[] tasks = new Task[len];
final boolean[] stop = { false };
for (int i = 0; i < len; i++) {
final Connection c = connList[i];
c.setAutoCommit(false);
tasks[i] = new Task() {
@Override
public void call() throws Exception {
while (!stop) {
c.createStatement().execute("merge into test values(1, 'x')");
c.commit();
Thread.sleep(1);
}
}
};
tasks[i].execute();
}
Thread.sleep(1000);
stop[0] = true;
for (int i = 0; i < len; i++) {
tasks[i].get();
}
for (int i = 0; i < len; i++) {
connList[i].close();
}
deleteDb(getTestName());
}
use of org.h2.util.Task in project h2database by h2database.
the class TestMvccMultiThreaded method testConcurrentUpdate.
private void testConcurrentUpdate() throws Exception {
deleteDb(getTestName());
int len = 2;
final Connection[] connList = new Connection[len];
for (int i = 0; i < len; i++) {
connList[i] = getConnection(getTestName() + ";MVCC=TRUE");
}
Connection conn = connList[0];
conn.createStatement().execute("create table test(id int primary key, value int)");
conn.createStatement().execute("insert into test values(0, 0)");
final int count = 1000;
Task[] tasks = new Task[len];
final CountDownLatch latch = new CountDownLatch(len);
for (int i = 0; i < len; i++) {
final int x = i;
tasks[i] = new Task() {
@Override
public void call() throws Exception {
for (int a = 0; a < count; a++) {
connList[x].createStatement().execute("update test set value=value+1");
latch.countDown();
latch.await();
}
}
};
tasks[i].execute();
}
for (int i = 0; i < len; i++) {
tasks[i].get();
}
ResultSet rs = conn.createStatement().executeQuery("select value from test");
rs.next();
assertEquals(count * len, rs.getInt(1));
for (int i = 0; i < len; i++) {
connList[i].close();
}
}
use of org.h2.util.Task in project h2database by h2database.
the class TestRowLocks method testCases.
private void testCases() throws Exception {
deleteDb(getTestName());
c1 = getConnection(getTestName() + ";MVCC=TRUE");
s1 = c1.createStatement();
s1.execute("SET LOCK_TIMEOUT 10000");
s1.execute("CREATE TABLE TEST AS " + "SELECT X ID, 'Hello' NAME FROM SYSTEM_RANGE(1, 3)");
c1.commit();
c1.setAutoCommit(false);
s1.execute("UPDATE TEST SET NAME='Hallo' WHERE ID=1");
c2 = getConnection(getTestName());
c2.setAutoCommit(false);
s2 = c2.createStatement();
assertEquals("Hallo", getSingleValue(s1, "SELECT NAME FROM TEST WHERE ID=1"));
assertEquals("Hello", getSingleValue(s2, "SELECT NAME FROM TEST WHERE ID=1"));
s2.execute("UPDATE TEST SET NAME='Hallo' WHERE ID=2");
assertThrows(ErrorCode.LOCK_TIMEOUT_1, s2).executeUpdate("UPDATE TEST SET NAME='Hi' WHERE ID=1");
c1.commit();
c2.commit();
assertEquals("Hallo", getSingleValue(s1, "SELECT NAME FROM TEST WHERE ID=1"));
assertEquals("Hallo", getSingleValue(s2, "SELECT NAME FROM TEST WHERE ID=1"));
s2.execute("UPDATE TEST SET NAME='H1' WHERE ID=1");
Task task = new Task() {
@Override
public void call() throws SQLException {
s1.execute("UPDATE TEST SET NAME='H2' WHERE ID=1");
}
};
task.execute();
Thread.sleep(100);
c2.commit();
task.get();
c1.commit();
assertEquals("H2", getSingleValue(s1, "SELECT NAME FROM TEST WHERE ID=1"));
assertEquals("H2", getSingleValue(s2, "SELECT NAME FROM TEST WHERE ID=1"));
c1.close();
c2.close();
}
Aggregations