global test timeouts + fix test race condition

This commit is contained in:
Xavier Léauté 2015-01-07 22:55:53 -08:00
parent 9acc8c72ee
commit d5f4182de4
1 changed files with 26 additions and 21 deletions

View File

@ -42,11 +42,10 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class ChainedExecutionQueryRunnerTest public class ChainedExecutionQueryRunnerTest
{ {
@Test @Test(timeout = 60000)
public void testQueryCancellation() throws Exception public void testQueryCancellation() throws Exception
{ {
ExecutorService exec = PrioritizedExecutorService.create( ExecutorService exec = PrioritizedExecutorService.create(
@ -124,8 +123,8 @@ public class ChainedExecutionQueryRunnerTest
); );
// wait for query to register and start // wait for query to register and start
Assert.assertTrue(queryIsRegistered.await(1, TimeUnit.SECONDS)); queryIsRegistered.await();
Assert.assertTrue(queriesStarted.await(1, TimeUnit.SECONDS)); queriesStarted.await();
// cancel the query // cancel the query
Assert.assertTrue(capturedFuture.hasCaptured()); Assert.assertTrue(capturedFuture.hasCaptured());
@ -139,14 +138,16 @@ public class ChainedExecutionQueryRunnerTest
Assert.assertTrue(e.getCause() instanceof QueryInterruptedException); Assert.assertTrue(e.getCause() instanceof QueryInterruptedException);
cause = (QueryInterruptedException)e.getCause(); cause = (QueryInterruptedException)e.getCause();
} }
Assert.assertTrue(queriesInterrupted.await(500, TimeUnit.MILLISECONDS)); queriesInterrupted.await();
Assert.assertNotNull(cause); Assert.assertNotNull(cause);
Assert.assertTrue(future.isCancelled()); Assert.assertTrue(future.isCancelled());
Assert.assertTrue(runner1.hasStarted); Assert.assertTrue(runner1.hasStarted);
Assert.assertTrue(runner2.hasStarted); Assert.assertTrue(runner2.hasStarted);
Assert.assertTrue(runner1.interrupted); Assert.assertTrue(runner1.interrupted);
Assert.assertTrue(runner2.interrupted); Assert.assertTrue(runner2.interrupted);
Assert.assertTrue(!runner3.hasStarted || runner3.interrupted); synchronized (runner3) {
Assert.assertTrue(!runner3.hasStarted || runner3.interrupted);
}
Assert.assertFalse(runner1.hasCompleted); Assert.assertFalse(runner1.hasCompleted);
Assert.assertFalse(runner2.hasCompleted); Assert.assertFalse(runner2.hasCompleted);
Assert.assertFalse(runner3.hasCompleted); Assert.assertFalse(runner3.hasCompleted);
@ -154,7 +155,7 @@ public class ChainedExecutionQueryRunnerTest
EasyMock.verify(watcher); EasyMock.verify(watcher);
} }
@Test @Test(timeout = 60000)
public void testQueryTimeout() throws Exception public void testQueryTimeout() throws Exception
{ {
ExecutorService exec = PrioritizedExecutorService.create( ExecutorService exec = PrioritizedExecutorService.create(
@ -233,8 +234,8 @@ public class ChainedExecutionQueryRunnerTest
); );
// wait for query to register and start // wait for query to register and start
Assert.assertTrue(queryIsRegistered.await(1, TimeUnit.SECONDS)); queryIsRegistered.await();
Assert.assertTrue(queriesStarted.await(1, TimeUnit.SECONDS)); queriesStarted.await();
Assert.assertTrue(capturedFuture.hasCaptured()); Assert.assertTrue(capturedFuture.hasCaptured());
ListenableFuture future = capturedFuture.getValue(); ListenableFuture future = capturedFuture.getValue();
@ -248,14 +249,16 @@ public class ChainedExecutionQueryRunnerTest
Assert.assertEquals("Query timeout", e.getCause().getMessage()); Assert.assertEquals("Query timeout", e.getCause().getMessage());
cause = (QueryInterruptedException)e.getCause(); cause = (QueryInterruptedException)e.getCause();
} }
Assert.assertTrue(queriesInterrupted.await(500, TimeUnit.MILLISECONDS)); queriesInterrupted.await();
Assert.assertNotNull(cause); Assert.assertNotNull(cause);
Assert.assertTrue(future.isCancelled()); Assert.assertTrue(future.isCancelled());
Assert.assertTrue(runner1.hasStarted); Assert.assertTrue(runner1.hasStarted);
Assert.assertTrue(runner2.hasStarted); Assert.assertTrue(runner2.hasStarted);
Assert.assertTrue(runner1.interrupted); Assert.assertTrue(runner1.interrupted);
Assert.assertTrue(runner2.interrupted); Assert.assertTrue(runner2.interrupted);
Assert.assertTrue(!runner3.hasStarted || runner3.interrupted); synchronized (runner3) {
Assert.assertTrue(!runner3.hasStarted || runner3.interrupted);
}
Assert.assertFalse(runner1.hasCompleted); Assert.assertFalse(runner1.hasCompleted);
Assert.assertFalse(runner2.hasCompleted); Assert.assertFalse(runner2.hasCompleted);
Assert.assertFalse(runner3.hasCompleted); Assert.assertFalse(runner3.hasCompleted);
@ -268,9 +271,9 @@ public class ChainedExecutionQueryRunnerTest
private final CountDownLatch start; private final CountDownLatch start;
private final CountDownLatch stop; private final CountDownLatch stop;
private boolean hasStarted = false; private volatile boolean hasStarted = false;
private boolean hasCompleted = false; private volatile boolean hasCompleted = false;
private boolean interrupted = false; private volatile boolean interrupted = false;
public DyingQueryRunner(CountDownLatch start, CountDownLatch stop) public DyingQueryRunner(CountDownLatch start, CountDownLatch stop)
{ {
@ -281,17 +284,19 @@ public class ChainedExecutionQueryRunnerTest
@Override @Override
public Sequence<Integer> run(Query<Integer> query, Map<String, Object> responseContext) public Sequence<Integer> run(Query<Integer> query, Map<String, Object> responseContext)
{ {
hasStarted = true; synchronized (this) { // ensure hasStarted and interrupted are updated simultaneously
start.countDown(); hasStarted = true;
if (Thread.interrupted()) { start.countDown();
interrupted = true; if (Thread.interrupted()) {
stop.countDown(); interrupted = true;
throw new QueryInterruptedException("I got killed"); stop.countDown();
throw new QueryInterruptedException("I got killed");
}
} }
// do a lot of work // do a lot of work
try { try {
Thread.sleep(500); Thread.sleep(5000);
} }
catch (InterruptedException e) { catch (InterruptedException e) {
interrupted = true; interrupted = true;