Search in sources :

Example 1 with RemoteDatabaseEvent

use of io.trino.plugin.jdbc.RemoteDatabaseEvent in project trino by trinodb.

the class TestTestingPostgreSqlServer method testCapturingCancelledStatement.

@Test(timeOut = 60_000)
public void testCapturingCancelledStatement() throws Exception {
    String sql = "SELECT pg_sleep(60)";
    // verify query was not run before
    RemoteDatabaseEvent running = new RemoteDatabaseEvent(sql, RUNNING);
    RemoteDatabaseEvent cancelled = new RemoteDatabaseEvent(sql, CANCELLED);
    assertThat(postgreSqlServer.getRemoteDatabaseEvents()).doesNotContain(running, cancelled);
    try (Connection connection = DriverManager.getConnection(postgreSqlServer.getJdbcUrl(), postgreSqlServer.getProperties());
        Statement statement = connection.createStatement()) {
        Future<Boolean> executeFuture = threadPool.submit(() -> statement.execute(sql));
        // wait for query to become running
        assertEventually(() -> assertThat(postgreSqlServer.getRemoteDatabaseEvents()).contains(running));
        // cancel the query
        statement.cancel();
        assertThatThrownBy(executeFuture::get).hasRootCauseInstanceOf(SQLException.class).hasRootCauseMessage("ERROR: canceling statement due to user request");
    }
    assertEventually(() -> assertThat(postgreSqlServer.getRemoteDatabaseEvents()).contains(cancelled));
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) RemoteDatabaseEvent(io.trino.plugin.jdbc.RemoteDatabaseEvent) Connection(java.sql.Connection) Test(org.testng.annotations.Test)

Example 2 with RemoteDatabaseEvent

use of io.trino.plugin.jdbc.RemoteDatabaseEvent in project trino by trinodb.

the class TestingPostgreSqlServer method getRemoteDatabaseEvents.

protected List<RemoteDatabaseEvent> getRemoteDatabaseEvents() {
    List<String> logs = getLogs();
    Iterator<String> logsIterator = logs.iterator();
    ImmutableList.Builder<RemoteDatabaseEvent> events = ImmutableList.builder();
    while (logsIterator.hasNext()) {
        String logLine = logsIterator.next().replaceAll(LOG_PREFIX_REGEXP, "");
        if (logLine.startsWith(LOG_RUNNING_STATEMENT_PREFIX)) {
            events.add(new RemoteDatabaseEvent(logLine.substring(LOG_RUNNING_STATEMENT_PREFIX.length()), RUNNING));
        }
        if (logLine.equals(LOG_CANCELLATION_EVENT)) {
            // next line must be present
            String cancelledStatementLogLine = logsIterator.next().replaceAll(LOG_PREFIX_REGEXP, "");
            if (cancelledStatementLogLine.startsWith(LOG_CANCELLED_STATEMENT_PREFIX)) {
                events.add(new RemoteDatabaseEvent(cancelledStatementLogLine.substring(LOG_CANCELLED_STATEMENT_PREFIX.length()), CANCELLED));
            }
        }
    // ignore unsupported log lines
    }
    return events.build();
}
Also used : ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RemoteDatabaseEvent(io.trino.plugin.jdbc.RemoteDatabaseEvent)

Example 3 with RemoteDatabaseEvent

use of io.trino.plugin.jdbc.RemoteDatabaseEvent in project trino by trinodb.

the class TestTestingPostgreSqlServer method testCapturingSuccessfulStatement.

@Test
public void testCapturingSuccessfulStatement() {
    String sql = "SELECT 1";
    RemoteDatabaseEvent event = new RemoteDatabaseEvent(sql, RUNNING);
    // verify query was not run before
    assertThat(postgreSqlServer.getRemoteDatabaseEvents()).doesNotContain(event);
    postgreSqlServer.execute(sql);
    // logging events is asynchronous, it may take some time till it is available
    assertEventually(() -> assertThat(postgreSqlServer.getRemoteDatabaseEvents()).contains(event));
}
Also used : RemoteDatabaseEvent(io.trino.plugin.jdbc.RemoteDatabaseEvent) Test(org.testng.annotations.Test)

Aggregations

RemoteDatabaseEvent (io.trino.plugin.jdbc.RemoteDatabaseEvent)3 Test (org.testng.annotations.Test)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1