1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server;
import com.yahoo.test.ManualClock;
import org.junit.Test;
import java.time.Duration;
import java.time.Instant;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* @author Ulf Lilleengen
*/
public class TimeoutBudgetTest {
public static TimeoutBudget day() {
return new TimeoutBudget(new ManualClock(Instant.now()), Duration.ofDays(1));
}
@Test
public void testTimeLeft() {
ManualClock clock = new ManualClock();
TimeoutBudget budget = new TimeoutBudget(clock, Duration.ofMillis(7));
assertThat(budget.timeLeft().toMillis(), is(7L));
clock.advance(Duration.ofMillis(1));
assertThat(budget.timeLeft().toMillis(), is(6L));
clock.advance(Duration.ofMillis(5));
assertThat(budget.timeLeft().toMillis(), is(1L));
assertThat(budget.timeLeft().toMillis(), is(1L));
clock.advance(Duration.ofMillis(1));
assertThat(budget.timeLeft().toMillis(), is(0L));
clock.advance(Duration.ofMillis(5));
assertThat(budget.timeLeft().toMillis(), is(0L));
clock.advance(Duration.ofMillis(1));
assertThat(budget.timesUsed(), is("[total: 13 ms]"));
}
@Test
public void testHasTimeLeft() {
ManualClock clock = new ManualClock();
TimeoutBudget budget = new TimeoutBudget(clock, Duration.ofMillis(7));
assertThat(budget.hasTimeLeft(), is(true));
clock.advance(Duration.ofMillis(1));
assertThat(budget.hasTimeLeft(), is(true));
clock.advance(Duration.ofMillis(5));
assertThat(budget.hasTimeLeft(), is(true));
assertThat(budget.hasTimeLeft(), is(true));
clock.advance(Duration.ofMillis(1));
assertThat(budget.hasTimeLeft(), is(false));
clock.advance(Duration.ofMillis(5));
assertThat(budget.hasTimeLeft(), is(false));
clock.advance(Duration.ofMillis(1));
assertThat(budget.timesUsed(), is("[total: 13 ms]"));
}
@Test
public void testHasTimeLeftWithLabels() {
ManualClock clock = new ManualClock();
TimeoutBudget budget = new TimeoutBudget(clock, Duration.ofMillis(7));
assertThat(budget.hasTimeLeft("a"), is(true));
clock.advance(Duration.ofMillis(1));
assertThat(budget.hasTimeLeft("b"), is(true));
clock.advance(Duration.ofMillis(5));
assertThat(budget.hasTimeLeft("c"), is(true));
assertThat(budget.hasTimeLeft("d"), is(true));
clock.advance(Duration.ofMillis(1));
assertThat(budget.timesUsed(), is("[a: 0 ms, b: 1 ms, c: 5 ms, d: 0 ms, total: 7 ms]"));
}
}
|