summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorHåkon Hallingstad <hakon@oath.com>2018-06-21 14:17:50 +0200
committerHåkon Hallingstad <hakon@oath.com>2018-06-21 14:17:50 +0200
commit3b3920201ae893730e78fb1211f292233014d4df (patch)
treecf4c1b662e7e5f5ac717bcbaee96668b2c6438d6 /vespajlib
parentbf4d047056ead5b91a8eb8c185a107e1c48aa5ad (diff)
Use ManualClock and remove Unchecked prefix
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/pom.xml6
-rw-r--r--vespajlib/src/main/java/com/yahoo/time/TimeBudget.java4
-rw-r--r--vespajlib/src/main/java/com/yahoo/time/TimeoutException.java (renamed from vespajlib/src/main/java/com/yahoo/time/UncheckedTimeoutException.java)8
-rw-r--r--vespajlib/src/test/java/com/yahoo/time/TimeBudgetTestCase.java13
4 files changed, 19 insertions, 12 deletions
diff --git a/vespajlib/pom.xml b/vespajlib/pom.xml
index 81c385c96ab..880d039bc54 100644
--- a/vespajlib/pom.xml
+++ b/vespajlib/pom.xml
@@ -71,6 +71,12 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
</dependency>
+ <dependency>
+ <groupId>com.yahoo.vespa</groupId>
+ <artifactId>testutil</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
<plugins>
diff --git a/vespajlib/src/main/java/com/yahoo/time/TimeBudget.java b/vespajlib/src/main/java/com/yahoo/time/TimeBudget.java
index 6f28d5d80e8..fe2657585bc 100644
--- a/vespajlib/src/main/java/com/yahoo/time/TimeBudget.java
+++ b/vespajlib/src/main/java/com/yahoo/time/TimeBudget.java
@@ -40,13 +40,13 @@ public class TimeBudget {
* 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.
+ * @throws TimeoutException 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) +
+ throw new TimeoutException("Time since start " + nonNegativeBetween(start, now) +
" exceeds timeout " + timeout);
}
diff --git a/vespajlib/src/main/java/com/yahoo/time/UncheckedTimeoutException.java b/vespajlib/src/main/java/com/yahoo/time/TimeoutException.java
index 3b53ffbdcf6..098d42ddb94 100644
--- a/vespajlib/src/main/java/com/yahoo/time/UncheckedTimeoutException.java
+++ b/vespajlib/src/main/java/com/yahoo/time/TimeoutException.java
@@ -4,15 +4,15 @@ package com.yahoo.time;
/**
* Exception thrown when a blocking operation times out.
*
- * <p>Mirrors {@link java.util.concurrent.TimeoutException}
+ * <p>Unchecked variant of {@link java.util.concurrent.TimeoutException}
*
* @author hakon
*/
@SuppressWarnings("serial")
-public class UncheckedTimeoutException extends RuntimeException {
- public UncheckedTimeoutException() { }
+public class TimeoutException extends RuntimeException {
+ public TimeoutException() { }
- public UncheckedTimeoutException(String message) {
+ public TimeoutException(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
index ef664f95d33..7f55272c2ec 100644
--- a/vespajlib/src/test/java/com/yahoo/time/TimeBudgetTestCase.java
+++ b/vespajlib/src/test/java/com/yahoo/time/TimeBudgetTestCase.java
@@ -1,6 +1,7 @@
// 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.yahoo.test.ManualClock;
import org.junit.Test;
import java.time.Clock;
@@ -10,28 +11,28 @@ 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));
+ ManualClock clock = new ManualClock();
+ clock.setInstant(Instant.ofEpochSecond(0));
TimeBudget timeBudget = TimeBudget.fromNow(clock, Duration.ofSeconds(10));
- when(clock.instant()).thenReturn(Instant.ofEpochSecond(7));
+ clock.advance(Duration.ofSeconds(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));
+ clock.setInstant(Instant.ofEpochSecond(9, 999000000));
assertEquals(1, timeBudget.timeLeftOrThrow().toMillis());
- when(clock.instant()).thenReturn(Instant.ofEpochSecond(9, 999000001));
+ clock.setInstant(Instant.ofEpochSecond(9, 999000001));
try {
timeBudget.timeLeftOrThrow();
fail();
- } catch (UncheckedTimeoutException e) {
+ } catch (TimeoutException e) {
// OK
}
}