Skip to content

Commit

Permalink
handle max time
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkyle committed Nov 13, 2024
1 parent a1dba9e commit 5500cbb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ public interface RemainingTime extends Supplier<TimeValue> {
* Create a {@link Supplier} that returns a decreasing {@link TimeValue} on each invocation, representing the amount of time until
* the call times out. The timer starts when this method is called and counts down from remainingTime to 0.
* currentTime should return the most up-to-date system time, for example Instant.now() or Clock.instant().
* {@link TimeValue#MAX_VALUE} is a special case where the remaining time is always TimeValue.MAX_VALUE.
*/
static RemainingTime from(Supplier<Instant> currentTime, TimeValue remainingTime) {
if (remainingTime.equals(TimeValue.MAX_VALUE)) {
return () -> TimeValue.MAX_VALUE;
}

var timeout = currentTime.get().plus(remainingTime.duration(), remainingTime.timeUnit().toChronoUnit());
var maxRemainingTime = remainingTime.nanos();
return () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public void testRemainingTimeMaxValue() {
assertThat(remainingTime.get(), Matchers.equalTo(TimeValue.ZERO));
}

public void testMaxTime() {
var remainingTime = RemainingTime.from(Instant::now, TimeValue.MAX_VALUE);
assertThat(remainingTime.get(), Matchers.equalTo(TimeValue.MAX_VALUE));
}

// always add the first value, which is read when RemainingTime.from is called, then add the test values
private Supplier<Instant> times(Instant... instants) {
var startTime = Stream.of(Instant.now());
Expand Down

0 comments on commit 5500cbb

Please sign in to comment.