Search in sources :

Example 11 with TransactionContextImpl

use of com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl in project java-spanner by googleapis.

the class TransactionRunnerImplTest method batchDmlException.

@SuppressWarnings("unchecked")
private long[] batchDmlException(int status) {
    Preconditions.checkArgument(status != Code.OK_VALUE);
    TransactionContextImpl transaction = TransactionContextImpl.newBuilder().setSession(session).setTransactionId(ByteString.copyFromUtf8(UUID.randomUUID().toString())).setOptions(Options.fromTransactionOptions()).setRpc(rpc).build();
    when(session.newTransaction(Options.fromTransactionOptions())).thenReturn(transaction);
    when(session.beginTransactionAsync()).thenReturn(ApiFutures.immediateFuture(ByteString.copyFromUtf8(UUID.randomUUID().toString())));
    when(session.getName()).thenReturn(SessionId.of("p", "i", "d", "test").getName());
    TransactionRunnerImpl runner = new TransactionRunnerImpl(session);
    runner.setSpan(mock(Span.class));
    ExecuteBatchDmlResponse response1 = ExecuteBatchDmlResponse.newBuilder().addResultSets(ResultSet.newBuilder().setStats(ResultSetStats.newBuilder().setRowCountExact(1L)).build()).setStatus(com.google.rpc.Status.newBuilder().setCode(status).build()).build();
    ExecuteBatchDmlResponse response2 = ExecuteBatchDmlResponse.newBuilder().addResultSets(ResultSet.newBuilder().setStats(ResultSetStats.newBuilder().setRowCountExact(1L)).build()).addResultSets(ResultSet.newBuilder().setStats(ResultSetStats.newBuilder().setRowCountExact(1L)).build()).setStatus(com.google.rpc.Status.newBuilder().setCode(Code.OK_VALUE).build()).build();
    when(rpc.executeBatchDml(Mockito.any(ExecuteBatchDmlRequest.class), Mockito.anyMap())).thenReturn(response1, response2);
    CommitResponse commitResponse = CommitResponse.newBuilder().setCommitTimestamp(Timestamp.getDefaultInstance()).build();
    when(rpc.commitAsync(Mockito.any(CommitRequest.class), Mockito.anyMap())).thenReturn(ApiFutures.immediateFuture(commitResponse));
    final Statement statement = Statement.of("UPDATE FOO SET BAR=1");
    final AtomicInteger numCalls = new AtomicInteger(0);
    long[] updateCount = runner.run(transaction1 -> {
        numCalls.incrementAndGet();
        return transaction1.batchUpdate(Arrays.asList(statement, statement));
    });
    if (status == Code.ABORTED_VALUE) {
        // Assert that the method ran twice because the first response aborted.
        assertThat(numCalls.get()).isEqualTo(2);
    }
    return updateCount;
}
Also used : TransactionContextImpl(com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl) CommitRequest(com.google.spanner.v1.CommitRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CommitResponse(com.google.spanner.v1.CommitResponse) ExecuteBatchDmlResponse(com.google.spanner.v1.ExecuteBatchDmlResponse) Span(io.opencensus.trace.Span) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest)

Aggregations

TransactionContextImpl (com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl)11 Test (org.junit.Test)8 ByteString (com.google.protobuf.ByteString)3 CommitRequest (com.google.spanner.v1.CommitRequest)3 ExecuteBatchDmlRequest (com.google.spanner.v1.ExecuteBatchDmlRequest)3 SessionPoolTransactionContext (com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext)2 SpannerRpc (com.google.cloud.spanner.spi.v1.SpannerRpc)2 CommitResponse (com.google.spanner.v1.CommitResponse)2 ExecuteBatchDmlResponse (com.google.spanner.v1.ExecuteBatchDmlResponse)2 RollbackRequest (com.google.spanner.v1.RollbackRequest)2 Span (io.opencensus.trace.Span)2 ApiFutures (com.google.api.core.ApiFutures)1 Timestamp (com.google.cloud.Timestamp)1 NUM_IN_USE_SESSIONS (com.google.cloud.spanner.MetricRegistryConstants.NUM_IN_USE_SESSIONS)1 NUM_READ_SESSIONS (com.google.cloud.spanner.MetricRegistryConstants.NUM_READ_SESSIONS)1 NUM_SESSIONS_BEING_PREPARED (com.google.cloud.spanner.MetricRegistryConstants.NUM_SESSIONS_BEING_PREPARED)1 NUM_WRITE_SESSIONS (com.google.cloud.spanner.MetricRegistryConstants.NUM_WRITE_SESSIONS)1 SPANNER_LABEL_KEYS (com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS)1 SPANNER_LABEL_KEYS_WITH_TYPE (com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS_WITH_TYPE)1 FakeMetricRegistry (com.google.cloud.spanner.MetricRegistryTestUtils.FakeMetricRegistry)1