Skip to content

Commit

Permalink
Fix race condition of Tesla.Mock.mock_global.
Browse files Browse the repository at this point in the history
Agent process is terminated asynchronously and can leak between tests, causing it to fail (update is not atomic, and returned pid might not be alive anymore).
  • Loading branch information
sircinek committed Mar 18, 2021
1 parent 9c1c1ff commit 3630fa3
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/tesla/mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,14 @@ defmodule Tesla.Mock do

defp agent_set(fun) do
case Process.whereis(__MODULE__) do
nil -> Agent.start_link(fn -> fun end, name: __MODULE__)
pid -> Agent.update(pid, fn _ -> fun end)
nil ->
ExUnit.Callbacks.start_supervised!(%{
id: __MODULE__,
start: {Agent, :start_link, [fn -> fun end, [{:name, __MODULE__}]]}
})

pid ->
Agent.update(pid, fn _ -> fun end)
end
end

Expand Down

0 comments on commit 3630fa3

Please sign in to comment.