Skip to content

Commit

Permalink
test: make ManualClock accessible to other time-based tests
Browse files Browse the repository at this point in the history
some time related tests may require manual time adjustments, thus will
benefits from existing ManualClock implementation.

Acked-by: Albert Rossi
Target: master
  • Loading branch information
kofemann committed Oct 27, 2022
1 parent 874b816 commit 88ecc6d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
30 changes: 0 additions & 30 deletions core/src/test/java/org/dcache/nfs/util/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@
*/
package org.dcache.nfs.util;

import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.Before;
import org.junit.Test;

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

private static class ManualClock extends Clock {

private final AtomicLong currentTime = new AtomicLong();

@Override
public Instant instant() {
return Instant.ofEpochMilli(currentTime.get());
}

void advance(long time, TimeUnit unit) {
currentTime.addAndGet(unit.toMillis(time));
}

void advance(Duration duration) {
currentTime.addAndGet(duration.toMillis());
}

@Override
public ZoneId getZone() {
return Clock.systemDefaultZone().getZone();
}

@Override
public Clock withZone(ZoneId zone) {
throw new UnsupportedClassVersionError();
}
}
}
39 changes: 39 additions & 0 deletions core/src/test/java/org/dcache/nfs/util/ManualClock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.dcache.nfs.util;

import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

/**
* An implementation of {@link Clock} that allows to manually advance the time.
*/
public class ManualClock extends Clock {

private final AtomicLong currentTime = new AtomicLong();

@Override
public Instant instant() {
return Instant.ofEpochMilli(currentTime.get());
}

public void advance(long time, TimeUnit unit) {
currentTime.addAndGet(unit.toMillis(time));
}

public void advance(Duration duration) {
currentTime.addAndGet(duration.toMillis());
}

@Override
public ZoneId getZone() {
return Clock.systemDefaultZone().getZone();
}

@Override
public Clock withZone(ZoneId zone) {
throw new UnsupportedClassVersionError();
}
}

0 comments on commit 88ecc6d

Please sign in to comment.