BufferPoolMetricSet implements MetricSet { @Override public Map<String, Metric> getMetrics() { final Map<String, Metric> gauges = new HashMap<>(); for (String pool : POOLS) { for (int i = 0; i < ATTRIBUTES.length; i++) { final String attribute = ATTRIBUTES[i]; final String name = NAMES[i]; try { final ObjectName on = new ObjectName("java.nio:type=BufferPool,name=" + pool); mBeanServer.getMBeanInfo(on); gauges.put(name(pool, name), new JmxAttributeGauge(mBeanServer, on, attribute)); } catch (JMException ignored) { LOGGER.debug("Unable to load buffer pool MBeans, possibly running on Java 6"); } } } return Collections.unmodifiableMap(gauges); } BufferPoolMetricSet(MBeanServer mBeanServer); @Override Map<String, Metric> getMetrics();  }
@Test public void includesGaugesForDirectAndMappedPools() { assertThat(buffers.getMetrics().keySet()) .containsOnly("direct.count", "mapped.used", "mapped.capacity", "direct.capacity", "mapped.count", "direct.used"); }
@Test public void ignoresGaugesForObjectsWhichCannotBeFound() throws Exception { when(mBeanServer.getMBeanInfo(mapped)).thenThrow(new InstanceNotFoundException()); assertThat(buffers.getMetrics().keySet()) .containsOnly("direct.count", "direct.capacity", "direct.used"); }
@Test public void includesAGaugeForDirectCount() throws Exception { final Gauge gauge = (Gauge) buffers.getMetrics().get("direct.count"); when(mBeanServer.getAttribute(direct, "Count")).thenReturn(100); assertThat(gauge.getValue()) .isEqualTo(100); }
@Test public void includesAGaugeForDirectMemoryUsed() throws Exception { final Gauge gauge = (Gauge) buffers.getMetrics().get("direct.used"); when(mBeanServer.getAttribute(direct, "MemoryUsed")).thenReturn(100); assertThat(gauge.getValue()) .isEqualTo(100); }
@Test public void includesAGaugeForDirectCapacity() throws Exception { final Gauge gauge = (Gauge) buffers.getMetrics().get("direct.capacity"); when(mBeanServer.getAttribute(direct, "TotalCapacity")).thenReturn(100); assertThat(gauge.getValue()) .isEqualTo(100); }
@Test public void includesAGaugeForMappedCount() throws Exception { final Gauge gauge = (Gauge) buffers.getMetrics().get("mapped.count"); when(mBeanServer.getAttribute(mapped, "Count")).thenReturn(100); assertThat(gauge.getValue()) .isEqualTo(100); }
@Test public void includesAGaugeForMappedMemoryUsed() throws Exception { final Gauge gauge = (Gauge) buffers.getMetrics().get("mapped.used"); when(mBeanServer.getAttribute(mapped, "MemoryUsed")).thenReturn(100); assertThat(gauge.getValue()) .isEqualTo(100); }
@Test public void includesAGaugeForMappedCapacity() throws Exception { final Gauge gauge = (Gauge) buffers.getMetrics().get("mapped.capacity"); when(mBeanServer.getAttribute(mapped, "TotalCapacity")).thenReturn(100); assertThat(gauge.getValue()) .isEqualTo(100); }