Home  >  Article  >  Why do my Java server tests pass when I add a System.out.println(in.readLine()) statement before the test, but fail without it?

Why do my Java server tests pass when I add a System.out.println(in.readLine()) statement before the test, but fail without it?

王林
王林forward
2024-02-22 13:34:06738browse

php editor Yuzai’s answer: Adding the System.out.println(in.readLine()) statement in the Java server test may affect the test results, because this statement will read the server’s input stream so that the server can receive it. ask. Without this statement, the server may not receive the request correctly, causing the test to fail. Therefore, adding this statement before the test is to ensure that the server can receive the request normally and pass the test.

Question content

I am trying to implement a server in java to handle the multiplayer minesweeper game. While trying to test my server to make sure it responds correctly to legitimate client requests, I found that only adding a system.out.println(in.readline()) statement after printing a specific client request to the server would pass the test output stream , but before calling the test itself.

For example, one request a client can make is a "view" request to see the current state of the board. I originally wrote a test for "look" like this:

@test(timeout = 10000)
    public void publishedtest() throws ioexception {

        thread thread = startminesweeperserver("board5");

        socket socket = connecttominesweeperserver(thread);
        
        bufferedreader in = new bufferedreader(new inputstreamreader(socket.getinputstream()));
        printwriter out = new printwriter(socket.getoutputstream(), true);

        out.println("look");
        assertequals("- - - - - - -", in.readline());
        assertequals("- - - - - - -", in.readline());
        assertequals("- - - - - - -", in.readline());
        assertequals("- - - - - - -", in.readline());
        assertequals("- - - - - - -", in.readline());
        assertequals("- - - - - - -", in.readline());
        assertequals("- - - - - - -", in.readline());

However, when I run the test like this, it fails and I get an error message stating that it expected [- - - - - - -] but got [].

However, when I add a print statement like this:

@Test(timeout = 10000)
    public void publishedTest() throws IOException {

        Thread thread = startMinesweeperServer("board5");

        Socket socket = connectToMinesweeperServer(thread);

        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

        out.println("look");
        System.out.println(in.readLine());
        assertEquals("- - - - - - -", in.readLine());
        assertEquals("- - - - - - -", in.readLine());
        assertEquals("- - - - - - -", in.readLine());
        assertEquals("- - - - - - -", in.readLine());
        assertEquals("- - - - - - -", in.readLine());
        assertEquals("- - - - - - -", in.readLine());
        assertEquals("- - - - - - -", in.readLine());

Test passed. Why is there such a situation? Does the print statement just give the client more time to read input from the server, or is there possibly something else going on? Happy to provide more snippets of my code if it helps.

Solution

Blind guess: Your server response started with an unexpected blank line.

The

system.out.println(in.readline());<code>println part doesn't really matter, just use in.readline() and you'll get Same result as it "consumes" the first row from the server. This is not a timing issue since readline() blocks until the entire line is received.

You can fix this on the server by removing anything that sends empty lines, or modifying the test to fix the problem:

out.println("look");
    assertEquals("", in.readLine()); // Add this line.
    assertEquals("- - - - - - -", in.readLine());
    assertEquals("- - - - - - -", in.readLine());
    assertEquals("- - - - - - -", in.readLine());
    assertEquals("- - - - - - -", in.readLine());
    assertEquals("- - - - - - -", in.readLine());
    assertEquals("- - - - - - -", in.readLine());
    assertEquals("- - - - - - -", in.readLine());

The above is the detailed content of Why do my Java server tests pass when I add a System.out.println(in.readLine()) statement before the test, but fail without it?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete