Search in sources :

Example 1 with SpannerApiFutures.get

use of com.google.cloud.spanner.SpannerApiFutures.get in project java-spanner by googleapis.

the class ConnectionAsyncApiTest method testExecuteQueryAsync.

private void testExecuteQueryAsync(Function<Connection, Void> connectionConfigurator, boolean executeAsStatement) {
    ApiFuture<Void> res;
    try (Connection connection = createConnection()) {
        connectionConfigurator.apply(connection);
        for (boolean timeout : new boolean[] { true, false }) {
            final AtomicInteger rowCount = new AtomicInteger();
            final AtomicBoolean receivedTimeout = new AtomicBoolean();
            if (timeout) {
                mockSpanner.setExecuteStreamingSqlExecutionTime(SimulatedExecutionTime.ofMinimumAndRandomTime(10, 0));
                connection.setStatementTimeout(1L, TimeUnit.NANOSECONDS);
            } else {
                mockSpanner.removeAllExecutionTimes();
                connection.clearStatementTimeout();
            }
            try (AsyncResultSet rs = executeAsStatement ? connection.executeAsync(SELECT_RANDOM_STATEMENT).getResultSetAsync() : connection.executeQueryAsync(SELECT_RANDOM_STATEMENT)) {
                res = rs.setCallback(executor, resultSet -> {
                    try {
                        while (true) {
                            switch(resultSet.tryNext()) {
                                case OK:
                                    rowCount.incrementAndGet();
                                    break;
                                case DONE:
                                    return CallbackResponse.DONE;
                                case NOT_READY:
                                    return CallbackResponse.CONTINUE;
                            }
                        }
                    } catch (SpannerException e) {
                        receivedTimeout.set(e.getErrorCode() == ErrorCode.DEADLINE_EXCEEDED);
                        throw e;
                    }
                });
            }
            try {
                SpannerApiFutures.get(res);
                assertThat(rowCount.get()).isEqualTo(RANDOM_RESULT_SET_ROW_COUNT);
                if (connection.isReadOnly() || !connection.isInTransaction()) {
                    assertThat(connection.getReadTimestamp()).isNotNull();
                }
                assertThat(timeout).isFalse();
            } catch (SpannerException e) {
                assertThat(e.getSuppressed()).hasLength(1);
                assertThat(e.getSuppressed()[0].getMessage()).contains(SELECT_RANDOM_STATEMENT.getSql());
                assertThat(e.getErrorCode()).isEqualTo(ErrorCode.DEADLINE_EXCEEDED);
                assertThat(timeout).isTrue();
                assertThat(receivedTimeout.get()).isTrue();
                // invalidate that transaction.
                if (!connection.isReadOnly() && connection.isInTransaction()) {
                    connection.clearStatementTimeout();
                    connection.rollback();
                }
            }
        }
    }
}
Also used : CheckAndCloseSpannersMode(com.google.cloud.spanner.connection.SpannerPool.CheckAndCloseSpannersMode) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Collections2(com.google.common.collect.Collections2) ResultSet(com.google.cloud.spanner.ResultSet) CommitRequest(com.google.spanner.v1.CommitRequest) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Assert.fail(org.junit.Assert.fail) SimulatedExecutionTime(com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) AfterClass(org.junit.AfterClass) Function(com.google.common.base.Function) CallbackResponse(com.google.cloud.spanner.AsyncResultSet.CallbackResponse) AbstractMessage(com.google.protobuf.AbstractMessage) SpannerApiFutures.get(com.google.cloud.spanner.SpannerApiFutures.get) Test(org.junit.Test) ReadyCallback(com.google.cloud.spanner.AsyncResultSet.ReadyCallback) Mutation(com.google.cloud.spanner.Mutation) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) ForceCloseSpannerFunction(com.google.cloud.spanner.ForceCloseSpannerFunction) Executors(java.util.concurrent.Executors) ApiFuture(com.google.api.core.ApiFuture) SettableApiFuture(com.google.api.core.SettableApiFuture) ErrorCode(com.google.cloud.spanner.ErrorCode) SpannerException(com.google.cloud.spanner.SpannerException) Statement(com.google.cloud.spanner.Statement) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) ResultType(com.google.cloud.spanner.connection.StatementResult.ResultType) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) SpannerApiFutures(com.google.cloud.spanner.SpannerApiFutures) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) SpannerException(com.google.cloud.spanner.SpannerException)

