Search in sources :

Example 1 with RequestHandlerTestHarness

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

the class ContinuousCqlRequestHandlerReprepareTest method should_try_next_node_when_prepare_fails_with_recoverable_error.

@Test
@UseDataProvider(value = "allDseProtocolVersions", location = DseTestDataProviders.class)
public void should_try_next_node_when_prepare_fails_with_recoverable_error(DseProtocolVersion version) {
    try (RequestHandlerTestHarness harness = continuousHarnessBuilder().withResponse(node1, defaultFrameOf(unprepared)).withResponse(node2, defaultFrameOf(DseTestFixtures.singleDseRow())).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(recoverable));
            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());
        // should have tried the next host
        verify(harness.getChannel(node2)).write(any(Query.class), anyBoolean(), anyMap(), any());
        assertThat(handler.getState()).isEqualTo(-1);
        assertThatStage(page1Future).isSuccess(rs -> {
            assertThat(rs.currentPage()).hasSize(1);
            assertThat(rs.hasMorePages()).isFalse();
            assertThat(rs.getExecutionInfo().getCoordinator()).isEqualTo(node2);
            assertThat(rs.getExecutionInfo().getErrors()).hasSize(1).allSatisfy(entry -> {
                assertThat(entry.getKey()).isEqualTo(node1);
                assertThat(entry.getValue()).isInstanceOf(UnexpectedResponseException.class).hasMessageContaining(recoverable.toString());
            });
        });
    }
}
Also used : Query(com.datastax.oss.protocol.internal.request.Query) 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) UnexpectedResponseException(com.datastax.oss.driver.internal.core.adminrequest.UnexpectedResponseException) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 2 with RequestHandlerTestHarness

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

the class ContinuousCqlRequestHandlerTest method should_time_out_if_other_page_takes_too_long.

@Test
@UseDataProvider(value = "allDseProtocolVersions", location = DseTestDataProviders.class)
public void should_time_out_if_other_page_takes_too_long(DseProtocolVersion version) throws Exception {
    RequestHandlerTestHarness.Builder builder = continuousHarnessBuilder().withProtocolVersion(version);
    PoolBehavior node1Behavior = builder.customBehavior(node1);
    try (RequestHandlerTestHarness harness = builder.build()) {
        CompletionStage<ContinuousAsyncResultSet> page1Future = new ContinuousCqlRequestHandler(UNDEFINED_IDEMPOTENCE_STATEMENT, harness.getSession(), harness.getContext(), "test").handle();
        // mark the initial request as successful, which should schedule a timeout for the first page
        node1Behavior.setWriteSuccess();
        CapturedTimeout page1Timeout = harness.nextScheduledTimeout();
        assertThat(page1Timeout.getDelay(TimeUnit.NANOSECONDS)).isEqualTo(TIMEOUT_FIRST_PAGE.toNanos());
        // the server replies with page 1, the corresponding timeout should be cancelled
        node1Behavior.setResponseSuccess(defaultFrameOf(DseTestFixtures.tenDseRows(1, false)));
        assertThat(page1Timeout.isCancelled()).isTrue();
        // request page 2, the queue is empty so this should request more pages and schedule another
        // timeout
        ContinuousAsyncResultSet page1 = CompletableFutures.getUninterruptibly(page1Future);
        CompletionStage<ContinuousAsyncResultSet> page2Future = page1.fetchNextPage();
        CapturedTimeout page2Timeout = harness.nextScheduledTimeout();
        assertThat(page2Timeout.getDelay(TimeUnit.NANOSECONDS)).isEqualTo(TIMEOUT_OTHER_PAGES.toNanos());
        page2Timeout.task().run(page2Timeout);
        assertThatStage(page2Future).isFailed(t -> assertThat(t).isInstanceOf(DriverTimeoutException.class).hasMessageContaining("Timed out waiting for page 2"));
    }
}
Also used : PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) ContinuousAsyncResultSet(com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet) CapturedTimeout(com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 3 with RequestHandlerTestHarness

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

the class ContinuousCqlRequestHandlerTest method should_time_out_if_first_page_takes_too_long.

@Test
@UseDataProvider(value = "allDseProtocolVersions", location = DseTestDataProviders.class)
public void should_time_out_if_first_page_takes_too_long(DseProtocolVersion version) throws Exception {
    RequestHandlerTestHarness.Builder builder = continuousHarnessBuilder().withProtocolVersion(version);
    PoolBehavior node1Behavior = builder.customBehavior(node1);
    try (RequestHandlerTestHarness harness = builder.build()) {
        CompletionStage<ContinuousAsyncResultSet> resultSetFuture = new ContinuousCqlRequestHandler(UNDEFINED_IDEMPOTENCE_STATEMENT, harness.getSession(), harness.getContext(), "test").handle();
        // mark the initial request as successful, which should schedule a timeout for the first page
        node1Behavior.setWriteSuccess();
        CapturedTimeout page1Timeout = harness.nextScheduledTimeout();
        assertThat(page1Timeout.getDelay(TimeUnit.NANOSECONDS)).isEqualTo(TIMEOUT_FIRST_PAGE.toNanos());
        page1Timeout.task().run(page1Timeout);
        assertThatStage(resultSetFuture).isFailed(t -> assertThat(t).isInstanceOf(DriverTimeoutException.class).hasMessageContaining("Timed out waiting for page 1"));
    }
}
Also used : PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) ContinuousAsyncResultSet(com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet) CapturedTimeout(com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 4 with RequestHandlerTestHarness

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

the class ContinuousCqlRequestHandlerTest method should_cancel_future_if_session_cancelled.

@Test
@UseDataProvider(value = "allDseProtocolVersions", location = DseTestDataProviders.class)
public void should_cancel_future_if_session_cancelled(DseProtocolVersion version) {
    RequestHandlerTestHarness.Builder builder = continuousHarnessBuilder().withProtocolVersion(version);
    PoolBehavior node1Behavior = builder.customBehavior(node1);
    try (RequestHandlerTestHarness harness = builder.build()) {
        ContinuousCqlRequestHandler handler = new ContinuousCqlRequestHandler(UNDEFINED_IDEMPOTENCE_STATEMENT, harness.getSession(), harness.getContext(), "test");
        CompletionStage<ContinuousAsyncResultSet> page1Future = handler.handle();
        node1Behavior.setResponseSuccess(defaultFrameOf(DseTestFixtures.tenDseRows(1, false)));
        // will be discarded
        node1Behavior.setResponseSuccess(defaultFrameOf(DseTestFixtures.tenDseRows(2, false)));
        ContinuousAsyncResultSet page1 = CompletableFutures.getUninterruptibly(page1Future);
        page1.cancel();
        assertThat(handler.getState()).isEqualTo(-2);
        assertThat(page1.fetchNextPage()).isCancelled();
    }
}
Also used : PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) 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 5 with RequestHandlerTestHarness

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

