@Test
  public void testConnectionInitializationFailures() {
    List<RequestInfo> requestInfoList = new ArrayList<>();
    requestInfoList.add(new RequestInfo(sslHost, sslPort, new MockSend(0), replicaOnSslNode, null));
    selector.setState(MockSelectorState.IdlePoll);
    Assert.assertEquals(0, selector.connectCallCount());
    // this sendAndPoll() should initiate a connect().
    List<ResponseInfo> responseInfoList =
        networkClient.sendAndPoll(requestInfoList, Collections.emptySet(), POLL_TIMEOUT_MS);
    // At this time a single connection would have been initiated for the above request.
    Assert.assertEquals(1, selector.connectCallCount());
    Assert.assertEquals(0, responseInfoList.size());
    responseInfoList.forEach(ResponseInfo::release);
    requestInfoList.clear();

    // Subsequent calls to sendAndPoll() should not initiate any connections.
    responseInfoList = networkClient.sendAndPoll(requestInfoList, Collections.emptySet(), POLL_TIMEOUT_MS);
    Assert.assertEquals(1, selector.connectCallCount());
    Assert.assertEquals(0, responseInfoList.size());
    responseInfoList.forEach(ResponseInfo::release);

    // Another connection should get initialized if a new request comes in for the same destination.
    requestInfoList.add(new RequestInfo(sslHost, sslPort, new MockSend(1), replicaOnSslNode, null));
    responseInfoList = networkClient.sendAndPoll(requestInfoList, Collections.emptySet(), POLL_TIMEOUT_MS);
    Assert.assertEquals(2, selector.connectCallCount());
    Assert.assertEquals(0, responseInfoList.size());
    responseInfoList.forEach(ResponseInfo::release);
    requestInfoList.clear();

    // Subsequent calls to sendAndPoll() should not initiate any more connections.
    responseInfoList = networkClient.sendAndPoll(requestInfoList, Collections.emptySet(), POLL_TIMEOUT_MS);
    Assert.assertEquals(2, selector.connectCallCount());
    Assert.assertEquals(0, responseInfoList.size());
    responseInfoList.forEach(ResponseInfo::release);

    // Once connect failure kicks in, the pending requests should be failed immediately.
    selector.setState(MockSelectorState.FailConnectionInitiationOnPoll);
    responseInfoList = networkClient.sendAndPoll(requestInfoList, Collections.emptySet(), POLL_TIMEOUT_MS);
    Assert.assertEquals(2, selector.connectCallCount());
    Assert.assertEquals(2, responseInfoList.size());
    Assert.assertEquals(NetworkClientErrorCode.NetworkError, responseInfoList.get(0).getError());
    Assert.assertEquals(NetworkClientErrorCode.NetworkError, responseInfoList.get(1).getError());
    responseInfoList.forEach(ResponseInfo::release);
    responseInfoList.clear();
  }
Test to ensure two things:
1. If a request comes in and there are no available connections to the destination,
only one connection is initiated, even if that connection is found to be pending when the
same request is looked at during a subsequent sendAndPoll.
2. For the above situation, if the subsequent pending connection fails, then the request is
immediately failed.
ambry-network/src/test/java/com/github/ambry/network/SocketNetworkClientTest.java