use of org.h2.util.Task in project h2database by h2database.
the class TestCompress method testMultiThreaded.
private void testMultiThreaded() throws Exception {
Task[] tasks = new Task[3];
for (int i = 0; i < tasks.length; i++) {
Task t = new Task() {
@Override
public void call() {
CompressTool tool = CompressTool.getInstance();
byte[] b = new byte[1024];
Random r = new Random();
while (!stop) {
r.nextBytes(b);
byte[] test = tool.expand(tool.compress(b, "LZF"));
assertEquals(b, test);
}
}
};
tasks[i] = t;
t.execute();
}
Thread.sleep(1000);
for (Task t : tasks) {
t.get();
}
}
use of org.h2.util.Task in project h2database by h2database.
the class TestFileLockSerialized method testTwoWriters.
private void testTwoWriters() throws Exception {
deleteDb("fileLockSerialized");
String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized";
final String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE";
Connection conn = getConnection(writeUrl, "sa", "sa");
conn.createStatement().execute("create table test(id identity) as " + "select x from system_range(1, 100)");
conn.close();
Task task = new Task() {
@Override
public void call() throws Exception {
while (!stop) {
Thread.sleep(10);
Connection c = getConnection(writeUrl, "sa", "sa");
c.createStatement().execute("select * from test");
c.close();
}
}
}.execute();
Thread.sleep(20);
for (int i = 0; i < 2; i++) {
conn = getConnection(writeUrl, "sa", "sa");
Statement stat = conn.createStatement();
stat.execute("drop table test");
stat.execute("create table test(id identity) as " + "select x from system_range(1, 100)");
conn.createStatement().execute("select * from test");
conn.close();
}
Thread.sleep(100);
conn = getConnection(writeUrl, "sa", "sa");
conn.createStatement().execute("select * from test");
conn.close();
task.get();
}
use of org.h2.util.Task in project h2database by h2database.
the class TestWeb method testStartWebServerWithConnection.
private void testStartWebServerWithConnection() throws Exception {
String old = System.getProperty(SysProperties.H2_BROWSER);
try {
System.setProperty(SysProperties.H2_BROWSER, "call:" + TestWeb.class.getName() + ".openBrowser");
Server.openBrowser("testUrl");
assertEquals("testUrl", lastUrl);
String oldUrl = lastUrl;
final Connection conn = getConnection(getTestName());
Task t = new Task() {
@Override
public void call() throws Exception {
Server.startWebServer(conn, true);
}
};
t.execute();
for (int i = 0; lastUrl == oldUrl; i++) {
if (i > 100) {
throw new Exception("Browser not started");
}
Thread.sleep(100);
}
String url = lastUrl;
WebClient client = new WebClient();
client.readSessionId(url);
url = client.getBaseUrl(url);
try {
client.get(url, "logout.do");
} catch (ConnectException e) {
// the server stops on logout
}
t.get();
conn.close();
} finally {
if (old != null) {
System.setProperty(SysProperties.H2_BROWSER, old);
} else {
System.clearProperty(SysProperties.H2_BROWSER);
}
}
}
use of org.h2.util.Task in project h2database by h2database.
the class TestBenchmark method testConcurrency.
private void testConcurrency(String fileName, int concurrency, final int count) throws Exception {
Thread.sleep(1000);
final MVStore store = new MVStore.Builder().cacheSize(256).cacheConcurrency(concurrency).fileName(fileName).open();
int threadCount = 128;
final CountDownLatch wait = new CountDownLatch(1);
final AtomicInteger counter = new AtomicInteger();
final AtomicBoolean stopped = new AtomicBoolean();
Task[] tasks = new Task[threadCount];
// Profiler prof = new Profiler().startCollecting();
for (int i = 0; i < threadCount; i++) {
final int x = i;
Task t = new Task() {
@Override
public void call() throws Exception {
MVMap<Integer, byte[]> map = store.openMap("test");
Random random = new Random(x);
wait.await();
while (!stopped.get()) {
int key = random.nextInt(count);
byte[] data = map.get(key);
if (data.length > 1) {
counter.incrementAndGet();
}
}
}
};
t.execute("t" + i);
tasks[i] = t;
}
wait.countDown();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stopped.set(true);
for (Task t : tasks) {
t.get();
}
// System.out.println(prof.getTop(5));
String msg = "concurrency " + concurrency + " threads " + threadCount + " requests: " + counter;
System.out.println(msg);
trace(msg);
store.close();
}
use of org.h2.util.Task in project h2database by h2database.
the class TestConcurrent method testConcurrentDataType.
private void testConcurrentDataType() throws InterruptedException {
final ObjectDataType type = new ObjectDataType();
final Object[] data = new Object[] { null, -1, 1, 10, "Hello", new Object[] { new byte[] { (byte) -1, (byte) 1 }, null }, new Object[] { new byte[] { (byte) 1, (byte) -1 }, 10 }, new Object[] { new byte[] { (byte) -1, (byte) 1 }, 20L }, new Object[] { new byte[] { (byte) 1, (byte) -1 }, 5 } };
Arrays.sort(data, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
return type.compare(o1, o2);
}
});
Task[] tasks = new Task[2];
for (int i = 0; i < tasks.length; i++) {
tasks[i] = new Task() {
@Override
public void call() throws Exception {
Random r = new Random();
WriteBuffer buff = new WriteBuffer();
while (!stop) {
int a = r.nextInt(data.length);
int b = r.nextInt(data.length);
int comp;
if (r.nextBoolean()) {
comp = type.compare(a, b);
} else {
comp = -type.compare(b, a);
}
buff.clear();
type.write(buff, a);
buff.clear();
type.write(buff, b);
if (a == b) {
assertEquals(0, comp);
} else {
assertEquals(a > b ? 1 : -1, comp);
}
}
}
};
tasks[i].execute();
}
try {
Thread.sleep(100);
} finally {
for (Task t : tasks) {
t.get();
}
}
}
Aggregations