private String blockingRequest(String connectionId, String s) throws Exception { selector.poll(1000L, asList(SelectorTest.createSend(connectionId, s))); while (true) { selector.poll(1000L); for (NetworkReceive receive : selector.completedReceives()) { if (receive.getConnectionId() == connectionId) { return SelectorTest.asString(receive); } } } }
/** * Send multiple requests to several connections in parallel. Validate that responses are received * in the order that requests were sent. */ @Test public void testNormalOperation() throws Exception { int conns = 5; // create connections ArrayList<String> connectionIds = new ArrayList<String>(); for (int i = 0; i < conns; i++) { connectionIds.add(blockingSSLConnect()); } // send echo requests and receive responses int responseCount = 0; List<NetworkSend> sends = new ArrayList<NetworkSend>(); for (int i = 0; i < conns; i++) { String connectionId = connectionIds.get(i); sends.add(SelectorTest.createSend(connectionId, connectionId + "&" + 0)); } // loop until we complete all requests while (responseCount < conns) { // do the i/o selector.poll(0L, sends); assertEquals("No disconnects should have occurred.", 0, selector.disconnected().size()); // handle any responses we may have gotten for (NetworkReceive receive : selector.completedReceives()) { String[] pieces = SelectorTest.asString(receive).split("&"); assertEquals("Should be in the form 'conn-counter'", 2, pieces.length); assertEquals("Check the source", receive.getConnectionId(), pieces[0]); assertEquals( "Check that the receive has kindly been rewound", 0, receive.getReceivedBytes().getPayload().position()); assertTrue( "Received connectionId is as expected ", connectionIds.contains(receive.getConnectionId())); assertEquals("Check the request counter", 0, Integer.parseInt(pieces[1])); responseCount++; } // prepare new sends for the next round sends.clear(); for (NetworkSend send : selector.completedSends()) { String dest = send.getConnectionId(); sends.add(SelectorTest.createSend(dest, dest + "&" + 0)); } } }