@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)
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)
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.
I"m getting the same test failure when using the active: :once
mode. But the test is not deterministic, sometimes it succeeded, sometimes fails
One workaround is to add a little process sleep before de assert.
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"
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
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.
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! ).