StatsFilter extends SimpleFilter<Req, Rep> { @Override public Future<Rep> apply(Req req, Service<Req, Rep> service) { Future<Rep> result = null; outstandingAsync.inc(); final Stopwatch stopwatch = Stopwatch.createStarted(); try { result = service.apply(req); serviceExec.registerSuccessfulEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS)); } finally { outstandingAsync.dec(); if (null == result) { serviceExec.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS)); } } return result; } StatsFilter(StatsLogger stats); @Override Future<Rep> apply(Req req, Service<Req, Rep> service);  }
@Test(timeout = 60000) public void testServiceSuccess() throws Exception { StatsLogger stats = new NullStatsLogger(); StatsFilter<String, String> filter = new StatsFilter<String, String>(stats); Future<String> result = filter.apply("", new ConstantService<String, String>(Future.value("result"))); assertEquals("result", Await.result(result)); }
@Test(timeout = 60000) public void testServiceFailure() throws Exception { StatsLogger stats = new NullStatsLogger(); StatsFilter<String, String> filter = new StatsFilter<String, String>(stats); try { filter.apply("", new RuntimeExService<String, String>()); } catch (RuntimeException ex) { } }