File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ void checkLocalConnection(ConnectionOptions options) {
5858
InstanceAdminStubSettings.newBuilder()
5959
.setCredentialsProvider(NoCredentialsProvider.create())
6060
.setTransportChannelProvider(
61-
InstantiatingGrpcChannelProvider.newBuilder().setEndpoint(host).build());
61+
InstantiatingGrpcChannelProvider.newBuilder()
62+
.setEndpoint(host)
63+
.setChannelConfigurator(
64+
input -> {
65+
input.usePlaintext();
66+
return input;
67+
})
68+
.build());
6269
testEmulatorSettings
6370
.listInstanceConfigsSettings()
6471
.setSimpleTimeoutNoRetries(Duration.ofSeconds(10L));
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import java.util.Arrays;
2727

2828
public class MockSpannerTestUtil {
29-
static final Statement SELECT1 = Statement.of("SELECT 1 AS COL1");
29+
public static final Statement SELECT1 = Statement.of("SELECT 1 AS COL1");
3030
private static final ResultSetMetadata SELECT1_METADATA =
3131
ResultSetMetadata.newBuilder()
3232
.setRowType(
@@ -41,7 +41,7 @@ public class MockSpannerTestUtil {
4141
.build())
4242
.build())
4343
.build();
44-
static final com.google.spanner.v1.ResultSet SELECT1_RESULTSET =
44+
public static final com.google.spanner.v1.ResultSet SELECT1_RESULTSET =
4545
com.google.spanner.v1.ResultSet.newBuilder()
4646
.addRows(
4747
ListValue.newBuilder()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.spanner.connection;
17+
18+
import static com.google.cloud.spanner.MockSpannerTestUtil.SELECT1;
19+
import static com.google.cloud.spanner.MockSpannerTestUtil.SELECT1_RESULTSET;
20+
21+
import com.google.cloud.spanner.MockSpannerServiceImpl;
22+
import com.google.cloud.spanner.ResultSet;
23+
import io.grpc.Server;
24+
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
25+
import java.net.InetSocketAddress;
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
30+
public class LocalConnectionCheckerTest {
31+
32+
private MockSpannerServiceImpl mockSpanner;
33+
private Server server;
34+
private InetSocketAddress address;
35+
36+
@Before
37+
public void setUp() throws Exception {
38+
mockSpanner = new MockSpannerServiceImpl();
39+
mockSpanner.setAbortProbability(0.0D); // We don't want any unpredictable aborted transactions.
40+
address = new InetSocketAddress("localhost", 0);
41+
server = NettyServerBuilder.forAddress(address).addService(mockSpanner).build();
42+
server.start();
43+
}
44+
45+
@After
46+
public void tearDown() throws Exception {
47+
server.shutdown();
48+
server.awaitTermination();
49+
}
50+
51+
@Test
52+
public void localConnectionCheckerWorksWithMockSpanner() {
53+
final String uri =
54+
String.format(
55+
"cloudspanner://localhost:%d/projects/proj/instances/inst/databases/db?usePlainText=true",
56+
server.getPort());
57+
final ConnectionOptions connectionOptions = ConnectionOptions.newBuilder().setUri(uri).build();
58+
mockSpanner.putStatementResult(
59+
MockSpannerServiceImpl.StatementResult.query(SELECT1, SELECT1_RESULTSET));
60+
61+
try (Connection connection = connectionOptions.getConnection();
62+
ResultSet resultSet = connection.executeQuery(SELECT1)) {
63+
while (resultSet.next()) {}
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)