the class ContinuousCqlRequestHandlerTest method should_send_backpressure_request_if_dse_v2.

@Test
public void should_send_backpressure_request_if_dse_v2() {
    RequestHandlerTestHarness.Builder builder = continuousHarnessBuilder().withProtocolVersion(DSE_V2);
    PoolBehavior node1Behavior = builder.customBehavior(node1);
    try (RequestHandlerTestHarness harness = builder.build()) {
        CompletionStage<ContinuousAsyncResultSet> page1Future = new ContinuousCqlRequestHandler(UNDEFINED_IDEMPOTENCE_STATEMENT, harness.getSession(), harness.getContext(), "test").handle();
        // simulate the arrival of 4 pages, the first one will complete page1 future above,
        // the following 3 will be enqueued
        node1Behavior.setResponseSuccess(defaultFrameOf(DseTestFixtures.tenDseRows(1, false)));
        node1Behavior.setResponseSuccess(defaultFrameOf(DseTestFixtures.tenDseRows(2, false)));
        node1Behavior.setResponseSuccess(defaultFrameOf(DseTestFixtures.tenDseRows(3, false)));
        node1Behavior.setResponseSuccess(defaultFrameOf(DseTestFixtures.tenDseRows(4, false)));
        // simulate the retrieval of 2 pages, this should dequeue page 2
        // and trigger a backpressure request as the queue is now half empty (2/4)
        ContinuousAsyncResultSet page1 = CompletableFutures.getCompleted(page1Future);
        CompletableFutures.getCompleted(page1.fetchNextPage());
        verify(node1Behavior.getChannel()).write(argThat(this::isBackpressureRequest), anyBoolean(), anyMap(), any());
        // should not mess with autoread in dse v2
        verify(node1Behavior.getChannel().config(), never()).setAutoRead(anyBoolean());
    }
}
Also used : PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) ContinuousAsyncResultSet(com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet) Test(org.junit.Test)

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