Skip to content
This repository was archived by the owner on Apr 7, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ void checkLocalConnection(ConnectionOptions options) {
InstanceAdminStubSettings.newBuilder()
.setCredentialsProvider(NoCredentialsProvider.create())
.setTransportChannelProvider(
InstantiatingGrpcChannelProvider.newBuilder().setEndpoint(host).build());
InstantiatingGrpcChannelProvider.newBuilder()
.setEndpoint(host)
.setChannelConfigurator(
input -> {
input.usePlaintext();
return input;
})
.build());
testEmulatorSettings
.listInstanceConfigsSettings()
.setSimpleTimeoutNoRetries(Duration.ofSeconds(10L));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.Arrays;

public class MockSpannerTestUtil {
static final Statement SELECT1 = Statement.of("SELECT 1 AS COL1");
public static final Statement SELECT1 = Statement.of("SELECT 1 AS COL1");
private static final ResultSetMetadata SELECT1_METADATA =
ResultSetMetadata.newBuilder()
.setRowType(
Expand All @@ -41,7 +41,7 @@ public class MockSpannerTestUtil {
.build())
.build())
.build();
static final com.google.spanner.v1.ResultSet SELECT1_RESULTSET =
public static final com.google.spanner.v1.ResultSet SELECT1_RESULTSET =
com.google.spanner.v1.ResultSet.newBuilder()
.addRows(
ListValue.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.spanner.connection;

import static com.google.cloud.spanner.MockSpannerTestUtil.SELECT1;
import static com.google.cloud.spanner.MockSpannerTestUtil.SELECT1_RESULTSET;

import com.google.cloud.spanner.MockSpannerServiceImpl;
import com.google.cloud.spanner.ResultSet;
import io.grpc.Server;
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
import java.net.InetSocketAddress;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class LocalConnectionCheckerTest {

private MockSpannerServiceImpl mockSpanner;
private Server server;
private InetSocketAddress address;

@Before
public void setUp() throws Exception {
mockSpanner = new MockSpannerServiceImpl();
mockSpanner.setAbortProbability(0.0D); // We don't want any unpredictable aborted transactions.
address = new InetSocketAddress("localhost", 0);
server = NettyServerBuilder.forAddress(address).addService(mockSpanner).build();
server.start();
}

@After
public void tearDown() throws Exception {
server.shutdown();
server.awaitTermination();
}

@Test
public void localConnectionCheckerWorksWithMockSpanner() {
final String uri =
String.format(
"cloudspanner://localhost:%d/projects/proj/instances/inst/databases/db?usePlainText=true",
server.getPort());
final ConnectionOptions connectionOptions = ConnectionOptions.newBuilder().setUri(uri).build();
mockSpanner.putStatementResult(
MockSpannerServiceImpl.StatementResult.query(SELECT1, SELECT1_RESULTSET));

try (Connection connection = connectionOptions.getConnection();
ResultSet resultSet = connection.executeQuery(SELECT1)) {
while (resultSet.next()) {}
}
}
}