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;
  }
  @Override
  public GPCatalogConnectorStore build() throws Exception {
    Preconditions.checkNotNull(serverUrl, "CSW Server URL must " + "not be null.");

    GPPoolConnectorKey keyConnector =
        super.proxyConfiguration != null
            ? new GPPoolConnectorKey(serverUrl, securityConnector, proxyConfiguration, version)
            : new GPPoolConnectorKey(serverUrl, securityConnector, version);

    GPCatalogConnectorStore catalogStore = catalogConnectoPool.borrowObject(keyConnector);

    catalogConnectoPool.returnObject(keyConnector, catalogStore);

    return catalogStore;
  }
  @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");
  }
 @Override
 public RConnection borrowObject()
     throws Exception, NoSuchElementException, IllegalStateException {
   String host = hostQueue.take();
   return keyedConnectionPool.borrowObject(host);
 }