MetricProvider implements Provider<Metric> { @Override public Metric get() { return metric; } MetricProvider(MetricConsumerProvider provider); @Override Metric get(); @Override void deconstruct();  }
@Test public void requireThatMetricProviderDelegatesToConsumerFactory() { MetricConsumer consumer = Mockito.mock(MetricConsumer.class); MetricProvider provider = MetricProviders.newInstance(consumer); Metric metric = provider.get(); assertNotNull(metric); Metric.Context fooCtx = Mockito.mock(Metric.Context.class); metric.add("foo", 6, fooCtx); metric.set("foo", 9, fooCtx); Mockito.verify(consumer, Mockito.times(1)).add("foo", 6, fooCtx); Mockito.verify(consumer, Mockito.times(1)).set("foo", 9, fooCtx); }
@Test public void requireThatThreadLocalConsumersAreProvided() throws Exception { AtomicInteger cnt = new AtomicInteger(0); final MetricProvider metricProvider = MetricProviders.newInstance(MetricConsumerFactories.newCounter(cnt)); assertEquals(0, cnt.get()); metricProvider.get().add("foo", 6, null); assertEquals(1, cnt.get()); metricProvider.get().add("bar", 9, null); assertEquals(1, cnt.get()); ExecutorService executor = Executors.newSingleThreadExecutor(); assertTrue(executor.submit(new MetricTask(metricProvider)).get(60, TimeUnit.SECONDS)); assertEquals(2, cnt.get()); assertTrue(executor.submit(new MetricTask(metricProvider)).get(60, TimeUnit.SECONDS)); assertEquals(2, cnt.get()); }