junit4 icon indicating copy to clipboard operation
junit4 copied to clipboard

Feature request: be able to query time remaining on a test

Open carl-mastrangelo opened this issue 7 years ago • 2 comments

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.

carl-mastrangelo avatar Dec 04 '17 21:12 carl-mastrangelo

Timeout has a method getTimeout. Does this method already fix your problem?

@Rule public final Timeout timeout = Timeout.seconds(5);

@Test public void foo() {
  future.get(
    timeout.getTimeout(Timeunit.SECONDS),
    Timeunit.SECONDS
  );
  // ...
}

Of course the timeout is more than getRemaining but you can change the timeout and everything else changes accordingly.

stefanbirkner avatar Jan 09 '18 03:01 stefanbirkner

getTimeout is a partial solution. I can't think of any problems with using it, but my gut feeling is that the remaining time would be preferred.

As a side note, how would that work when used in conjunction with DisableOnDebug ?

carl-mastrangelo avatar Jan 09 '18 20:01 carl-mastrangelo