|
| 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