From bef1cf502873fcb0f3ba19a4171b28fc008ede64 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Mon, 2 May 2016 18:49:40 -0500 Subject: [PATCH 01/10] Add new test illustrating spawning processes --- lib/koans/10_processes.ex | 6 ++++++ test/koans/processes_koans_test.exs | 1 + 2 files changed, 7 insertions(+) diff --git a/lib/koans/10_processes.ex b/lib/koans/10_processes.ex index 321979b..39cf5a9 100644 --- a/lib/koans/10_processes.ex +++ b/lib/koans/10_processes.ex @@ -11,6 +11,12 @@ defmodule Processes do assert information[:status] == ___ end + koan "New processes are spawned" do + pid = spawn(fn -> nil end) + + assert Process.alive?(pid) == ___ + end + koan "You can send messages to any process you want" do send self(), "hola!" assert_receive ___ diff --git a/test/koans/processes_koans_test.exs b/test/koans/processes_koans_test.exs index cbc7940..e8468ef 100644 --- a/test/koans/processes_koans_test.exs +++ b/test/koans/processes_koans_test.exs @@ -6,6 +6,7 @@ defmodule ProcessesTests do answers = [ true, :running, + true, "hola!", :how_are_you?, {:waited_too_long, "I am inpatient"}, From dacaf4c0b16fbf98d268c1eb284a225db0e852b6 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Mon, 2 May 2016 18:50:35 -0500 Subject: [PATCH 02/10] Actually illustrate use of receive function --- lib/koans/10_processes.ex | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/koans/10_processes.ex b/lib/koans/10_processes.ex index 39cf5a9..3a0b6a5 100644 --- a/lib/koans/10_processes.ex +++ b/lib/koans/10_processes.ex @@ -19,7 +19,10 @@ defmodule Processes do koan "You can send messages to any process you want" do send self(), "hola!" - assert_receive ___ + + receive do + msg -> assert msg == ___ + end end koan "A common pattern is to include the sender in the message" do From 6772308e9ad6ffbbeb8c2bcbe5004e78cf6d7451 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Mon, 2 May 2016 18:51:01 -0500 Subject: [PATCH 03/10] Add a little more context to example description --- lib/koans/10_processes.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/koans/10_processes.ex b/lib/koans/10_processes.ex index 3a0b6a5..95610d2 100644 --- a/lib/koans/10_processes.ex +++ b/lib/koans/10_processes.ex @@ -25,7 +25,7 @@ defmodule Processes do end end - koan "A common pattern is to include the sender in the message" do + koan "A common pattern is to include the sender in the message, so that it can reply" do pid = spawn(fn -> receive do {:hello, sender} -> send sender, :how_are_you? _ -> assert false From b955a4db64981c3649284ab3f72ee26b74d3436d Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Mon, 2 May 2016 18:54:22 -0500 Subject: [PATCH 04/10] Remove unneeded case --- lib/koans/10_processes.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/koans/10_processes.ex b/lib/koans/10_processes.ex index 95610d2..7ffe618 100644 --- a/lib/koans/10_processes.ex +++ b/lib/koans/10_processes.ex @@ -28,7 +28,6 @@ defmodule Processes do koan "A common pattern is to include the sender in the message, so that it can reply" do pid = spawn(fn -> receive do {:hello, sender} -> send sender, :how_are_you? - _ -> assert false end end) From 2d7982e9a3f9d2b064529f50c96563c024404898 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Mon, 2 May 2016 18:55:00 -0500 Subject: [PATCH 05/10] Remove indirection of wait/0 --- lib/koans/10_processes.ex | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/koans/10_processes.ex b/lib/koans/10_processes.ex index 7ffe618..96bc378 100644 --- a/lib/koans/10_processes.ex +++ b/lib/koans/10_processes.ex @@ -69,7 +69,11 @@ defmodule Processes do {:EXIT, _pid, reason} -> send parent, {:exited, reason} end end) - wait() + + receive do + :ready -> true + end + Process.exit(pid, :random_reason) assert_receive ___ @@ -118,10 +122,4 @@ defmodule Processes do assert_receive ___ end - - def wait do - receive do - :ready -> true - end - end end From 8ea68548fe79bb9f028f961bde9fe54f3f617268 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Mon, 2 May 2016 18:56:01 -0500 Subject: [PATCH 06/10] Simplify example --- lib/koans/10_processes.ex | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/koans/10_processes.ex b/lib/koans/10_processes.ex index 96bc378..f38ac00 100644 --- a/lib/koans/10_processes.ex +++ b/lib/koans/10_processes.ex @@ -88,12 +88,7 @@ defmodule Processes do end koan "Exiting yourself on the other hand DOES terminate you" do - pid = spawn(fn -> receive do - :bye -> Process.exit(self(), :normal) - end - end) - - send pid, :bye + pid = spawn(fn -> Process.exit(self, :normal) end) :timer.sleep(100) assert Process.alive?(pid) == ___ end From 71dfd882c6d7f3cac865a47549bfe303b8ee483c Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Mon, 2 May 2016 18:56:13 -0500 Subject: [PATCH 07/10] Tweak wording --- lib/koans/10_processes.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/koans/10_processes.ex b/lib/koans/10_processes.ex index f38ac00..67a1437 100644 --- a/lib/koans/10_processes.ex +++ b/lib/koans/10_processes.ex @@ -87,7 +87,7 @@ defmodule Processes do assert Process.alive?(pid) == ___ 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 -> Process.exit(self, :normal) end) :timer.sleep(100) assert Process.alive?(pid) == ___ From cc47f7c9c63a0987aba4544847c6012efa0e95da Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Mon, 2 May 2016 18:56:27 -0500 Subject: [PATCH 08/10] Simplify cases --- lib/koans/10_processes.ex | 22 +++++----------------- test/koans/processes_koans_test.exs | 4 ++-- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/lib/koans/10_processes.ex b/lib/koans/10_processes.ex index 67a1437..ace7878 100644 --- a/lib/koans/10_processes.ex +++ b/lib/koans/10_processes.ex @@ -94,27 +94,15 @@ defmodule Processes do end koan "Parent processes can be informed about exiting children, if they trap and link" do - parent = self() - spawn(fn -> - Process.flag(:trap_exit, true) - spawn_link(fn -> Process.exit(self(), :normal) end) - receive do - {:EXIT, _pid ,reason} -> send parent, {:exited, reason} - end - end) + Process.flag(:trap_exit, true) + spawn_link(fn -> Process.exit(self, :normal) end) - assert_receive ___ + assert_receive {:EXIT, _pid, ___} end koan "If you monitor your children, you'll be automatically informed for their depature" do - parent = self() - spawn(fn -> - spawn_monitor(fn -> Process.exit(self(), :normal) end) - receive do - {:DOWN, _ref, :process, _pid, reason} -> send parent, {:exited, reason} - end - end) + spawn_monitor(fn -> Process.exit(self(), :normal) end) - assert_receive ___ + assert_receive {:DOWN, _ref, :process, _pid, ___} end end diff --git a/test/koans/processes_koans_test.exs b/test/koans/processes_koans_test.exs index e8468ef..4149d52 100644 --- a/test/koans/processes_koans_test.exs +++ b/test/koans/processes_koans_test.exs @@ -15,8 +15,8 @@ defmodule ProcessesTests do {:exited, :random_reason}, true, false, - {:exited, :normal}, - {:exited, :normal}, + :normal, + :normal, ] test_all(Processes, answers) From 161148320676179f3d4984ae7f96afd9f9388064 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Mon, 2 May 2016 19:22:58 -0500 Subject: [PATCH 09/10] Reword some more examples --- lib/koans/10_processes.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/koans/10_processes.ex b/lib/koans/10_processes.ex index ace7878..d73a2a0 100644 --- a/lib/koans/10_processes.ex +++ b/lib/koans/10_processes.ex @@ -1,7 +1,7 @@ defmodule Processes do use Koans - koan "Tests run in a process" do + koan "You are a process" do assert Process.alive?(self()) == ___ end @@ -11,13 +11,13 @@ defmodule Processes do assert information[:status] == ___ end - koan "New processes are spawned" do + koan "New processes are spawned functions" do pid = spawn(fn -> nil end) assert Process.alive?(pid) == ___ end - koan "You can send messages to any process you want" do + koan "You can send messages to processes" do send self(), "hola!" receive do From b2458bb4721ac36337934606d5f6dea77390831d Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Mon, 2 May 2016 19:23:10 -0500 Subject: [PATCH 10/10] Consistent one-liner for receive-blocked processes --- lib/koans/10_processes.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/koans/10_processes.ex b/lib/koans/10_processes.ex index d73a2a0..4b56c5d 100644 --- a/lib/koans/10_processes.ex +++ b/lib/koans/10_processes.ex @@ -80,16 +80,16 @@ defmodule Processes do end koan "Trying to quit normally has no effect" do - pid = spawn(fn -> receive do - end - end) + pid = spawn(fn -> receive do end end) Process.exit(pid, :normal) + assert Process.alive?(pid) == ___ end koan "Exiting normally yourself on the other hand DOES terminate you" do pid = spawn(fn -> Process.exit(self, :normal) end) :timer.sleep(100) + assert Process.alive?(pid) == ___ end