Search in sources :

Example 11 with ReactiveResultSet

use of com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet in project java-driver by datastax.

the class UpdateReactiveIT method should_update_entity_if_condition_is_met_reactive.

@Test
public void should_update_entity_if_condition_is_met_reactive() {
    Flowable.fromPublisher(dao.updateReactive(new Product(FLAMETHROWER.getId(), "Description for length 10", new Dimensions(10, 1, 1)))).blockingSubscribe();
    assertThat(Flowable.fromPublisher(dao.findByIdReactive(FLAMETHROWER.getId())).blockingSingle()).isNotNull();
    Product otherProduct = new Product(FLAMETHROWER.getId(), "Other description", new Dimensions(1, 1, 1));
    ReactiveResultSet rs = dao.updateIfLengthReactive(otherProduct, 10);
    ReactiveRow row = Flowable.fromPublisher(rs).blockingSingle();
    assertThat(row.wasApplied()).isTrue();
    assertThat(row.getColumnDefinitions().contains("dimensions")).isFalse();
    assertThat(Single.fromPublisher(rs.getColumnDefinitions()).blockingGet().contains("dimensions")).isFalse();
    assertThat(Single.fromPublisher(rs.wasApplied()).blockingGet()).isTrue();
}
Also used : ReactiveResultSet(com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet) MappedReactiveResultSet(com.datastax.dse.driver.api.mapper.reactive.MappedReactiveResultSet) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) Test(org.junit.Test)

Example 12 with ReactiveResultSet

use of com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet in project java-driver by datastax.

the class CqlRequestReactiveProcessorTest method should_complete_single_page_result.

@Test
@UseDataProvider(value = "allDseAndOssProtocolVersions", location = DseTestDataProviders.class)
public void should_complete_single_page_result(ProtocolVersion version) {
    try (RequestHandlerTestHarness harness = RequestHandlerTestHarness.builder().withProtocolVersion(version).withResponse(node1, defaultFrameOf(singleDseRow())).build()) {
        DefaultSession session = harness.getSession();
        InternalDriverContext context = harness.getContext();
        ReactiveResultSet publisher = new CqlRequestReactiveProcessor(new CqlRequestAsyncProcessor()).process(UNDEFINED_IDEMPOTENCE_STATEMENT, session, context, "test");
        List<ReactiveRow> rows = Flowable.fromPublisher(publisher).toList().blockingGet();
        assertThat(rows).hasSize(1);
        ReactiveRow row = rows.get(0);
        assertThat(row.getString("message")).isEqualTo("hello, world");
        ExecutionInfo executionInfo = row.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();
        Flowable<ExecutionInfo> execInfosFlowable = Flowable.fromPublisher(publisher.getExecutionInfos());
        assertThat(execInfosFlowable.toList().blockingGet()).containsExactly(executionInfo);
        Flowable<ColumnDefinitions> colDefsFlowable = Flowable.fromPublisher(publisher.getColumnDefinitions());
        assertThat(colDefsFlowable.toList().blockingGet()).containsExactly(row.getColumnDefinitions());
        Flowable<Boolean> wasAppliedFlowable = Flowable.fromPublisher(publisher.wasApplied());
        assertThat(wasAppliedFlowable.toList().blockingGet()).containsExactly(row.wasApplied());
    }
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) 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) 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)

Example 13 with ReactiveResultSet

use of com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet 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)

Example 14 with ReactiveResultSet

use of com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet in project java-driver by datastax.

the class DefaultReactiveResultSetIT method should_write.

@Test
public void should_write() {
    SimpleStatement statement = SimpleStatement.builder("INSERT INTO test_reactive_write (pk, cc, v) VALUES (?, ?, ?)").addPositionalValue(0).addPositionalValue(1).addPositionalValue(2).setExecutionProfile(sessionRule.slowProfile()).build();
    ReactiveResultSet rs = sessionRule.session().executeReactive(statement);
    List<ReactiveRow> results = Flowable.fromPublisher(rs).toList().blockingGet();
    assertThat(results).isEmpty();
    List<ExecutionInfo> execInfos = Flowable.<ExecutionInfo>fromPublisher(rs.getExecutionInfos()).toList().blockingGet();
    assertThat(execInfos).hasSize(1);
    List<ColumnDefinitions> colDefs = Flowable.<ColumnDefinitions>fromPublisher(rs.getColumnDefinitions()).toList().blockingGet();
    assertThat(colDefs).hasSize(1).containsExactly(EmptyColumnDefinitions.INSTANCE);
    List<Boolean> wasApplied = Flowable.fromPublisher(rs.wasApplied()).toList().blockingGet();
    assertThat(wasApplied).hasSize(1).containsExactly(true);
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) EmptyColumnDefinitions(com.datastax.oss.driver.internal.core.cql.EmptyColumnDefinitions) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ReactiveResultSet(com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) Test(org.junit.Test)

