Refactor Koan with slow/fast process example

A combined solution of the examples provided by
@iamvery and myself.
This commit is contained in:
Sven Gehring
2017-03-22 15:10:21 +01:00
parent 2881c95c00
commit c708936456
2 changed files with 25 additions and 11 deletions

View File

@@ -17,21 +17,23 @@ defmodule Processes do
assert is_pid(self()) == ___
end
koan "Like our current process, new processes are spawned functions" do
pid = spawn(fn -> nil end)
koan "New processes are spawned functions" do
value = spawn(fn -> nil end)
assert is_pid(pid) == ___
assert is_pid(value) == ___
end
koan "Processes exit after executing their function, unless they wait for something" do
wait_forever = fn ->
receive do
end
end
koan "Processes die when their function exits" do
fast_process = spawn(fn -> :timer.sleep(10) end)
slow_process = spawn(fn -> :timer.sleep(1000) end)
pid = spawn(wait_forever)
# All spawned functions are executed concurrently with the current process.
# You check back on slow_process and fast_process 50ms later. Let's
# see if they are still alive!
:timer.sleep(50)
assert Process.alive?(pid) == ___
assert Process.alive?(fast_process) == ___
assert Process.alive?(slow_process) == ___
end
koan "Processes can send and receive messages" do
@@ -42,6 +44,17 @@ defmodule Processes do
end
end
koan "A process will wait forever for a message" do
wait_forever = fn ->
receive do
end
end
pid = spawn(wait_forever)
assert Process.alive?(pid) == ___
end
koan "Received messages are queued, first in first out" do
send self(), "hola!"
send self(), "como se llama?"

View File

@@ -8,8 +8,9 @@ defmodule ProcessesTests do
:running,
true,
true,
true,
{:multiple, [false, true]},
"hola!",
true,
{:multiple, ["hola!", "como se llama?"]},
:how_are_you?,
{:multiple, ["O", "HAI"]},