You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.0 KiB

package common.ecommerce.shared.testkit;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
/**
* 可推进的时钟。用来验证 TTL、提前续期、水位线等与时间有关的边界。
*/
public final class MutableClock extends Clock {
private Instant now;
private final ZoneId zone;
public MutableClock(Instant now, ZoneId zone) {
this.now = now;
this.zone = zone;
}
public static MutableClock atUtc(String iso) {
return new MutableClock(Instant.parse(iso), ZoneOffset.UTC);
}
public MutableClock advance(Duration duration) {
now = now.plus(duration);
return this;
}
public void set(Instant instant) {
this.now = instant;
}
@Override
public ZoneId getZone() {
return zone;
}
@Override
public Clock withZone(ZoneId zone) {
return new MutableClock(now, zone);
}
@Override
public Instant instant() {
return now;
}
}