Search in sources :

Example 6 with Task

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();
    }
}
Also used : Task(org.h2.util.Task) Random(java.util.Random) CompressTool(org.h2.tools.CompressTool)

Example 7 with Task

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();
}
Also used : Task(org.h2.util.Task) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) SQLException(java.sql.SQLException)

Example 8 with Task

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);
        }
    }
}
Also used : Task(org.h2.util.Task) Connection(java.sql.Connection) ServletException(javax.servlet.ServletException) SQLException(java.sql.SQLException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConnectException(java.net.ConnectException)

Example 9 with Task

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();
}
Also used : Task(org.h2.util.Task) CountDownLatch(java.util.concurrent.CountDownLatch) MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 10 with Task

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();
        }
    }
}
Also used : Task(org.h2.util.Task) WriteBuffer(org.h2.mvstore.WriteBuffer) Random(java.util.Random) ObjectDataType(org.h2.mvstore.type.ObjectDataType) ConcurrentModificationException(java.util.ConcurrentModificationException)

Aggregations

Task (org.h2.util.Task)71 Connection (java.sql.Connection)33 Statement (java.sql.Statement)27 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 PreparedStatement (java.sql.PreparedStatement)22 SQLException (java.sql.SQLException)22 MVStore (org.h2.mvstore.MVStore)20 Random (java.util.Random)18 ResultSet (java.sql.ResultSet)14 JdbcConnection (org.h2.jdbc.JdbcConnection)7 IOException (java.io.IOException)5 ServerSocket (java.net.ServerSocket)5 Socket (java.net.Socket)5 ConcurrentModificationException (java.util.ConcurrentModificationException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 SSLServerSocket (javax.net.ssl.SSLServerSocket)4 SSLSocket (javax.net.ssl.SSLSocket)4 OutputStream (java.io.OutputStream)3 PipedInputStream (java.io.PipedInputStream)3 PipedOutputStream (java.io.PipedOutputStream)3