Skip to content

Commit e017966

Browse files
test(spanner): unflake ITBulkConnectionTest.testBulkCreateConnectionsMultiThreaded
Ensure ExecutorService in ITBulkConnectionTest.testBulkCreateConnectionsMultiThreaded is always shut down in a try-finally block if the test fails, and use JUnit assertTrue/assertNotNull assertions.
1 parent a37cff6 commit e017966

1 file changed

Lines changed: 27 additions & 17 deletions

File tree

‎java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/it/ITBulkConnectionTest.java‎

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.hamcrest.CoreMatchers.is;
2020
import static org.hamcrest.CoreMatchers.notNullValue;
2121
import static org.hamcrest.MatcherAssert.assertThat;
22+
import static org.junit.Assert.assertNotNull;
2223
import static org.junit.Assert.assertNull;
2324
import static org.junit.Assert.assertTrue;
2425

@@ -70,24 +71,33 @@ public void testBulkCreateConnectionsSingleThreaded() {
7071
@Test
7172
public void testBulkCreateConnectionsMultiThreaded() throws Exception {
7273
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+
}
8285
}
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+
}
91101
}
92102
// close Spanner instances explicitly. This method will throw an exception if there are any
93103
// connections still open in the pool

0 commit comments

Comments
 (0)