Koans about processes including infrastructure.

This commit is contained in:
Felipe Sere
2016-03-06 21:27:51 +00:00
parent f2b4cec409
commit c450cc3b19
6 changed files with 111 additions and 5 deletions

53
lib/koans/10_processes.ex Normal file
View File

@@ -0,0 +1,53 @@
defmodule Processes do
use Koans
koan "tests run in a process!" do
assert Process.alive?(self)
end
koan "can spew out information about a process" do
information = Process.info(self)
assert information[:status] == :running
end
koan "process can send messages to itself" do
send self(), "hola!"
receive do
message -> assert message == "hola!"
end
end
koan "a spawned process is independent of the current process" do
pid = spawn(fn -> receive do
{:hello, thing} -> assert thing == "world"
_ -> assert false
end
end)
send pid, {:hello, "world"}
end
koan "a common pattern is to include the sender in the message" do
pid = spawn(fn -> receive do
{:hello, sender} -> send sender, :how_are_you?
_ -> assert false
end
end)
send pid, {:hello, self()}
assert_receive :how_are_you?
end
koan "you don't have to wait forever for messages" do
parent = self
spawn(fn -> receive do
_ -> assert false
after
10 -> send parent, {:waited_too_long, "I am inpatient"}
end
end)
assert_receive {:waited_too_long, "I am inpatient"}
end
end