|
1 | 1 | package org.dataloader;
|
2 | 2 |
|
| 3 | +import org.dataloader.fixtures.TestingClock; |
3 | 4 | import org.junit.Test;
|
4 | 5 |
|
5 |
| -import java.time.Clock; |
6 |
| -import java.time.Duration; |
7 | 6 | import java.time.Instant;
|
8 |
| -import java.time.ZoneId; |
9 |
| -import java.util.concurrent.CompletableFuture; |
10 |
| -import java.util.concurrent.atomic.AtomicReference; |
11 | 7 |
|
| 8 | +import static org.dataloader.fixtures.TestKit.keysAsValues; |
12 | 9 | import static org.hamcrest.Matchers.equalTo;
|
13 | 10 | import static org.junit.Assert.assertThat;
|
14 | 11 |
|
15 | 12 | @SuppressWarnings("UnusedReturnValue")
|
16 | 13 | public class DataLoaderTimeTest {
|
17 | 14 |
|
18 |
| - private <T> BatchLoader<T, T> keysAsValues() { |
19 |
| - return CompletableFuture::completedFuture; |
20 |
| - } |
21 |
| - |
22 |
| - AtomicReference<Clock> clockRef = new AtomicReference<>(); |
23 | 15 |
|
24 | 16 | @Test
|
25 | 17 | public void should_set_and_instant_if_dispatched() {
|
26 |
| - Clock clock = zeroEpoch(); |
27 |
| - clockRef.set(clock); |
28 |
| - |
29 |
| - Instant startInstant = now(); |
30 | 18 |
|
31 |
| - @SuppressWarnings("deprecation") |
32 |
| - DataLoader<Integer, Integer> dataLoader = new DataLoader<Integer, Integer>(keysAsValues()) { |
33 |
| - @Override |
34 |
| - Clock clock() { |
35 |
| - return clockRef.get(); |
36 |
| - } |
37 |
| - }; |
| 19 | + TestingClock clock = new TestingClock(); |
| 20 | + DataLoader<Integer, Integer> dataLoader = new ClockDataLoader<>(keysAsValues(), clock); |
| 21 | + Instant then = clock.instant(); |
38 | 22 |
|
39 |
| - long sinceMS = msSince(dataLoader.getLastDispatchTime()); |
| 23 | + long sinceMS = dataLoader.getTimeSinceDispatch().toMillis(); |
40 | 24 | assertThat(sinceMS, equalTo(0L));
|
41 |
| - assertThat(startInstant, equalTo(dataLoader.getLastDispatchTime())); |
| 25 | + assertThat(then, equalTo(dataLoader.getLastDispatchTime())); |
42 | 26 |
|
43 |
| - jump(clock, 1000); |
44 |
| - dataLoader.dispatch(); |
| 27 | + then = clock.instant(); |
| 28 | + clock.jump(1000); |
45 | 29 |
|
46 |
| - sinceMS = msSince(dataLoader.getLastDispatchTime()); |
| 30 | + sinceMS = dataLoader.getTimeSinceDispatch().toMillis(); |
47 | 31 | assertThat(sinceMS, equalTo(1000L));
|
48 |
| - } |
| 32 | + assertThat(then, equalTo(dataLoader.getLastDispatchTime())); |
49 | 33 |
|
50 |
| - private long msSince(Instant lastDispatchTime) { |
51 |
| - return Duration.between(lastDispatchTime, now()).toMillis(); |
52 |
| - } |
| 34 | + // dispatch and hence reset the time of last dispatch |
| 35 | + then = clock.instant(); |
| 36 | + dataLoader.dispatch(); |
53 | 37 |
|
54 |
| - private Instant now() { |
55 |
| - return Instant.now(clockRef.get()); |
56 |
| - } |
| 38 | + sinceMS = dataLoader.getTimeSinceDispatch().toMillis(); |
| 39 | + assertThat(sinceMS, equalTo(0L)); |
| 40 | + assertThat(then, equalTo(dataLoader.getLastDispatchTime())); |
57 | 41 |
|
58 |
| - private Clock jump(Clock clock, int millis) { |
59 |
| - clock = Clock.offset(clock, Duration.ofMillis(millis)); |
60 |
| - clockRef.set(clock); |
61 |
| - return clock; |
62 | 42 | }
|
63 | 43 |
|
64 |
| - private Clock zeroEpoch() { |
65 |
| - return Clock.fixed(Instant.ofEpochMilli(0), ZoneId.systemDefault()); |
66 |
| - } |
67 | 44 |
|
68 | 45 | }
|
0 commit comments