use of org.h2.util.Task in project h2database by h2database.
the class TestOptimizations method testQueryCacheConcurrentUse.
private void testQueryCacheConcurrentUse() throws Exception {
if (config.lazy) {
return;
}
final Connection conn = getConnection("optimizations");
Statement stat = conn.createStatement();
stat.execute("create table test(id int primary key, data clob)");
stat.execute("insert into test values(0, space(10000))");
stat.execute("insert into test values(1, space(10001))");
Task[] tasks = new Task[2];
for (int i = 0; i < tasks.length; i++) {
tasks[i] = new Task() {
@Override
public void call() throws Exception {
PreparedStatement prep = conn.prepareStatement("select * from test where id = ?");
while (!stop) {
int x = (int) (Math.random() * 2);
prep.setInt(1, x);
ResultSet rs = prep.executeQuery();
rs.next();
String data = rs.getString(2);
if (data.length() != 10000 + x) {
throw new Exception(data.length() + " != " + x);
}
rs.close();
}
}
};
tasks[i].execute();
}
Thread.sleep(1000);
for (Task t : tasks) {
t.get();
}
stat.execute("drop table test");
conn.close();
}
use of org.h2.util.Task in project h2database by h2database.
the class TestConnectionPool method testTimeout.
private void testTimeout() throws Exception {
String url = getURL("connectionPool", true), user = getUser();
String password = getPassword();
final JdbcConnectionPool man = JdbcConnectionPool.create(url, user, password);
man.setLoginTimeout(1);
createClassProxy(man.getClass());
assertThrows(IllegalArgumentException.class, man).setMaxConnections(-1);
man.setMaxConnections(2);
// connection 1 (of 2)
Connection conn = man.getConnection();
Task t = new Task() {
@Override
public void call() {
while (!stop) {
// this calls notifyAll
man.setMaxConnections(1);
man.setMaxConnections(2);
}
}
};
t.execute();
long time = System.nanoTime();
Connection conn2 = null;
try {
// connection 2 (of 1 or 2) may fail
conn2 = man.getConnection();
// connection 3 (of 1 or 2) must fail
man.getConnection();
fail();
} catch (SQLException e) {
if (conn2 != null) {
conn2.close();
}
assertContains(e.toString().toLowerCase(), "timeout");
time = System.nanoTime() - time;
assertTrue("timeout after " + TimeUnit.NANOSECONDS.toMillis(time) + " ms", time > TimeUnit.SECONDS.toNanos(1));
} finally {
conn.close();
t.get();
}
man.dispose();
}
use of org.h2.util.Task in project h2database by h2database.
the class TestConcurrent method testConcurrentRead.
private static void testConcurrentRead() throws InterruptedException {
final MVStore s = openStore(null);
final MVMap<Integer, Integer> m = s.openMap("data");
final int size = 3;
int x = (int) s.getCurrentVersion();
for (int i = 0; i < size; i++) {
m.put(i, x);
}
s.commit();
Task task = new Task() {
@Override
public void call() throws Exception {
while (!stop) {
long v = s.getCurrentVersion() - 1;
Map<Integer, Integer> old = m.openVersion(v);
for (int i = 0; i < size; i++) {
Integer x = old.get(i);
if (x == null || (int) v != x) {
Map<Integer, Integer> old2 = m.openVersion(v);
throw new AssertionError(x + "<>" + v + " at " + i + " " + old2);
}
}
}
}
};
task.execute();
try {
Thread.sleep(1);
for (int j = 0; j < 100; j++) {
x = (int) s.getCurrentVersion();
for (int i = 0; i < size; i++) {
m.put(i, x);
}
s.commit();
Thread.sleep(1);
}
} finally {
task.get();
}
s.close();
}
use of org.h2.util.Task in project h2database by h2database.
the class TestConcurrent method testConcurrentStoreAndRemoveMap.
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);
m.put(1, 1);
}
final AtomicInteger counter = new AtomicInteger();
Task task = new Task() {
@Override
public void call() throws Exception {
while (!stop) {
counter.incrementAndGet();
s.commit();
}
}
};
task.execute();
Thread.sleep(1);
for (int i = 0; i < count || counter.get() < count; i++) {
MVMap<Integer, Integer> m = s.openMap("d" + i);
m.put(1, 10);
s.removeMap(m);
if (task.isFinished()) {
break;
}
}
task.get();
} finally {
s.close();
}
}
use of org.h2.util.Task in project h2database by h2database.
the class TestConcurrent method testConcurrentMap.
/**
* Test the concurrent map implementation.
*/
private static 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() {
@Override
public void call() throws Exception {
try {
while (!stop) {
if (rand.nextBoolean()) {
m.put(rand.nextInt(size), 1);
} else {
m.remove(rand.nextInt(size));
}
m.get(rand.nextInt(size));
m.firstKey();
m.lastKey();
m.ceilingKey(5);
m.floorKey(5);
m.higherKey(5);
m.lowerKey(5);
for (Iterator<Integer> it = m.keyIterator(null); it.hasNext(); ) {
it.next();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
task.execute();
Thread.sleep(1);
for (int j = 0; j < 100; j++) {
for (int i = 0; i < 100; i++) {
if (rand.nextBoolean()) {
m.put(rand.nextInt(size), 2);
} else {
m.remove(rand.nextInt(size));
}
m.get(rand.nextInt(size));
}
s.commit();
Thread.sleep(1);
}
task.get();
} finally {
s.close();
}
}
Aggregations