use of com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture in project java-spanner by googleapis.
the class AsyncTransactionManagerTest method asyncTransactionManagerBatchUpdateAborted.
@Test
public void asyncTransactionManagerBatchUpdateAborted() throws Exception {
final AtomicInteger attempt = new AtomicInteger();
try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
TransactionContextFuture transactionContextFuture = manager.beginAsync();
while (true) {
try {
transactionContextFuture.then((transaction, ignored) -> {
if (attempt.incrementAndGet() == 1) {
return transaction.batchUpdateAsync(ImmutableList.of(UPDATE_STATEMENT, UPDATE_ABORTED_STATEMENT));
} else {
return transaction.batchUpdateAsync(ImmutableList.of(UPDATE_STATEMENT, UPDATE_STATEMENT));
}
}, executor).commitAsync().get();
break;
} catch (AbortedException e) {
transactionContextFuture = manager.resetForRetryAsync();
}
}
}
assertThat(attempt.get()).isEqualTo(2);
// There should only be 1 CommitRequest, as the first attempt should abort already after the
// ExecuteBatchDmlRequest.
assertThat(mockSpanner.getRequestTypes()).containsExactly(BatchCreateSessionsRequest.class, ExecuteBatchDmlRequest.class, BeginTransactionRequest.class, ExecuteBatchDmlRequest.class, CommitRequest.class);
}
use of com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture in project java-spanner by googleapis.
the class AsyncTransactionManagerTest method asyncTransactionManagerWithBatchUpdateCommitFails.
@Test
public void asyncTransactionManagerWithBatchUpdateCommitFails() throws Exception {
mockSpanner.setCommitExecutionTime(SimulatedExecutionTime.ofException(Status.RESOURCE_EXHAUSTED.withDescription("mutation limit exceeded").asRuntimeException()));
try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
TransactionContextFuture transactionContextFuture = manager.beginAsync();
SpannerException e = assertThrows(SpannerException.class, () -> get(transactionContextFuture.then((transactionContext, ignored) -> transactionContext.batchUpdateAsync(ImmutableList.of(UPDATE_STATEMENT, UPDATE_STATEMENT)), executor).commitAsync()));
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.RESOURCE_EXHAUSTED);
assertThat(e.getMessage()).contains("mutation limit exceeded");
}
assertThat(mockSpanner.getRequestTypes()).containsExactly(BatchCreateSessionsRequest.class, ExecuteBatchDmlRequest.class, CommitRequest.class);
}
use of com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture in project java-spanner by googleapis.
the class AsyncTransactionManagerTest method asyncTransactionManagerCommitFails.
@Test
public void asyncTransactionManagerCommitFails() throws Exception {
mockSpanner.setCommitExecutionTime(SimulatedExecutionTime.ofException(Status.RESOURCE_EXHAUSTED.withDescription("mutation limit exceeded").asRuntimeException()));
try (AsyncTransactionManager mgr = client().transactionManagerAsync()) {
TransactionContextFuture txn = mgr.beginAsync();
SpannerException e = assertThrows(SpannerException.class, () -> get(txn.then(AsyncTransactionManagerHelper.executeUpdateAsync(UPDATE_STATEMENT), executor).commitAsync()));
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.RESOURCE_EXHAUSTED);
assertThat(e.getMessage()).contains("mutation limit exceeded");
}
}
use of com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture in project java-spanner by googleapis.
the class AsyncTransactionManagerTest method asyncTransactionManagerReadRow.
@Test
public void asyncTransactionManagerReadRow() throws Exception {
try (AsyncTransactionManager manager = client().transactionManagerAsync()) {
TransactionContextFuture transactionContextFuture = manager.beginAsync();
while (true) {
try {
AsyncTransactionStep<Struct, String> value = transactionContextFuture.then((transactionContext, ignored) -> transactionContext.readRowAsync(READ_TABLE_NAME, Key.of(1L), READ_COLUMN_NAMES), executor).then((ignored, input) -> ApiFutures.immediateFuture(input.getString("Value")), executor);
value.commitAsync().get();
assertThat(value.get()).isEqualTo("v1");
break;
} catch (AbortedException e) {
transactionContextFuture = manager.resetForRetryAsync();
}
}
}
}
use of com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture in project java-spanner by googleapis.
the class AsyncTransactionManagerTest method asyncTransactionManagerBatchUpdateAbortedBeforeFirstStatement.
@Test
public void asyncTransactionManagerBatchUpdateAbortedBeforeFirstStatement() throws Exception {
final AtomicInteger attempt = new AtomicInteger();
try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
TransactionContextFuture transactionContextFuture = manager.beginAsync();
while (true) {
try {
transactionContextFuture.then((transactionContext, ignored) -> {
if (attempt.incrementAndGet() == 1) {
mockSpanner.abortNextStatement();
}
return transactionContext.batchUpdateAsync(ImmutableList.of(UPDATE_STATEMENT, UPDATE_STATEMENT));
}, executor).commitAsync().get();
break;
} catch (AbortedException e) {
transactionContextFuture = manager.resetForRetryAsync();
}
}
}
assertThat(attempt.get()).isEqualTo(2);
// There should only be 1 CommitRequest, as the first attempt should abort already after the
// ExecuteBatchDmlRequest.
assertThat(mockSpanner.getRequestTypes()).containsExactly(BatchCreateSessionsRequest.class, ExecuteBatchDmlRequest.class, BeginTransactionRequest.class, ExecuteBatchDmlRequest.class, CommitRequest.class);
}
Aggregations