# Flakiness in testing #NV3

**URL:** <https://forum.codecrafters.io/t/flakiness-in-testing-nv3/3818>\
**Category:** Challenges\
**Tags:** challenge:kafka\
**Created:** [December 30, 2024, 6:00pm UTC](https://forum.codecrafters.io/t/flakiness-in-testing-nv3/3818 "2024-12-30T18:00:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![akhi3030](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/akhi3030/32/3793_2.png) [@akhi3030](https://forum.codecrafters.io/u/akhi3030)\
**Post date:** [December 30, 2024, 6:00pm UTC](https://forum.codecrafters.io/t/flakiness-in-testing-nv3/3818/1 "2024-12-30T18:00:17Z")

</div>

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.

```auto
➜ 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](https://github.com/akhi3030/codecrafters-kafka-rust) and here’s a snippet of my code:

````rust
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);
            }
        }
    }
}```
````

---

<div class="post-metadata">

**Author:** ![akhi3030](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/akhi3030/32/3793_2.png) [@akhi3030](https://forum.codecrafters.io/u/akhi3030)\
**Post date:** [December 30, 2024, 8:21pm UTC](https://forum.codecrafters.io/t/flakiness-in-testing-nv3/3818/2 "2024-12-30T20:21:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [December 31, 2024, 6:09am UTC](https://forum.codecrafters.io/t/flakiness-in-testing-nv3/3818/4 "2024-12-31T06:09:41Z")

</div>

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

```rust
            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();
            }

```

---

<div class="post-metadata">

**Author:** ![akhi3030](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/akhi3030/32/3793_2.png) [@akhi3030](https://forum.codecrafters.io/u/akhi3030)\
**Post date:** [January 5, 2025, 5:45pm UTC](https://forum.codecrafters.io/t/flakiness-in-testing-nv3/3818/6 "2025-01-05T17:45:49Z")

</div>

Thank you so much! That seems to work.

---

<div class="post-metadata">

**Author:** ![akhi3030](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/akhi3030/32/3793_2.png) [@akhi3030](https://forum.codecrafters.io/u/akhi3030)\
**Post date:** [January 5, 2025, 5:47pm UTC](https://forum.codecrafters.io/t/flakiness-in-testing-nv3/3818/7 "2025-01-05T17:47:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [January 5, 2025, 6:08pm UTC](https://forum.codecrafters.io/t/flakiness-in-testing-nv3/3818/8 "2025-01-05T18:08:59Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [January 10, 2025, 6:09pm UTC](https://forum.codecrafters.io/t/flakiness-in-testing-nv3/3818/9 "2025-01-10T18:09:16Z")

</div>

Closed on January 10, 2025, 6:09pm UTC

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