Search in sources :

Example 21 with TransactionOptions

use of com.google.spanner.v1.TransactionOptions in project java-datastore by googleapis.

the class ITDatastoreTest method testRunInTransactionWithReadWriteOption.

@Test
public void testRunInTransactionWithReadWriteOption() {
    EasyMock.expect(rpcMock.beginTransaction(EasyMock.anyObject(BeginTransactionRequest.class))).andReturn(BeginTransactionResponse.getDefaultInstance());
    EasyMock.expect(rpcMock.rollback(EasyMock.anyObject(RollbackRequest.class))).andReturn(RollbackResponse.getDefaultInstance()).once();
    EasyMock.expect(rpcMock.beginTransaction(EasyMock.anyObject(BeginTransactionRequest.class))).andReturn(BeginTransactionResponse.getDefaultInstance());
    EasyMock.expect(rpcMock.commit(EasyMock.anyObject(CommitRequest.class))).andReturn(CommitResponse.newBuilder().build());
    EasyMock.replay(rpcFactoryMock, rpcMock);
    Datastore mockDatastore = rpcMockOptions.getService();
    Datastore.TransactionCallable<Integer> callable = new Datastore.TransactionCallable<Integer>() {

        private Integer attempts = 1;

        @Override
        public Integer run(DatastoreReaderWriter transaction) {
            if (attempts < 2) {
                ++attempts;
                throw new DatastoreException(10, "", "ABORTED", false, null);
            }
            return attempts;
        }
    };
    TransactionOptions options = TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.getDefaultInstance()).build();
    Integer result = mockDatastore.runInTransaction(callable, options);
    EasyMock.verify(rpcFactoryMock, rpcMock);
    assertEquals(2, result.intValue());
}
Also used : CommitRequest(com.google.datastore.v1.CommitRequest) BeginTransactionRequest(com.google.datastore.v1.BeginTransactionRequest) TransactionOptions(com.google.datastore.v1.TransactionOptions) Test(org.junit.Test)

Aggregations

TransactionOptions (com.google.spanner.v1.TransactionOptions)16 Test (org.junit.Test)13 TransactionSelector (com.google.spanner.v1.TransactionSelector)6 Session (com.google.spanner.v1.Session)5 ArrayList (java.util.ArrayList)5 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)4 AbstractMessage (com.google.protobuf.AbstractMessage)4 ByteString (com.google.protobuf.ByteString)4 Mutation (com.google.spanner.v1.Mutation)4 PartitionResponse (com.google.spanner.v1.PartitionResponse)4 SessionName (com.google.spanner.v1.SessionName)4 Transaction (com.google.spanner.v1.Transaction)4 StatusRuntimeException (io.grpc.StatusRuntimeException)4 TransactionOptions (com.google.datastore.v1.TransactionOptions)3 BeginTransactionRequest (com.google.spanner.v1.BeginTransactionRequest)3 CommitRequest (com.google.spanner.v1.CommitRequest)3 PartitionQueryRequest (com.google.spanner.v1.PartitionQueryRequest)3 BeginTransactionRequest (com.google.datastore.v1.BeginTransactionRequest)2 CommitRequest (com.google.datastore.v1.CommitRequest)2 GcpManagedChannel (com.google.grpc.gcp.GcpManagedChannel)2