Search in sources :

Example 21 with PoolBehavior

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

the class ContinuousCqlRequestHandlerTest method should_complete_multi_page_result.

@Test
@UseDataProvider(value = "allDseProtocolVersions", location = DseTestDataProviders.class)
public void should_complete_multi_page_result(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)));
        assertThatStage(page1Future).isSuccess(page1 -> {
            assertThat(page1.hasMorePages()).isTrue();
            assertThat(page1.pageNumber()).isEqualTo(1);
            Iterator<Row> rows = page1.currentPage().iterator();
            assertThat(rows.hasNext()).isTrue();
            assertThat(rows).toIterable().hasSize(10);
            ExecutionInfo executionInfo = page1.getExecutionInfo();
            assertThat(executionInfo.getCoordinator()).isEqualTo(node1);
            assertThat(executionInfo.getErrors()).isEmpty();
            assertThat(executionInfo.getIncomingPayload()).isEmpty();
            assertThat(executionInfo.getPagingState()).isNotNull();
            assertThat(executionInfo.getSpeculativeExecutionCount()).isEqualTo(0);
            assertThat(executionInfo.getSuccessfulExecutionIndex()).isEqualTo(0);
            assertThat(executionInfo.getWarnings()).isEmpty();
        });
        ContinuousAsyncResultSet page1 = CompletableFutures.getCompleted(page1Future);
        assertThat(handler.getPendingResult()).isNull();
        CompletionStage<ContinuousAsyncResultSet> page2Future = page1.fetchNextPage();
        assertThat(handler.getPendingResult()).isNotNull();
        node1Behavior.setResponseSuccess(defaultFrameOf(DseTestFixtures.tenDseRows(2, true)));
        assertThatStage(page2Future).isSuccess(page2 -> {
            assertThat(page2.hasMorePages()).isFalse();
            assertThat(page2.pageNumber()).isEqualTo(2);
            Iterator<Row> rows = page2.currentPage().iterator();
            assertThat(rows.hasNext()).isTrue();
            assertThat(rows).toIterable().hasSize(10);
            ExecutionInfo executionInfo = page2.getExecutionInfo();
            assertThat(executionInfo.getCoordinator()).isEqualTo(node1);
            assertThat(executionInfo.getErrors()).isEmpty();
            assertThat(executionInfo.getIncomingPayload()).isEmpty();
            assertThat(executionInfo.getPagingState()).isNull();
            assertThat(executionInfo.getSpeculativeExecutionCount()).isEqualTo(0);
            assertThat(executionInfo.getSuccessfulExecutionIndex()).isEqualTo(0);
            assertThat(executionInfo.getWarnings()).isEmpty();
        });
    }
}
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) 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 22 with PoolBehavior

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

the class ContinuousCqlRequestHandlerTest method should_cancel_session_if_future_cancelled.

@Test
@UseDataProvider(value = "allDseProtocolVersions", location = DseTestDataProviders.class)
public void should_cancel_session_if_future_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();
        page1Future.toCompletableFuture().cancel(true);
        // this should be ignored
        node1Behavior.setResponseSuccess(defaultFrameOf(DseTestFixtures.tenDseRows(1, false)));
        assertThat(handler.getState()).isEqualTo(-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) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 23 with PoolBehavior

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

the class ContinuousCqlRequestHandlerTest method should_send_cancel_request_if_dse_v2.

@Test
public void should_send_cancel_request_if_dse_v2() {
    RequestHandlerTestHarness.Builder builder = continuousHarnessBuilder().withProtocolVersion(DSE_V2);
    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();
        page1Future.toCompletableFuture().cancel(true);
        assertThat(handler.getState()).isEqualTo(-2);
        verify(node1Behavior.getChannel()).write(argThat(this::isCancelRequest), anyBoolean(), anyMap(), any());
    }
}
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)

Example 24 with PoolBehavior

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

the class ContinuousCqlRequestHandlerTest method should_toggle_channel_autoread_if_dse_v1.

@Test
public void should_toggle_channel_autoread_if_dse_v1() {
    RequestHandlerTestHarness.Builder builder = continuousHarnessBuilder().withProtocolVersion(DSE_V1);
    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 5 pages, the first one will complete page1 future above,
        // the following 4 will be enqueued and should trigger autoread off
        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)));
        node1Behavior.setResponseSuccess(defaultFrameOf(DseTestFixtures.tenDseRows(5, false)));
        verify(node1Behavior.getChannel().config()).setAutoRead(false);
        // simulate the retrieval of 2 pages, this should dequeue page 2
        // and trigger autoread on
        ContinuousAsyncResultSet page1 = CompletableFutures.getCompleted(page1Future);
        CompletableFutures.getCompleted(page1.fetchNextPage());
        verify(node1Behavior.getChannel().config()).setAutoRead(true);
        // in DSE_V1, the backpressure request should not have been sent
        verify(node1Behavior.getChannel(), never()).write(any(Revise.class), anyBoolean(), anyMap(), any());
    }
}
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) Revise(com.datastax.dse.protocol.internal.request.Revise) Test(org.junit.Test)

