diff options
author | Håkon Hallingstad <hakon@oath.com> | 2018-06-21 13:41:42 +0200 |
---|---|---|
committer | Håkon Hallingstad <hakon@oath.com> | 2018-06-21 13:41:42 +0200 |
commit | bf4d047056ead5b91a8eb8c185a107e1c48aa5ad (patch) | |
tree | ac7232761679c250532894ea5940ce7378e87e2a /vespajlib | |
parent | 5258489bf992e8176e136362759ac079494b6f94 (diff) |
Move TimeBudget to vespajlib and use Clock
Diffstat (limited to 'vespajlib')
3 files changed, 119 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/time/TimeBudget.java b/vespajlib/src/main/java/com/yahoo/time/TimeBudget.java new file mode 100644 index 00000000000..6f28d5d80e8 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/time/TimeBudget.java @@ -0,0 +1,63 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.time; + +import java.time.Clock; +import java.time.Duration; +import java.time.Instant; + +/** + * A TimeBudget can be used to track the time of an ongoing operation with a timeout. + * + * @author hakon + */ +public class TimeBudget { + private final Clock clock; + private final Instant start; + private final Duration timeout; + + /** Returns a TimeBudget with a start time of now, and with the given timeout. */ + public static TimeBudget fromNow(Clock clock, Duration timeout) { + return new TimeBudget(clock, clock.instant(), timeout); + } + + private TimeBudget(Clock clock, Instant start, Duration timeout) { + this.clock = clock; + this.start = start; + this.timeout = makeNonNegative(timeout); + } + + /** Returns time since start. */ + public Duration timePassed() { + return nonNegativeBetween(start, clock.instant()); + } + + /** Returns the original timeout. */ + public Duration originalTimeout() { + return timeout; + } + + /** + * Returns the time until deadline. + * + * @return time until deadline. It's toMillis() is guaranteed to be positive. + * @throws UncheckedTimeoutException if the deadline has been reached or passed. + */ + public Duration timeLeftOrThrow() { + Instant now = clock.instant(); + Duration left = Duration.between(now, start.plus(timeout)); + if (left.toMillis() <= 0) { + throw new UncheckedTimeoutException("Time since start " + nonNegativeBetween(start, now) + + " exceeds timeout " + timeout); + } + + return left; + } + + private static Duration nonNegativeBetween(Instant start, Instant end) { + return makeNonNegative(Duration.between(start, end)); + } + + private static Duration makeNonNegative(Duration duration) { + return duration.isNegative() ? Duration.ZERO : duration; + } +} diff --git a/vespajlib/src/main/java/com/yahoo/time/UncheckedTimeoutException.java b/vespajlib/src/main/java/com/yahoo/time/UncheckedTimeoutException.java new file mode 100644 index 00000000000..3b53ffbdcf6 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/time/UncheckedTimeoutException.java @@ -0,0 +1,18 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.time; + +/** + * Exception thrown when a blocking operation times out. + * + * <p>Mirrors {@link java.util.concurrent.TimeoutException} + * + * @author hakon + */ +@SuppressWarnings("serial") +public class UncheckedTimeoutException extends RuntimeException { + public UncheckedTimeoutException() { } + + public UncheckedTimeoutException(String message) { + super(message); + } +} diff --git a/vespajlib/src/test/java/com/yahoo/time/TimeBudgetTestCase.java b/vespajlib/src/test/java/com/yahoo/time/TimeBudgetTestCase.java new file mode 100644 index 00000000000..ef664f95d33 --- /dev/null +++ b/vespajlib/src/test/java/com/yahoo/time/TimeBudgetTestCase.java @@ -0,0 +1,38 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.time; + +import org.junit.Test; + +import java.time.Clock; +import java.time.Duration; +import java.time.Instant; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class TimeBudgetTestCase { + private final Clock clock = mock(Clock.class); + + @Test + public void testBasics() { + when(clock.instant()).thenReturn(Instant.ofEpochSecond(0)); + TimeBudget timeBudget = TimeBudget.fromNow(clock, Duration.ofSeconds(10)); + + when(clock.instant()).thenReturn(Instant.ofEpochSecond(7)); + assertEquals(Duration.ofSeconds(3), timeBudget.timeLeftOrThrow()); + + // Verify that toMillis() of >=1 is fine, but 0 is not. + + when(clock.instant()).thenReturn(Instant.ofEpochSecond(9, 999000000)); + assertEquals(1, timeBudget.timeLeftOrThrow().toMillis()); + when(clock.instant()).thenReturn(Instant.ofEpochSecond(9, 999000001)); + try { + timeBudget.timeLeftOrThrow(); + fail(); + } catch (UncheckedTimeoutException e) { + // OK + } + } +}
\ No newline at end of file |