Search in sources :

Example 26 with ContinuousAsyncResultSet

use of com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet in project java-driver by datastax.

the class ContinuousCqlRequestHandlerRetryTest method should_rethrow_error_if_not_idempotent_and_error_unsafe_or_policy_rethrows.

@Test
@UseDataProvider("failureAndNotIdempotent")
public void should_rethrow_error_if_not_idempotent_and_error_unsafe_or_policy_rethrows(FailureScenario failureScenario, boolean defaultIdempotence, Statement<?> statement, DseProtocolVersion version) {
    // For two of the possible exceptions, the retry policy is called even if the statement is not
    // idempotent
    boolean shouldCallRetryPolicy = (failureScenario.expectedExceptionClass.equals(UnavailableException.class) || failureScenario.expectedExceptionClass.equals(ReadTimeoutException.class));
    RequestHandlerTestHarness.Builder harnessBuilder = continuousHarnessBuilder().withProtocolVersion(version).withDefaultIdempotence(defaultIdempotence);
    failureScenario.mockRequestError(harnessBuilder, node1);
    harnessBuilder.withResponse(node2, defaultFrameOf(DseTestFixtures.singleDseRow()));
    try (RequestHandlerTestHarness harness = harnessBuilder.build()) {
        if (shouldCallRetryPolicy) {
            failureScenario.mockRetryPolicyVerdict(harness.getContext().getRetryPolicy(anyString()), RetryVerdict.RETHROW);
        }
        ContinuousCqlRequestHandler handler = new ContinuousCqlRequestHandler(statement, harness.getSession(), harness.getContext(), "test");
        CompletionStage<ContinuousAsyncResultSet> resultSetFuture = handler.handle();
        assertThat(handler.getState()).isEqualTo(-2);
        assertThatStage(resultSetFuture).isFailed(error -> {
            assertThat(error).isInstanceOf(failureScenario.expectedExceptionClass);
            // When non idempotent, the policy is bypassed completely:
            if (!shouldCallRetryPolicy) {
                Mockito.verifyNoMoreInteractions(harness.getContext().getRetryPolicy(anyString()));
            }
            Mockito.verify(nodeMetricUpdater1).incrementCounter(eq(failureScenario.errorMetric), anyString());
            Mockito.verify(nodeMetricUpdater1, atMost(1)).updateTimer(eq(DefaultNodeMetric.CQL_MESSAGES), anyString(), anyLong(), eq(TimeUnit.NANOSECONDS));
            Mockito.verifyNoMoreInteractions(nodeMetricUpdater1);
        });
    }
}
Also used : RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) ContinuousAsyncResultSet(com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 27 with ContinuousAsyncResultSet

use of com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet in project java-driver by datastax.

the class DefaultContinuousResultSetTest method mockPage.

private static ContinuousAsyncResultSet mockPage(boolean nextPage, Integer... data) {
    ContinuousAsyncResultSet page = Mockito.mock(ContinuousAsyncResultSet.class);
    ColumnDefinitions columnDefinitions = Mockito.mock(ColumnDefinitions.class);
    Mockito.when(page.getColumnDefinitions()).thenReturn(columnDefinitions);
    ExecutionInfo executionInfo = Mockito.mock(ExecutionInfo.class);
    Mockito.when(page.getExecutionInfo()).thenReturn(executionInfo);
    if (nextPage) {
        Mockito.when(page.hasMorePages()).thenReturn(true);
        Mockito.when(page.fetchNextPage()).thenReturn(Mockito.spy(new CompletableFuture<>()));
    } else {
        Mockito.when(page.hasMorePages()).thenReturn(false);
        Mockito.when(page.fetchNextPage()).thenThrow(new IllegalStateException());
    }
    Iterator<Integer> rows = Arrays.asList(data).iterator();
    CountingIterator<Row> iterator = new CountingIterator<Row>(data.length) {

        @Override
        protected Row computeNext() {
            return rows.hasNext() ? mockRow(rows.next()) : endOfData();
        }
    };
    Mockito.when(page.currentPage()).thenReturn(() -> iterator);
    Mockito.when(page.remaining()).thenAnswer(invocation -> iterator.remaining());
    return page;
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) CompletableFuture(java.util.concurrent.CompletableFuture) CountingIterator(com.datastax.oss.driver.internal.core.util.CountingIterator) ContinuousAsyncResultSet(com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) Row(com.datastax.oss.driver.api.core.cql.Row)

Example 28 with ContinuousAsyncResultSet

use of com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet in project java-driver by datastax.

the class DefaultContinuousResultSetTest method should_create_result_set_from_multiple_pages.

@Test
public void should_create_result_set_from_multiple_pages() {
    // Given
    ContinuousAsyncResultSet page1 = mockPage(true, 0, 1, 2);
    ContinuousAsyncResultSet page2 = mockPage(true, 3, 4, 5);
    ContinuousAsyncResultSet page3 = mockPage(false, 6, 7, 8);
    complete(page1.fetchNextPage(), page2);
    complete(page2.fetchNextPage(), page3);
    // When
    ResultSet resultSet = new DefaultContinuousResultSet(page1);
    // Then
    assertThat(resultSet.iterator().hasNext()).isTrue();
    assertThat(resultSet.getColumnDefinitions()).isSameAs(page1.getColumnDefinitions());
    assertThat(resultSet.getExecutionInfo()).isSameAs(page1.getExecutionInfo());
    assertThat(resultSet.getExecutionInfos()).containsExactly(page1.getExecutionInfo());
    Iterator<Row> iterator = resultSet.iterator();
    assertNextRow(iterator, 0);
    assertNextRow(iterator, 1);
    assertNextRow(iterator, 2);
    assertThat(iterator.hasNext()).isTrue();
    // This should have triggered the fetch of page2
    assertThat(resultSet.getExecutionInfo()).isEqualTo(page2.getExecutionInfo());
    assertThat(resultSet.getExecutionInfos()).containsExactly(page1.getExecutionInfo(), page2.getExecutionInfo());
    assertNextRow(iterator, 3);
    assertNextRow(iterator, 4);
    assertNextRow(iterator, 5);
    assertThat(iterator.hasNext()).isTrue();
    // This should have triggered the fetch of page3
    assertThat(resultSet.getExecutionInfo()).isEqualTo(page3.getExecutionInfo());
    assertThat(resultSet.getExecutionInfos()).containsExactly(page1.getExecutionInfo(), page2.getExecutionInfo(), page3.getExecutionInfo());
    assertNextRow(iterator, 6);
    assertNextRow(iterator, 7);
    assertNextRow(iterator, 8);
}
Also used : ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) ContinuousAsyncResultSet(com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet) ContinuousAsyncResultSet(com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet) Row(com.datastax.oss.driver.api.core.cql.Row) Test(org.junit.Test)

Aggregations

ContinuousAsyncResultSet (com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet)28 Test (org.junit.Test)26 RequestHandlerTestHarness (com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness)23 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)20 Row (com.datastax.oss.driver.api.core.cql.Row)10 PoolBehavior (com.datastax.oss.driver.internal.core.cql.PoolBehavior)9 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)8 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)2 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)2 ServerError (com.datastax.oss.driver.api.core.servererrors.ServerError)2 Request (com.datastax.oss.driver.api.core.session.Request)2 Session (com.datastax.oss.driver.api.core.session.Session)2 LoadBalancingPolicyWrapper (com.datastax.oss.driver.internal.core.metadata.LoadBalancingPolicyWrapper)2 CapturedTimeout (com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout)2 Prepare (com.datastax.oss.protocol.internal.request.Prepare)2 Query (com.datastax.oss.protocol.internal.request.Query)2 Error (com.datastax.oss.protocol.internal.response.Error)2 InOrder (org.mockito.InOrder)2 Revise (com.datastax.dse.protocol.internal.request.Revise)1 AllNodesFailedException (com.datastax.oss.driver.api.core.AllNodesFailedException)1