Search in sources :

Example 21 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestTools method testWrongServer.

private void testWrongServer() throws Exception {
    // try to connect when the server is not running
    assertThrows(ErrorCode.CONNECTION_BROKEN_1, this).getConnection("jdbc:h2:tcp://localhost:9001/test");
    final ServerSocket serverSocket = new ServerSocket(9001);
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                Socket socket = serverSocket.accept();
                byte[] data = new byte[1024];
                data[0] = 'x';
                OutputStream out = socket.getOutputStream();
                out.write(data);
                out.close();
                socket.close();
            }
        }
    };
    try {
        task.execute();
        Thread.sleep(100);
        try {
            getConnection("jdbc:h2:tcp://localhost:9001/test");
            fail();
        } catch (SQLException e) {
            assertEquals(ErrorCode.CONNECTION_BROKEN_1, e.getErrorCode());
        }
    } finally {
        serverSocket.close();
        task.getException();
    }
}
Also used : Task(org.h2.util.Task) SQLException(java.sql.SQLException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ServerSocket(java.net.ServerSocket) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 22 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestShell method test.

private void test(final boolean commandLineArgs) throws IOException {
    PipedInputStream testIn = new PipedInputStream();
    PipedOutputStream out = new PipedOutputStream(testIn);
    toolOut = new PrintStream(out, true);
    out = new PipedOutputStream();
    PrintStream testOut = new PrintStream(out, true);
    toolIn = new PipedInputStream(out);
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            try {
                Shell shell = new Shell();
                shell.setIn(toolIn);
                shell.setOut(toolOut);
                shell.setErr(toolOut);
                if (commandLineArgs) {
                    shell.runTool("-url", "jdbc:h2:mem:", "-user", "sa", "-password", "sa");
                } else {
                    shell.runTool();
                }
            } finally {
                toolOut.close();
            }
        }
    };
    task.execute();
    InputStreamReader reader = new InputStreamReader(testIn);
    lineReader = new LineNumberReader(reader);
    read("");
    read("Welcome to H2 Shell");
    read("Exit with");
    if (!commandLineArgs) {
        read("[Enter]");
        testOut.println("jdbc:h2:mem:");
        read("URL");
        testOut.println("");
        read("Driver");
        testOut.println("sa");
        read("User");
        testOut.println("sa");
        read("Password");
    }
    read("Commands are case insensitive");
    read("help or ?");
    read("list");
    read("maxwidth");
    read("autocommit");
    read("history");
    read("quit or exit");
    read("");
    testOut.println("history");
    read("sql> No history");
    testOut.println("1");
    read("sql> Not found");
    testOut.println("select 1 a;");
    read("sql> A");
    read("1");
    read("(1 row,");
    testOut.println("history");
    read("sql> #1: select 1 a");
    read("To re-run a statement, type the number and press and enter");
    testOut.println("1");
    read("sql> select 1 a");
    read("A");
    read("1");
    read("(1 row,");
    testOut.println("select 'x' || space(1000) large, 'y' small;");
    read("sql> LARGE");
    read("x");
    read("(data is partially truncated)");
    read("(1 row,");
    testOut.println("select x, 's' s from system_range(0, 10001);");
    read("sql> X    | S");
    for (int i = 0; i < 10000; i++) {
        read((i + "     ").substring(0, 4) + " | s");
    }
    for (int i = 10000; i <= 10001; i++) {
        read((i + "     ").substring(0, 5) + " | s");
    }
    read("(10002 rows,");
    testOut.println("select error;");
    read("sql> Error:");
    if (read("").startsWith("Column \"ERROR\" not found")) {
        read("");
    }
    testOut.println("create table test(id int primary key, name varchar)\n;");
    read("sql> ...>");
    testOut.println("insert into test values(1, 'Hello');");
    read("sql>");
    testOut.println("select null n, * from test;");
    read("sql> N    | ID | NAME");
    read("null | 1  | Hello");
    read("(1 row,");
    // test history
    for (int i = 0; i < 30; i++) {
        testOut.println("select " + i + " ID from test;");
        read("sql> ID");
        read("" + i);
        read("(1 row,");
    }
    testOut.println("20");
    read("sql> select 10 ID from test");
    read("ID");
    read("10");
    read("(1 row,");
    testOut.println("maxwidth");
    read("sql> Usage: maxwidth <integer value>");
    read("Maximum column width is now 100");
    testOut.println("maxwidth 80");
    read("sql> Maximum column width is now 80");
    testOut.println("autocommit");
    read("sql> Usage: autocommit [true|false]");
    read("Autocommit is now true");
    testOut.println("autocommit false");
    read("sql> Autocommit is now false");
    testOut.println("autocommit true");
    read("sql> Autocommit is now true");
    testOut.println("list");
    read("sql> Result list mode is now on");
    testOut.println("select 1 first, 2 second;");
    read("sql> FIRST : 1");
    read("SECOND: 2");
    read("(1 row, ");
    testOut.println("select x from system_range(1, 3);");
    read("sql> X: 1");
    read("");
    read("X: 2");
    read("");
    read("X: 3");
    read("(3 rows, ");
    testOut.println("select x, 2 as y from system_range(1, 3) where 1 = 0;");
    read("sql> X");
    read("Y");
    read("(0 rows, ");
    testOut.println("list");
    read("sql> Result list mode is now off");
    testOut.println("help");
    read("sql> Commands are case insensitive");
    read("help or ?");
    read("list");
    read("maxwidth");
    read("autocommit");
    read("history");
    read("quit or exit");
    read("");
    testOut.println("exit");
    read("sql>");
    task.get();
}
Also used : PrintStream(java.io.PrintStream) Task(org.h2.util.Task) Shell(org.h2.tools.Shell) InputStreamReader(java.io.InputStreamReader) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) LineNumberReader(java.io.LineNumberReader)

