java.net.SocketException:运行测试时套接字已关闭

时间:2018-09-21 09:02:14

标签: java junit

我正在编写测试以测试服务器和客户端的运行状况。在测试中,我通过在端口3000上创建套接字来运行客户端。并且ServerSocket我还指定了端口3000。在正常部署下,一切正常。但是测试失败了

java.net.SocketException: Socket closed

at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at java.net.SocketInputStream.read(SocketInputStream.java:224)
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:337)
at java.io.DataInputStream.readUTF(DataInputStream.java:589)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at com.epam.preproduction.linnyk.server.factory.TCPServerTest.testGetCount(TCPServerTest.java:54)

我的服务器:

public abstract class Server implements Runnable {
    protected ServerCommand serverCommandController;

    private final Socket clientSocket;

    public Server(Socket clientSocket) {
        this.clientSocket = clientSocket;
        serverCommandController = ApplicationContext.getServerCommandController();
    }
@Override
    public void run() {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                 BufferedWriter output = new BufferedWriter(new PrintWriter(clientSocket.getOutputStream()))) {
                checkPortAndConnection(reader, output);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (!clientSocket.isConnected()) {
                        clientSocket.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

public class TCPServer extends Server {

    public TCPServer(Socket clientSocket) {
        super(clientSocket);
    }

    @Override
    protected String getRequest(BufferedReader input) throws IOException {
        return input.readLine();
    }

    @Override
    protected void setResponse(BufferedWriter output, String request) throws IOException {
        String result = serverCommandController.execute(RequestParser.tcpParserCommand(request));
        output.write(result + "\n");
        output.flush();
    }

MyTest:

    public class TCPServerTest {


    private static StoreService productService;
    private static Server serverSide;
    private static Socket socket;

    private static DataInputStream ois;
    private static DataOutputStream oos;

    @BeforeClass
    public static void init() throws IOException {
        Main main = new Main();
        main.init();
        productService = new StoreServiceImpl(new ProductRepositoryImpl());
        socket = new Socket(InetAddress.getLocalHost(), 3000);
        Runnable server = () -> {
            serverSide = new TCPServer(socket);
            try {
                serverSide.run();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        };
        new Thread(server).start();
        ois = new DataInputStream(socket.getInputStream());
        oos = new DataOutputStream(socket.getOutputStream());
    }

    @Test
    public void testGetCount() throws IOException {
        oos.writeUTF("get count");
        oos.flush();
        String response = ois.readUTF();
        int count = productService.getProductMap().size();
        String expectedCount = String.valueOf(count);
        assertEquals(expectedCount, response);
    }

    @AfterClass
    public static void tearDown() throws IOException {
        oos.writeUTF("exit");
        oos.flush();
        socket.close();
    }

I do not understand a bit where I could make a mistake. Can you tell me where the error in the test is? Thank you in advance)

发现错误,该方法向我的请求中添加了oos.writeUTF("get count"); / t,如何将其删除?

0 个答案:

没有答案