blob: 914f842d842dfd32046f6ef0ce5bc01166486823 (
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 Yahoo. 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());
}
}
|