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));
}
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();
}
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));
}
Aggregations