Search in sources :

Example 1 with FakeClock

use of com.twitter.common.util.testing.FakeClock in project commons by twitter.

the class HttpStatsFilterIntegrationTest method setUp.

@Before
public void setUp() {
    Stats.flush();
    server = new JettyHttpServerDispatch();
    server.listen(0);
    server.registerFilter(GuiceFilter.class, "/*");
    clock = new FakeClock();
    final Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(TestServlet.class).in(Singleton.class);
            bind(Clock.class).toInstance(clock);
            bind(HttpStatsFilter.class).in(Singleton.class);
        }
    }, new JerseyServletModule() {

        @Override
        protected void configureServlets() {
            filter("/*").through(HttpStatsFilter.class);
            serve("/*").with(GuiceContainer.class, ImmutableMap.of(PROPERTY_CONTAINER_RESPONSE_FILTERS, HttpStatsFilter.class.getName()));
        }
    });
    server.getRootContext().addEventListener(new GuiceServletContextListener() {

        @Override
        protected Injector getInjector() {
            return injector;
        }
    });
    ClientConfig config = new DefaultClientConfig();
    client = Client.create(config);
}
Also used : JettyHttpServerDispatch(com.twitter.common.net.http.JettyHttpServerDispatch) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) FakeClock(com.twitter.common.util.testing.FakeClock) Injector(com.google.inject.Injector) Singleton(com.google.inject.Singleton) JerseyServletModule(com.sun.jersey.guice.JerseyServletModule) GuiceContainer(com.sun.jersey.guice.spi.container.servlet.GuiceContainer) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) GuiceServletContextListener(com.google.inject.servlet.GuiceServletContextListener) AbstractModule(com.google.inject.AbstractModule) Before(org.junit.Before)

Example 2 with FakeClock

use of com.twitter.common.util.testing.FakeClock in project commons by twitter.

the class MarkDeadStrategyWithHostCheckTest method setUp.

@Before
public void setUp() {
    wrappedStrategy = createMock(new Clazz<LoadBalancingStrategy<String>>() {
    });
    onBackendsChosen = createMock(new Clazz<Closure<Collection<String>>>() {
    });
    random = createMock(Random.class);
    clock = new FakeClock();
    Function<String, BackoffDecider> backoffFactory = new Function<String, BackoffDecider>() {

        @Override
        public BackoffDecider apply(String s) {
            return BackoffDecider.builder(s).withSeedSize(1).withClock(clock).withRandom(random).withTolerateFailureRate(0.5).withStrategy(new TruncatedBinaryBackoff(INITIAL_BACKOFF, MAX_BACKOFF)).withRecoveryType(BackoffDecider.RecoveryType.FULL_CAPACITY).withRequestWindow(MAX_BACKOFF).build();
        }
    };
    markDead = new MarkDeadStrategyWithHostCheck<String>(wrappedStrategy, backoffFactory);
}
Also used : Function(com.google.common.base.Function) TruncatedBinaryBackoff(com.twitter.common.util.TruncatedBinaryBackoff) Random(com.twitter.common.util.Random) BackoffDecider(com.twitter.common.util.BackoffDecider) FakeClock(com.twitter.common.util.testing.FakeClock) Collection(java.util.Collection) Before(org.junit.Before)

Example 3 with FakeClock

use of com.twitter.common.util.testing.FakeClock in project commons by twitter.

the class RequestLoggerTest method setUp.

@Before
public void setUp() throws Exception {
    clock = new FakeClock();
    sink = createMock(LogSink.class);
    request = createMock(Request.class);
    response = createMock(Response.class);
    log = new RequestLogger(clock, sink);
}
Also used : Response(org.mortbay.jetty.Response) LogSink(com.twitter.common.net.http.RequestLogger.LogSink) FakeClock(com.twitter.common.util.testing.FakeClock) Request(org.mortbay.jetty.Request) Before(org.junit.Before)

Example 4 with FakeClock

use of com.twitter.common.util.testing.FakeClock in project commons by twitter.

the class HttpStatsFilterTest method setUp.

@Before
public void setUp() throws Exception {
    clock = new FakeClock();
    request = createMock(HttpServletRequest.class);
    response = createMock(HttpServletResponse.class);
    filterChain = createMock(FilterChain.class);
    filter = new HttpStatsFilter(clock);
    containerRequest = createMock(ContainerRequest.class);
    containerResponse = createMock(ContainerResponse.class);
    injectContextVars();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ContainerResponse(com.sun.jersey.spi.container.ContainerResponse) FakeClock(com.twitter.common.util.testing.FakeClock) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) ContainerRequest(com.sun.jersey.spi.container.ContainerRequest) Before(org.junit.Before)

Example 5 with FakeClock

use of com.twitter.common.util.testing.FakeClock in project commons by twitter.

the class HistogramTest method testHistogramWithMemoryConstraints.

@Test
public void testHistogramWithMemoryConstraints() {
    int n = 10000;
    FakeClock clock = new FakeClock();
    int slices = 2;
    double error = DEFAULT_PRECISION.getEpsilon() * n * slices;
    // We suppose here that 32KB is enough for the default precision of (slices + 1) histograms
    HistogramInterface hist = new Histogram(name, Amount.of(1L, Time.MINUTES), slices, Amount.of(32L, Data.KB), null, DEFAULT_QUANTILES, clock);
    for (int i = 1; i <= n; ++i) {
        hist.add(i);
    }
    clock.advance(Amount.of(31L, Time.SECONDS));
    Snapshot sample = hist.snapshot();
    assertEquals(1L, sample.min());
    assertEquals((long) n, sample.max());
    assertEquals((long) n, sample.count());
    assertEquals((long) (n * (n + 1) / 2), sample.sum());
    assertEquals(n * (n + 1) / (2.0 * n), sample.avg(), 0.1);
    long[] expected = new long[DEFAULT_QUANTILES.length];
    for (int i = 0; i < DEFAULT_QUANTILES.length; i++) {
        expected[i] = (long) (DEFAULT_QUANTILES[i] * n);
    }
    checkQuantiles(expected, sample, error);
}
Also used : FakeClock(com.twitter.common.util.testing.FakeClock) 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