Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wait for initialization to finish before test #161

Merged
merged 1 commit into from Apr 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -33,8 +33,8 @@
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import org.junit.After;
import org.junit.Before;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
Expand All @@ -47,13 +47,13 @@ public class ITSpannerOptionsTest {
@ClassRule public static IntegrationTestEnv env = new IntegrationTestEnv();
private static Database db;

@Before
public void setUp() throws Exception {
@BeforeClass
public static void setUp() throws Exception {
db = env.getTestHelper().createTestDatabase();
}

@After
public void tearDown() throws Exception {
@AfterClass
public static void tearDown() throws Exception {
db.drop();
}

Expand All @@ -65,9 +65,9 @@ public void tearDown() throws Exception {

@Test
public void testCloseAllThreadsWhenClosingSpanner() throws InterruptedException {
// The IT environment has already started some worker threads.
int baseThreadCount = getNumberOfThreadsWithName(SPANNER_THREAD_NAME);
for (int i = 0; i < NUMBER_OF_TEST_RUNS; i++) {
waitForStartup();
assertThat(getNumberOfThreadsWithName(SPANNER_THREAD_NAME)).isAtMost(baseThreadCount);
// Create Spanner instance.
// We make a copy of the options instance, as SpannerOptions caches any service object
Expand Down Expand Up @@ -136,6 +136,7 @@ public void testCloseAllThreadsWhenClosingSpanner() throws InterruptedException

@Test
public void testMultipleSpannersFromSameSpannerOptions() throws InterruptedException {
waitForStartup();
int baseThreadCount = getNumberOfThreadsWithName(SPANNER_THREAD_NAME);
SpannerOptions options = env.getTestHelper().getOptions().toBuilder().build();
try (Spanner spanner1 = options.getService()) {
Expand Down Expand Up @@ -167,6 +168,17 @@ public void testMultipleSpannersFromSameSpannerOptions() throws InterruptedExcep
assertThat(getNumberOfThreadsWithName(SPANNER_THREAD_NAME)).isAtMost(baseThreadCount);
}

private void waitForStartup() throws InterruptedException {
// Wait until the IT environment has already started all base worker threads.
int threadCount;
Stopwatch watch = Stopwatch.createStarted();
do {
threadCount = getNumberOfThreadsWithName(SPANNER_THREAD_NAME);
Thread.sleep(100L);
} while (getNumberOfThreadsWithName(SPANNER_THREAD_NAME) > threadCount
&& watch.elapsed(TimeUnit.SECONDS) < 5);
}

private int getNumberOfThreadsWithName(String serviceName) {
Pattern pattern = Pattern.compile(String.format(THREAD_PATTERN, serviceName));
ThreadGroup group = Thread.currentThread().getThreadGroup();
Expand Down