aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/time/TimeBudgetTestCase.java
blob: ef664f95d333be161afca1a42f704f0a3ea9ecc6 (plain) (blame)
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
// 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
        }
    }
}