elixirkoans / elixir-koans

Elixir learning exercises
MIT License
2.21k stars 597 forks source link

weird answer for process koan about message queue #275

Closed EB-Plum closed 2 years ago

EB-Plum commented 2 years ago

https://github.com/elixirkoans/elixir-koans/blob/f5e3f75448e9a44bb3445d670f65ed37a216c792/lib/koans/15_processes.ex#L62-L68

# lib/koans/15_processes.ex. line 62
koan "Received messages are queued, first in first out" do
    send(self(), "hola!")
    send(self(), "como se llama?")
    send(self(), "1")
    send(self(), "2")
    send(self(), "3")

    assert_receive "1"
    assert_receive "3"
    assert_receive "como se llama?"
    assert_receive "hola!"
    assert_receive "2"
  end

i tried wrong answer(?), but somehow it passed. is this just my fault? or assert_receive acting weird?

EB-Plum commented 2 years ago

https://hexdocs.pm/ex_unit/1.13/ExUnit.Assertions.html#assert_receive/3 according to doc, it seems assert_receive is not considering message order. it only checks if message was delivered or not.

just in case, if anyone like me want to see the queue. try code below.

koan "Received messages are queued, first in first out" do
    send(self(), "hola!")
    send(self(), "como se llama?")

    {:messages, queue } = Process.info(self(), :messages)

    assert Enum.at(queue, 0) == ___
    assert Enum.at(queue, 1) == ___
end
iamvery commented 2 years ago

👀 #276