aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/lang/CachedSupplierTest.java
blob: 2d9040ef9e893c251ce39b444a21a330e3779898 (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
39
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.lang;

import com.yahoo.test.ManualClock;
import org.junit.Test;

import java.time.Duration;

import static org.junit.Assert.assertEquals;

/**
 * @author freva
 */
public class CachedSupplierTest {

    @Test
    public void test() {
        ManualClock clock = new ManualClock();
        MutableInteger integer = new MutableInteger(0);
        CachedSupplier<Integer> supplier = new CachedSupplier<>(() -> integer.add(1), Duration.ofMinutes(1), clock);

        assertEquals(1, supplier.get().intValue());
        assertEquals(1, supplier.get().intValue());

        clock.advance(Duration.ofSeconds(30));
        assertEquals(1, supplier.get().intValue());

        clock.advance(Duration.ofSeconds(31));
        assertEquals(2, supplier.get().intValue());
        assertEquals(2, supplier.get().intValue());

        supplier.invalidate();
        assertEquals(3, supplier.get().intValue());
        assertEquals(3, supplier.get().intValue());

        clock.advance(Duration.ofSeconds(61));
        assertEquals(4, supplier.get().intValue());
    }
}