use of com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_COUNT in project java-spanner by googleapis.
the class AsyncTransactionManagerTest method asyncTransactionManagerUpdateAborted.
@Test
public void asyncTransactionManagerUpdateAborted() throws Exception {
try (AsyncTransactionManager manager = client().transactionManagerAsync()) {
// Temporarily set the result of the update to 2 rows.
mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT + 1L));
final AtomicInteger attempt = new AtomicInteger();
TransactionContextFuture transactionContextFuture = manager.beginAsync();
while (true) {
try {
CommitTimestampFuture commitTimestampFuture = transactionContextFuture.then((ignored1, ignored2) -> {
if (attempt.incrementAndGet() == 1) {
// Abort the first attempt.
mockSpanner.abortNextStatement();
} else {
// Set the result of the update statement back to 1 row.
mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
}
return ApiFutures.immediateFuture(null);
}, executor).then((transactionContext, ignored) -> transactionContext.executeUpdateAsync(UPDATE_STATEMENT), executor).commitAsync();
assertThat(commitTimestampFuture.get()).isNotNull();
break;
} catch (AbortedException e) {
transactionContextFuture = manager.resetForRetryAsync();
}
}
assertThat(attempt.get()).isEqualTo(2);
} finally {
mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
}
}
use of com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_COUNT in project java-spanner by googleapis.
the class AsyncTransactionManagerTest method asyncTransactionManagerFireAndForgetInvalidBatchUpdate.
@Test
public void asyncTransactionManagerFireAndForgetInvalidBatchUpdate() throws Exception {
try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
TransactionContextFuture transactionContextFuture = manager.beginAsync();
while (true) {
try {
AsyncTransactionStep<Void, long[]> updateCounts = transactionContextFuture.then((transactionContext, ignored) -> {
transactionContext.batchUpdateAsync(ImmutableList.of(UPDATE_STATEMENT, INVALID_UPDATE_STATEMENT));
return ApiFutures.<Void>immediateFuture(null);
}, executor).then((transactionContext, ignored) -> transactionContext.batchUpdateAsync(ImmutableList.of(UPDATE_STATEMENT, UPDATE_STATEMENT)), executor);
updateCounts.commitAsync().get();
assertThat(updateCounts.get()).asList().containsExactly(UPDATE_COUNT, UPDATE_COUNT);
break;
} catch (AbortedException e) {
transactionContextFuture = manager.resetForRetryAsync();
}
}
}
assertThat(mockSpanner.getRequestTypes()).containsExactly(BatchCreateSessionsRequest.class, ExecuteBatchDmlRequest.class, ExecuteBatchDmlRequest.class, CommitRequest.class);
}
use of com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_COUNT in project java-spanner by googleapis.
the class AsyncTransactionManagerTest method asyncTransactionManagerWithBatchUpdateCommitAborted.
@Test
public void asyncTransactionManagerWithBatchUpdateCommitAborted() throws Exception {
try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
// Temporarily set the result of the update to 2 rows.
mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT + 1L));
final AtomicInteger attempt = new AtomicInteger();
TransactionContextFuture txn = manager.beginAsync();
while (true) {
try {
AsyncTransactionStep<Void, long[]> updateCounts = txn.then((ignored1, ignored2) -> {
if (attempt.get() > 0) {
// Set the result of the update statement back to 1 row.
mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
}
return ApiFutures.<Void>immediateFuture(null);
}, executor).then((transactionContext, ignored) -> transactionContext.batchUpdateAsync(ImmutableList.of(UPDATE_STATEMENT, UPDATE_STATEMENT)), executor);
updateCounts.then((transaction, ignored) -> {
if (attempt.incrementAndGet() == 1) {
mockSpanner.abortTransaction(transaction);
}
return ApiFutures.immediateFuture(null);
}, executor).commitAsync().get();
assertThat(updateCounts.get()).asList().containsExactly(UPDATE_COUNT, UPDATE_COUNT);
assertThat(attempt.get()).isEqualTo(2);
break;
} catch (AbortedException e) {
txn = manager.resetForRetryAsync();
}
}
} finally {
mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
}
assertThat(mockSpanner.getRequestTypes()).containsExactly(BatchCreateSessionsRequest.class, ExecuteBatchDmlRequest.class, CommitRequest.class, BeginTransactionRequest.class, ExecuteBatchDmlRequest.class, CommitRequest.class);
}
Aggregations