|
17 | 17 | package com.google.cloud.spanner; |
18 | 18 |
|
19 | 19 | import static com.google.cloud.spanner.MockSpannerTestUtil.READ_ONE_KEY_VALUE_STATEMENT; |
| 20 | +import static com.google.cloud.spanner.MockSpannerTestUtil.TEST_DATABASE; |
| 21 | +import static com.google.cloud.spanner.MockSpannerTestUtil.TEST_INSTANCE; |
| 22 | +import static com.google.cloud.spanner.MockSpannerTestUtil.TEST_PROJECT; |
20 | 23 | import static com.google.common.truth.Truth.assertThat; |
| 24 | +import static org.junit.Assert.assertEquals; |
| 25 | +import static org.junit.Assert.assertThrows; |
21 | 26 | import static org.junit.Assert.assertTrue; |
| 27 | +import static org.mockito.Mockito.mock; |
| 28 | +import static org.mockito.Mockito.when; |
22 | 29 |
|
| 30 | +import com.google.cloud.NoCredentials; |
23 | 31 | import com.google.spanner.v1.BeginTransactionRequest; |
24 | 32 | import com.google.spanner.v1.ExecuteSqlRequest; |
| 33 | +import io.grpc.ManagedChannelBuilder; |
25 | 34 | import java.util.concurrent.CountDownLatch; |
26 | 35 | import java.util.concurrent.TimeUnit; |
27 | 36 | import org.junit.Test; |
@@ -148,4 +157,39 @@ public void testMultipleQueriesOnlyCallsBeginTransactionOnce() throws Exception |
148 | 157 | BeginTransactionRequest.class, ExecuteSqlRequest.class, ExecuteSqlRequest.class); |
149 | 158 | } |
150 | 159 | } |
| 160 | + |
| 161 | + @Test(timeout = 5000) |
| 162 | + public void createAsyncResultSet_handlesExceptionCorrectly() throws Exception { |
| 163 | + SpannerOptions.CloseableExecutorProvider mockExecutorProvider = |
| 164 | + mock(SpannerOptions.CloseableExecutorProvider.class); |
| 165 | + when(mockExecutorProvider.getExecutor()) |
| 166 | + .thenThrow(new RuntimeException("Failed to get executor")); |
| 167 | + |
| 168 | + String endpoint = address.getHostString() + ":" + server.getPort(); |
| 169 | + SpannerOptions options = |
| 170 | + SpannerOptions.newBuilder() |
| 171 | + .setProjectId(TEST_PROJECT) |
| 172 | + .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) |
| 173 | + .setHost("http://" + endpoint) |
| 174 | + .setCredentials(NoCredentials.getInstance()) |
| 175 | + .setAsyncExecutorProvider(mockExecutorProvider) |
| 176 | + .setSessionPoolOption( |
| 177 | + SessionPoolOptions.newBuilder() |
| 178 | + .setFailOnSessionLeak() |
| 179 | + .setWaitForMinSessions(org.threeten.bp.Duration.ofSeconds(2)) |
| 180 | + .build()) |
| 181 | + .build(); |
| 182 | + |
| 183 | + try (Spanner testSpanner = options.getService()) { |
| 184 | + DatabaseClient client = |
| 185 | + testSpanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE)); |
| 186 | + try (ReadOnlyTransaction transaction = client.readOnlyTransaction()) { |
| 187 | + RuntimeException e = |
| 188 | + assertThrows( |
| 189 | + RuntimeException.class, |
| 190 | + () -> transaction.executeQueryAsync(READ_ONE_KEY_VALUE_STATEMENT)); |
| 191 | + assertEquals("Failed to get executor", e.getMessage()); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
151 | 195 | } |
0 commit comments