Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.SerialIntegrationTest;
Expand All @@ -28,6 +30,7 @@
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.experimental.categories.Category;
Expand Down Expand Up @@ -65,22 +68,27 @@ public void testBulkCreateConnectionsSingleThreaded() {
}

@Test
public void testBulkCreateConnectionsMultiThreaded() throws InterruptedException {
public void testBulkCreateConnectionsMultiThreaded() throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(50);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we add a try-finally block to ensure that the executor is shut down also when the test fails, like this (and also use JUnit style assertions):

@Test
public void testBulkCreateConnectionsMultiThreaded() throws Exception {
  ExecutorService executor = Executors.newFixedThreadPool(50);
  try {
    List<Future<?>> futures = new ArrayList<>(NUMBER_OF_TEST_CONNECTIONS);
    for (int i = 0; i < NUMBER_OF_TEST_CONNECTIONS; i++) {
      futures.add(
          executor.submit(
              () -> {
                try (ITConnection connection = createConnection()) {
                  try (ResultSet resultSet = connection.executeQuery(Statement.of("select 1"))) {
                    assertTrue(resultSet.next());
                    assertNotNull(connection.getReadTimestamp());
                  }
                }
                return null;
              }));
    }
    executor.shutdown();
    assertTrue(
        "Executor did not terminate within timeout; active threads remain",
        executor.awaitTermination(60L, TimeUnit.SECONDS));
    for (Future<?> future : futures) {
      assertNull(future.get());
    }
  } finally {
    if (!executor.isTerminated()) {
      executor.shutdownNow();
      executor.awaitTermination(5L, TimeUnit.SECONDS);
    }
  }
  closeSpanner();
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in follow-up PR #14610.

List<Future<?>> futures = new ArrayList<>(NUMBER_OF_TEST_CONNECTIONS);
for (int i = 0; i < NUMBER_OF_TEST_CONNECTIONS; i++) {
executor.submit(
() -> {
try (ITConnection connection = createConnection()) {
try (ResultSet rs = connection.executeQuery(Statement.of("select 1"))) {
assertThat(rs.next(), is(true));
assertThat(connection.getReadTimestamp(), is(notNullValue()));
}
}
return null;
});
futures.add(
executor.submit(
() -> {
try (ITConnection connection = createConnection()) {
try (ResultSet rs = connection.executeQuery(Statement.of("select 1"))) {
assertThat(rs.next(), is(true));
assertThat(connection.getReadTimestamp(), is(notNullValue()));
}
}
return null;
}));
}
executor.shutdown();
executor.awaitTermination(10L, TimeUnit.SECONDS);
assertTrue(executor.awaitTermination(60L, TimeUnit.SECONDS));
for (Future<?> future : futures) {
assertNull(future.get());
}
// close Spanner instances explicitly. This method will throw an exception if there are any
// connections still open in the pool
closeSpanner();
Expand Down
Loading