Graphite implements GraphiteSender { @Override public void connect() throws IllegalStateException, IOException { if (isConnected()) { throw new IllegalStateException("Already connected"); } InetSocketAddress address = this.address; if (address == null || hostname != null) { address = new InetSocketAddress(InetAddress.getByName(hostname), port); } if (address.getAddress() == null) { throw new UnknownHostException(address.getHostName()); } this.socket = socketFactory.createSocket(address.getAddress(), address.getPort()); this.writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), charset)); } Graphite(String hostname, int port);  Graphite(String hostname, int port, SocketFactory socketFactory);  Graphite(String hostname, int port, SocketFactory socketFactory, Charset charset);  Graphite(InetSocketAddress address);  Graphite(InetSocketAddress address, SocketFactory socketFactory);  Graphite(InetSocketAddress address, SocketFactory socketFactory, Charset charset); @Override void connect(); @Override boolean isConnected(); @Override void send(String name, String value, long timestamp); @Override int getFailures(); @Override void flush(); @Override void close();  }
@Test public void connectsToGraphiteWithInetSocketAddress() throws Exception { graphite = new Graphite(address, socketFactory); graphite.connect(); verify(socketFactory).createSocket(address.getAddress(), address.getPort()); }
@Test public void connectsToGraphiteWithHostAndPort() throws Exception { graphite = new Graphite(host, port, socketFactory); graphite.connect(); verify(socketFactory).createSocket(address.getAddress(), port); }
@Test public void doesNotAllowDoubleConnections() throws Exception { graphite = new Graphite(address, socketFactory); graphite.connect(); try { graphite.connect(); failBecauseExceptionWasNotThrown(IllegalStateException.class); } catch (IllegalStateException e) { assertThat(e.getMessage()) .isEqualTo("Already connected"); } }
@Test public void notifiesIfGraphiteIsUnavailable() { final String unavailableHost = "unknown-host-10el6m7yg56ge7dmcom"; InetSocketAddress unavailableAddress = new InetSocketAddress(unavailableHost, 1234); try (Graphite unavailableGraphite = new Graphite(unavailableAddress, socketFactory)) { unavailableGraphite.connect(); failBecauseExceptionWasNotThrown(UnknownHostException.class); } catch (Exception e) { assertThat(e.getMessage()) .isEqualTo(unavailableHost); } }