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

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. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment