EchoProtocolTcpTask extends AbstractEchoProtocolTask { public QoSTestResult call() throws Exception { final QoSTestResult result = initQoSTestResult(QosMeasurementType.ECHO_PROTOCOL); try { onStart(result); try { System.out.println("ECHO_PROTOCOL_TCP_TASK: " + getTestServerAddr() + ":" + getTestServerPort()); if (this.testPort != null && this.testHost != null) { try (Socket socket = getSocket(testHost, testPort, false, (int)(timeout/1000000))){ socket.setSoTimeout((int)(timeout/1000000)); final long startTime = System.nanoTime(); sendMessage(socket, this.payload + "\n"); final String testResponse = readLine(socket); final long duration = System.nanoTime() - startTime; result.getResultMap().put(RESULT, testResponse); socket.close(); if (this.payload.equals(testResponse)) { result.getResultMap().put(RESULT_STATUS, "OK"); } else { result.getResultMap().put(RESULT_STATUS, "ERROR"); } result.getResultMap().put(RESULT_RTT_NS, Long.toString(duration)); } catch (SocketTimeoutException e) { result.getResultMap().put(RESULT_STATUS, "TIMEOUT"); } catch (Exception e) { result.getResultMap().put(RESULT_STATUS, "ERROR"); } } else { result.getResultMap().put(RESULT_STATUS, "ERROR"); } } catch (Exception e) { e.printStackTrace(); result.getResultMap().put(RESULT_STATUS, "ERROR"); } return result; } catch (Exception e) { throw e; } finally { if (this.testPort != null) { result.getResultMap().put(RESULT_PORT, testPort); } if (this.testHost != null) { result.getResultMap().put("echo_protocol_objective_host", this.testHost); } result.getResultMap().put(RESULT_TIMEOUT, timeout); if (this.payload != null) { result.getResultMap().put(AbstractEchoProtocolTask.PARAM_PAYLOAD, this.payload); } result.getResultMap().put(AbstractEchoProtocolTask.RESULT_PROTOCOL, AbstractEchoProtocolTask.PROTOCOL_TCP); onEnd(result); } } EchoProtocolTcpTask(QualityOfServiceTest nnTest, TaskDesc taskDesc, int threadId); QoSTestResult call();  }
@Test public void testCorrectTcpEchoResponse(@Mocked final Socket socket) throws Exception { new Expectations() {{ socket.getOutputStream(); result = testOutputStream; socket.getInputStream(); result = new ByteArrayInputStream(CORRECT_MESSAGE.getBytes()); }}; final EchoProtocolTcpTask task = new EchoProtocolTcpTask(qosTest, clientHolder.getTaskDescList().get(0), 0); final QoSTestResult res = task.call(); assertEquals("Payload sent to server != " + CORRECT_MESSAGE, CORRECT_MESSAGE + "\n", testOutputStream.toString()); assertEquals("Result did not return OK","OK", res.getResultMap().get(AbstractEchoProtocolTask.RESULT_STATUS)); assertEquals("Wrong echo service host in result", ECHO_SERVICE_HOST, res.getResultMap().get("echo_protocol_objective_host")); assertEquals("Wrong protocol type in result", AbstractEchoProtocolTask.PROTOCOL_TCP, res.getResultMap().get(AbstractEchoProtocolTask.RESULT_PROTOCOL)); assertEquals("Payload stored in result !=" + CORRECT_MESSAGE, CORRECT_MESSAGE, res.getResultMap().get(AbstractEchoProtocolTask.RESULT)); }
@Test public void testWrongTcpEchoResponse (@Mocked final Socket socket) throws Exception { new Expectations() {{ socket.getOutputStream(); result = testOutputStream; socket.getInputStream(); result = new ByteArrayInputStream(WRONG_MESSAGE.getBytes()); }}; final EchoProtocolTcpTask task = new EchoProtocolTcpTask(qosTest, clientHolder.getTaskDescList().get(0), 0); final QoSTestResult res = task.call(); assertEquals("Payload sent to server != " + CORRECT_MESSAGE, CORRECT_MESSAGE + "\n", testOutputStream.toString()); assertEquals("Result did not return ERROR","ERROR", res.getResultMap().get(AbstractEchoProtocolTask.RESULT_STATUS)); assertEquals("Wrong echo service host in result", ECHO_SERVICE_HOST, res.getResultMap().get("echo_protocol_objective_host")); assertEquals("Wrong protocol type in result", AbstractEchoProtocolTask.PROTOCOL_TCP, res.getResultMap().get(AbstractEchoProtocolTask.RESULT_PROTOCOL)); assertEquals("Payload stored in result !=" + WRONG_MESSAGE, WRONG_MESSAGE, res.getResultMap().get(AbstractEchoProtocolTask.RESULT)); }
@Test public void testCorrectTcpEchoResponseWithAdditionalWhiteSpace (@Mocked final Socket socket) throws Exception { new Expectations() {{ socket.getOutputStream(); result = testOutputStream; socket.getInputStream(); result = new ByteArrayInputStream(WRONG_MESSAGE_ADDITIONAL_WHITESPACE.getBytes()); }}; final EchoProtocolTcpTask task = new EchoProtocolTcpTask(qosTest, clientHolder.getTaskDescList().get(0), 0); final QoSTestResult res = task.call(); assertEquals("Payload sent to server != " + CORRECT_MESSAGE, CORRECT_MESSAGE + "\n", testOutputStream.toString()); assertEquals("Result did not return ERROR","ERROR", res.getResultMap().get(AbstractEchoProtocolTask.RESULT_STATUS)); assertEquals("Wrong echo service host in result", ECHO_SERVICE_HOST, res.getResultMap().get("echo_protocol_objective_host")); assertEquals("Wrong protocol type in result", AbstractEchoProtocolTask.PROTOCOL_TCP, res.getResultMap().get(AbstractEchoProtocolTask.RESULT_PROTOCOL)); assertEquals("Payload stored in result !=" + WRONG_MESSAGE_ADDITIONAL_WHITESPACE, WRONG_MESSAGE_ADDITIONAL_WHITESPACE, res.getResultMap().get(AbstractEchoProtocolTask.RESULT)); }
@Test public void testTcpEchoResponseWithSocketError (@Mocked final Socket socket) throws Exception { new Expectations() {{ socket.getOutputStream(); result = new SocketException("Forcefully thrown exception"); socket.connect((SocketAddress) any, anyInt); }}; final EchoProtocolTcpTask task = new EchoProtocolTcpTask(qosTest, clientHolder.getTaskDescList().get(0), 0); final QoSTestResult res = task.call(); assertEquals("Result did not return ERROR","ERROR", res.getResultMap().get(AbstractEchoProtocolTask.RESULT_STATUS)); assertEquals("Wrong echo service host in result", ECHO_SERVICE_HOST, res.getResultMap().get("echo_protocol_objective_host")); assertEquals("Wrong protocol type in result", AbstractEchoProtocolTask.PROTOCOL_TCP, res.getResultMap().get(AbstractEchoProtocolTask.RESULT_PROTOCOL)); }
@Test public void testTcpEchoResponseWithSocketTimeoutError(@Mocked final Socket socket) throws Exception { new Expectations() {{ socket.getOutputStream(); result = testOutputStream; socket.getInputStream(); result = new SocketTimeoutException("Forcefully thrown exception"); }}; final EchoProtocolTcpTask task = new EchoProtocolTcpTask(qosTest, clientHolder.getTaskDescList().get(0), 0); final QoSTestResult res = task.call(); assertEquals("Result did not return TIMEOUT","TIMEOUT", res.getResultMap().get(AbstractEchoProtocolTask.RESULT_STATUS)); assertEquals("Wrong echo service host in result", ECHO_SERVICE_HOST, res.getResultMap().get("echo_protocol_objective_host")); assertEquals("Wrong protocol type in result", AbstractEchoProtocolTask.PROTOCOL_TCP, res.getResultMap().get(AbstractEchoProtocolTask.RESULT_PROTOCOL)); }