Example 25 with PoolBehavior

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

the class CqlRequestReactiveProcessorTest method should_complete_multi_page_result.

@Test
@UseDataProvider(value = "allDseAndOssProtocolVersions", location = DseTestDataProviders.class)
public void should_complete_multi_page_result(ProtocolVersion version) {
    RequestHandlerTestHarness.Builder builder = RequestHandlerTestHarness.builder().withProtocolVersion(version);
    PoolBehavior node1Behavior = builder.customBehavior(node1);
    try (RequestHandlerTestHarness harness = builder.build()) {
        DefaultSession session = harness.getSession();
        InternalDriverContext context = harness.getContext();
        // The 2nd page is obtained by an "external" call to session.executeAsync(),
        // so we need to mock that.
        CompletableFuture<AsyncResultSet> page2Future = new CompletableFuture<>();
        when(session.executeAsync(any(Statement.class))).thenAnswer(invocation -> page2Future);
        ExecutionInfo mockInfo = mock(ExecutionInfo.class);
        ReactiveResultSet publisher = new CqlRequestReactiveProcessor(new CqlRequestAsyncProcessor()).process(UNDEFINED_IDEMPOTENCE_STATEMENT, session, context, "test");
        Flowable<ReactiveRow> rowsPublisher = Flowable.fromPublisher(publisher).cache();
        rowsPublisher.subscribe();
        // emulate arrival of page 1
        node1Behavior.setResponseSuccess(defaultFrameOf(DseTestFixtures.tenDseRows(1, false)));
        // emulate arrival of page 2 following the call to session.executeAsync()
        page2Future.complete(Conversions.toResultSet(DseTestFixtures.tenDseRows(2, true), mockInfo, harness.getSession(), harness.getContext()));
        List<ReactiveRow> rows = rowsPublisher.toList().blockingGet();
        assertThat(rows).hasSize(20);
        ReactiveRow first = rows.get(0);
        ExecutionInfo firstExecutionInfo = first.getExecutionInfo();
        assertThat(firstExecutionInfo.getCoordinator()).isEqualTo(node1);
        assertThat(firstExecutionInfo.getErrors()).isEmpty();
        assertThat(firstExecutionInfo.getIncomingPayload()).isEmpty();
        assertThat(firstExecutionInfo.getPagingState()).isNotNull();
        assertThat(firstExecutionInfo.getSpeculativeExecutionCount()).isEqualTo(0);
        assertThat(firstExecutionInfo.getSuccessfulExecutionIndex()).isEqualTo(0);
        assertThat(firstExecutionInfo.getWarnings()).isEmpty();
        ReactiveRow inSecondPage = rows.get(10);
        ExecutionInfo secondExecutionInfo = inSecondPage.getExecutionInfo();
        assertThat(secondExecutionInfo).isSameAs(mockInfo);
        Flowable<ExecutionInfo> execInfosFlowable = Flowable.fromPublisher(publisher.getExecutionInfos());
        assertThat(execInfosFlowable.toList().blockingGet()).containsExactly(firstExecutionInfo, secondExecutionInfo);
        Flowable<ColumnDefinitions> colDefsFlowable = Flowable.fromPublisher(publisher.getColumnDefinitions());
        assertThat(colDefsFlowable.toList().blockingGet()).containsExactly(first.getColumnDefinitions());
        Flowable<Boolean> wasAppliedFlowable = Flowable.fromPublisher(publisher.wasApplied());
        assertThat(wasAppliedFlowable.toList().blockingGet()).containsExactly(first.wasApplied());
    }
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) Statement(com.datastax.oss.driver.api.core.cql.Statement) RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) CqlRequestAsyncProcessor(com.datastax.oss.driver.internal.core.cql.CqlRequestAsyncProcessor) CompletableFuture(java.util.concurrent.CompletableFuture) ReactiveResultSet(com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) DefaultSession(com.datastax.oss.driver.internal.core.session.DefaultSession) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

PoolBehavior (com.datastax.oss.driver.internal.core.cql.PoolBehavior)25 Test (org.junit.Test)25 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)20 GraphTestUtils.createGraphBinaryModule (com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule)14 GraphBinaryModule (com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule)14 RequestHandlerTestHarness (com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness)14 CapturedTimeout (com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout)10 ContinuousAsyncResultSet (com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet)9 AsyncGraphResultSet (com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet)8 SpeculativeExecutionPolicy (com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy)7 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)6 Node (com.datastax.oss.driver.api.core.metadata.Node)6 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)6 DefaultSession (com.datastax.oss.driver.internal.core.session.DefaultSession)4 Error (com.datastax.oss.protocol.internal.response.Error)4 GraphNode (com.datastax.dse.driver.api.core.graph.GraphNode)3 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)3 ReactiveRow (com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow)2 GraphResultSet (com.datastax.dse.driver.api.core.graph.GraphResultSet)2 ReactiveGraphNode (com.datastax.dse.driver.api.core.graph.reactive.ReactiveGraphNode)2