Merge pull request #95 from iamvery/process-tests

Restructure/simplify process koans
This commit is contained in:
Uku Taht
2016-05-03 22:41:44 +01:00
2 changed files with 32 additions and 42 deletions

View File

@@ -1,7 +1,7 @@
defmodule Processes do defmodule Processes do
use Koans use Koans
koan "Tests run in a process" do koan "You are a process" do
assert Process.alive?(self()) == ___ assert Process.alive?(self()) == ___
end end
@@ -11,15 +11,23 @@ defmodule Processes do
assert information[:status] == ___ assert information[:status] == ___
end end
koan "You can send messages to any process you want" do koan "New processes are spawned functions" do
send self(), "hola!" pid = spawn(fn -> nil end)
assert_receive ___
assert Process.alive?(pid) == ___
end end
koan "A common pattern is to include the sender in the message" do koan "You can send messages to processes" do
send self(), "hola!"
receive do
msg -> assert msg == ___
end
end
koan "A common pattern is to include the sender in the message, so that it can reply" do
pid = spawn(fn -> receive do pid = spawn(fn -> receive do
{:hello, sender} -> send sender, :how_are_you? {:hello, sender} -> send sender, :how_are_you?
_ -> assert false
end end
end) end)
@@ -61,59 +69,40 @@ defmodule Processes do
{:EXIT, _pid, reason} -> send parent, {:exited, reason} {:EXIT, _pid, reason} -> send parent, {:exited, reason}
end end
end) end)
wait()
receive do
:ready -> true
end
Process.exit(pid, :random_reason) Process.exit(pid, :random_reason)
assert_receive ___ assert_receive ___
end end
koan "Trying to quit normally has no effect" do koan "Trying to quit normally has no effect" do
pid = spawn(fn -> receive do pid = spawn(fn -> receive do end end)
end
end)
Process.exit(pid, :normal) Process.exit(pid, :normal)
assert Process.alive?(pid) == ___ assert Process.alive?(pid) == ___
end end
koan "Exiting yourself on the other hand DOES terminate you" do koan "Exiting normally yourself on the other hand DOES terminate you" do
pid = spawn(fn -> receive do pid = spawn(fn -> Process.exit(self, :normal) end)
:bye -> Process.exit(self(), :normal)
end
end)
send pid, :bye
:timer.sleep(100) :timer.sleep(100)
assert Process.alive?(pid) == ___ assert Process.alive?(pid) == ___
end end
koan "Parent processes can be informed about exiting children, if they trap and link" do koan "Parent processes can be informed about exiting children, if they trap and link" do
parent = self() Process.flag(:trap_exit, true)
spawn(fn -> spawn_link(fn -> Process.exit(self, :normal) end)
Process.flag(:trap_exit, true)
spawn_link(fn -> Process.exit(self(), :normal) end)
receive do
{:EXIT, _pid ,reason} -> send parent, {:exited, reason}
end
end)
assert_receive ___ assert_receive {:EXIT, _pid, ___}
end end
koan "If you monitor your children, you'll be automatically informed for their depature" do koan "If you monitor your children, you'll be automatically informed for their depature" do
parent = self() spawn_monitor(fn -> Process.exit(self(), :normal) end)
spawn(fn ->
spawn_monitor(fn -> Process.exit(self(), :normal) end)
receive do
{:DOWN, _ref, :process, _pid, reason} -> send parent, {:exited, reason}
end
end)
assert_receive ___ assert_receive {:DOWN, _ref, :process, _pid, ___}
end
def wait do
receive do
:ready -> true
end
end end
end end

View File

@@ -6,6 +6,7 @@ defmodule ProcessesTests do
answers = [ answers = [
true, true,
:running, :running,
true,
"hola!", "hola!",
:how_are_you?, :how_are_you?,
{:waited_too_long, "I am inpatient"}, {:waited_too_long, "I am inpatient"},
@@ -14,8 +15,8 @@ defmodule ProcessesTests do
{:exited, :random_reason}, {:exited, :random_reason},
true, true,
false, false,
{:exited, :normal}, :normal,
{:exited, :normal}, :normal,
] ]
test_all(Processes, answers) test_all(Processes, answers)