# Network Programming in Elixir and Erlang (B1.0): Test fails (p25)

**URL:** <https://forum.devtalk.com/t/network-programming-in-elixir-and-erlang-b1-0-test-fails-p25/167910>\
**Category:** PragProg Customers\
**Tags:** book-network-programming-in-elixir-and-erlang, errata\
**Created:** [28 August 2024 14:28 UTC](https://forum.devtalk.com/t/network-programming-in-elixir-and-erlang-b1-0-test-fails-p25/167910 "2024-08-28T14:28:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![deldutt](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/d/5f8ce5/32.png) [@deldutt](https://forum.devtalk.com/u/deldutt)\
**Post date:** [28 August 2024 14:28 UTC](https://forum.devtalk.com/t/network-programming-in-elixir-and-erlang-b1-0-test-fails-p25/167910/1 "2024-08-28T14:28:02Z")

</div>

### @whatyouhide

Test that is in page 25 fails. The name of the test is: _handles fragmented data_.  
Elixir version: 1.17.2 (compiled with Erlang/OTP 27)

---

<div class="post-metadata">

**Author:** ![dan](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://forum.devtalk.com/u/dan)\
**Post date:** [20 November 2024 18:19 UTC](https://forum.devtalk.com/t/network-programming-in-elixir-and-erlang-b1-0-test-fails-p25/167910/2 "2024-11-20T18:19:33Z")

</div>

I could not get the single call to :gen\_tcp.recv/3 to pull in all the data from the two :gen\_tcp.send/2 calls as written. I consequently wrapped the :gen\_tcp.recv/3 in a recursive call and subsequently was able to get the test to pass as written.

---

<div class="post-metadata">

**Author:** ![amedeo](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@amedeo](https://forum.devtalk.com/u/amedeo)\
**Post date:** [18 December 2024 18:46 UTC](https://forum.devtalk.com/t/network-programming-in-elixir-and-erlang-b1-0-test-fails-p25/167910/3 "2024-12-18T18:46:48Z")

</div>

I"m getting the same test failure when using the `active: :once` mode. But the test is not deterministic, sometimes it succeeded, sometimes fails

---

<div class="post-metadata">

**Author:** ![mursisoy](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mursisoy/32/7224_2.png) [@mursisoy](https://forum.devtalk.com/u/mursisoy)\
**Post date:** [16 March 2025 16:10 UTC](https://forum.devtalk.com/t/network-programming-in-elixir-and-erlang-b1-0-test-fails-p25/167910/4 "2025-03-16T16:10:51Z")

</div>

One workaround is to add a little process sleep before de assert.

```auto
assert :ok = :gen_tcp.send(socket, "Hello")
assert :ok = :gen_tcp.send(socket, " world\nand one more\n")
Process.sleep(100)
assert {:ok, data} = :gen_tcp.recv(socket, 0, 500)
assert data == "Hello world\nand one more\n"

```

---

<div class="post-metadata">

**Author:** ![FormlessEvoker](https://forum.devtalk.com/user_avatar/forum.devtalk.com/formlessevoker/32/7370_2.png) [@FormlessEvoker](https://forum.devtalk.com/u/FormlessEvoker)\
**Post date:** [1 May 2025 15:07 UTC](https://forum.devtalk.com/t/network-programming-in-elixir-and-erlang-b1-0-test-fails-p25/167910/5 "2025-05-01T15:07:10Z")

</div>

This does work for me, but my college Operating Systems professor would have a stroke if I used `sleep` as a solution to a race condition. I can hear him now, just thinking about it 😅

---

<div class="post-metadata">

**Author:** ![FormlessEvoker](https://forum.devtalk.com/user_avatar/forum.devtalk.com/formlessevoker/32/7370_2.png) [@FormlessEvoker](https://forum.devtalk.com/u/FormlessEvoker)\
**Post date:** [1 May 2025 15:07 UTC](https://forum.devtalk.com/t/network-programming-in-elixir-and-erlang-b1-0-test-fails-p25/167910/6 "2025-05-01T15:07:15Z")

</div>

It appeared to work a bit better when I had the socket in `active: :once` mode and `packet: :line`, but it did eventually fail, so that was probably a fluke.

I think this situation could honestly be turned into a really neat lesson in the book about how to ensure you have received all messages/packets before running a handler. The book could maybe even just point out that if you run the test multiple times, it will occasionally fail, and then use that as a hint about a topic that will be fixed/addressed in a later section.

---

<div class="post-metadata">

**Author:** ![whatyouhide](https://forum.devtalk.com/user_avatar/forum.devtalk.com/whatyouhide/32/798_2.png) [@whatyouhide](https://forum.devtalk.com/u/whatyouhide)\
**Post date:** [17 May 2025 20:25 UTC](https://forum.devtalk.com/t/network-programming-in-elixir-and-erlang-b1-0-test-fails-p25/167910/7 "2025-05-17T20:25:53Z")

</div>

Great catch folks! The issue here is that `gen_tcp:send/2` is sometimes buffering packets and sometimes not, so sometimes it’ll send both lines as a single TCP packet and sometimes as two. I added an aside to the book, I agree that this is an interesting callout to make (and lesson to learn about testing TCP! 🙃).
