Search in sources :

Example 11 with RequestHandlerTestHarness

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

the class ContinuousCqlRequestHandlerRetryTest method should_try_next_node_if_idempotent_and_retry_policy_decides_so.

@Test
@UseDataProvider("failureAndIdempotent")
public void should_try_next_node_if_idempotent_and_retry_policy_decides_so(FailureScenario failureScenario, boolean defaultIdempotence, Statement<?> statement, DseProtocolVersion version) {
    RequestHandlerTestHarness.Builder harnessBuilder = continuousHarnessBuilder().withProtocolVersion(version).withDefaultIdempotence(defaultIdempotence);
    failureScenario.mockRequestError(harnessBuilder, node1);
    harnessBuilder.withResponse(node2, defaultFrameOf(DseTestFixtures.singleDseRow()));
    try (RequestHandlerTestHarness harness = harnessBuilder.build()) {
        failureScenario.mockRetryPolicyVerdict(harness.getContext().getRetryPolicy(anyString()), RetryVerdict.RETRY_NEXT);
        ContinuousCqlRequestHandler handler = new ContinuousCqlRequestHandler(statement, harness.getSession(), harness.getContext(), "test");
        CompletionStage<ContinuousAsyncResultSet> resultSetFuture = handler.handle();
        assertThat(handler.getState()).isEqualTo(-1);
        assertThatStage(resultSetFuture).isSuccess(resultSet -> {
            Iterator<Row> rows = resultSet.currentPage().iterator();
            assertThat(rows.hasNext()).isTrue();
            assertThat(rows.next().getString("message")).isEqualTo("hello, world");
            ExecutionInfo executionInfo = resultSet.getExecutionInfo();
            assertThat(executionInfo.getCoordinator()).isEqualTo(node2);
            assertThat(executionInfo.getErrors()).hasSize(1);
            assertThat(executionInfo.getErrors().get(0).getKey()).isEqualTo(node1);
            Mockito.verify(nodeMetricUpdater1).incrementCounter(eq(failureScenario.errorMetric), anyString());
            Mockito.verify(nodeMetricUpdater1).incrementCounter(eq(DefaultNodeMetric.RETRIES), anyString());
            Mockito.verify(nodeMetricUpdater1).incrementCounter(eq(failureScenario.retryMetric), 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) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) Row(com.datastax.oss.driver.api.core.cql.Row) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 12 with RequestHandlerTestHarness

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

the class ContinuousCqlRequestHandlerRetryTest method should_rethrow_error_if_idempotent_and_retry_policy_decides_so.

@Test
@UseDataProvider("failureAndIdempotent")
public void should_rethrow_error_if_idempotent_and_retry_policy_decides_so(FailureScenario failureScenario, boolean defaultIdempotence, Statement<?> statement, DseProtocolVersion version) {
    RequestHandlerTestHarness.Builder harnessBuilder = continuousHarnessBuilder().withProtocolVersion(version).withDefaultIdempotence(defaultIdempotence);
    failureScenario.mockRequestError(harnessBuilder, node1);
    try (RequestHandlerTestHarness harness = harnessBuilder.build()) {
        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);
            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 13 with RequestHandlerTestHarness

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

the class ContinuousGraphRequestHandlerTest method should_return_paged_results.

@Test
@UseDataProvider(location = DseTestDataProviders.class, value = "supportedGraphProtocols")
public void should_return_paged_results(GraphProtocol graphProtocol) throws IOException {
    String profileName = "test-graph";
    when(nodeMetricUpdater1.isEnabled(DseNodeMetric.GRAPH_MESSAGES, profileName)).thenReturn(true);
    GraphBinaryModule module = createGraphBinaryModule(mockContext);
    GraphRequestHandlerTestHarness.Builder builder = GraphRequestHandlerTestHarness.builder().withGraphProtocolForTestConfig(graphProtocol);
    PoolBehavior node1Behavior = builder.customBehavior(node);
    try (RequestHandlerTestHarness harness = builder.build()) {
        GraphStatement<?> graphStatement = ScriptGraphStatement.newInstance("mockQuery").setExecutionProfileName(profileName);
        ContinuousGraphRequestHandler handler = new ContinuousGraphRequestHandler(graphStatement, harness.getSession(), harness.getContext(), "test", module, new GraphSupportChecker());
        // send the initial request
        CompletionStage<AsyncGraphResultSet> page1Future = handler.handle();
        node1Behavior.setResponseSuccess(defaultDseFrameOf(tenGraphRows(graphProtocol, module, 1, false)));
        assertThatStage(page1Future).isSuccess(page1 -> {
            assertThat(page1.hasMorePages()).isTrue();
            assertThat(page1.currentPage()).hasSize(10).allMatch(GraphNode::isVertex);
            ExecutionInfo executionInfo = page1.getRequestExecutionInfo();
            assertThat(executionInfo.getCoordinator()).isEqualTo(node);
            assertThat(executionInfo.getErrors()).isEmpty();
            assertThat(executionInfo.getIncomingPayload()).isEmpty();
            assertThat(executionInfo.getSpeculativeExecutionCount()).isEqualTo(0);
            assertThat(executionInfo.getSuccessfulExecutionIndex()).isEqualTo(0);
            assertThat(executionInfo.getWarnings()).isEmpty();
        });
        AsyncGraphResultSet page1 = CompletableFutures.getCompleted(page1Future);
        CompletionStage<AsyncGraphResultSet> page2Future = page1.fetchNextPage();
        node1Behavior.setResponseSuccess(defaultDseFrameOf(tenGraphRows(graphProtocol, module, 2, true)));
        assertThatStage(page2Future).isSuccess(page2 -> {
            assertThat(page2.hasMorePages()).isFalse();
            assertThat(page2.currentPage()).hasSize(10).allMatch(GraphNode::isVertex);
            ExecutionInfo executionInfo = page2.getRequestExecutionInfo();
            assertThat(executionInfo.getCoordinator()).isEqualTo(node);
            assertThat(executionInfo.getErrors()).isEmpty();
            assertThat(executionInfo.getIncomingPayload()).isEmpty();
            assertThat(executionInfo.getSpeculativeExecutionCount()).isEqualTo(0);
            assertThat(executionInfo.getSuccessfulExecutionIndex()).isEqualTo(0);
            assertThat(executionInfo.getWarnings()).isEmpty();
        });
        validateMetrics(profileName, harness);
    }
}
Also used : PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) GraphNode(com.datastax.dse.driver.api.core.graph.GraphNode) GraphTestUtils.createGraphBinaryModule(com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule) GraphBinaryModule(com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule) AsyncGraphResultSet(com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 14 with RequestHandlerTestHarness

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

the class ContinuousGraphRequestHandlerTest method should_honor_default_timeout.

@Test
public void should_honor_default_timeout() throws Exception {
    // given
    GraphBinaryModule binaryModule = createGraphBinaryModule(mockContext);
    Duration defaultTimeout = Duration.ofSeconds(1);
    RequestHandlerTestHarness.Builder builder = GraphRequestHandlerTestHarness.builder().withGraphTimeout(defaultTimeout);
    PoolBehavior node1Behavior = builder.customBehavior(node);
    try (RequestHandlerTestHarness harness = builder.build()) {
        DriverExecutionProfile profile = harness.getContext().getConfig().getDefaultProfile();
        when(profile.isDefined(DseDriverOption.GRAPH_SUB_PROTOCOL)).thenReturn(true);
        when(profile.getString(DseDriverOption.GRAPH_SUB_PROTOCOL)).thenReturn(GraphProtocol.GRAPH_BINARY_1_0.toInternalCode());
        GraphStatement<?> graphStatement = ScriptGraphStatement.newInstance("mockQuery");
        // when
        ContinuousGraphRequestHandler handler = new ContinuousGraphRequestHandler(graphStatement, harness.getSession(), harness.getContext(), "test", binaryModule, new GraphSupportChecker());
        // send the initial request
        CompletionStage<AsyncGraphResultSet> page1Future = handler.handle();
        // acknowledge the write, will set the global timeout
        node1Behavior.verifyWrite();
        node1Behavior.setWriteSuccess();
        CapturedTimeout globalTimeout = harness.nextScheduledTimeout();
        assertThat(globalTimeout.getDelay(TimeUnit.NANOSECONDS)).isEqualTo(defaultTimeout.toNanos());
        // will trigger the global timeout and complete it exceptionally
        globalTimeout.task().run(globalTimeout);
        assertThat(page1Future.toCompletableFuture()).isCompletedExceptionally();
        assertThatThrownBy(() -> page1Future.toCompletableFuture().get()).hasRootCauseExactlyInstanceOf(DriverTimeoutException.class).hasMessageContaining("Query timed out after " + defaultTimeout);
    }
}
Also used : PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) DriverTimeoutException(com.datastax.oss.driver.api.core.DriverTimeoutException) RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) Duration(java.time.Duration) GraphTestUtils.createGraphBinaryModule(com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule) GraphBinaryModule(com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule) AsyncGraphResultSet(com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet) CapturedTimeout(com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout) Test(org.junit.Test)

Example 15 with RequestHandlerTestHarness

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

the class ContinuousCqlRequestHandlerReprepareTest method should_abort_when_prepare_fails_with_unrecoverable_error.

@Test
@UseDataProvider(value = "allDseProtocolVersions", location = DseTestDataProviders.class)
public void should_abort_when_prepare_fails_with_unrecoverable_error(DseProtocolVersion version) {
    try (RequestHandlerTestHarness harness = continuousHarnessBuilder().withResponse(node1, defaultFrameOf(unprepared)).withProtocolVersion(version).build()) {
        when(harness.getSession().getRepreparePayloads()).thenReturn(repreparePayloads);
        when(harness.getChannel(node1).write(any(Prepare.class), anyBoolean(), anyMap(), any())).then(invocation -> {
            AdminRequestHandler<?> admin = invocation.getArgument(3);
            admin.onResponse(defaultFrameOf(unrecoverable));
            return future;
        });
        ContinuousCqlRequestHandler handler = new ContinuousCqlRequestHandler(UNDEFINED_IDEMPOTENCE_STATEMENT, harness.getSession(), harness.getContext(), "test");
        CompletionStage<ContinuousAsyncResultSet> page1Future = handler.handle();
        verify(harness.getChannel(node1)).write(any(Query.class), anyBoolean(), anyMap(), any());
        verify(harness.getChannel(node1)).write(any(Prepare.class), anyBoolean(), anyMap(), any());
        assertThat(handler.getState()).isEqualTo(-2);
        assertThat(page1Future).isCompletedExceptionally();
        Throwable t = catchThrowable(() -> page1Future.toCompletableFuture().get());
        assertThat(t).hasRootCauseInstanceOf(SyntaxError.class).hasMessageContaining("bad query");
    }
}
Also used : Query(com.datastax.oss.protocol.internal.request.Query) SyntaxError(com.datastax.oss.driver.api.core.servererrors.SyntaxError) RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) Prepare(com.datastax.oss.protocol.internal.request.Prepare) ContinuousAsyncResultSet(com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

RequestHandlerTestHarness (com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness)35 Test (org.junit.Test)34 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)27 ContinuousAsyncResultSet (com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet)23 PoolBehavior (com.datastax.oss.driver.internal.core.cql.PoolBehavior)14 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)12 Row (com.datastax.oss.driver.api.core.cql.Row)7 ReactiveRow (com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow)4 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)4 ColumnDefinitions (com.datastax.oss.driver.api.core.cql.ColumnDefinitions)4 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)4 DefaultSession (com.datastax.oss.driver.internal.core.session.DefaultSession)4 CapturedTimeout (com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout)4 AsyncGraphResultSet (com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet)3 ContinuousCqlRequestAsyncProcessor (com.datastax.dse.driver.internal.core.cql.continuous.ContinuousCqlRequestAsyncProcessor)3 GraphTestUtils.createGraphBinaryModule (com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule)3 GraphBinaryModule (com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule)3 CqlRequestAsyncProcessor (com.datastax.oss.driver.internal.core.cql.CqlRequestAsyncProcessor)3 Prepare (com.datastax.oss.protocol.internal.request.Prepare)3 Query (com.datastax.oss.protocol.internal.request.Query)3