@Test
  public void testNormalOperation() throws Exception {
    int conns = 5;
    int reqs = 500;

    // create connections
    InetSocketAddress addr = new InetSocketAddress("localhost", server.port);
    ArrayList<String> connectionIds = new ArrayList<String>();
    for (int i = 0; i < conns; i++) {
      String connectionId = selector.connect(addr, BUFFER_SIZE, BUFFER_SIZE, PortType.PLAINTEXT);
      connectionIds.add(connectionId);
    }

    // send echo requests and receive responses
    int[] requests = new int[conns];
    int[] responses = new int[conns];
    int responseCount = 0;
    List<NetworkSend> sends = new ArrayList<NetworkSend>();
    for (int i = 0; i < conns; i++) {
      String connectionId = connectionIds.get(i);
      sends.add(createSend(connectionId, connectionId + "&" + 0));
    }

    // loop until we complete all requests
    while (responseCount < conns * reqs) {
      // do the i/o
      selector.poll(100L, sends);

      assertEquals("No disconnects should have occurred.", 0, selector.disconnected().size());

      // handle any responses we may have gotten
      for (NetworkReceive receive : selector.completedReceives()) {
        ByteBuf payload = receive.getReceivedBytes().content();
        String[] pieces = asString(payload).split("&");
        assertEquals("Should be in the form 'conn-counter'", 2, pieces.length);
        assertEquals("Check the source", receive.getConnectionId(), pieces[0]);
        int index = Integer.parseInt(receive.getConnectionId().split("_")[1]);
        assertEquals("Check the request counter", responses[index], Integer.parseInt(pieces[1]));
        responses[index]++; // increment the expected counter
        responseCount++;
        receive.getReceivedBytes().release();
      }

      // prepare new sends for the next round
      sends.clear();
      for (NetworkSend send : selector.completedSends()) {
        String dest = send.getConnectionId();
        String[] pieces = dest.split("_");
        int index = Integer.parseInt(pieces[1]);
        requests[index]++;
        if (requests[index] < reqs) {
          sends.add(createSend(dest, dest + "&" + requests[index]));
        }
      }
    }
  }
Send multiple requests to several connections in parallel. Validate that responses are received in the order that
requests were sent.
ambry-network/src/test/java/com/github/ambry/network/SelectorTest.java