MINOR: Fix for MetadataQuorumCommandErrorTest.testRelativeTimeMs (#13784)

Reviewers: Divij Vaidya <diviv@amazon.com>, David Jacot <djacot@confluent.io>
This commit is contained in:
Federico Valeri 2023-05-31 18:48:26 +02:00 committed by GitHub
parent 03ab563206
commit 7e9a82c732
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 3 deletions

View File

@ -195,7 +195,7 @@ public class MetadataQuorumCommand {
static long relativeTimeMs(long timestampMs, String desc) {
Instant lastTimestamp = Instant.ofEpochMilli(timestampMs);
Instant now = Instant.now();
if (!(lastTimestamp.isAfter(Instant.EPOCH) && lastTimestamp.isBefore(now))) {
if (!(lastTimestamp.isAfter(Instant.EPOCH) && (lastTimestamp.isBefore(now) || lastTimestamp.equals(now)))) {
throw new KafkaException(
format("Error while computing relative time, possible drift in system clock.%n" +
"Current timestamp is %d, %s timestamp is %d", now.toEpochMilli(), desc, timestampMs)

View File

@ -53,12 +53,14 @@ public class MetadataQuorumCommandErrorTest {
@Test
public void testRelativeTimeMs() {
long validEpochMs = Instant.now().minusSeconds(5).toEpochMilli();
assertTrue(MetadataQuorumCommand.relativeTimeMs(validEpochMs, "test") >= 0);
long nowMs = Instant.now().toEpochMilli();
assertTrue(MetadataQuorumCommand.relativeTimeMs(nowMs, "test") >= 0);
long invalidEpochMs = Instant.EPOCH.minus(1, ChronoUnit.DAYS).toEpochMilli();
assertThrows(KafkaException.class, () -> MetadataQuorumCommand.relativeTimeMs(invalidEpochMs, "test"));
long futureTimestampMs = Instant.now().plus(1, ChronoUnit.DAYS).toEpochMilli();
assertThrows(KafkaException.class, () -> MetadataQuorumCommand.relativeTimeMs(futureTimestampMs, "test"));
long futureEpochMs = Instant.now().plus(1, ChronoUnit.DAYS).toEpochMilli();
assertThrows(KafkaException.class, () -> MetadataQuorumCommand.relativeTimeMs(futureEpochMs, "test"));
}
}