Skip to content

Commit

Permalink
Merge pull request #456 from sircinek/fix-race-condition-in-tesla-moc…
Browse files Browse the repository at this point in the history
…k-global

Fix race condition of Tesla.Mock.mock_global/1
  • Loading branch information
teamon authored Mar 24, 2021
2 parents a66651f + 3630fa3 commit 4aa32ae
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 4aa32ae

Please sign in to comment.