Search in sources :

Example 1 with ForceCloseSpannerFunction

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

the class ConnectionAsyncApiTest method testExecuteBatchUpdate.

private void testExecuteBatchUpdate(Function<Connection, Void> connectionConfigurator) {
    try (Connection connection = createConnection()) {
        connectionConfigurator.apply(connection);
        for (boolean timeout : new boolean[] { true, false }) {
            if (timeout) {
                mockSpanner.setExecuteBatchDmlExecutionTime(SimulatedExecutionTime.ofMinimumAndRandomTime(10, 0));
                connection.setStatementTimeout(1L, TimeUnit.NANOSECONDS);
            } else {
                mockSpanner.removeAllExecutionTimes();
                connection.clearStatementTimeout();
            }
            try {
                long[] updateCounts = connection.executeBatchUpdate(ImmutableList.of(INSERT_STATEMENT, INSERT_STATEMENT));
                assertThat(updateCounts).asList().containsExactly(1L, 1L);
                if (connection.isInTransaction()) {
                    connection.commit();
                }
                assertThat(connection.getCommitTimestamp()).isNotNull();
                assertThat(timeout).isFalse();
            } catch (SpannerException e) {
                assertThat(e.getErrorCode()).isEqualTo(ErrorCode.DEADLINE_EXCEEDED);
                assertThat(timeout).isTrue();
                // invalidate that transaction.
                if (!connection.isReadOnly() && connection.isInTransaction()) {
                    connection.clearStatementTimeout();
                    connection.rollback();
                }
            }
        }
    }
    // Close the Spanner pool to prevent requests from this test from interfering with other tests.
    SpannerPool.INSTANCE.checkAndCloseSpanners(CheckAndCloseSpannersMode.ERROR, new ForceCloseSpannerFunction(100L, TimeUnit.MILLISECONDS));
}
Also used : ForceCloseSpannerFunction(com.google.cloud.spanner.ForceCloseSpannerFunction) SpannerException(com.google.cloud.spanner.SpannerException)

Example 2 with ForceCloseSpannerFunction

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

the class AbstractMockServerTest method stopServer.

@AfterClass
public static void stopServer() {
    try {
        SpannerPool.INSTANCE.checkAndCloseSpanners(CheckAndCloseSpannersMode.ERROR, new ForceCloseSpannerFunction(100L, TimeUnit.MILLISECONDS));
    } finally {
        Logger.getLogger(AbstractFuture.class.getName()).setUseParentHandlers(futureParentHandlers);
        Logger.getLogger(LogExceptionRunnable.class.getName()).setUseParentHandlers(exceptionRunnableParentHandlers);
        Logger.getLogger("io.grpc.netty.shaded.io.grpc.netty.NettyServerHandler").setUseParentHandlers(nettyServerParentHandlers);
        Logger.getLogger("io.grpc.internal.AbstractClientStream").setUseParentHandlers(clientStreamParentHandlers);
    }
    server.shutdown();
}
Also used : ForceCloseSpannerFunction(com.google.cloud.spanner.ForceCloseSpannerFunction) AfterClass(org.junit.AfterClass)

Aggregations

ForceCloseSpannerFunction (com.google.cloud.spanner.ForceCloseSpannerFunction)2 SpannerException (com.google.cloud.spanner.SpannerException)1 AfterClass (org.junit.AfterClass)1