LabeledMetrics { public static Counter counter(MonitoringInfoMetricName metricName) { return new DelegatingCounter(metricName); }  static Counter counter(MonitoringInfoMetricName metricName);  }
@Test public void testCounterDoesNotFailOperationsWhenNoMetricsContainerPresent() { MetricsEnvironment.setCurrentContainer(null); assertNull(MetricsEnvironment.getCurrentContainer()); HashMap<String, String> labels = new HashMap<String, String>(); String urn = MonitoringInfoConstants.Urns.ELEMENT_COUNT; MonitoringInfoMetricName name = MonitoringInfoMetricName.named(urn, labels); Counter counter = LabeledMetrics.counter(name); counter.inc(); counter.inc(5L); counter.dec(); counter.dec(5L); }
@Test public void testOperationsUpdateCounterFromContainerWhenContainerIsPresent() { HashMap<String, String> labels = new HashMap<String, String>(); String urn = MonitoringInfoConstants.Urns.ELEMENT_COUNT; MonitoringInfoMetricName name = MonitoringInfoMetricName.named(urn, labels); MetricsContainer mockContainer = Mockito.mock(MetricsContainer.class); Counter mockCounter = Mockito.mock(Counter.class); when(mockContainer.getCounter(name)).thenReturn(mockCounter); Counter counter = LabeledMetrics.counter(name); MetricsEnvironment.setCurrentContainer(mockContainer); counter.inc(); verify(mockCounter).inc(1); counter.inc(47L); verify(mockCounter).inc(47); counter.dec(5L); verify(mockCounter).inc(-5); }