Search in sources :

Example 1 with DefaultAsyncResultSet

use of com.datastax.oss.driver.internal.core.cql.DefaultAsyncResultSet in project java-driver by datastax.

the class AsyncPagingIterableWrapperTest method should_wrap_result_set.

@Test
public void should_wrap_result_set() throws Exception {
    // Given
    // two pages of data:
    ExecutionInfo executionInfo1 = mockExecutionInfo();
    DefaultAsyncResultSet resultSet1 = new DefaultAsyncResultSet(columnDefinitions, executionInfo1, mockData(0, 5), session, context);
    DefaultAsyncResultSet resultSet2 = new DefaultAsyncResultSet(columnDefinitions, mockExecutionInfo(), mockData(5, 10), session, context);
    // chain them together:
    ByteBuffer mockPagingState = ByteBuffer.allocate(0);
    when(executionInfo1.getPagingState()).thenReturn(mockPagingState);
    Statement<?> mockNextStatement = mock(Statement.class);
    when(((Statement) statement).copy(mockPagingState)).thenReturn(mockNextStatement);
    when(session.executeAsync(mockNextStatement)).thenAnswer(invocation -> CompletableFuture.completedFuture(resultSet2));
    // When
    MappedAsyncPagingIterable<Integer> iterable1 = resultSet1.map(row -> row.getInt("i"));
    // Then
    for (int i = 0; i < 5; i++) {
        assertThat(iterable1.one()).isEqualTo(i);
        assertThat(iterable1.remaining()).isEqualTo(resultSet1.remaining()).isEqualTo(4 - i);
    }
    assertThat(iterable1.hasMorePages()).isTrue();
    MappedAsyncPagingIterable<Integer> iterable2 = iterable1.fetchNextPage().toCompletableFuture().get();
    for (int i = 5; i < 10; i++) {
        assertThat(iterable2.one()).isEqualTo(i);
        assertThat(iterable2.remaining()).isEqualTo(resultSet2.remaining()).isEqualTo(9 - i);
    }
    assertThat(iterable2.hasMorePages()).isFalse();
}
Also used : DefaultAsyncResultSet(com.datastax.oss.driver.internal.core.cql.DefaultAsyncResultSet) Statement(com.datastax.oss.driver.api.core.cql.Statement) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with DefaultAsyncResultSet

use of com.datastax.oss.driver.internal.core.cql.DefaultAsyncResultSet in project java-driver by datastax.

the class AsyncPagingIterableWrapperTest method should_share_iteration_progress_with_wrapped_result_set.

/**
 * Checks that consuming from the wrapper consumes from the source, and vice-versa.
 */
@Test
public void should_share_iteration_progress_with_wrapped_result_set() {
    // Given
    DefaultAsyncResultSet resultSet = new DefaultAsyncResultSet(columnDefinitions, mockExecutionInfo(), mockData(0, 10), session, context);
    // When
    MappedAsyncPagingIterable<Integer> iterable = resultSet.map(row -> row.getInt("i"));
    // Consume alternatively from the source and mapped iterable, and check that they stay in sync
    for (int i = 0; i < 10; i++) {
        Object element = (i % 2 == 0 ? resultSet : iterable).one();
        assertThat(element).isNotNull();
        assertThat(iterable.remaining()).isEqualTo(resultSet.remaining()).isEqualTo(9 - i);
    }
    assertThat(resultSet.hasMorePages()).isFalse();
    assertThat(iterable.hasMorePages()).isFalse();
}
Also used : DefaultAsyncResultSet(com.datastax.oss.driver.internal.core.cql.DefaultAsyncResultSet) Test(org.junit.Test)

Aggregations

DefaultAsyncResultSet (com.datastax.oss.driver.internal.core.cql.DefaultAsyncResultSet)2 Test (org.junit.Test)2 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)1 Statement (com.datastax.oss.driver.api.core.cql.Statement)1 ByteBuffer (java.nio.ByteBuffer)1