|
42 | 42 | import com.google.cloud.spanner.SessionPool.Clock;
|
43 | 43 | import com.google.cloud.spanner.SessionPool.PooledSession;
|
44 | 44 | import com.google.cloud.spanner.SessionPool.SessionConsumerImpl;
|
| 45 | +import com.google.cloud.spanner.SpannerImpl.ClosedException; |
45 | 46 | import com.google.cloud.spanner.TransactionRunner.TransactionCallable;
|
46 | 47 | import com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl;
|
47 | 48 | import com.google.cloud.spanner.spi.v1.SpannerRpc;
|
|
58 | 59 | import com.google.spanner.v1.RollbackRequest;
|
59 | 60 | import io.opencensus.metrics.LabelValue;
|
60 | 61 | import io.opencensus.metrics.MetricRegistry;
|
| 62 | +import java.io.PrintWriter; |
| 63 | +import java.io.StringWriter; |
61 | 64 | import java.util.ArrayList;
|
62 | 65 | import java.util.Arrays;
|
63 | 66 | import java.util.Collection;
|
@@ -165,6 +168,26 @@ public void run() {
|
165 | 168 | Mockito.anyInt(), Mockito.anyBoolean(), any(SessionConsumer.class));
|
166 | 169 | }
|
167 | 170 |
|
| 171 | +@Test |
| 172 | +public void testClosedPoolIncludesClosedException() { |
| 173 | +pool = createPool(); |
| 174 | +assertThat(pool.isValid()).isTrue(); |
| 175 | +closePoolWithStacktrace(); |
| 176 | +try { |
| 177 | +pool.getReadSession(); |
| 178 | +fail("missing expected exception"); |
| 179 | +} catch (IllegalStateException e) { |
| 180 | +assertThat(e.getCause()).isInstanceOf(ClosedException.class); |
| 181 | +StringWriter sw = new StringWriter(); |
| 182 | +e.getCause().printStackTrace(new PrintWriter(sw)); |
| 183 | +assertThat(sw.toString()).contains("closePoolWithStacktrace"); |
| 184 | +} |
| 185 | +} |
| 186 | + |
| 187 | +private void closePoolWithStacktrace() { |
| 188 | +pool.closeAsync(new SpannerImpl.ClosedException()); |
| 189 | +} |
| 190 | + |
168 | 191 | @Test
|
169 | 192 | public void sessionCreation() {
|
170 | 193 | setupMockSessionCreation();
|
@@ -203,7 +226,7 @@ public void poolLifo() {
|
203 | 226 | public void poolClosure() throws Exception {
|
204 | 227 | setupMockSessionCreation();
|
205 | 228 | pool = createPool();
|
206 |
| -pool.closeAsync().get(5L, TimeUnit.SECONDS); |
| 229 | +pool.closeAsync(new SpannerImpl.ClosedException()).get(5L, TimeUnit.SECONDS); |
207 | 230 | }
|
208 | 231 |
|
209 | 232 | @Test
|
@@ -237,7 +260,7 @@ public void run() {
|
237 | 260 | // Clear the exception to suppress logging of expected exceptions.
|
238 | 261 | Session.clearException();
|
239 | 262 | session1.close();
|
240 |
| -pool.closeAsync().get(5L, TimeUnit.SECONDS); |
| 263 | +pool.closeAsync(new SpannerImpl.ClosedException()).get(5L, TimeUnit.SECONDS); |
241 | 264 | verify(mockSession1).asyncClose();
|
242 | 265 | verify(mockSession2).asyncClose();
|
243 | 266 | }
|
@@ -260,7 +283,7 @@ public void run() {
|
260 | 283 | }
|
261 | 284 | })
|
262 | 285 | .start();
|
263 |
| -pool.closeAsync().get(5L, TimeUnit.SECONDS); |
| 286 | +pool.closeAsync(new SpannerImpl.ClosedException()).get(5L, TimeUnit.SECONDS); |
264 | 287 | stop.set(true);
|
265 | 288 | }
|
266 | 289 |
|
@@ -316,7 +339,7 @@ public Void call() throws Exception {
|
316 | 339 | CountDownLatch latch = new CountDownLatch(1);
|
317 | 340 | getSessionAsync(latch, failed);
|
318 | 341 | insideCreation.await();
|
319 |
| -pool.closeAsync(); |
| 342 | +pool.closeAsync(new SpannerImpl.ClosedException()); |
320 | 343 | releaseCreation.countDown();
|
321 | 344 | latch.await();
|
322 | 345 | assertThat(failed.get()).isTrue();
|
@@ -374,7 +397,7 @@ public Void call() throws Exception {
|
374 | 397 | CountDownLatch latch = new CountDownLatch(1);
|
375 | 398 | getReadWriteSessionAsync(latch, failed);
|
376 | 399 | insideCreation.await();
|
377 |
| -pool.closeAsync(); |
| 400 | +pool.closeAsync(new SpannerImpl.ClosedException()); |
378 | 401 | releaseCreation.countDown();
|
379 | 402 | latch.await();
|
380 | 403 | assertThat(failed.get()).isTrue();
|
@@ -411,7 +434,7 @@ public Void call() throws Exception {
|
411 | 434 | CountDownLatch latch = new CountDownLatch(1);
|
412 | 435 | getSessionAsync(latch, failed);
|
413 | 436 | insideCreation.await();
|
414 |
| -ListenableFuture<Void> f = pool.closeAsync(); |
| 437 | +ListenableFuture<Void> f = pool.closeAsync(new SpannerImpl.ClosedException()); |
415 | 438 | releaseCreation.countDown();
|
416 | 439 | f.get();
|
417 | 440 | assertThat(f.isDone()).isTrue();
|
@@ -456,7 +479,7 @@ public Session answer(InvocationOnMock invocation) throws Throwable {
|
456 | 479 | CountDownLatch latch = new CountDownLatch(1);
|
457 | 480 | getReadWriteSessionAsync(latch, failed);
|
458 | 481 | insidePrepare.await();
|
459 |
| -ListenableFuture<Void> f = pool.closeAsync(); |
| 482 | +ListenableFuture<Void> f = pool.closeAsync(new SpannerImpl.ClosedException()); |
460 | 483 | releasePrepare.countDown();
|
461 | 484 | f.get();
|
462 | 485 | assertThat(f.isDone()).isTrue();
|
@@ -487,7 +510,7 @@ public void run() {
|
487 | 510 | PooledSession Session = pool.getReadSession();
|
488 | 511 | // Suppress expected Session warning.
|
489 | 512 | Session.clearException();
|
490 |
| -pool.closeAsync(); |
| 513 | +pool.closeAsync(new SpannerImpl.ClosedException()); |
491 | 514 | expectedException.expect(IllegalStateException.class);
|
492 | 515 | pool.getReadSession();
|
493 | 516 | }
|
@@ -925,7 +948,7 @@ public void run() {
|
925 | 948 | runMaintainanceLoop(clock, pool, cycles);
|
926 | 949 | // We will still close 2 sessions since at any point in time only 1 session was in use.
|
927 | 950 | assertThat(pool.numIdleSessionsRemoved()).isEqualTo(2L);
|
928 |
| -pool.closeAsync().get(5L, TimeUnit.SECONDS); |
| 951 | +pool.closeAsync(new SpannerImpl.ClosedException()).get(5L, TimeUnit.SECONDS); |
929 | 952 | }
|
930 | 953 |
|
931 | 954 | @Test
|
@@ -976,7 +999,7 @@ public void run() {
|
976 | 999 | // The session pool only keeps MinSessions + MaxIdleSessions alive.
|
977 | 1000 | verify(session, times(options.getMinSessions() + options.getMaxIdleSessions()))
|
978 | 1001 | .singleUse(any(TimestampBound.class));
|
979 |
| -pool.closeAsync().get(5L, TimeUnit.SECONDS); |
| 1002 | +pool.closeAsync(new SpannerImpl.ClosedException()).get(5L, TimeUnit.SECONDS); |
980 | 1003 | }
|
981 | 1004 |
|
982 | 1005 | @Test
|
@@ -1061,7 +1084,7 @@ public void run() {
|
1061 | 1084 | assertThat(pool.getNumberOfAvailableWritePreparedSessions())
|
1062 | 1085 | .isEqualTo((int) Math.ceil(options.getMinSessions() * options.getWriteSessionsFraction()));
|
1063 | 1086 |
|
1064 |
| -pool.closeAsync().get(5L, TimeUnit.SECONDS); |
| 1087 | +pool.closeAsync(new SpannerImpl.ClosedException()).get(5L, TimeUnit.SECONDS); |
1065 | 1088 | }
|
1066 | 1089 |
|
1067 | 1090 | private void waitForExpectedSessionPool(int expectedSessions, float writeFraction)
|
@@ -1447,7 +1470,7 @@ public Integer run(TransactionContext transaction) throws Exception {
|
1447 | 1470 | .isTrue();
|
1448 | 1471 | }
|
1449 | 1472 | }
|
1450 |
| -pool.closeAsync(); |
| 1473 | +pool.closeAsync(new SpannerImpl.ClosedException()); |
1451 | 1474 | }
|
1452 | 1475 | }
|
1453 | 1476 | }
|
|
0 commit comments