File tree Expand file tree Collapse file tree
aws-xray-recorder-sdk-core/src
main/java/com/amazonaws/xray/strategy/sampling/reservoir
test/java/com/amazonaws/xray/strategy/sampling/reservoir Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ public class Reservoir {
1111 private final int tracesPerSecond ;
1212 private final MaxFunction maxFunction ;
1313 private final AtomicInteger usage = new AtomicInteger (0 );
14- private final AtomicLong nextReset = new AtomicLong ( 0 ) ;
14+ private final AtomicLong nextReset ;
1515
1616 public Reservoir () {
1717 this (0 );
@@ -21,6 +21,9 @@ public Reservoir(int tracesPerSecond) {
2121 this .tracesPerSecond = tracesPerSecond ;
2222 this .maxFunction =
2323 tracesPerSecond < 10 ? new LessThan10 (tracesPerSecond ) : new AtLeast10 (tracesPerSecond );
24+
25+ long now = System .nanoTime ();
26+ this .nextReset = new AtomicLong (now + NANOS_PER_SECOND );
2427 }
2528
2629 public boolean take () {
Original file line number Diff line number Diff line change @@ -36,6 +36,19 @@ public class ReservoirTest {
3636 assertFalse (reservoir .take ());
3737 }
3838
39+ @ Test public void samplesFairNegativeNanoTime () {
40+ mockStatic (System .class );
41+ when (System .nanoTime ()).thenReturn (-2 * NANOS_PER_SECOND );
42+ Reservoir reservoir = new Reservoir (10 );
43+
44+ when (System .nanoTime ()).thenReturn (-2 * NANOS_PER_SECOND + 1 );
45+ assertTrue (reservoir .take ());
46+ when (System .nanoTime ()).thenReturn (-2 * NANOS_PER_SECOND + 2 );
47+ assertTrue (reservoir .take ());
48+ when (System .nanoTime ()).thenReturn (-2 * NANOS_PER_SECOND + 2 );
49+ assertFalse (reservoir .take ());
50+ }
51+
3952 @ Test public void resetsAfterASecond () {
4053 mockStatic (System .class );
4154
You can’t perform that action at this time.
0 commit comments