@Test
  public void testOutOfOrderConnectionEstablishment() {
    selector.setState(MockSelectorState.DelayFailAlternateConnect);
    List<RequestInfo> requestInfoList = new ArrayList<>();
    requestInfoList.add(new RequestInfo(sslHost, sslPort, new MockSend(2), replicaOnSslNode, null));
    requestInfoList.add(new RequestInfo(sslHost, sslPort, new MockSend(3), replicaOnSslNode, null));
    List<ResponseInfo> responseInfoList =
        networkClient.sendAndPoll(requestInfoList, Collections.emptySet(), POLL_TIMEOUT_MS);
    requestInfoList.clear();
    Assert.assertEquals(2, selector.connectCallCount());
    Assert.assertEquals(0, responseInfoList.size());
    responseInfoList.forEach(ResponseInfo::release);

    // Invoke sendAndPoll() again, the Connection C1 will get disconnected
    responseInfoList = networkClient.sendAndPoll(requestInfoList, Collections.emptySet(), POLL_TIMEOUT_MS);
    // Verify that no more connection is initiated in network client
    Assert.assertEquals(2, selector.connectCallCount());
    // There should be 2 responses, one is success from Request R1, another is from Connection C1 timeout.
    Assert.assertEquals(2, responseInfoList.size());
    Assert.assertNull(responseInfoList.get(1).getError());
    // Verify that Request R1 get sent via Connection C2. Note that 2 is correlation Id of R1
    Assert.assertEquals(2, ((MockSend) responseInfoList.get(1).getRequestInfo().getRequest()).getCorrelationId());
    // Verify the timeout connection event is added into response list, which will be handled by ResponseHandler directly.
    Assert.assertEquals("Mismatch in error code", NetworkClientErrorCode.NetworkError,
        responseInfoList.get(0).getError());
    Assert.assertNull(responseInfoList.get(0).getRequestInfo());
    responseInfoList.forEach(ResponseInfo::release);
    responseInfoList.clear();

    // Invoke sendAndPoll() again, Request R2 will get sent via Connection C2
    responseInfoList = networkClient.sendAndPoll(requestInfoList, Collections.emptySet(), POLL_TIMEOUT_MS);
    Assert.assertEquals(2, selector.connectCallCount());
    Assert.assertEquals(1, responseInfoList.size());
    Assert.assertNull(responseInfoList.get(0).getError());
    // Verify the correlation Id of Request R2
    Assert.assertEquals(3, responseInfoList.get(0).getRequestInfo().getRequest().getCorrelationId());
    responseInfoList.forEach(ResponseInfo::release);
    responseInfoList.clear();
    selector.setState(MockSelectorState.Good);
  }
Test the following case:
Connection C1 gets initiated in the context of Request R1
Connection C2 gets initiated in the context of Request R2
Connection C2 gets established first.
Request R1 checks out connection C2 because it is earlier in the queue
(although C2 was initiated on behalf of R2)
Request R1 gets sent on C2
Connection C1 gets disconnected, which was initiated in the context of Request R1
Request R1 is completed.
Request R2 reuses C1 and gets completed.
ambry-network/src/test/java/com/github/ambry/network/SocketNetworkClientTest.java