Table of Contents
Question content
Solution
Home Java 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?

Feb 22, 2024 pm 01:34 PM

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());
Copy after login

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());
Copy after login

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());
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)