-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
39 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |