From fcec619e4a379a4ff80ad518e4298b2a05742651 Mon Sep 17 00:00:00 2001 From: Kurt Alfred Kluever Date: Mon, 23 Oct 2023 10:55:10 -0700 Subject: [PATCH] improve randomization of `FakeFileTimeSource`. RELNOTES=n/a PiperOrigin-RevId: 575867828 --- .../common/jimfs/FakeFileTimeSource.java | 34 ++++++++----------- .../common/jimfs/FileSystemStateTest.java | 4 +-- .../common/jimfs/JimfsFileChannelTest.java | 23 +++++++------ 3 files changed, 28 insertions(+), 33 deletions(-) diff --git a/jimfs/src/test/java/com/google/common/jimfs/FakeFileTimeSource.java b/jimfs/src/test/java/com/google/common/jimfs/FakeFileTimeSource.java index 942c8354..991cc206 100644 --- a/jimfs/src/test/java/com/google/common/jimfs/FakeFileTimeSource.java +++ b/jimfs/src/test/java/com/google/common/jimfs/FakeFileTimeSource.java @@ -16,13 +16,11 @@ package com.google.common.jimfs; -import static com.google.common.base.Preconditions.checkNotNull; - -import com.google.common.math.LongMath; import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.nio.file.attribute.FileTime; +import java.time.Duration; +import java.time.Instant; import java.util.Random; -import java.util.concurrent.TimeUnit; /** Fake implementation of {@link FileTimeSource}. */ final class FakeFileTimeSource implements FileTimeSource { @@ -34,27 +32,23 @@ final class FakeFileTimeSource implements FileTimeSource { randomize(); } - @CanIgnoreReturnValue - FakeFileTimeSource setNow(FileTime now) { - this.now = checkNotNull(now); - return this; - } - - @CanIgnoreReturnValue - private FakeFileTimeSource setNowMillis(long millis) { - return setNow(FileTime.fromMillis(millis)); - } - @CanIgnoreReturnValue FakeFileTimeSource randomize() { - // Use a random int rather than long as an easy way of ensuring we don't get something near - // Long.MAX_VALUE - return setNowMillis(random.nextInt()); + Instant randomNow = + Instant.ofEpochSecond( + random + .longs(Instant.MIN.getEpochSecond(), Instant.MAX.getEpochSecond()) + .findAny() + .getAsLong(), + random.nextInt(1_000_000_000)); + this.now = FileTime.from(randomNow); + return this; } @CanIgnoreReturnValue - FakeFileTimeSource advance(long duration, TimeUnit unit) { - return setNowMillis(LongMath.checkedAdd(now.toMillis(), unit.toMillis(duration))); + FakeFileTimeSource advance(Duration duration) { + this.now = FileTime.from(now.toInstant().plus(duration)); + return this; } @Override diff --git a/jimfs/src/test/java/com/google/common/jimfs/FileSystemStateTest.java b/jimfs/src/test/java/com/google/common/jimfs/FileSystemStateTest.java index 7274ab1e..8ad5c51c 100644 --- a/jimfs/src/test/java/com/google/common/jimfs/FileSystemStateTest.java +++ b/jimfs/src/test/java/com/google/common/jimfs/FileSystemStateTest.java @@ -17,7 +17,6 @@ package com.google.common.jimfs; import static com.google.common.truth.Truth.assertThat; -import static java.util.concurrent.TimeUnit.SECONDS; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -28,6 +27,7 @@ import java.io.Closeable; import java.io.IOException; import java.nio.file.ClosedFileSystemException; +import java.time.Duration; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; @@ -66,7 +66,7 @@ public void testCheckOpen() throws IOException { @Test public void testNow() { assertThat(state.now()).isEqualTo(fileTimeSource.now()); - fileTimeSource.advance(1, SECONDS); + fileTimeSource.advance(Duration.ofSeconds(1)); assertThat(state.now()).isEqualTo(fileTimeSource.now()); } diff --git a/jimfs/src/test/java/com/google/common/jimfs/JimfsFileChannelTest.java b/jimfs/src/test/java/com/google/common/jimfs/JimfsFileChannelTest.java index 28723069..295dd7c1 100644 --- a/jimfs/src/test/java/com/google/common/jimfs/JimfsFileChannelTest.java +++ b/jimfs/src/test/java/com/google/common/jimfs/JimfsFileChannelTest.java @@ -49,6 +49,7 @@ import java.nio.channels.NonWritableChannelException; import java.nio.file.OpenOption; import java.nio.file.attribute.FileTime; +import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; @@ -237,68 +238,68 @@ public void testFileTimeUpdates() throws IOException { // accessedTime FileTime accessTime = file.getLastAccessTime(); - fileTimeSource.advance(2, MILLISECONDS); + fileTimeSource.advance(Duration.ofMillis(2)); channel.read(ByteBuffer.allocate(10)); assertNotEquals(accessTime, file.getLastAccessTime()); accessTime = file.getLastAccessTime(); - fileTimeSource.advance(2, MILLISECONDS); + fileTimeSource.advance(Duration.ofMillis(2)); channel.read(ByteBuffer.allocate(10), 0); assertNotEquals(accessTime, file.getLastAccessTime()); accessTime = file.getLastAccessTime(); - fileTimeSource.advance(2, MILLISECONDS); + fileTimeSource.advance(Duration.ofMillis(2)); channel.read(new ByteBuffer[] {ByteBuffer.allocate(10)}); assertNotEquals(accessTime, file.getLastAccessTime()); accessTime = file.getLastAccessTime(); - fileTimeSource.advance(2, MILLISECONDS); + fileTimeSource.advance(Duration.ofMillis(2)); channel.read(new ByteBuffer[] {ByteBuffer.allocate(10)}, 0, 1); assertNotEquals(accessTime, file.getLastAccessTime()); accessTime = file.getLastAccessTime(); - fileTimeSource.advance(2, MILLISECONDS); + fileTimeSource.advance(Duration.ofMillis(2)); channel.transferTo(0, 10, new ByteBufferChannel(10)); assertNotEquals(accessTime, file.getLastAccessTime()); // modified FileTime modifiedTime = file.getLastModifiedTime(); - fileTimeSource.advance(2, MILLISECONDS); + fileTimeSource.advance(Duration.ofMillis(2)); channel.write(ByteBuffer.allocate(10)); assertNotEquals(modifiedTime, file.getLastModifiedTime()); modifiedTime = file.getLastModifiedTime(); - fileTimeSource.advance(2, MILLISECONDS); + fileTimeSource.advance(Duration.ofMillis(2)); channel.write(ByteBuffer.allocate(10), 0); assertNotEquals(modifiedTime, file.getLastModifiedTime()); modifiedTime = file.getLastModifiedTime(); - fileTimeSource.advance(2, MILLISECONDS); + fileTimeSource.advance(Duration.ofMillis(2)); channel.write(new ByteBuffer[] {ByteBuffer.allocate(10)}); assertNotEquals(modifiedTime, file.getLastModifiedTime()); modifiedTime = file.getLastModifiedTime(); - fileTimeSource.advance(2, MILLISECONDS); + fileTimeSource.advance(Duration.ofMillis(2)); channel.write(new ByteBuffer[] {ByteBuffer.allocate(10)}, 0, 1); assertNotEquals(modifiedTime, file.getLastModifiedTime()); modifiedTime = file.getLastModifiedTime(); - fileTimeSource.advance(2, MILLISECONDS); + fileTimeSource.advance(Duration.ofMillis(2)); channel.truncate(0); assertNotEquals(modifiedTime, file.getLastModifiedTime()); modifiedTime = file.getLastModifiedTime(); - fileTimeSource.advance(2, MILLISECONDS); + fileTimeSource.advance(Duration.ofMillis(2)); channel.transferFrom(new ByteBufferChannel(10), 0, 10); assertNotEquals(modifiedTime, file.getLastModifiedTime());