samedi 5 décembre 2015

JUnit testing hangs at clientSocket.accept()?

I need to test some client/server application using JUnit and I'm having trouble testing the connection of a client to the server. It seems that the test hangs at the "Socket clientSocket = serverSocket.accept()" line in the server class, and it's not letting me connect a new client.

Here's the JUnit class:

 @Test
        public void testNetServerComm_1() throws Exception {
            server = new NetServerComm();
            assertNotNull(server);
        }

        @Test
        public void testAddMessageToQueue_1() throws Exception {
            NetServerComm fixture = new NetServerComm();
            NetServerSideMessage msg = new NetServerSideMessage(birdsong.comm.ServerSideMessage.Type.BIRDSONG_MESSAGE, "",
                    "");
            fixture.addMessageToQueue(msg);
            assertEquals(1, fixture.getMsgQueueSize());
        }

    @Test
        public void testClientIsConnected_1() throws Exception {
            NetServerComm fixture = new NetServerComm();
            fixture.start();

            //test doesn't go further that this..

            String nickname = "john";
            new NetClientComm().connect("127.0.0.1", nickname);
            boolean result = fixture.isClientConnected(nickname);
            assertTrue(result);
        }

Client Class:

//connects to the server
@Override
    public void connect(String host, String nickname) {
        try {
            clientNickname = nickname;
            System.out.println("Address= " + host + " and NickName = " + nickname);
            socket = new Socket(host, NetServerComm.PORTO); //8080
            System.out.println("Socket = " + socket);
            serverMsg = new NetServerSideMessage(ServerSideMessage.Type.CLIENT_CONNECTED, null, clientNickname);
            out = new ObjectOutputStream(socket.getOutputStream());
            out.writeObject(serverMsg);
            isConnected = true;
            messageHandler = new Thread(new MessageHandler(socket));
            messageHandler.start();
        } catch (IOException e) {
            System.out.println("Failed connecting to the server.");
            e.printStackTrace();
        }
    }

Server Class:

//starts the server
@Override
    public void start() {
        try {
            serverSocket = new ServerSocket(PORTO); //8080
            System.out.println("Servidor is online: " + serverSocket);
            try {
                while (true) {
                    Socket clientSocket = serverSocket.accept();

                     //test hangs at the method above^^^

                    checkNewClient(clientSocket);
                    if (nameCheck == true)
                        new Thread(new ClientHandler(clientSocket)).start();
                }
            } finally {
                System.out.println("Closing the server..");
                serverSocket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Aucun commentaire:

Enregistrer un commentaire