Search in sources :

Example 6 with AvaticaStatement

use of org.apache.calcite.avatica.AvaticaStatement in project calcite-avatica by apache.

the class RemoteMetaTest method testCancel.

/** Test case for
   * <a href="https://issues.apache.org/jira/browse/CALCITE-1301">[CALCITE-1301]
   * Add cancel flag to AvaticaStatement</a>. */
@Test
public void testCancel() throws Exception {
    ConnectionSpec.getDatabaseLock().lock();
    try (AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url)) {
        final AvaticaStatement statement = conn.createStatement();
        final String sql = "select * from (values ('a', 1), ('b', 2))";
        final ResultSet rs = statement.executeQuery(sql);
        int count = 0;
        loop: for (; ; ) {
            switch(count++) {
                case 0:
                    assertThat(rs.next(), is(true));
                    break;
                case 1:
                    rs.getStatement().cancel();
                    try {
                        boolean x = rs.next();
                        fail("expected exception, got " + x);
                    } catch (SQLException e) {
                        assertThat(e.getMessage(), is("Statement canceled"));
                    }
                    break loop;
                default:
                    fail("count: " + count);
            }
        }
        assertThat(count, is(2));
        assertThat(statement.isClosed(), is(false));
        rs.close();
        assertThat(statement.isClosed(), is(false));
        statement.close();
        assertThat(statement.isClosed(), is(true));
        statement.close();
        assertThat(statement.isClosed(), is(true));
    } finally {
        ConnectionSpec.getDatabaseLock().unlock();
    }
}
Also used : AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) StringContains.containsString(org.hamcrest.core.StringContains.containsString) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) Test(org.junit.Test)

Aggregations

AvaticaStatement (org.apache.calcite.avatica.AvaticaStatement)6 ResultSet (java.sql.ResultSet)5 AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)5 Test (org.junit.Test)3 SQLException (java.sql.SQLException)2 StringContains.containsString (org.hamcrest.core.StringContains.containsString)2 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)1 RpcException (org.apache.drill.exec.rpc.RpcException)1