use of com.hazelcast.sql.SqlService in project hazelcast by hazelcast.
the class SqlTestSupport method assertRowsAnyOrder.
/**
* Execute a query and wait until it completes. Assert that the returned
* rows contain the expected rows, in any order.
*
* @param instance Hazelcast Instance to be used
* @param sql The query
* @param arguments The query arguments
* @param expectedRows Expected rows
*/
public static void assertRowsAnyOrder(HazelcastInstance instance, String sql, List<Object> arguments, Collection<Row> expectedRows) {
SqlStatement statement = new SqlStatement(sql);
arguments.forEach(statement::addParameter);
SqlService sqlService = instance.getSql();
List<Row> actualRows = new ArrayList<>();
try (SqlResult result = sqlService.execute(statement)) {
result.iterator().forEachRemaining(row -> actualRows.add(new Row(row)));
}
assertThat(actualRows).containsExactlyInAnyOrderElementsOf(expectedRows);
}
use of com.hazelcast.sql.SqlService in project hazelcast by hazelcast.
the class SqlTestSupport method assertTipOfStream.
/**
* Execute the given `sql` and assert that the first rows it returns are those in
* `expectedRows`. Ignores the rest of rows.
* <p>
* This is useful for asserting initial output of streaming queries where
* the output arrives in a stable order.
*/
public static void assertTipOfStream(String sql, Collection<Row> expectedRows) {
assert !expectedRows.isEmpty() : "no point in asserting a zero-length tip of a stream";
SqlService sqlService = instance().getSql();
CompletableFuture<Void> future = new CompletableFuture<>();
Deque<Row> rows = new ArrayDeque<>();
Thread thread = new Thread(() -> {
SqlStatement statement = new SqlStatement(sql);
try (SqlResult result = sqlService.execute(statement)) {
Iterator<SqlRow> iterator = result.iterator();
for (int i = 0; i < expectedRows.size() && iterator.hasNext(); i++) {
rows.add(new Row(iterator.next()));
}
future.complete(null);
} catch (Throwable e) {
e.printStackTrace();
future.completeExceptionally(e);
}
});
thread.start();
try {
try {
future.get(10, TimeUnit.SECONDS);
} catch (TimeoutException e) {
thread.interrupt();
thread.join();
}
} catch (Exception e) {
throw sneakyThrow(e);
}
List<Row> actualRows = new ArrayList<>(rows);
assertThat(actualRows).containsExactlyElementsOf(expectedRows);
}
Aggregations