多线程JUnit测试与ServerSockets和套接字



我正试图为我编写的服务器/客户端编写一个JUnit测试,因此我创建了Runnable作为服务器,而我当前的线程作为客户端。在尝试写入客户端套接字的输出流之前,我在服务器Runnable上调用start()。然而,我的程序总是在Socket client = new Socket("hostname", 0);之后终止,我不知道为什么。我的猜测是,这是因为我试图在同一测试中创建套接字和客户端?因此,绑定一个ip作为客户端,同时监听该ip会导致不正常的行为吗?这是正确的吗?我该如何解决这个困境?

public void test() {
        int result;
        String strMsg = "dasda";
        try {
            Thread serverThread = new Thread(new ServerRunnable());
            serverThread.start();
            Socket client = new Socket("hostname", 0);
            OutputStream os = client.getOutputStream();
            os.write(strMsg.getBytes());
            InputStream is = client.getInputStream();
            while (true){
                result = is.read();
                ByteBuffer bb = ByteBuffer.allocate(4);
                bb.putInt(result);
                String input = new String(bb.array());
                if (input=="Success") return;
            }
        } catch (IOException e1){
            fail("IOException on client");
        }
    }
    class ServerRunnable implements Runnable {
        ServerSocket server;
        public ServerRunnable(){
            server = new ServerSocket(0);
        }
        public void run(){
            try {
                active = true;
                    while (active) {
                     Socket sock = server.accept();
                    }
            } catch (IOException e1){
                fail("IOException in Server");
            }
        }
    }

new ServerSocket(0)将创建一个监听任何空闲端口的服务器,该端口在每次运行测试时都会变化。虽然new Socket("hostname", 0)正在尝试连接端口0,但将失败。

由于您首先初始化服务器,因此您可以在ServerSocket上调用getLocalPort()以获取服务器正在侦听的端口,然后在创建客户端Socket时使用此端口号。

您可能还需要将主机从"hostname"更改为"localhost",以便它将连接到本地计算机的端口。

下面是一个改编自你的代码的例子。主要看的是Socket client = new Socket("localhost", sr.getPort());

public void test() throws Exception {
    // start up the server
    ServerRunnable sr = new ServerRunnable();
    Thread serverThread = new Thread(sr);
    serverThread.start();
    // connect the client to the port the server is listening on
    Socket client = new Socket("localhost", sr.getPort());
    // client socket interactions go here
    client.close();
}
class ServerRunnable implements Runnable {
    private ServerSocket server;
    public ServerRunnable() throws IOException {
        // listen on any free port
        server = new ServerSocket(0);
    }
    public void run() {
        try {
            while (true) {
                Socket sock = server.accept();
                // normally you will need to start a thread to handle
                // the new socket so that the server will be able to accept
                // new connections but this may not be necessary for
                // unit testing where only a single connection occurs.
                sock.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public int getPort() {
        return server.getLocalPort();
    }
}

最新更新