Skip to content

Commit

Permalink
improve randomization of FakeFileTimeSource.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 575867828
  • Loading branch information
kluever authored and Jimfs Team committed Oct 23, 2023
1 parent bdcd240 commit fcec619
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 33 deletions.
34 changes: 14 additions & 20 deletions jimfs/src/test/java/com/google/common/jimfs/FakeFileTimeSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit fcec619

Please sign in to comment.