diff options
author | HÃ¥kon Hallingstad <hakon@oath.com> | 2018-06-22 09:29:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-22 09:29:45 +0200 |
commit | 47bde30f37a075c664116fc918252573fc472ab7 (patch) | |
tree | 8da6f593afbdacb8e88f49278574991803d274c3 /jdisc_core | |
parent | 7b34c4af8e19d5ccaad898b7c289433d8a6a7395 (diff) |
Revert "Revert "Move TimeBudget to vespajlib and use Clock""
Diffstat (limited to 'jdisc_core')
-rw-r--r-- | jdisc_core/src/main/java/com/yahoo/jdisc/TimeBudget.java | 55 | ||||
-rw-r--r-- | jdisc_core/src/test/java/com/yahoo/jdisc/TimeBudgetTestCase.java | 29 |
2 files changed, 0 insertions, 84 deletions
diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/TimeBudget.java b/jdisc_core/src/main/java/com/yahoo/jdisc/TimeBudget.java deleted file mode 100644 index 39322b6e83a..00000000000 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/TimeBudget.java +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.jdisc; - -import javax.annotation.concurrent.Immutable; -import java.time.Duration; -import java.time.Instant; - -/** - * A TimeBudget tracks the current time compared to a start time and deadline. - * - * @author hakon - */ -@Immutable -public class TimeBudget { - private final Timer timer; - 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(Timer timer, Duration timeout) { - return new TimeBudget(timer, timer.currentTime(), timeout); - } - - private TimeBudget(Timer timer, Instant start, Duration timeout) { - this.timer = timer; - this.start = start; - this.timeout = timeout; - } - - /** Time until 'headroom' before deadline. Guaranteed to be non-negative. */ - public Duration timeBeforeDeadline(Duration headroom) { - return nonNegativeBetween(now(), deadline().minus(headroom)); - } - - /** Returns the original timeout. */ - public Duration originalTimeout() { - return timeout; - } - - 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; - } - - private Instant now() { - return timer.currentTime(); - } - - private Instant deadline() { - return start.plus(timeout); - } -} diff --git a/jdisc_core/src/test/java/com/yahoo/jdisc/TimeBudgetTestCase.java b/jdisc_core/src/test/java/com/yahoo/jdisc/TimeBudgetTestCase.java deleted file mode 100644 index 5afc205beb3..00000000000 --- a/jdisc_core/src/test/java/com/yahoo/jdisc/TimeBudgetTestCase.java +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.jdisc; - -import org.junit.Test; - -import java.time.Duration; -import java.time.Instant; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class TimeBudgetTestCase { - private final Timer timer = mock(Timer.class); - - @Test - public void testBasics() { - when(timer.currentTime()).thenReturn(Instant.ofEpochSecond(0)); - TimeBudget timeBudget = TimeBudget.fromNow(timer, Duration.ofSeconds(10)); - - when(timer.currentTime()).thenReturn(Instant.ofEpochSecond(7)); - assertEquals(Duration.ofSeconds(3), timeBudget.timeBeforeDeadline(Duration.ofSeconds(0))); - assertEquals(Duration.ofSeconds(1), timeBudget.timeBeforeDeadline(Duration.ofSeconds(2))); - assertEquals(Duration.ofSeconds(0), timeBudget.timeBeforeDeadline(Duration.ofSeconds(5))); - - when(timer.currentTime()).thenReturn(Instant.ofEpochSecond(11)); - assertEquals(Duration.ofSeconds(0), timeBudget.timeBeforeDeadline(Duration.ofSeconds(0))); - } -}
\ No newline at end of file |