use of com.hazelcast.sql.impl.ResultIterator in project hazelcast by hazelcast.
the class SqlTestSupport method assertRowsEventuallyInAnyOrder.
/**
* Execute a query and wait for the results to contain all the {@code
* expectedRows}. Suitable for streaming queries that don't terminate, but
* return a deterministic set of rows. Rows can arrive in any order.
* <p>
* After all expected rows are received, the method further waits a little
* more if any extra rows are received, and fails, if they are.
*
* @param sql The query
* @param arguments The query arguments
* @param expectedRows Expected rows
*/
public static void assertRowsEventuallyInAnyOrder(String sql, List<Object> arguments, Collection<Row> expectedRows) {
SqlService sqlService = instance().getSql();
CompletableFuture<Void> future = new CompletableFuture<>();
Deque<Row> rows = new ArrayDeque<>();
Thread thread = new Thread(() -> {
SqlStatement statement = new SqlStatement(sql);
arguments.forEach(statement::addParameter);
try (SqlResult result = sqlService.execute(statement)) {
ResultIterator<SqlRow> iterator = (ResultIterator<SqlRow>) result.iterator();
for (int i = 0; i < expectedRows.size() && iterator.hasNext() || iterator.hasNext(50, TimeUnit.MILLISECONDS) == YES; 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).containsExactlyInAnyOrderElementsOf(expectedRows);
}
use of com.hazelcast.sql.impl.ResultIterator in project hazelcast by hazelcast.
the class SqlClientResultTest method when_checkingHasNextWithTimeout_then_timeoutIsLongerThanParam.
@Test
public void when_checkingHasNextWithTimeout_then_timeoutIsLongerThanParam() {
try (SqlResult result = execute("select * from table(generate_stream(1))")) {
assertTrue(result.isRowSet());
ResultIterator<SqlRow> iterator = (ResultIterator<SqlRow>) result.iterator();
long shortestSleep = Long.MAX_VALUE;
for (int i = 0; i < 2; i++) {
long startNanos = System.nanoTime();
while (iterator.hasNext(10, TimeUnit.MILLISECONDS) == ResultIterator.HasNextResult.TIMEOUT) {
shortestSleep = Math.min(shortestSleep, System.nanoTime() - startNanos);
startNanos = System.nanoTime();
}
iterator.next();
}
assertGreaterOrEquals("shortestSleep", shortestSleep, TimeUnit.MILLISECONDS.toNanos(10));
}
}
use of com.hazelcast.sql.impl.ResultIterator in project hazelcast by hazelcast.
the class SqlClientResultTest method when_checkingHasNextWithTimeout_then_timeoutOccurs.
@Test
public void when_checkingHasNextWithTimeout_then_timeoutOccurs() {
try (SqlResult result = execute("select * from table(generate_stream(1))")) {
assertTrue(result.isRowSet());
ResultIterator<SqlRow> iterator = (ResultIterator<SqlRow>) result.iterator();
int timeoutCount = 0;
for (int i = 0; i < 2; i++) {
while (iterator.hasNext(10, TimeUnit.MILLISECONDS) == ResultIterator.HasNextResult.TIMEOUT) {
timeoutCount++;
}
iterator.next();
}
assertNotEquals(0, timeoutCount);
}
}
Aggregations