Set clock precision to milliseconds for Datetime->Instant migration (#2999)

Our existing precision is milliseconds so we want to stick with that for
Instants. If we want to increase the precision globally after that we can do so
all in one go post-migration, but for now, it would be a bad thing to have mixed
precision going on just depending on whether a class happens to be migrated yet
or not.

This PR also migrates all existing DateTime.nowUtc() calls to use the Clock
interface, so that when they are migrated they will get the benefit of this
precision-setting as well.

BUG= http://b/496985355
This commit is contained in:
Ben McIlwain
2026-04-03 20:38:26 +00:00
committed by GitHub
parent d2881b47dc
commit 49f14b5e1b
69 changed files with 466 additions and 320 deletions
@@ -18,6 +18,7 @@ import com.google.common.annotations.VisibleForTesting;
import google.registry.monitoring.blackbox.exception.UndeterminedStateException;
import google.registry.monitoring.blackbox.message.EppRequestMessage;
import google.registry.monitoring.blackbox.message.OutboundMessageType;
import google.registry.util.Clock;
import io.netty.channel.Channel;
import jakarta.inject.Inject;
import jakarta.inject.Named;
@@ -33,6 +34,7 @@ public abstract class EppToken extends Token {
private static AtomicInteger clientIdSuffix = new AtomicInteger();
protected final String tld;
protected final Clock clock;
private String host;
private String currentDomainName;
@@ -40,15 +42,16 @@ public abstract class EppToken extends Token {
* Always the constructor used to provide any {@link EppToken}, with {@code tld} and {@code host}
* specified by Dagger.
*/
protected EppToken(String tld, String host) {
protected EppToken(String tld, String host, Clock clock) {
this.tld = tld;
this.host = host;
this.clock = clock;
currentDomainName = newDomainName(getNewTRID());
}
/** Constructor used when passing on same {@link Channel} to next {@link Token}. */
protected EppToken(String tld, String host, Channel channel) {
this(tld, host);
protected EppToken(String tld, String host, Clock clock, Channel channel) {
this(tld, host, clock);
setChannel(channel);
}
@@ -79,7 +82,7 @@ public abstract class EppToken extends Token {
private String getNewTRID() {
return String.format(
"prober-%s-%d-%d",
"localhost", System.currentTimeMillis(), clientIdSuffix.incrementAndGet());
"localhost", clock.nowUtc().getMillis(), clientIdSuffix.incrementAndGet());
}
/** Return a fully qualified domain label to use, derived from the client transaction ID. */
@@ -103,13 +106,13 @@ public abstract class EppToken extends Token {
public static class Transient extends EppToken {
@Inject
public Transient(@Named("eppTld") String tld, @Named("eppHost") String host) {
super(tld, host);
public Transient(@Named("eppTld") String tld, @Named("eppHost") String host, Clock clock) {
super(tld, host, clock);
}
@Override
public Token next() {
return new Transient(tld, host());
return new Transient(tld, host(), clock);
}
}
@@ -121,18 +124,18 @@ public abstract class EppToken extends Token {
public static class Persistent extends EppToken {
@Inject
public Persistent(@Named("eppTld") String tld, @Named("eppHost") String host) {
super(tld, host);
public Persistent(@Named("eppTld") String tld, @Named("eppHost") String host, Clock clock) {
super(tld, host, clock);
}
/** Constructor used on call to {@code next} to preserve channel. */
private Persistent(String tld, String host, Channel channel) {
super(tld, host, channel);
private Persistent(String tld, String host, Clock clock, Channel channel) {
super(tld, host, clock, channel);
}
@Override
public Token next() {
return new Persistent(tld, host(), channel());
return new Persistent(tld, host(), clock, channel());
}
}
}
@@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat;
import google.registry.monitoring.blackbox.exception.UndeterminedStateException;
import google.registry.monitoring.blackbox.message.EppRequestMessage;
import google.registry.monitoring.blackbox.util.EppUtils;
import google.registry.testing.FakeClock;
import io.netty.channel.Channel;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@@ -28,9 +29,10 @@ class EppTokenTest {
private static String TEST_HOST = "host";
private static String TEST_TLD = "tld";
private final FakeClock fakeClock = new FakeClock();
private EppToken persistentEppToken = new EppToken.Persistent(TEST_TLD, TEST_HOST);
private EppToken transientEppToken = new EppToken.Transient(TEST_TLD, TEST_HOST);
private EppToken persistentEppToken = new EppToken.Persistent(TEST_TLD, TEST_HOST, fakeClock);
private EppToken transientEppToken = new EppToken.Transient(TEST_TLD, TEST_HOST, fakeClock);
@Test
void testMessageModificationSuccess_PersistentToken() throws UndeterminedStateException {