Skip to content

Commit

Permalink
Merge pull request #52 from chang-chao/master
Browse files Browse the repository at this point in the history
Initializes Reservoir reset setting  eagerly.
  • Loading branch information
chrisradek authored Jan 15, 2019
2 parents 35d99a5 + e54f28f commit 0ef1114
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
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

0 comments on commit 0ef1114

Please sign in to comment.