TcpTask extends AbstractQoSTask { public QoSTestResult call() throws Exception { final QoSTestResult result = initQoSTestResult(QosMeasurementType.TCP); try { onStart(result); if (this.testPortIn != null) { result.getResultMap().put(RESULT_IN, "FAILED"); } if (this.testPortOut != null) { result.getResultMap().put(RESULT_OUT, "FAILED"); } Socket socketIn = null; try { System.out.println("TCPTASK: " + getTestServerAddr() + ":" + getTestServerPort()); String response = null; if (this.testPortOut != null) { final CountDownLatch latch = new CountDownLatch(1); final ControlConnectionResponseCallback callback = new ControlConnectionResponseCallback() { public void onResponse(final String response, final String request) { if (response != null && response.startsWith("OK")) { System.out.println("got response: " + response); Socket socketOut = null; try { socketOut = getSocket(getTestServerAddr(), testPortOut, false, (int)(timeout/1000000)); socketOut.setSoTimeout((int)(timeout/1000000)); sendMessage(socketOut, "PING\n"); final String testResponse = readLine(socketOut); System.out.println("TCP OUT TEST response: " + testResponse); result.getResultMap().put(RESULT_RESPONSE_OUT, testResponse); socketOut.close(); result.getResultMap().put(RESULT_OUT, "OK"); } catch (SocketTimeoutException e) { result.getResultMap().put(RESULT_OUT, "TIMEOUT"); } catch (Exception e) { result.getResultMap().put(RESULT_OUT, "ERROR"); } finally { latch.countDown(); } } else { result.getResultMap().put(RESULT_OUT, "ERROR"); latch.countDown(); } } }; sendCommand("TCPTEST OUT " + testPortOut, callback); if(!latch.await(timeout, TimeUnit.NANOSECONDS)) { result.getResultMap().put(RESULT_OUT, "TIMEOUT"); } } if (this.testPortIn != null) { try (ServerSocket serverSocket = new ServerSocket(testPortIn)) { sendCommand("TCPTEST IN " + testPortIn, null); serverSocket.setSoTimeout((int)(timeout/1000000)); socketIn = serverSocket.accept(); socketIn.setSoTimeout((int)(timeout/1000000)); response = readLine(socketIn); System.out.println("TCP IN TEST response: " + response); result.getResultMap().put(RESULT_RESPONSE_IN, response); socketIn.close(); result.getResultMap().put(RESULT_IN, "OK"); } catch (SocketTimeoutException e) { result.getResultMap().put(RESULT_IN, "TIMEOUT"); } catch (Exception e) { result.getResultMap().put(RESULT_IN, "ERROR"); } } } catch (Exception e) { e.printStackTrace(); } if (this.testPortIn != null) { result.getResultMap().put(RESULT_PORT_IN, testPortIn); } if (this.testPortOut != null) { result.getResultMap().put(RESULT_PORT_OUT, testPortOut); } result.getResultMap().put(RESULT_TIMEOUT, timeout); return result; } catch (Exception e) { throw e; } finally { onEnd(result); } } TcpTask(QualityOfServiceTest nnTest, TaskDesc taskDesc, int threadId); QoSTestResult call(); @Override void initTask(); QosMeasurementType getTestType(); boolean needsQoSControlConnection(); final static String PARAM_PORT_IN; final static String PARAM_PORT_OUT; final static String PARAM_TIMEOUT; final static String RESULT_PORT_IN; final static String RESULT_PORT_OUT; final static String RESULT_TIMEOUT; final static String RESULT_IN; final static String RESULT_OUT; final static String RESULT_RESPONSE_OUT; final static String RESULT_RESPONSE_IN; }
@Test public void testTcpOutgoingTimeout (@Mocked final QoSControlConnection controlConnection, @Mocked final Socket socket) throws Exception { new Expectations() {{ controlConnection.sendTaskCommand((AbstractQoSTask) any, withPrefix("TCPTEST OUT"), (ControlConnectionResponseCallback) any); result = new Delegate () { public void delegateMethod (AbstractQoSTask qoSTask, String cmd, ControlConnectionResponseCallback callback) { callback.onResponse("OK", cmd); } }; socket.getInputStream(); result = new SocketTimeoutException("Forcefully thrown exception"); socket.getOutputStream(); result = testOutputStream; }}; final TcpTask tcpTask = new TcpTask(qosTest, clientHolder.getTaskDescList().get(0), 0); tcpTask.setControlConnection(controlConnection); final QoSTestResult res = tcpTask.call(); assertEquals("Result did not return TIMEOUT","TIMEOUT", res.getResultMap().get(TcpTask.RESULT_OUT)); assertEquals("Payload sent to server != PING", "PING\n", testOutputStream.toString()); assertEquals("Incorrect test out port", 80, res.getResultMap().get(TcpTask.RESULT_PORT_OUT)); }
@Test public void testTcpOutgoingConnectionError (@Mocked final QoSControlConnection controlConnection, @Mocked final Socket socket) throws Exception { new Expectations() {{ controlConnection.sendTaskCommand((AbstractQoSTask) any, withPrefix("TCPTEST OUT"), (ControlConnectionResponseCallback) any); result = new Delegate () { public void delegateMethod (AbstractQoSTask qoSTask, String cmd, ControlConnectionResponseCallback callback) { callback.onResponse("OK", cmd); } }; socket.getInputStream(); result = new ConnectException("Forcefully thrown exception"); }}; final TcpTask tcpTask = new TcpTask(qosTest, clientHolder.getTaskDescList().get(0), 0); tcpTask.setControlConnection(controlConnection); final QoSTestResult res = tcpTask.call(); assertEquals("Result did not return ERROR","ERROR", res.getResultMap().get(TcpTask.RESULT_OUT)); assertEquals("Incorrect test out port", 80, res.getResultMap().get(TcpTask.RESULT_PORT_OUT)); }
@Test public void testTcpIncomingSuccessful (@Mocked final QoSControlConnection controlConnection, @Mocked final ServerSocket serverSocket, @Mocked final Socket socket) throws Exception { new Expectations() {{ controlConnection.sendTaskCommand((AbstractQoSTask) any, "TCPTEST IN 80", (ControlConnectionResponseCallback) any); times = 1; socket.getInputStream(); result = new ByteArrayInputStream("PING".getBytes()); }}; final TcpTask tcpTask = new TcpTask(qosTest, incomingTaskDesc, 0); tcpTask.setControlConnection(controlConnection); final QoSTestResult res = tcpTask.call(); assertEquals("Result did not return OK","OK", res.getResultMap().get(TcpTask.RESULT_IN)); assertEquals("Incorrect received result", "PING", res.getResultMap().get(TcpTask.RESULT_RESPONSE_IN)); assertEquals("Incorrect test in port", 80, res.getResultMap().get(TcpTask.RESULT_PORT_IN)); }
@Test public void testTcpIncomingTimeout (@Mocked final QoSControlConnection controlConnection, @Mocked final ServerSocket serverSocket, @Mocked final Socket socket) throws Exception { new Expectations() {{ controlConnection.sendTaskCommand((AbstractQoSTask) any, "TCPTEST IN 80", (ControlConnectionResponseCallback) any); times = 1; socket.getInputStream(); result = new SocketTimeoutException("Forcefully thrown exception"); }}; final TcpTask tcpTask = new TcpTask(qosTest, incomingTaskDesc, 0); tcpTask.setControlConnection(controlConnection); final QoSTestResult res = tcpTask.call(); assertEquals("Result did not return TIMEOUT","TIMEOUT", res.getResultMap().get(TcpTask.RESULT_IN)); assertEquals("Incorrect test in port", 80, res.getResultMap().get(TcpTask.RESULT_PORT_IN)); }
@Test public void testTcpIncomingError (@Mocked final QoSControlConnection controlConnection, @Mocked final ServerSocket serverSocket, @Mocked final Socket socket) throws Exception { new Expectations() {{ controlConnection.sendTaskCommand((AbstractQoSTask) any, "TCPTEST IN 80", (ControlConnectionResponseCallback) any); times = 1; serverSocket.accept(); result = new SocketException("Forcefully thrown exception"); }}; final TcpTask tcpTask = new TcpTask(qosTest, incomingTaskDesc, 0); tcpTask.setControlConnection(controlConnection); final QoSTestResult res = tcpTask.call(); assertEquals("Result did not return ERROR","ERROR", res.getResultMap().get(TcpTask.RESULT_IN)); assertEquals("Incorrect test in port", 80, res.getResultMap().get(TcpTask.RESULT_PORT_IN)); }
@Test public void testTcpOutgoingSuccessful (@Mocked final QoSControlConnection controlConnection, @Mocked final Socket socket) throws Exception { new Expectations() {{ controlConnection.sendTaskCommand((AbstractQoSTask) any, withPrefix("TCPTEST OUT"), (ControlConnectionResponseCallback) any); result = new Delegate () { public void delegateMethod (AbstractQoSTask qoSTask, String cmd, ControlConnectionResponseCallback callback) { callback.onResponse("OK", cmd); } }; socket.getInputStream(); result = new ByteArrayInputStream("PING".getBytes()); socket.getOutputStream(); result = testOutputStream; }}; final TcpTask tcpTask = new TcpTask(qosTest, clientHolder.getTaskDescList().get(0), 0); tcpTask.setControlConnection(controlConnection); final QoSTestResult res = tcpTask.call(); assertEquals("Result did not return OK","OK", res.getResultMap().get(TcpTask.RESULT_OUT)); assertEquals("Payload sent to server != PING", "PING\n", testOutputStream.toString()); assertEquals("Incorrect received result", "PING", res.getResultMap().get(TcpTask.RESULT_RESPONSE_OUT)); assertEquals("Incorrect test out port", 80, res.getResultMap().get(TcpTask.RESULT_PORT_OUT)); }
@Test public void testTcpOutgoingControllerSendsError (@Mocked final QoSControlConnection controlConnection) throws Exception { new Expectations() {{ controlConnection.sendTaskCommand((AbstractQoSTask) any, withPrefix("TCPTEST OUT"), (ControlConnectionResponseCallback) any); result = new Delegate () { public void delegateMethod (AbstractQoSTask qoSTask, String cmd, ControlConnectionResponseCallback callback) { callback.onResponse("ERROR", cmd); } }; }}; final TcpTask tcpTask = new TcpTask(qosTest, clientHolder.getTaskDescList().get(0), 0); tcpTask.setControlConnection(controlConnection); final QoSTestResult res = tcpTask.call(); assertEquals("Result did not return ERROR","ERROR", res.getResultMap().get(TcpTask.RESULT_OUT)); assertEquals("Incorrect test out port", 80, res.getResultMap().get(TcpTask.RESULT_PORT_OUT)); }
@Test public void testTcpOutgoingControllerSendsNull (@Mocked final InetAddress inetAddress, @Mocked final QoSControlConnection controlConnection, @Mocked final Socket socket) throws Exception { new Expectations() {{ controlConnection.sendTaskCommand((AbstractQoSTask) any, withPrefix("TCPTEST OUT"), (ControlConnectionResponseCallback) any); result = new Delegate () { public void delegateMethod (AbstractQoSTask qoSTask, String cmd, ControlConnectionResponseCallback callback) { callback.onResponse(null, cmd); } }; }}; final TcpTask tcpTask = new TcpTask(qosTest, clientHolder.getTaskDescList().get(0), 0); tcpTask.setControlConnection(controlConnection); final QoSTestResult res = tcpTask.call(); assertEquals("Result did not return ERROR","ERROR", res.getResultMap().get(TcpTask.RESULT_OUT)); assertEquals("Incorrect test out port", 80, res.getResultMap().get(TcpTask.RESULT_PORT_OUT)); }