use of com.twitter.common.util.testing.FakeClock in project commons by twitter.
the class WindowedTest method testWindowed.
@Test
public void testWindowed() {
Amount<Long, Time> window = Amount.of(1L, Time.MINUTES);
int slices = 3;
Amount<Long, Time> delta = Amount.of(Amount.of(1L, Time.MINUTES).as(Time.NANOSECONDS) / 3, Time.NANOSECONDS);
FakeClock clock = new FakeClock();
WindowedBox win = new WindowedBox(window, slices, clock);
// [0][0][0][0]
clock.advance(Amount.of(1L, Time.NANOSECONDS));
win.increment();
// [0][0][0][1]
Assert.assertEquals(0, win.sum());
clock.advance(delta);
win.increment();
win.increment();
Assert.assertEquals(1, win.sum());
// [0][0][1][2]
clock.advance(delta);
win.increment();
win.increment();
win.increment();
Assert.assertEquals(3, win.sum());
// [0][1][2][3]
clock.advance(delta);
win.increment();
win.increment();
win.increment();
win.increment();
Assert.assertEquals(6, win.sum());
// [1][2][3][4]
clock.advance(delta);
win.increment();
win.increment();
win.increment();
win.increment();
win.increment();
Assert.assertEquals(9, win.sum());
// [2][3][4][5]
}
use of com.twitter.common.util.testing.FakeClock in project commons by twitter.
the class TrafficMonitorTest method setUp.
@Before
public void setUp() {
clock = new FakeClock();
monitor = new TrafficMonitor<String>("test service", clock);
}
use of com.twitter.common.util.testing.FakeClock in project commons by twitter.
the class TimeSeriesRepositoryImplTest method setUp.
@Before
public void setUp() {
control = createStrictControl();
statRegistry = control.createMock(StatRegistry.class);
repo = new TimeSeriesRepositoryImpl(statRegistry, SAMPLE_PERIOD, RETENTION_PERIOD);
clock = new FakeClock();
}
use of com.twitter.common.util.testing.FakeClock in project commons by twitter.
the class WindowedStatsTest method testWindowStats.
@Test
public void testWindowStats() {
FakeClock clock = new FakeClock();
WindowedStatistics ws = new WindowedStatistics(window, slices, clock);
ws.accumulate(1L);
assertEmpty(ws);
clock.advance(Amount.of(1 + sliceDuration, Time.NANOSECONDS));
ws.refresh();
assertEquals(1L, ws.max());
assertEquals(1L, ws.min());
assertEquals(1L, ws.populationSize());
assertEquals(1L, ws.sum());
assertEquals(1.0, ws.mean(), 0.01);
assertEquals(0.0, ws.standardDeviation(), 0.01);
clock.advance(Amount.of(slices * sliceDuration, Time.NANOSECONDS));
ws.refresh();
assertEmpty(ws);
}
use of com.twitter.common.util.testing.FakeClock in project commons by twitter.
the class WindowedStatsTest method testAddNewValueToFullWS.
@Test
public void testAddNewValueToFullWS() {
FakeClock clock = new FakeClock();
WindowedStatistics ws = new WindowedStatistics(window, slices, clock);
// AAAAA
// BBBBB
// CCCCC
// DDDDD
// | | | |
// ---------------------------------> t
// t=0 t=1 t=2 t=3
// t=0 fill {D}
long n = 1000L;
for (int i = 0; i < n; i++) {
ws.accumulate(i);
}
// read {A,B,C}, which should be empty
assertEmpty(ws);
clock.advance(Amount.of(1 + sliceDuration, Time.NANOSECONDS));
ws.refresh();
// t=1, read {B,C,D} which shouldn't be empty
assertEquals(n - 1L, ws.max());
assertEquals(0L, ws.min());
assertEquals(n, ws.populationSize());
assertEquals(n * (n - 1) / 2, ws.sum());
assertEquals((n - 1) / 2.0, ws.mean(), 0.01);
clock.advance(Amount.of(1 + sliceDuration, Time.NANOSECONDS));
ws.refresh();
// t=2, read {C,D,A} which shouldn't be empty as well
assertEquals(n - 1L, ws.max());
assertEquals(0L, ws.min());
assertEquals(n, ws.populationSize());
assertEquals(n * (n - 1) / 2, ws.sum());
assertEquals((n - 1) / 2.0, ws.mean(), 0.01);
clock.advance(Amount.of(1 + sliceDuration, Time.NANOSECONDS));
ws.refresh();
// t=3, read {D,A,B} which shouldn't be empty as well
assertEquals(n - 1L, ws.max());
assertEquals(0L, ws.min());
assertEquals(n, ws.populationSize());
assertEquals(n * (n - 1) / 2, ws.sum());
assertEquals((n - 1) / 2.0, ws.mean(), 0.01);
clock.advance(Amount.of(1 + sliceDuration, Time.NANOSECONDS));
ws.refresh();
// t=4, read {A,B,C} which must be empty (cleaned by the Windowed class)
assertEmpty(ws);
}
Aggregations