Search in sources :

Example 1 with ResultIterator

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);
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) JetSqlRow(com.hazelcast.sql.impl.row.JetSqlRow) SqlResult(com.hazelcast.sql.SqlResult) ResultIterator(com.hazelcast.sql.impl.ResultIterator) ArrayList(java.util.ArrayList) ArrayDeque(java.util.ArrayDeque) TimeoutException(java.util.concurrent.TimeoutException) HazelcastException(com.hazelcast.core.HazelcastException) SqlStatement(com.hazelcast.sql.SqlStatement) CompletableFuture(java.util.concurrent.CompletableFuture) SqlService(com.hazelcast.sql.SqlService) SqlRow(com.hazelcast.sql.SqlRow) JetSqlRow(com.hazelcast.sql.impl.row.JetSqlRow) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with ResultIterator

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));
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) SqlResult(com.hazelcast.sql.SqlResult) ResultIterator(com.hazelcast.sql.impl.ResultIterator) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with ResultIterator

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);
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) SqlResult(com.hazelcast.sql.SqlResult) ResultIterator(com.hazelcast.sql.impl.ResultIterator) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

SqlResult (com.hazelcast.sql.SqlResult)3 SqlRow (com.hazelcast.sql.SqlRow)3 ResultIterator (com.hazelcast.sql.impl.ResultIterator)3 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 Test (org.junit.Test)2 HazelcastException (com.hazelcast.core.HazelcastException)1 SqlService (com.hazelcast.sql.SqlService)1 SqlStatement (com.hazelcast.sql.SqlStatement)1 JetSqlRow (com.hazelcast.sql.impl.row.JetSqlRow)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 TimeoutException (java.util.concurrent.TimeoutException)1