use of com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture in project java-spanner by googleapis.
the class AsyncTransactionManagerTest method asyncTransactionManagerUpdate.
@Test
public void asyncTransactionManagerUpdate() throws Exception {
try (AsyncTransactionManager manager = client().transactionManagerAsync()) {
TransactionContextFuture transactionContextFuture = manager.beginAsync();
while (true) {
try {
AsyncTransactionStep<Void, Long> updateCount = transactionContextFuture.then((transactionContext, ignored) -> transactionContext.executeUpdateAsync(UPDATE_STATEMENT), executor);
CommitTimestampFuture commitTimestamp = updateCount.commitAsync();
assertThat(updateCount.get()).isEqualTo(UPDATE_COUNT);
assertThat(commitTimestamp.get()).isNotNull();
break;
} catch (AbortedException e) {
transactionContextFuture = manager.resetForRetryAsync();
}
}
}
}
use of com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture in project java-spanner by googleapis.
the class AsyncTransactionManagerTest method asyncTransactionManagerQuery.
@Test
public void asyncTransactionManagerQuery() throws Exception {
mockSpanner.putStatementResult(StatementResult.query(Statement.of("SELECT FirstName FROM Singers WHERE ID=1"), MockSpannerTestUtil.READ_FIRST_NAME_SINGERS_RESULTSET));
final long singerId = 1L;
try (AsyncTransactionManager manager = client().transactionManagerAsync()) {
TransactionContextFuture transactionContextFuture = manager.beginAsync();
while (true) {
final String column = "FirstName";
CommitTimestampFuture commitTimestamp = transactionContextFuture.then((transactionContext, ignored) -> transactionContext.readRowAsync("Singers", Key.of(singerId), Collections.singleton(column)), executor).then((transaction, input) -> {
String name = input.getString(column);
return transaction.bufferAsync(Mutation.newUpdateBuilder("Singers").set(column).to(name.toUpperCase()).build());
}, executor).commitAsync();
try {
commitTimestamp.get();
break;
} catch (AbortedException e) {
transactionContextFuture = manager.resetForRetryAsync();
}
}
}
}
use of com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture in project java-spanner by googleapis.
the class AsyncTransactionManagerTest method asyncTransactionManagerIsNonBlocking.
@Test
public void asyncTransactionManagerIsNonBlocking() throws Exception {
mockSpanner.freeze();
try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
TransactionContextFuture transactionContextFuture = manager.beginAsync();
while (true) {
try {
AsyncTransactionStep<Void, Long> updateCount = transactionContextFuture.then((transactionContext, ignored) -> transactionContext.executeUpdateAsync(UPDATE_STATEMENT), executor);
CommitTimestampFuture commitTimestamp = updateCount.commitAsync();
mockSpanner.unfreeze();
assertThat(updateCount.get(10L, TimeUnit.SECONDS)).isEqualTo(UPDATE_COUNT);
assertThat(commitTimestamp.get(10L, TimeUnit.SECONDS)).isNotNull();
break;
} catch (AbortedException e) {
transactionContextFuture = manager.resetForRetryAsync();
}
}
}
}
use of com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture in project java-spanner by googleapis.
the class AsyncTransactionManagerTest method asyncTransactionManagerCommitAborted.
@Test
public void asyncTransactionManagerCommitAborted() throws Exception {
final AtomicInteger attempt = new AtomicInteger();
CountDownLatch abortedLatch = new CountDownLatch(1);
try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
TransactionContextFuture transactionContextFuture = manager.beginAsync();
while (true) {
try {
attempt.incrementAndGet();
AsyncTransactionStep<Void, Long> updateCount = transactionContextFuture.then((transaction, ignored) -> transaction.executeUpdateAsync(UPDATE_STATEMENT), executor);
updateCount.then((transaction, ignored) -> {
if (attempt.get() == 1) {
mockSpanner.abortTransaction(transaction);
abortedLatch.countDown();
}
return ApiFutures.immediateFuture(null);
}, executor);
abortedLatch.await(10L, TimeUnit.SECONDS);
CommitTimestampFuture commitTimestamp = updateCount.commitAsync();
assertThat(updateCount.get()).isEqualTo(UPDATE_COUNT);
assertThat(commitTimestamp.get()).isNotNull();
assertThat(attempt.get()).isEqualTo(2);
break;
} catch (AbortedException e) {
transactionContextFuture = manager.resetForRetryAsync();
}
}
}
}
use of com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture 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));
}
}
Aggregations