Description
When using the Timeout
rule, it would sometimes be convenient to be able to get how much time is remaining in the test. That would allow code that calls Future.get(long, Timeout)
, CountDownLatch.await(long, Timeunit)
, Thread.sleep(long)
, Object.wait(long)
, and any other code that waits to cooperate with the testing framework.
The use case for this is when bumping the timeout for a test, it becomes necessary to update all of the other uses of time. For example:
@Rule public final Timeout timeout = Timeout.seconds(5);
@Test public void foo() {
future.get(5, Timeunit.SECONDS);
// ...
}
Suppose this test is slow or possibly flaky. Just incrementing the timeout
to be higher won't work because the get is still going to fail early. Modifying the call to be future.get(Integer.MAX_VALUE, TimeUnit.SECONDS)
is cumbersome and doesn't convey the intent of the test. As a strawman API, I would suggest something like the following:
@Rule public final Timeout timeout = Timeout.seconds(5);
@Test public void foo() {
future.get(timeout.getRemaining(Timeunit.SECONDS), Timeunit.SECONDS);
// ...
}
It would be okay for this to not be exactly accurate (as compared with "deadlines"). It's more about letting the reader know that the waiting will happen to the end of the test, rather than some seemingly arbitrary duration.