@Test
public void waitForAllInFlightTasksToComplete() throws InterruptedException {

    CountDownLatch taskSubmissions = new CountDownLatch(2);
    List<CountDownLatch> taskGates = Lists.newArrayList(new CountDownLatch(1), new CountDownLatch(1));
    RangeComparator comparator = comparator(context(0L, 100L));
    Thread t = new Thread(() -> comparator.compare(keys(0, 1),
                                                   keys(0, 1),
                                                   waitUntilNotified(taskSubmissions, taskGates)),
                          "CallingThread");
    t.setDaemon(true);
    t.start();
    // wait for both tasks to be submitted then check that the calling thread enters a waiting state
    taskSubmissions.await();
    assertThreadWaits(t);
    // let the first task complete and check that the calling thread is still waiting
    taskGates.get(0).countDown();
    assertThreadWaits(t);
    // let the second task run and wait for the caller to terminate
    taskGates.get(1).countDown();
    t.join();
    assertNothingReported(errors, mismatches, journal);
    assertCompared(0, 1);
}