QoSControlConnection extends AbstractRMBTTest implements Runnable { public void connect() throws Exception { isRunning.set(true); try { controlSocket = connect(null, InetAddress.getByName(params.getHost()), params.getPort(), AbstractQoSTask.QOS_SERVER_PROTOCOL_VERSION, "ACCEPT", params.isEncryption(), AbstractQoSTask.CONTROL_CONNECTION_TIMEOUT); if (controlSocket == null) { isRunning.set(false); couldNotConnect.set(true); } } catch (Exception e) { isRunning.set(false); couldNotConnect.set(true); throw e; } } QoSControlConnection(RMBTClient client, RMBTTestParameter params); void sendTaskCommand(AbstractQoSTask qosTask, String command, ControlConnectionResponseCallback callback); @Override void run(); void connect(); void close(); TreeSet<Integer> getConcurrencyGroupSet(); final static Pattern ID_REGEX_PATTERN; final AtomicBoolean isRunning; final AtomicBoolean couldNotConnect; }
@Test(expected = SocketException.class) public void basicConnectionExceptionTest (@Mocked final Socket socket, @Mocked final InetAddress inetAddress) throws Exception { new Expectations() {{ InetAddress.getByName(anyString); result = loopbackAddress; socket.connect((SocketAddress) any, anyInt); times = 1; result = new SocketException("Forcefully thrown exception"); }}; controlConnection = new QoSControlConnection(clientHolder, testParameter); try { controlConnection.connect(); } catch (SocketException ex) { assertEquals("Forcefully thrown exception", ex.getMessage()); assertFalse("ControlConnection wrongly says it's running", controlConnection.isRunning.get()); assertTrue("ControlConnection wrongly says it could connect", controlConnection.couldNotConnect.get()); throw ex; } }
@Test public void basicConnectionProtocolErrorTest (@Mocked final Socket socket, @Mocked final InetAddress inetAddress, @Mocked final BufferedReader bufferedReader) throws Exception { new Expectations() {{ InetAddress.getByName(anyString); result = loopbackAddress; bufferedReader.readLine(); result = "NOT A PROTOCOL"; socket.getOutputStream(); times = 1; result = testOutputStream; }}; controlConnection = new QoSControlConnection(clientHolder, sslTestParameter); controlConnection.connect(); assertNull("Control socket is not null despite protocol error", controlConnection.controlSocket); assertFalse("QoSControlConnection reports as running", controlConnection.isRunning.get()); assertTrue("QoSControlConnection does not show connection issue", controlConnection.couldNotConnect.get()); }
@Test public void basicConnectionNoAcceptErrorTest (@Mocked final Socket socket, @Mocked final InetAddress inetAddress, @Mocked final BufferedReader bufferedReader) throws Exception { new Expectations() {{ InetAddress.getByName(anyString); result = loopbackAddress; bufferedReader.readLine(); returns(AbstractQoSTask.QOS_SERVER_PROTOCOL_VERSION, "NOT A PROTOCOL"); socket.getOutputStream(); times = 1; result = testOutputStream; }}; controlConnection = new QoSControlConnection(clientHolder, testParameter); controlConnection.connect(); assertNull("Control socket is not null despite protocol error", controlConnection.controlSocket); assertFalse("QoSControlConnection reports as running", controlConnection.isRunning.get()); assertTrue("QoSControlConnection does not show connection issue", controlConnection.couldNotConnect.get()); }
@Test public void basicConnectionNoOKErrorTest (@Mocked final Socket socket, @Mocked final InetAddress inetAddress, @Mocked final BufferedReader bufferedReader) throws Exception { new Expectations() {{ InetAddress.getByName(anyString); result = loopbackAddress; bufferedReader.readLine(); returns(AbstractQoSTask.QOS_SERVER_PROTOCOL_VERSION, "ACCEPT", "NOT A PROTOCOL"); socket.getOutputStream(); times = 1; result = testOutputStream; }}; controlConnection = new QoSControlConnection(clientHolder, testParameter); controlConnection.connect(); assertNull("Control socket is not null despite protocol error", controlConnection.controlSocket); assertFalse("QoSControlConnection reports as running", controlConnection.isRunning.get()); assertTrue("QoSControlConnection does not show connection issue", controlConnection.couldNotConnect.get()); }
@Test public void basicConnectionNoOKNullErrorTest (@Mocked final Socket socket, @Mocked final InetAddress inetAddress, @Mocked final BufferedReader bufferedReader) throws Exception { new Expectations() {{ InetAddress.getByName(anyString); result = loopbackAddress; bufferedReader.readLine(); returns(AbstractQoSTask.QOS_SERVER_PROTOCOL_VERSION, "ACCEPT", (String) null); socket.getOutputStream(); times = 1; result = testOutputStream; }}; controlConnection = new QoSControlConnection(clientHolder, testParameter); controlConnection.connect(); assertNull("Control socket is not null despite protocol error", controlConnection.controlSocket); assertFalse("QoSControlConnection reports as running", controlConnection.isRunning.get()); assertTrue("QoSControlConnection does not show connection issue", controlConnection.couldNotConnect.get()); }