|
19 | 19 | import static org.hamcrest.CoreMatchers.is; |
20 | 20 | import static org.hamcrest.CoreMatchers.notNullValue; |
21 | 21 | import static org.hamcrest.MatcherAssert.assertThat; |
| 22 | +import static org.junit.Assert.assertNotNull; |
22 | 23 | import static org.junit.Assert.assertNull; |
23 | 24 | import static org.junit.Assert.assertTrue; |
24 | 25 |
|
@@ -70,24 +71,33 @@ public void testBulkCreateConnectionsSingleThreaded() { |
70 | 71 | @Test |
71 | 72 | public void testBulkCreateConnectionsMultiThreaded() throws Exception { |
72 | 73 | ExecutorService executor = Executors.newFixedThreadPool(50); |
73 | | - List<Future<?>> futures = new ArrayList<>(NUMBER_OF_TEST_CONNECTIONS); |
74 | | - for (int i = 0; i < NUMBER_OF_TEST_CONNECTIONS; i++) { |
75 | | - futures.add( |
76 | | - executor.submit( |
77 | | - () -> { |
78 | | - try (ITConnection connection = createConnection()) { |
79 | | - try (ResultSet rs = connection.executeQuery(Statement.of("select 1"))) { |
80 | | - assertThat(rs.next(), is(true)); |
81 | | - assertThat(connection.getReadTimestamp(), is(notNullValue())); |
| 74 | + try { |
| 75 | + List<Future<?>> futures = new ArrayList<>(NUMBER_OF_TEST_CONNECTIONS); |
| 76 | + for (int i = 0; i < NUMBER_OF_TEST_CONNECTIONS; i++) { |
| 77 | + futures.add( |
| 78 | + executor.submit( |
| 79 | + () -> { |
| 80 | + try (ITConnection connection = createConnection()) { |
| 81 | + try (ResultSet resultSet = connection.executeQuery(Statement.of("select 1"))) { |
| 82 | + assertTrue(resultSet.next()); |
| 83 | + assertNotNull(connection.getReadTimestamp()); |
| 84 | + } |
82 | 85 | } |
83 | | - } |
84 | | - return null; |
85 | | - })); |
86 | | - } |
87 | | - executor.shutdown(); |
88 | | - assertTrue(executor.awaitTermination(60L, TimeUnit.SECONDS)); |
89 | | - for (Future<?> future : futures) { |
90 | | - assertNull(future.get()); |
| 86 | + return null; |
| 87 | + })); |
| 88 | + } |
| 89 | + executor.shutdown(); |
| 90 | + assertTrue( |
| 91 | + "Executor did not terminate within timeout; active threads remain", |
| 92 | + executor.awaitTermination(60L, TimeUnit.SECONDS)); |
| 93 | + for (Future<?> future : futures) { |
| 94 | + assertNull(future.get()); |
| 95 | + } |
| 96 | + } finally { |
| 97 | + if (!executor.isTerminated()) { |
| 98 | + executor.shutdownNow(); |
| 99 | + executor.awaitTermination(5L, TimeUnit.SECONDS); |
| 100 | + } |
91 | 101 | } |
92 | 102 | // close Spanner instances explicitly. This method will throw an exception if there are any |
93 | 103 | // connections still open in the pool |
|
0 commit comments