From 88ecc6db382c4de3c8cfc7bb91635d70eeea0bc4 Mon Sep 17 00:00:00 2001 From: Tigran Mkrtchyan Date: Mon, 24 Oct 2022 10:33:51 +0200 Subject: [PATCH] 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 --- .../java/org/dcache/nfs/util/CacheTest.java | 30 -------------- .../java/org/dcache/nfs/util/ManualClock.java | 39 +++++++++++++++++++ 2 files changed, 39 insertions(+), 30 deletions(-) create mode 100644 core/src/test/java/org/dcache/nfs/util/ManualClock.java diff --git a/core/src/test/java/org/dcache/nfs/util/CacheTest.java b/core/src/test/java/org/dcache/nfs/util/CacheTest.java index 2dd67331..55c9ca13 100644 --- a/core/src/test/java/org/dcache/nfs/util/CacheTest.java +++ b/core/src/test/java/org/dcache/nfs/util/CacheTest.java @@ -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; @@ -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(); - } - } } \ No newline at end of file diff --git a/core/src/test/java/org/dcache/nfs/util/ManualClock.java b/core/src/test/java/org/dcache/nfs/util/ManualClock.java new file mode 100644 index 00000000..b91f5734 --- /dev/null +++ b/core/src/test/java/org/dcache/nfs/util/ManualClock.java @@ -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(); + } +}