Search in sources :

Example 1 with READ_COLUMN_NAMES

use of com.google.cloud.spanner.MockSpannerTestUtil.READ_COLUMN_NAMES in project java-spanner by googleapis.

the class AsyncTransactionManagerTest method asyncTransactionManagerChain.

@Test
public void asyncTransactionManagerChain() throws Exception {
    try (AsyncTransactionManager manager = client().transactionManagerAsync()) {
        TransactionContextFuture transactionContextFuture = manager.beginAsync();
        while (true) {
            try {
                CommitTimestampFuture commitTimestamp = transactionContextFuture.then((transaction, ignored) -> transaction.executeUpdateAsync(UPDATE_STATEMENT), executor).then((transactionContext, ignored) -> transactionContext.readRowAsync(READ_TABLE_NAME, Key.of(1L), READ_COLUMN_NAMES), executor).then((ignored, input) -> ApiFutures.immediateFuture(input.getString("Value")), executor).then((ignored, input) -> {
                    assertThat(input).isEqualTo("v1");
                    return ApiFutures.immediateFuture(null);
                }, executor).commitAsync();
                assertThat(commitTimestamp.get()).isNotNull();
                break;
            } catch (AbortedException e) {
                transactionContextFuture = manager.resetForRetryAsync();
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) BatchCreateSessionsRequest(com.google.spanner.v1.BatchCreateSessionsRequest) TimeoutException(java.util.concurrent.TimeoutException) StatementResult(com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UPDATE_STATEMENT(com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_STATEMENT) TransactionSelector(com.google.spanner.v1.TransactionSelector) Status(io.grpc.Status) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) Parameterized(org.junit.runners.Parameterized) CommitTimestampFuture(com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture) ApiFutures(com.google.api.core.ApiFutures) SpannerApiFutures.get(com.google.cloud.spanner.SpannerApiFutures.get) Collection(java.util.Collection) READ_COLUMN_NAMES(com.google.cloud.spanner.MockSpannerTestUtil.READ_COLUMN_NAMES) Range(com.google.common.collect.Range) ApiFutureCallback(com.google.api.core.ApiFutureCallback) Executors(java.util.concurrent.Executors) ApiFuture(com.google.api.core.ApiFuture) SettableApiFuture(com.google.api.core.SettableApiFuture) UPDATE_COUNT(com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_COUNT) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) Iterables(com.google.common.collect.Iterables) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) INVALID_UPDATE_STATEMENT(com.google.cloud.spanner.MockSpannerTestUtil.INVALID_UPDATE_STATEMENT) READ_TABLE_NAME(com.google.cloud.spanner.MockSpannerTestUtil.READ_TABLE_NAME) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) CommitRequest(com.google.spanner.v1.CommitRequest) ImmutableList(com.google.common.collect.ImmutableList) SimulatedExecutionTime(com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime) TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) AbstractMessage(com.google.protobuf.AbstractMessage) Executor(java.util.concurrent.Executor) Assert.assertNotNull(org.junit.Assert.assertNotNull) Parameter(org.junit.runners.Parameterized.Parameter) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) RollbackRequest(com.google.spanner.v1.RollbackRequest) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ReadOption(com.google.cloud.spanner.Options.ReadOption) AsyncTransactionFunction(com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionFunction) AsyncTransactionStep(com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionStep) SessionPoolTransactionContext(com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext) UPDATE_ABORTED_STATEMENT(com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_ABORTED_STATEMENT) TransactionContextImpl(com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) CommitTimestampFuture(com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture) Test(org.junit.Test)

Example 2 with READ_COLUMN_NAMES

use of com.google.cloud.spanner.MockSpannerTestUtil.READ_COLUMN_NAMES 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();
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) BatchCreateSessionsRequest(com.google.spanner.v1.BatchCreateSessionsRequest) TimeoutException(java.util.concurrent.TimeoutException) StatementResult(com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UPDATE_STATEMENT(com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_STATEMENT) TransactionSelector(com.google.spanner.v1.TransactionSelector) Status(io.grpc.Status) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) Parameterized(org.junit.runners.Parameterized) CommitTimestampFuture(com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture) ApiFutures(com.google.api.core.ApiFutures) SpannerApiFutures.get(com.google.cloud.spanner.SpannerApiFutures.get) Collection(java.util.Collection) READ_COLUMN_NAMES(com.google.cloud.spanner.MockSpannerTestUtil.READ_COLUMN_NAMES) Range(com.google.common.collect.Range) ApiFutureCallback(com.google.api.core.ApiFutureCallback) Executors(java.util.concurrent.Executors) ApiFuture(com.google.api.core.ApiFuture) SettableApiFuture(com.google.api.core.SettableApiFuture) UPDATE_COUNT(com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_COUNT) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) Iterables(com.google.common.collect.Iterables) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) INVALID_UPDATE_STATEMENT(com.google.cloud.spanner.MockSpannerTestUtil.INVALID_UPDATE_STATEMENT) READ_TABLE_NAME(com.google.cloud.spanner.MockSpannerTestUtil.READ_TABLE_NAME) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) CommitRequest(com.google.spanner.v1.CommitRequest) ImmutableList(com.google.common.collect.ImmutableList) SimulatedExecutionTime(com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime) TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) AbstractMessage(com.google.protobuf.AbstractMessage) Executor(java.util.concurrent.Executor) Assert.assertNotNull(org.junit.Assert.assertNotNull) Parameter(org.junit.runners.Parameterized.Parameter) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) RollbackRequest(com.google.spanner.v1.RollbackRequest) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ReadOption(com.google.cloud.spanner.Options.ReadOption) AsyncTransactionFunction(com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionFunction) AsyncTransactionStep(com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionStep) SessionPoolTransactionContext(com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext) UPDATE_ABORTED_STATEMENT(com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_ABORTED_STATEMENT) TransactionContextImpl(com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) Test(org.junit.Test)

Example 3 with READ_COLUMN_NAMES

use of com.google.cloud.spanner.MockSpannerTestUtil.READ_COLUMN_NAMES in project java-spanner by googleapis.

the class AsyncTransactionManagerTest method asyncTransactionManagerRead.

@Test
public void asyncTransactionManagerRead() throws Exception {
    try (AsyncTransactionManager manager = client().transactionManagerAsync()) {
        TransactionContextFuture transactionContextFuture = manager.beginAsync();
        while (true) {
            try {
                AsyncTransactionStep<Void, List<String>> values = transactionContextFuture.then((transactionContext, ignored) -> transactionContext.readAsync(READ_TABLE_NAME, KeySet.all(), READ_COLUMN_NAMES).toListAsync(input -> input.getString("Value"), MoreExecutors.directExecutor()), executor);
                // Commit the transaction.
                values.commitAsync().get();
                assertThat(values.get()).containsExactly("v1", "v2", "v3");
                break;
            } catch (AbortedException e) {
                transactionContextFuture = manager.resetForRetryAsync();
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) BatchCreateSessionsRequest(com.google.spanner.v1.BatchCreateSessionsRequest) TimeoutException(java.util.concurrent.TimeoutException) StatementResult(com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UPDATE_STATEMENT(com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_STATEMENT) TransactionSelector(com.google.spanner.v1.TransactionSelector) Status(io.grpc.Status) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) Parameterized(org.junit.runners.Parameterized) CommitTimestampFuture(com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture) ApiFutures(com.google.api.core.ApiFutures) SpannerApiFutures.get(com.google.cloud.spanner.SpannerApiFutures.get) Collection(java.util.Collection) READ_COLUMN_NAMES(com.google.cloud.spanner.MockSpannerTestUtil.READ_COLUMN_NAMES) Range(com.google.common.collect.Range) ApiFutureCallback(com.google.api.core.ApiFutureCallback) Executors(java.util.concurrent.Executors) ApiFuture(com.google.api.core.ApiFuture) SettableApiFuture(com.google.api.core.SettableApiFuture) UPDATE_COUNT(com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_COUNT) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) Iterables(com.google.common.collect.Iterables) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) INVALID_UPDATE_STATEMENT(com.google.cloud.spanner.MockSpannerTestUtil.INVALID_UPDATE_STATEMENT) READ_TABLE_NAME(com.google.cloud.spanner.MockSpannerTestUtil.READ_TABLE_NAME) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) CommitRequest(com.google.spanner.v1.CommitRequest) ImmutableList(com.google.common.collect.ImmutableList) SimulatedExecutionTime(com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime) TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) AbstractMessage(com.google.protobuf.AbstractMessage) Executor(java.util.concurrent.Executor) Assert.assertNotNull(org.junit.Assert.assertNotNull) Parameter(org.junit.runners.Parameterized.Parameter) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) RollbackRequest(com.google.spanner.v1.RollbackRequest) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ReadOption(com.google.cloud.spanner.Options.ReadOption) AsyncTransactionFunction(com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionFunction) AsyncTransactionStep(com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionStep) SessionPoolTransactionContext(com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext) UPDATE_ABORTED_STATEMENT(com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_ABORTED_STATEMENT) TransactionContextImpl(com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) Test(org.junit.Test)

Aggregations

ApiFuture (com.google.api.core.ApiFuture)3 ApiFutureCallback (com.google.api.core.ApiFutureCallback)3 ApiFutures (com.google.api.core.ApiFutures)3 SettableApiFuture (com.google.api.core.SettableApiFuture)3 AsyncTransactionFunction (com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionFunction)3 AsyncTransactionStep (com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionStep)3 CommitTimestampFuture (com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture)3 TransactionContextFuture (com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture)3 SimulatedExecutionTime (com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime)3 StatementResult (com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult)3 INVALID_UPDATE_STATEMENT (com.google.cloud.spanner.MockSpannerTestUtil.INVALID_UPDATE_STATEMENT)3 READ_COLUMN_NAMES (com.google.cloud.spanner.MockSpannerTestUtil.READ_COLUMN_NAMES)3 READ_TABLE_NAME (com.google.cloud.spanner.MockSpannerTestUtil.READ_TABLE_NAME)3 UPDATE_ABORTED_STATEMENT (com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_ABORTED_STATEMENT)3 UPDATE_COUNT (com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_COUNT)3 UPDATE_STATEMENT (com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_STATEMENT)3 ReadOption (com.google.cloud.spanner.Options.ReadOption)3 SessionPoolTransactionContext (com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext)3 SpannerApiFutures.get (com.google.cloud.spanner.SpannerApiFutures.get)3 TransactionContextImpl (com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl)3