GraphiteMetricObserver extends BaseMetricObserver { public void stop() { try { if (socket != null) { socket.close(); socket = null; LOGGER.info("Disconnected from graphite server: {}", graphiteServerURI); } } catch (IOException e) { LOGGER.warn("Error Stopping", e); } } GraphiteMetricObserver(String metricPrefix, String graphiteServerAddress);  GraphiteMetricObserver(String metricPrefix, String graphiteServerAddress,
                                GraphiteNamingConvention namingConvention); void stop(); @Override void updateImpl(List<Metric> metrics);  }
@Test public void testSuccessfulSend() throws Exception { final int port = getAvailablePort(); SocketReceiverTester receiver = new SocketReceiverTester(port); receiver.start(); String host = getLocalHostIp() + ":" + port; GraphiteMetricObserver gw = new GraphiteMetricObserver("serverA", host); try { List<Metric> metrics = new ArrayList<>(); metrics.add(BasicGraphiteNamingConventionTest.getOSMetric("AvailableProcessors")); gw.update(metrics); receiver.waitForConnected(); String[] lines = receiver.waitForLines(1); assertEquals(1, lines.length); int found = lines[0].indexOf("serverA.java.lang.OperatingSystem.AvailableProcessors"); assertEquals(found, 0); } finally { receiver.stop(); gw.stop(); } }
@Test public void testReconnection() throws Exception { final int port = getAvailablePort(); SocketReceiverTester receiver = new SocketReceiverTester(port); receiver.start(); String host = getLocalHostIp() + ":" + port; GraphiteMetricObserver gw = new GraphiteMetricObserver("serverA", host); try { List<Metric> metrics = new ArrayList<>(); metrics.add(BasicGraphiteNamingConventionTest.getOSMetric("AvailableProcessors")); gw.update(metrics); receiver.waitForConnected(); String[] lines = receiver.waitForLines(1); assertEquals(1, lines.length); int found = lines[0].indexOf("serverA.java.lang.OperatingSystem.AvailableProcessors"); assertEquals(found, 0); receiver.stop(); receiver = new SocketReceiverTester(port); receiver.start(); gw.update(metrics); assertEquals(0, gw.getFailedUpdateCount()); gw.update(metrics); assertEquals(1, gw.getFailedUpdateCount()); gw.update(metrics); assertEquals(1, gw.getFailedUpdateCount()); receiver.waitForConnected(); receiver.waitForLines(1); found = lines[0].indexOf("serverA.java.lang.OperatingSystem.AvailableProcessors"); assertEquals(found, 0); } finally { receiver.stop(); gw.stop(); } }