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());
}
Aggregations