diff --git a/lib/koans/15_processes.ex b/lib/koans/15_processes.ex index 65c3419..d141e5d 100644 --- a/lib/koans/15_processes.ex +++ b/lib/koans/15_processes.ex @@ -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?" diff --git a/test/koans/processes_koans_test.exs b/test/koans/processes_koans_test.exs index d3dd9a5..2b50ac0 100644 --- a/test/koans/processes_koans_test.exs +++ b/test/koans/processes_koans_test.exs @@ -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"]},