Example 23 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestFileSystem method testConcurrent.

private void testConcurrent(String fsBase) throws Exception {
    String s = FileUtils.createTempFile(fsBase + "/tmp", ".tmp", false, false);
    File file = new File(TestBase.BASE_TEST_DIR + "/tmp");
    file.getParentFile().mkdirs();
    file.delete();
    RandomAccessFile ra = new RandomAccessFile(file, "rw");
    FileUtils.delete(s);
    final FileChannel f = FileUtils.open(s, "rw");
    final int size = getSize(10, 50);
    f.write(ByteBuffer.allocate(size * 64 * 1024));
    Random random = new Random(1);
    System.gc();
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            ByteBuffer byteBuff = ByteBuffer.allocate(16);
            while (!stop) {
                for (int pos = 0; pos < size; pos++) {
                    byteBuff.clear();
                    f.read(byteBuff, pos * 64 * 1024);
                    byteBuff.position(0);
                    int x = byteBuff.getInt();
                    int y = byteBuff.getInt();
                    assertEquals(x, y);
                    Thread.yield();
                }
            }
        }
    };
    task.execute();
    int[] data = new int[size];
    try {
        ByteBuffer byteBuff = ByteBuffer.allocate(16);
        int operations = 10000;
        for (int i = 0; i < operations; i++) {
            byteBuff.position(0);
            byteBuff.putInt(i);
            byteBuff.putInt(i);
            byteBuff.flip();
            int pos = random.nextInt(size);
            f.write(byteBuff, pos * 64 * 1024);
            data[pos] = i;
            pos = random.nextInt(size);
            byteBuff.clear();
            f.read(byteBuff, pos * 64 * 1024);
            byteBuff.limit(16);
            byteBuff.position(0);
            int x = byteBuff.getInt();
            int y = byteBuff.getInt();
            assertEquals(x, y);
            assertEquals(data[pos], x);
        }
    } catch (Throwable e) {
        e.printStackTrace();
        fail("Exception: " + e);
    } finally {
        task.get();
        f.close();
        ra.close();
        file.delete();
        FileUtils.delete(s);
        System.gc();
    }
}
Also used : Task(org.h2.util.Task) RandomAccessFile(java.io.RandomAccessFile) Random(java.util.Random) FileChannel(java.nio.channels.FileChannel) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ByteBuffer(java.nio.ByteBuffer)

Example 24 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestNetUtils method testFrequentConnections.

private static void testFrequentConnections(boolean ssl, int count) throws Exception {
    final ServerSocket serverSocket = NetUtils.createServerSocket(PORT, ssl);
    final AtomicInteger counter = new AtomicInteger(count);
    Task serverThread = new Task() {

        @Override
        public void call() {
            while (!stop) {
                try {
                    Socket socket = serverSocket.accept();
                    // System.out.println("opened " + counter);
                    socket.close();
                } catch (Exception e) {
                // ignore
                }
            }
        // System.out.println("stopped ");
        }
    };
    serverThread.execute();
    try {
        Set<ConnectWorker> workers = new HashSet<>();
        for (int i = 0; i < WORKER_COUNT; i++) {
            workers.add(new ConnectWorker(ssl, counter));
        }
        // ensure the server is started
        Thread.sleep(100);
        for (ConnectWorker worker : workers) {
            worker.start();
        }
        for (ConnectWorker worker : workers) {
            worker.join();
            Exception e = worker.getException();
            if (e != null) {
                e.printStackTrace();
            }
        }
    } finally {
        try {
            serverSocket.close();
        } catch (Exception e) {
        // ignore
        }
        serverThread.get();
    }
}
Also used : Task(org.h2.util.Task) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 25 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestNetUtils method createServerSocketTask.

private Task createServerSocketTask(final ServerSocket serverSocket) {
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            Socket ss = null;
            try {
                ss = serverSocket.accept();
                ss.getOutputStream().write(123);
            } finally {
                closeSilently(ss);
            }
        }
    };
    return task;
}
Also used : Task(org.h2.util.Task) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket)

Aggregations

Task (org.h2.util.Task)69 Connection (java.sql.Connection)68 Statement (java.sql.Statement)64 PreparedStatement (java.sql.PreparedStatement)60 ResultSet (java.sql.ResultSet)48 SQLException (java.sql.SQLException)42 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 SimpleResultSet (org.h2.tools.SimpleResultSet)24 MVStore (org.h2.mvstore.MVStore)20 Random (java.util.Random)19 JdbcConnection (org.h2.jdbc.JdbcConnection)19 CallableStatement (java.sql.CallableStatement)14 DbException (org.h2.message.DbException)13 IOException (java.io.IOException)10 JdbcSQLException (org.h2.jdbc.JdbcSQLException)7 ArrayList (java.util.ArrayList)6 Expression (org.h2.expression.Expression)6 ValueString (org.h2.value.ValueString)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 OutputStream (java.io.OutputStream)4