public void statsShowSpuriousValues() {
final long startTime = 1445468640; // Some start time : Oct 21, 2015
final int resetDurationMs = 1000;
final int numberOfSampleWindows = 2;
final int tinyDurationMs = 10;
Time mockTime = mock(Time.class);
when(mockTime.milliseconds()).thenReturn(startTime);
RequestCounter rc = new RequestCounter("Tests.statsShowSpuriousValues",
resetDurationMs / numberOfSampleWindows,
mockTime);
// Add some new stats and verify they were calculated correctly
rc.addRequest(100 * NS_PER_MS, 0, 1000, 100, 1);
rc.addRequest(50 * NS_PER_MS, 0, 1000, 100, 2);
rc.addRequest(50 * NS_PER_MS, 0, 1000, 100, 3);
rc.addRequest(50 * NS_PER_MS, 0, 1000, 100, 4);
rc.addRequest(50 * NS_PER_MS, 0, 1000, 100, 5);
// Jump into the counter window just a little (10 ms)
when(mockTime.milliseconds()).thenReturn(startTime + tinyDurationMs);
assertTrue("Throughput should not be absurdly high at beginning of window...", rc.getThroughput() <= 10);
// Jump in time past the reset duration
when(mockTime.milliseconds()).thenReturn(startTime + resetDurationMs + 1);
assertEquals("Make sure counter's value has expired", 0d, rc.getThroughput(), 0.0f);
}