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()) == ___ assert is_pid(self()) == ___
end end
koan "Like our current process, new processes are spawned functions" do koan "New processes are spawned functions" do
pid = spawn(fn -> nil end) value = spawn(fn -> nil end)
assert is_pid(pid) == ___ assert is_pid(value) == ___
end end
koan "Processes exit after executing their function, unless they wait for something" do koan "Processes die when their function exits" do
wait_forever = fn -> fast_process = spawn(fn -> :timer.sleep(10) end)
receive do slow_process = spawn(fn -> :timer.sleep(1000) end)
end
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 end
koan "Processes can send and receive messages" do koan "Processes can send and receive messages" do
@@ -42,6 +44,17 @@ defmodule Processes do
end end
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 koan "Received messages are queued, first in first out" do
send self(), "hola!" send self(), "hola!"
send self(), "como se llama?" send self(), "como se llama?"

View File

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