private static GraphiteWriter getGraphiteWriter(OutputStream out, List<String> typeNames)
      throws Exception {
    GenericKeyedObjectPool<InetSocketAddress, Socket> pool = mock(GenericKeyedObjectPool.class);
    Socket socket = mock(Socket.class);
    when(pool.borrowObject(Matchers.any(InetSocketAddress.class))).thenReturn(socket);

    when(socket.getOutputStream()).thenReturn(out);

    GraphiteWriter writer =
        GraphiteWriter.builder().setHost("localhost").setPort(2003).addTypeNames(typeNames).build();
    writer.setPool(pool);

    return writer;
  }
  @Test
  public void socketInvalidatedWhenError() throws Exception {
    GenericKeyedObjectPool<InetSocketAddress, Socket> pool = mock(GenericKeyedObjectPool.class);
    Socket socket = mock(Socket.class);
    when(pool.borrowObject(Matchers.any(InetSocketAddress.class))).thenReturn(socket);
    UnflushableByteArrayOutputStream out = new UnflushableByteArrayOutputStream();
    when(socket.getOutputStream()).thenReturn(out);

    GraphiteWriter writer = GraphiteWriter.builder().setHost("localhost").setPort(2003).build();
    writer.setPool(pool);

    writer.doWrite(dummyServer(), dummyQuery(), dummyResults());
    Mockito.verify(pool)
        .invalidateObject(Matchers.any(InetSocketAddress.class), Matchers.eq(socket));
    Mockito.verify(pool, Mockito.never())
        .returnObject(Matchers.any(InetSocketAddress.class), Matchers.eq(socket));
  }
  @Test
  public void booleanAsNumberWorks() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    GenericKeyedObjectPool<InetSocketAddress, Socket> pool = mock(GenericKeyedObjectPool.class);
    Socket socket = mock(Socket.class);
    when(pool.borrowObject(Matchers.any(InetSocketAddress.class))).thenReturn(socket);

    when(socket.getOutputStream()).thenReturn(out);

    GraphiteWriter writer =
        GraphiteWriter.builder().setHost("localhost").setPort(123).setBooleanAsNumber(true).build();
    writer.setPool(pool);

    writer.doWrite(dummyServer(), dummyQuery(), singleTrueResult());

    // check that the booleanAsNumber property was picked up from the JSON
    assertThat(out.toString())
        .startsWith("servers.host_example_net_4321.VerboseMemory.Verbose 1 0");
  }