@Test
  public void testCloseWithDanglingRequest() throws Exception {
    Properties props = new Properties();
    props.setProperty(NetworkConfig.NETWORK_CLIENT_ENABLE_CONNECTION_REPLENISHMENT, "true");
    VerifiableProperties vprops = new VerifiableProperties(props);
    NetworkConfig networkConfig = new NetworkConfig(vprops);
    MockSelector mockSelector = new MockSelector(networkConfig);
    NetworkMetrics localNetworkMetrics = new NetworkMetrics(new MetricRegistry());
    SocketNetworkClient localNetworkClient =
        new SocketNetworkClient(mockSelector, networkConfig, localNetworkMetrics, MAX_PORTS_PLAIN_TEXT, MAX_PORTS_SSL,
            CHECKOUT_TIMEOUT_MS, time);
    DataNodeId dataNodeId = localPlainTextDataNodes.get(0);
    ReplicaId replicaId = sslDisabledClusterMap.getReplicaIds(dataNodeId).get(0);
    List<RequestInfo> requestInfoList = new ArrayList<>();
    List<ResponseInfo> responseInfoList;
    requestInfoList.add(
        new RequestInfo(dataNodeId.getHostname(), dataNodeId.getPortToConnectTo(), new MockSend(1), replicaId, null));
    requestInfoList.add(
        new RequestInfo(dataNodeId.getHostname(), dataNodeId.getPortToConnectTo(), new MockSend(2), replicaId, null));

    // This call would create two connection and not send any requests out
    localNetworkClient.sendAndPoll(requestInfoList, Collections.EMPTY_SET, POLL_TIMEOUT_MS);
    requestInfoList.clear();

    requestInfoList.add(
        new RequestInfo(dataNodeId.getHostname(), dataNodeId.getPortToConnectTo(), new MockSend(3), replicaId, null));
    requestInfoList.add(
        new RequestInfo(dataNodeId.getHostname(), dataNodeId.getPortToConnectTo(), new MockSend(4), replicaId, null));

    mockSelector.setState(MockSelectorState.IdlePoll);
    // This call would send first two requests out. At the same time, keep last two requests in the pendingRequests queue.
    localNetworkClient.sendAndPoll(requestInfoList, Collections.EMPTY_SET, POLL_TIMEOUT_MS);
    localNetworkClient.close();
  }
Test when close the router, the dangling requests will be correctly released.
ambry-network/src/test/java/com/github/ambry/network/SocketNetworkClientTest.java