From 90490c9c84852ed05f65b26e10eff5cd05066dfb Mon Sep 17 00:00:00 2001 From: erik-signal <113138376+erik-signal@users.noreply.github.com> Date: Fri, 21 Oct 2022 15:27:15 -0400 Subject: [PATCH] Clean up the TestClock code a bit more. --- .../textsecuregcm/storage/AccountsTest.java | 2 +- .../controllers/DonationControllerTest.java | 2 +- .../textsecuregcm/util/TestClock.java | 33 +++++++++---------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsTest.java index d488b9d51..c16c1d773 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsTest.java @@ -79,7 +79,7 @@ class AccountsTest { .build()) .build(); - private TestClock clock = TestClock.pinned(Instant.EPOCH); + private final TestClock clock = TestClock.pinned(Instant.EPOCH); private DynamicConfigurationManager mockDynamicConfigManager; private Accounts accounts; diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/tests/controllers/DonationControllerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/tests/controllers/DonationControllerTest.java index a5f80a6e7..94dd67b39 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/tests/controllers/DonationControllerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/tests/controllers/DonationControllerTest.java @@ -100,7 +100,7 @@ class DonationControllerTest { 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; RedeemedReceiptsManager redeemedReceiptsManager; AccountsManager accountsManager; diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/util/TestClock.java b/service/src/test/java/org/whispersystems/textsecuregcm/util/TestClock.java index ad665f255..3d33ab75b 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/util/TestClock.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/util/TestClock.java @@ -1,21 +1,21 @@ package org.whispersystems.textsecuregcm.util; -import java.time.Duration; +import java.time.Clock; import java.time.Instant; import java.time.ZoneId; import java.util.Optional; /** * Clock class specialized for testing. - * + *

* This clock can be pinned to a particular instant or can provide the "normal" time. - * + *

* Unlike normal clocks it can be dynamically pinned and unpinned to help with testing. * It should not be used in production. */ -public class TestClock extends java.time.Clock { +public class TestClock extends Clock { - private Optional pinnedInstant; + private volatile Optional pinnedInstant; private final ZoneId zoneId; private TestClock(Optional maybePinned, ZoneId id) { @@ -25,10 +25,10 @@ public class TestClock extends java.time.Clock { /** * Instantiate a test clock that returns the "real" time. - * + *

* The clock can later be pinned to an instant if desired. * - * @return + * @return unpinned test clock. */ public static TestClock now() { 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. - * + *

* The clock can later be pinned to a different instant or unpinned if desired. - * + *

* 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. */ 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. - * + *

* This modifies the existing clock in-place. * - * @param instant + * @param instant the instant to pin the clock to. */ public void pin(Instant 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. - * + *

* This modifies the existing clock in-place. */ public void unpin() { @@ -69,20 +69,19 @@ public class TestClock extends java.time.Clock { } + @Override public TestClock withZone(ZoneId id) { return new TestClock(pinnedInstant, id); } + @Override public ZoneId getZone() { return zoneId; } + @Override public Instant instant() { return pinnedInstant.orElseGet(Instant::now); } - public long millis() { - return instant().toEpochMilli(); - } - }