Example 2 with SpannerApiFutures.get

use of com.google.cloud.spanner.SpannerApiFutures.get in project java-spanner by googleapis.

the class ConnectionAsyncApiTest method testExecuteQueryAsyncIsNonBlocking.

private void testExecuteQueryAsyncIsNonBlocking(Function<Connection, Void> connectionConfigurator) {
    ApiFuture<Void> res;
    final AtomicInteger rowCount = new AtomicInteger();
    mockSpanner.freeze();
    try (Connection connection = createConnection()) {
        connectionConfigurator.apply(connection);
        try (AsyncResultSet rs = connection.executeQueryAsync(SELECT_RANDOM_STATEMENT)) {
            res = rs.setCallback(executor, resultSet -> {
                while (true) {
                    switch(resultSet.tryNext()) {
                        case OK:
                            rowCount.incrementAndGet();
                            break;
                        case DONE:
                            return CallbackResponse.DONE;
                        case NOT_READY:
                            return CallbackResponse.CONTINUE;
                    }
                }
            });
            mockSpanner.unfreeze();
        }
        SpannerApiFutures.get(res);
        assertThat(rowCount.get()).isEqualTo(RANDOM_RESULT_SET_ROW_COUNT);
    }
}
Also used : CheckAndCloseSpannersMode(com.google.cloud.spanner.connection.SpannerPool.CheckAndCloseSpannersMode) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Collections2(com.google.common.collect.Collections2) ResultSet(com.google.cloud.spanner.ResultSet) CommitRequest(com.google.spanner.v1.CommitRequest) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Assert.fail(org.junit.Assert.fail) SimulatedExecutionTime(com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) AfterClass(org.junit.AfterClass) Function(com.google.common.base.Function) CallbackResponse(com.google.cloud.spanner.AsyncResultSet.CallbackResponse) AbstractMessage(com.google.protobuf.AbstractMessage) SpannerApiFutures.get(com.google.cloud.spanner.SpannerApiFutures.get) Test(org.junit.Test) ReadyCallback(com.google.cloud.spanner.AsyncResultSet.ReadyCallback) Mutation(com.google.cloud.spanner.Mutation) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) ForceCloseSpannerFunction(com.google.cloud.spanner.ForceCloseSpannerFunction) Executors(java.util.concurrent.Executors) ApiFuture(com.google.api.core.ApiFuture) SettableApiFuture(com.google.api.core.SettableApiFuture) ErrorCode(com.google.cloud.spanner.ErrorCode) SpannerException(com.google.cloud.spanner.SpannerException) Statement(com.google.cloud.spanner.Statement) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) ResultType(com.google.cloud.spanner.connection.StatementResult.ResultType) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) SpannerApiFutures(com.google.cloud.spanner.SpannerApiFutures) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet)

Aggregations

ApiFuture (com.google.api.core.ApiFuture)2 SettableApiFuture (com.google.api.core.SettableApiFuture)2 AsyncResultSet (com.google.cloud.spanner.AsyncResultSet)2 CallbackResponse (com.google.cloud.spanner.AsyncResultSet.CallbackResponse)2 ReadyCallback (com.google.cloud.spanner.AsyncResultSet.ReadyCallback)2 ErrorCode (com.google.cloud.spanner.ErrorCode)2 ForceCloseSpannerFunction (com.google.cloud.spanner.ForceCloseSpannerFunction)2 SimulatedExecutionTime (com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime)2 Mutation (com.google.cloud.spanner.Mutation)2 ResultSet (com.google.cloud.spanner.ResultSet)2 SpannerApiFutures (com.google.cloud.spanner.SpannerApiFutures)2 SpannerApiFutures.get (com.google.cloud.spanner.SpannerApiFutures.get)2 SpannerException (com.google.cloud.spanner.SpannerException)2 Statement (com.google.cloud.spanner.Statement)2 CheckAndCloseSpannersMode (com.google.cloud.spanner.connection.SpannerPool.CheckAndCloseSpannersMode)2 ResultType (com.google.cloud.spanner.connection.StatementResult.ResultType)2 Function (com.google.common.base.Function)2 Collections2 (com.google.common.collect.Collections2)2 ImmutableList (com.google.common.collect.ImmutableList)2 Lists (com.google.common.collect.Lists)2