Name Agent modules to avoid process leaking between examples

🤔 I'm not sure why this was an issue, because the agents are
started with a "link" and each example is run in a process itself...
Seems like we need better isolation between tests.
This commit is contained in:
Jay Hayes
2022-02-01 07:27:48 -06:00
parent 277e43c8dd
commit b8f7d1b411

View File

@@ -14,31 +14,31 @@ defmodule Agents do
end end
koan "Update to update the state" do koan "Update to update the state" do
Agent.start_link(fn -> "Hi there" end, name: __MODULE__) Agent.start_link(fn -> "Hi there" end, name: :greeter)
Agent.update(__MODULE__, fn old -> Agent.update(:greeter, fn old ->
String.upcase(old) String.upcase(old)
end) end)
assert Agent.get(__MODULE__, & &1) == ___ assert Agent.get(:greeter, & &1) == ___
end end
koan "Use get_and_update when you need to read and change a value in one go" do koan "Use get_and_update when you need to read and change a value in one go" do
Agent.start_link(fn -> ["Milk"] end, name: __MODULE__) Agent.start_link(fn -> ["Milk"] end, name: :groceries)
old_list = old_list =
Agent.get_and_update(__MODULE__, fn old -> Agent.get_and_update(:groceries, fn old ->
{old, ["Bread" | old]} {old, ["Bread" | old]}
end) end)
assert old_list == ___ assert old_list == ___
assert Agent.get(__MODULE__, & &1) == ___ assert Agent.get(:groceries, & &1) == ___
end end
koan "Somebody has to switch off the light at the end of the day" do koan "Somebody has to switch off the light at the end of the day" do
{:ok, pid} = Agent.start_link(fn -> ["Milk"] end, name: __MODULE__) {:ok, pid} = Agent.start_link(fn -> "Fin." end, name: :stoppable)
Agent.stop(__MODULE__) Agent.stop(:stoppable)
assert Process.alive?(pid) == ___ assert Process.alive?(pid) == ___
end end