Flakiness in testing #NV3

I’m stuck on Stage #NV3

My code is pretty much identical to the code others have submitted and for whom the test passes. For me the test passes locally and it passed once on the test server but more often than not, fails on the test server. I am not sure what the source of flakiness is here.

➜  codecrafters-kafka-rust git:(master) ✗ codecrafters test
Initiating test run...

⚡ This is a turbo test run. https://codecrafters.io/turbo

Running tests. Logs should appear shortly...

[compile]    Compiling codecrafters-kafka v0.1.0 (/app)
[compile]     Finished `release` profile [optimized] target(s) in 0.73s
[compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.

[tester::#NV3] Running tests for Stage #NV3 (Send Correlation ID)
[tester::#NV3] $ ./your_program.sh /tmp/server.properties
[tester::#NV3] Sending "ApiVersions" (version: 4) request (Correlation id: 7)
[tester::#NV3] ✓ Correlation ID: 7
[tester::#NV3] Test passed.

[tester::#VI6] Running tests for Stage #VI6 (Bind to a port)
[tester::#VI6] $ ./your_program.sh /tmp/server.properties
[tester::#VI6] Connecting to port 9092...
[tester::#VI6] Test passed.

Test passed. Congrats!

Submit your changes to move to the next step:

$ codecrafters submit
➜  codecrafters-kafka-rust git:(master) ✗ codecrafters submit
Submitting changes (commit: e48a43c)...

⚡ This is a turbo test run. https://codecrafters.io/turbo

Running tests. Logs should appear shortly...

[compile]    Compiling codecrafters-kafka v0.1.0 (/app)
[compile]     Finished `release` profile [optimized] target(s) in 0.78s
[compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.

[tester::#NV3] Running tests for Stage #NV3 (Send Correlation ID)
[tester::#NV3] $ ./your_program.sh /tmp/server.properties
[tester::#NV3] Sending "ApiVersions" (version: 4) request (Correlation id: 7)
[tester::#NV3] error reading from connection: read tcp 127.0.0.1:39494->127.0.0.1:9092: read: connection reset by peer
[tester::#NV3] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)

View our article on debugging test failures: https://codecrafters.io/debug

Above we see that when I ran codecrafters test, the tests passed, but when I ran codecrafters submit, the tests failed.

I have published my code in GitHub - akhi3030/codecrafters-kafka-rust and here’s a snippet of my code:

use std::io::Write;
use std::net::TcpListener;

fn main() {
    let listener = TcpListener::bind("127.0.0.1:9092").unwrap();
    for stream in listener.incoming() {
        match stream {
            Ok(mut stream) => {
                let buf: [u8; 8] = [0u8, 0, 0, 0, 0, 0, 0, 7];
                stream.write_all(&buf).unwrap();
            }
            Err(e) => {
                println!("error: {}", e);
            }
        }
    }
}```

I randomly ran codecrafters submit again and it passed. So I am going to move to the next stage. I am not sure if the flakiness will come back in the next stages though.

Hi @akhi3030, you can avoid the flakiness by shutting down the stream manually:

            Ok(mut stream) => {
                let buf: [u8; 8] = [0u8, 0, 0, 0, 0, 0, 0, 7];
                stream.write_all(&buf).unwrap();
                stream.shutdown(std::net::Shutdown::Both).unwrap();
            }

Thank you so much! That seems to work.

1 Like

@andy1li: out of curiousity, how did you figure this out.

@akhi3030 To be honest, I didn’t figure this out myself—other users have come across this before.

That said, it’s best practice to close the connection when it’s no longer needed, similar to closing a file after you’re done with it.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.