Skip to content

Commit 88ecc6d

Browse files
committed
test: make ManualClock accessible to other time-based tests
some time related tests may require manual time adjustments, thus will benefits from existing ManualClock implementation. Acked-by: Albert Rossi Target: master
1 parent 874b816 commit 88ecc6d

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

core/src/test/java/org/dcache/nfs/util/CacheTest.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@
1919
*/
2020
package org.dcache.nfs.util;
2121

22-
import java.time.Clock;
2322
import java.time.Duration;
2423
import java.time.Instant;
25-
import java.time.ZoneId;
2624
import java.util.concurrent.TimeUnit;
27-
import java.util.concurrent.atomic.AtomicLong;
2825
import org.junit.Before;
2926
import org.junit.Test;
3027

@@ -130,31 +127,4 @@ public void testClear() {
130127
assertTrue("Not all entries are removed", _cache.entries().isEmpty());
131128
}
132129

133-
private static class ManualClock extends Clock {
134-
135-
private final AtomicLong currentTime = new AtomicLong();
136-
137-
@Override
138-
public Instant instant() {
139-
return Instant.ofEpochMilli(currentTime.get());
140-
}
141-
142-
void advance(long time, TimeUnit unit) {
143-
currentTime.addAndGet(unit.toMillis(time));
144-
}
145-
146-
void advance(Duration duration) {
147-
currentTime.addAndGet(duration.toMillis());
148-
}
149-
150-
@Override
151-
public ZoneId getZone() {
152-
return Clock.systemDefaultZone().getZone();
153-
}
154-
155-
@Override
156-
public Clock withZone(ZoneId zone) {
157-
throw new UnsupportedClassVersionError();
158-
}
159-
}
160130
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.dcache.nfs.util;
2+
3+
import java.time.Clock;
4+
import java.time.Duration;
5+
import java.time.Instant;
6+
import java.time.ZoneId;
7+
import java.util.concurrent.TimeUnit;
8+
import java.util.concurrent.atomic.AtomicLong;
9+
10+
/**
11+
* An implementation of {@link Clock} that allows to manually advance the time.
12+
*/
13+
public class ManualClock extends Clock {
14+
15+
private final AtomicLong currentTime = new AtomicLong();
16+
17+
@Override
18+
public Instant instant() {
19+
return Instant.ofEpochMilli(currentTime.get());
20+
}
21+
22+
public void advance(long time, TimeUnit unit) {
23+
currentTime.addAndGet(unit.toMillis(time));
24+
}
25+
26+
public void advance(Duration duration) {
27+
currentTime.addAndGet(duration.toMillis());
28+
}
29+
30+
@Override
31+
public ZoneId getZone() {
32+
return Clock.systemDefaultZone().getZone();
33+
}
34+
35+
@Override
36+
public Clock withZone(ZoneId zone) {
37+
throw new UnsupportedClassVersionError();
38+
}
39+
}

0 commit comments

Comments
 (0)