Example 15 with ReactiveResultSet

use of com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet in project java-driver by datastax.

the class DefaultReactiveResultSetIT method should_write_batch_cas.

@Test
public void should_write_batch_cas() {
    BatchStatement batch = createCASBatch();
    CqlSession session = sessionRule.session();
    // execute batch for the first time: all inserts should succeed and the server should return
    // only one acknowledgement row with just the [applied] column = true
    ReactiveResultSet rs = session.executeReactive(batch);
    List<ReactiveRow> results = Flowable.fromPublisher(rs).toList().blockingGet();
    assertThat(results).hasSize(1);
    ReactiveRow row = results.get(0);
    assertThat(row.getExecutionInfo()).isNotNull();
    assertThat(row.getColumnDefinitions()).hasSize(1);
    assertThat(row.wasApplied()).isTrue();
    assertThat(row.getBoolean("[applied]")).isTrue();
    List<ExecutionInfo> execInfos = Flowable.<ExecutionInfo>fromPublisher(rs.getExecutionInfos()).toList().blockingGet();
    assertThat(execInfos).hasSize(1).containsExactly(row.getExecutionInfo());
    List<ColumnDefinitions> colDefs = Flowable.<ColumnDefinitions>fromPublisher(rs.getColumnDefinitions()).toList().blockingGet();
    assertThat(colDefs).hasSize(1).containsExactly(row.getColumnDefinitions());
    List<Boolean> wasApplied = Flowable.fromPublisher(rs.wasApplied()).toList().blockingGet();
    assertThat(wasApplied).hasSize(1).containsExactly(row.wasApplied());
    // delete 5 out of 10 rows
    partiallyDeleteInsertedRows();
    // re-execute same statement: server should return 5 rows for the 5 failed inserts, each one
    // with [applied] = false
    rs = session.executeReactive(batch);
    results = Flowable.fromPublisher(rs).toList().blockingGet();
    assertThat(results).hasSize(5);
    for (int i = 0; i < 5; i++) {
        row = results.get(i);
        assertThat(row.getExecutionInfo()).isNotNull();
        assertThat(row.getColumnDefinitions()).hasSize(4);
        assertThat(row.wasApplied()).isFalse();
        assertThat(row.getBoolean("[applied]")).isFalse();
        assertThat(row.getInt("pk")).isEqualTo(0);
        assertThat(row.getInt("cc")).isEqualTo(i);
        assertThat(row.getInt("v")).isEqualTo(i + 1);
    }
    execInfos = Flowable.<ExecutionInfo>fromPublisher(rs.getExecutionInfos()).toList().blockingGet();
    assertThat(execInfos).hasSize(1).containsExactly(row.getExecutionInfo());
    colDefs = Flowable.<ColumnDefinitions>fromPublisher(rs.getColumnDefinitions()).toList().blockingGet();
    assertThat(colDefs).hasSize(1).containsExactly(row.getColumnDefinitions());
    wasApplied = Flowable.fromPublisher(rs.wasApplied()).toList().blockingGet();
    assertThat(wasApplied).hasSize(1).containsExactly(row.wasApplied());
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) EmptyColumnDefinitions(com.datastax.oss.driver.internal.core.cql.EmptyColumnDefinitions) BatchStatement(com.datastax.oss.driver.api.core.cql.BatchStatement) ReactiveResultSet(com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test)

Aggregations

ReactiveResultSet (com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet)15 Test (org.junit.Test)15 ReactiveRow (com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow)13 MappedReactiveResultSet (com.datastax.dse.driver.api.mapper.reactive.MappedReactiveResultSet)9 ColumnDefinitions (com.datastax.oss.driver.api.core.cql.ColumnDefinitions)6 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)6 UUID (java.util.UUID)5 EmptyColumnDefinitions (com.datastax.oss.driver.internal.core.cql.EmptyColumnDefinitions)4 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)3 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)2 CqlRequestAsyncProcessor (com.datastax.oss.driver.internal.core.cql.CqlRequestAsyncProcessor)2 RequestHandlerTestHarness (com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness)2 DefaultSession (com.datastax.oss.driver.internal.core.session.DefaultSession)2 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)2 CqlSession (com.datastax.oss.driver.api.core.CqlSession)1 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)1 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)1 BatchStatement (com.datastax.oss.driver.api.core.cql.BatchStatement)1 Statement (com.datastax.oss.driver.api.core.cql.Statement)1 PoolBehavior (com.datastax.oss.driver.internal.core.cql.PoolBehavior)1