Reduce flakiness is ShutdownWaiter's tests
This removes the race in testInterruptNonfullWait. However, both
testInterruptNonfullWait and testBasicUsage check timings, so they could
still be flaky on a slow machine. We can further tweak them as
appropriate.
diff --git a/test/com/google/enterprise/adaptor/ShutdownWaiterTest.java b/test/com/google/enterprise/adaptor/ShutdownWaiterTest.java
index e819e24..3d25883 100644
--- a/test/com/google/enterprise/adaptor/ShutdownWaiterTest.java
+++ b/test/com/google/enterprise/adaptor/ShutdownWaiterTest.java
@@ -19,6 +19,7 @@
import org.junit.*;
import org.junit.rules.ExpectedException;
+import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -52,7 +53,7 @@
long timeTakenUs = TimeUnit.MICROSECONDS.convert(
System.nanoTime() - start, TimeUnit.NANOSECONDS);
assertFalse(Thread.currentThread().isInterrupted());
- assertTrue("shutdown took " + timeTakenUs + "µs", timeTakenUs < 1000);
+ assertTrue("shutdown took " + timeTakenUs + "µs", timeTakenUs < 1300);
}
@Test
@@ -91,6 +92,7 @@
@Test
public void testInterruptNonfullWait() throws Exception {
final AtomicBoolean completed = new AtomicBoolean();
+ final CountDownLatch latch = new CountDownLatch(1);
Thread testThread = new Thread() {
@Override
public void run() {
@@ -100,6 +102,7 @@
throw new RuntimeException(e);
}
try {
+ latch.countDown();
Thread.sleep(1000);
} catch (InterruptedException ex) {
completed.set(true);
@@ -109,8 +112,8 @@
}
};
testThread.start();
- // Give time to testThread to get started.
- Thread.sleep(1);
+ // Wait until testThread gets started.
+ latch.await();
long start = System.nanoTime();
// This will need to interrupt and wait for the thread to stop, but should
// not need tons of time.