### Question:
ConnectionPool implements AutoCloseable { public <T> CompletableFuture<T> withConnection(Function<QueryReadyConnection.AutoCommit, CompletableFuture<T>> fn) { return withConnection(config.defaultTimeout, config.defaultTimeoutUnit, fn); } @SuppressWarnings("initialization")  ConnectionPool(Config config); CompletableFuture<QueryReadyConnection.AutoCommit> borrowConnection(); CompletableFuture<QueryReadyConnection.AutoCommit> borrowConnection(long timeout, TimeUnit unit); @SuppressWarnings("dereference.of.nullable") void returnConnection(QueryReadyConnection.@Nullable AutoCommit conn); CompletableFuture<T> withConnection(Function<QueryReadyConnection.AutoCommit, CompletableFuture<T>> fn); CompletableFuture<T> withConnection(long waitForConnTimeout, TimeUnit waitForConnUnit,
      Function<QueryReadyConnection.AutoCommit, CompletableFuture<T>> fn); CompletableFuture<Void> terminateAll(); @Override void close();  }### Answer:
@Test public void testFirstLazyConnectionIsOnlyOne() throws Exception { try (ConnectionPool pool = new ConnectionPool(newDefaultConfig().poolSize(5))) { long count = pool.withConnection(this::getConnectionCount).get(); Assert.assertEquals(1L, count); count = pool.withConnection(this::getConnectionCount).get(); Assert.assertEquals(1L, count); } }
@Test public void testSecondLazyConnectionCreatedWhenNeeded() throws Exception { try (ConnectionPool pool = new ConnectionPool(newDefaultConfig().poolSize(5))) { CountDownLatch firstLatch = new CountDownLatch(1); CountDownLatch secondLatch = new CountDownLatch(1); CompletableFuture<Long> firstCount = pool.withConnection(c -> waitOn(getConnectionCount(c), firstLatch)); CompletableFuture<Long> secondCount = pool.withConnection(c -> waitOn(secondLatch).thenCompose(__ -> getConnectionCount(c))); firstLatch.countDown(); Assert.assertEquals(1L, firstCount.get().longValue()); secondLatch.countDown(); Assert.assertEquals(2L, secondCount.get().longValue()); } }