Clean up the TestClock code a bit more.
This commit is contained in:
parent
8ea794baef
commit
90490c9c84
|
@ -79,7 +79,7 @@ class AccountsTest {
|
||||||
.build())
|
.build())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
private TestClock clock = TestClock.pinned(Instant.EPOCH);
|
private final TestClock clock = TestClock.pinned(Instant.EPOCH);
|
||||||
private DynamicConfigurationManager<DynamicConfiguration> mockDynamicConfigManager;
|
private DynamicConfigurationManager<DynamicConfiguration> mockDynamicConfigManager;
|
||||||
private Accounts accounts;
|
private Accounts accounts;
|
||||||
|
|
||||||
|
|
|
@ -100,7 +100,7 @@ class DonationControllerTest {
|
||||||
Map.of(1L, "TEST1", 2L, "TEST2", 3L, "TEST3"));
|
Map.of(1L, "TEST1", 2L, "TEST2", 3L, "TEST3"));
|
||||||
}
|
}
|
||||||
|
|
||||||
Clock clock = TestClock.pinned(Instant.ofEpochSecond(nowEpochSeconds));
|
final Clock clock = TestClock.pinned(Instant.ofEpochSecond(nowEpochSeconds));
|
||||||
ServerZkReceiptOperations zkReceiptOperations;
|
ServerZkReceiptOperations zkReceiptOperations;
|
||||||
RedeemedReceiptsManager redeemedReceiptsManager;
|
RedeemedReceiptsManager redeemedReceiptsManager;
|
||||||
AccountsManager accountsManager;
|
AccountsManager accountsManager;
|
||||||
|
|
|
@ -1,21 +1,21 @@
|
||||||
package org.whispersystems.textsecuregcm.util;
|
package org.whispersystems.textsecuregcm.util;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Clock;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clock class specialized for testing.
|
* Clock class specialized for testing.
|
||||||
*
|
* <p>
|
||||||
* This clock can be pinned to a particular instant or can provide the "normal" time.
|
* This clock can be pinned to a particular instant or can provide the "normal" time.
|
||||||
*
|
* <p>
|
||||||
* Unlike normal clocks it can be dynamically pinned and unpinned to help with testing.
|
* Unlike normal clocks it can be dynamically pinned and unpinned to help with testing.
|
||||||
* It should not be used in production.
|
* It should not be used in production.
|
||||||
*/
|
*/
|
||||||
public class TestClock extends java.time.Clock {
|
public class TestClock extends Clock {
|
||||||
|
|
||||||
private Optional<Instant> pinnedInstant;
|
private volatile Optional<Instant> pinnedInstant;
|
||||||
private final ZoneId zoneId;
|
private final ZoneId zoneId;
|
||||||
|
|
||||||
private TestClock(Optional<Instant> maybePinned, ZoneId id) {
|
private TestClock(Optional<Instant> maybePinned, ZoneId id) {
|
||||||
|
@ -25,10 +25,10 @@ public class TestClock extends java.time.Clock {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiate a test clock that returns the "real" time.
|
* Instantiate a test clock that returns the "real" time.
|
||||||
*
|
* <p>
|
||||||
* The clock can later be pinned to an instant if desired.
|
* The clock can later be pinned to an instant if desired.
|
||||||
*
|
*
|
||||||
* @return
|
* @return unpinned test clock.
|
||||||
*/
|
*/
|
||||||
public static TestClock now() {
|
public static TestClock now() {
|
||||||
return new TestClock(Optional.empty(), ZoneId.of("UTC"));
|
return new TestClock(Optional.empty(), ZoneId.of("UTC"));
|
||||||
|
@ -36,12 +36,12 @@ public class TestClock extends java.time.Clock {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiate a test clock pinned to a particular instant.
|
* Instantiate a test clock pinned to a particular instant.
|
||||||
*
|
* <p>
|
||||||
* The clock can later be pinned to a different instant or unpinned if desired.
|
* The clock can later be pinned to a different instant or unpinned if desired.
|
||||||
*
|
* <p>
|
||||||
* Unlike the fixed constructor no time zone is required (it defaults to UTC).
|
* Unlike the fixed constructor no time zone is required (it defaults to UTC).
|
||||||
*
|
*
|
||||||
* @param instant
|
* @param instant the instant to pin the clock to.
|
||||||
* @return test clock pinned to the given instant.
|
* @return test clock pinned to the given instant.
|
||||||
*/
|
*/
|
||||||
public static TestClock pinned(Instant instant) {
|
public static TestClock pinned(Instant instant) {
|
||||||
|
@ -50,10 +50,10 @@ public class TestClock extends java.time.Clock {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pin this test clock to the given instance.
|
* Pin this test clock to the given instance.
|
||||||
*
|
* <p>
|
||||||
* This modifies the existing clock in-place.
|
* This modifies the existing clock in-place.
|
||||||
*
|
*
|
||||||
* @param instant
|
* @param instant the instant to pin the clock to.
|
||||||
*/
|
*/
|
||||||
public void pin(Instant instant) {
|
public void pin(Instant instant) {
|
||||||
this.pinnedInstant = Optional.of(instant);
|
this.pinnedInstant = Optional.of(instant);
|
||||||
|
@ -61,7 +61,7 @@ public class TestClock extends java.time.Clock {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unpin this test clock so it will being returning the "real" time.
|
* Unpin this test clock so it will being returning the "real" time.
|
||||||
*
|
* <p>
|
||||||
* This modifies the existing clock in-place.
|
* This modifies the existing clock in-place.
|
||||||
*/
|
*/
|
||||||
public void unpin() {
|
public void unpin() {
|
||||||
|
@ -69,20 +69,19 @@ public class TestClock extends java.time.Clock {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
public TestClock withZone(ZoneId id) {
|
public TestClock withZone(ZoneId id) {
|
||||||
return new TestClock(pinnedInstant, id);
|
return new TestClock(pinnedInstant, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public ZoneId getZone() {
|
public ZoneId getZone() {
|
||||||
return zoneId;
|
return zoneId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public Instant instant() {
|
public Instant instant() {
|
||||||
return pinnedInstant.orElseGet(Instant::now);
|
return pinnedInstant.orElseGet(Instant::now);
|
||||||
}
|
}
|
||||||
|
|
||||||
public long millis() {
|
|
||||||
return instant().toEpochMilli();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue