/** Sending a request to a node not listening on that port should result in disconnection */ @Test public void testConnectionRefused() throws Exception { String connectionId = selector.connect( new InetSocketAddress("localhost", 6668), BUFFER_SIZE, BUFFER_SIZE, PortType.SSL); while (selector.disconnected().contains(connectionId)) { selector.poll(1000L); } }
@Test public void testGetSelectors() { List<Selector> expResult = new ArrayList<Selector>(); expResult.add(Selector.createSelector("One.two#three")); expResult.add(Selector.createSelector("Four.five#six")); Rule instance = new Rule(expResult, Collections.EMPTY_LIST); List result = instance.getSelectors(); assertEquals(expResult, result); }
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); } } } }
/** Validate that the client can intentionally disconnect and reconnect */ @Test public void testClientDisconnect() throws Exception { String connectionId = blockingSSLConnect(); selector.disconnect(connectionId); selector.poll(10, asList(SelectorTest.createSend(connectionId, "hello1"))); assertEquals("Request should not have succeeded", 0, selector.completedSends().size()); assertEquals("There should be a disconnect", 1, selector.disconnected().size()); assertTrue( "The disconnect should be from our node", selector.disconnected().contains(connectionId)); connectionId = blockingSSLConnect(); assertEquals("hello2", blockingRequest(connectionId, "hello2")); }
/* connect and wait for the connection to complete */ private String blockingSSLConnect() throws IOException { String connectionId = selector.connect( new InetSocketAddress("localhost", server.port), BUFFER_SIZE, BUFFER_SIZE, PortType.SSL); while (!selector.connected().contains(connectionId)) { selector.poll(10000L); } return connectionId; }
@Test public void testCloseAfterConnectCall() throws IOException { String connectionId = selector.connect( new InetSocketAddress("localhost", server.port), BUFFER_SIZE, BUFFER_SIZE, PortType.SSL); selector.close(connectionId); selector.poll(0); Assert.assertTrue( "Channel should have been added to disconnected list", selector.disconnected().contains(connectionId)); }
@Test public void testSSLConnect() throws IOException { String connectionId = selector.connect( new InetSocketAddress("localhost", server.port), BUFFER_SIZE, BUFFER_SIZE, PortType.SSL); while (!selector.connected().contains(connectionId)) { selector.poll(10000L); } Assert.assertTrue( "Channel should have been ready by now ", selector.isChannelReady(connectionId)); }
/** * 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)); } } }
/** * Validate that when the server disconnects, a client send ends up with that node in the * disconnected list. */ @Test public void testServerDisconnect() throws Exception { // connect and do a simple request String connectionId = blockingSSLConnect(); assertEquals("hello", blockingRequest(connectionId, "hello")); // disconnect server.closeConnections(); while (!selector.disconnected().contains(connectionId)) { selector.poll(1000L); } // reconnect and do another request connectionId = blockingSSLConnect(); assertEquals("hello", blockingRequest(connectionId, "hello")); }
/** * Sending a request to a node with a bad hostname should result in an exception during connect */ @Test(expected = IOException.class) public void testNoRouteToHost() throws Exception { selector.connect( new InetSocketAddress("asdf.asdf.dsc", server.port), BUFFER_SIZE, BUFFER_SIZE, PortType.SSL); }
/** Sending a request with one already in flight should result in an exception */ @Test(expected = IllegalStateException.class) public void testCantSendWithInProgress() throws Exception { String connectionId = blockingSSLConnect(); selector.poll( 1000L, asList( SelectorTest.createSend(connectionId, "test1"), SelectorTest.createSend(connectionId, "test2"))); }
@After public void teardown() throws Exception { selector.close(); server.close(); }