aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test
diff options
context:
space:
mode:
authorHÃ¥kon Hallingstad <hakon@oath.com>2018-06-22 09:29:45 +0200
committerGitHub <noreply@github.com>2018-06-22 09:29:45 +0200
commit47bde30f37a075c664116fc918252573fc472ab7 (patch)
tree8da6f593afbdacb8e88f49278574991803d274c3 /vespajlib/src/test
parent7b34c4af8e19d5ccaad898b7c289433d8a6a7395 (diff)
Revert "Revert "Move TimeBudget to vespajlib and use Clock""
Diffstat (limited to 'vespajlib/src/test')
-rw-r--r--vespajlib/src/test/java/com/yahoo/time/TimeBudgetTestCase.java40
1 files changed, 40 insertions, 0 deletions
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..ddd57c71a0d
--- /dev/null
+++ b/vespajlib/src/test/java/com/yahoo/time/TimeBudgetTestCase.java
@@ -0,0 +1,40 @@
+// 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 com.google.common.util.concurrent.UncheckedTimeoutException;
+import com.yahoo.test.ManualClock;
+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;
+
+public class TimeBudgetTestCase {
+ private final Clock clock = mock(Clock.class);
+
+ @Test
+ public void testBasics() {
+ ManualClock clock = new ManualClock();
+ clock.setInstant(Instant.ofEpochSecond(0));
+ TimeBudget timeBudget = TimeBudget.fromNow(clock, Duration.ofSeconds(10));
+
+ clock.advance(Duration.ofSeconds(7));
+ assertEquals(Duration.ofSeconds(3), timeBudget.timeLeftOrThrow());
+
+ // Verify that toMillis() of >=1 is fine, but 0 is not.
+
+ clock.setInstant(Instant.ofEpochSecond(9, 999000000));
+ assertEquals(1, timeBudget.timeLeftOrThrow().toMillis());
+ clock.setInstant(Instant.ofEpochSecond(9, 999000001));
+ try {
+ timeBudget.timeLeftOrThrow();
+ fail();
+ } catch (UncheckedTimeoutException e) {
+ // OK
+ }
+ }
+} \ No newline at end of file