Search in sources :

Example 16 with FakeClock

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]
}
Also used : FakeClock(com.twitter.common.util.testing.FakeClock) Time(com.twitter.common.quantity.Time) Test(org.junit.Test)

Example 17 with FakeClock

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);
}
Also used : FakeClock(com.twitter.common.util.testing.FakeClock) Before(org.junit.Before)

Example 18 with FakeClock

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();
}
Also used : FakeClock(com.twitter.common.util.testing.FakeClock) Before(org.junit.Before)

Example 19 with 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);
}
Also used : FakeClock(com.twitter.common.util.testing.FakeClock) WindowedStatistics(com.twitter.common.stats.WindowedStatistics) Test(org.junit.Test)

Example 20 with FakeClock

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);
}
Also used : FakeClock(com.twitter.common.util.testing.FakeClock) WindowedStatistics(com.twitter.common.stats.WindowedStatistics) Test(org.junit.Test)

Aggregations

FakeClock (com.twitter.common.util.testing.FakeClock)24 Test (org.junit.Test)14 Before (org.junit.Before)9 Time (com.twitter.common.quantity.Time)7 WindowedStatistics (com.twitter.common.stats.WindowedStatistics)6 Function (com.google.common.base.Function)2 BackoffDecider (com.twitter.common.util.BackoffDecider)2 Random (com.twitter.common.util.Random)2 TruncatedBinaryBackoff (com.twitter.common.util.TruncatedBinaryBackoff)2 Collection (java.util.Collection)2 AbstractModule (com.google.inject.AbstractModule)1 Injector (com.google.inject.Injector)1 Singleton (com.google.inject.Singleton)1 GuiceServletContextListener (com.google.inject.servlet.GuiceServletContextListener)1 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)1 DefaultClientConfig (com.sun.jersey.api.client.config.DefaultClientConfig)1 JerseyServletModule (com.sun.jersey.guice.JerseyServletModule)1 GuiceContainer (com.sun.jersey.guice.spi.container.servlet.GuiceContainer)1 ContainerRequest (com.sun.jersey.spi.container.ContainerRequest)1 ContainerResponse (com.sun.jersey.spi.container.ContainerResponse)1