Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initializes Reservoir reset setting eagerly. #52

Merged
merged 2 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Reservoir {
private final int tracesPerSecond;
private final MaxFunction maxFunction;
private final AtomicInteger usage = new AtomicInteger(0);
private final AtomicLong nextReset = new AtomicLong(0);
private final AtomicLong nextReset;

public Reservoir() {
this(0);
Expand All @@ -21,6 +21,9 @@ public Reservoir(int tracesPerSecond) {
this.tracesPerSecond = tracesPerSecond;
this.maxFunction =
tracesPerSecond < 10 ? new LessThan10(tracesPerSecond) : new AtLeast10(tracesPerSecond);

long now = System.nanoTime();
this.nextReset = new AtomicLong(now + NANOS_PER_SECOND);
}

public boolean take() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ public class ReservoirTest {
assertFalse(reservoir.take());
}

@Test public void samplesFairNegativeNanoTime() {
mockStatic(System.class);
when(System.nanoTime()).thenReturn(-2 * NANOS_PER_SECOND);
Reservoir reservoir = new Reservoir(10);

when(System.nanoTime()).thenReturn(-2 * NANOS_PER_SECOND + 1);
assertTrue(reservoir.take());
when(System.nanoTime()).thenReturn(-2 * NANOS_PER_SECOND + 2);
assertTrue(reservoir.take());
when(System.nanoTime()).thenReturn(-2 * NANOS_PER_SECOND + 2);
assertFalse(reservoir.take());
}

@Test public void resetsAfterASecond() {
mockStatic(System.class);

Expand Down