Skip to content

Commit

Permalink
Remove redundant ownership check (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
ypconstante authored Jun 25, 2024
1 parent 2b67d0a commit 26dd4cb
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions lib/mox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ defmodule Mox do
:ok

{:error, error} ->
raise ArgumentError, expectation_error_to_message(error, mock)
raise_cannot_add_expectation!(error, mock)
end
end

Expand All @@ -698,30 +698,40 @@ defmodule Mox do
:ok

{:error, error} ->
raise ArgumentError, expectation_error_to_message(error, mock)
raise_cannot_add_expectation!(error, mock)
end
end

defp expectation_error_to_message({:currently_allowed, owner_pid}, mock) do
defp raise_cannot_add_expectation!(
%NimbleOwnership.Error{reason: {:already_allowed, owner_pid}},
mock
) do
inspected = inspect(self())

"""
raise ArgumentError, """
cannot add expectations/stubs to #{inspect(mock)} in the current process (#{inspected}) \
because the process has been allowed by #{inspect(owner_pid)}. \
You cannot define expectations/stubs in a process that has been allowed
"""
end

defp expectation_error_to_message({:not_shared_owner, global_pid}, mock) do
defp raise_cannot_add_expectation!(
%NimbleOwnership.Error{reason: {:not_shared_owner, global_pid}},
mock
) do
inspected = inspect(self())

"""
raise ArgumentError, """
cannot add expectations/stubs to #{inspect(mock)} in the current process (#{inspected}) \
because Mox is in global mode and the global process is #{inspect(global_pid)}. \
Only the process that set Mox to global can set expectations/stubs in global mode
"""
end

defp raise_cannot_add_expectation!(error, _mock) do
raise error
end

@doc """
Allows other processes to share expectations and stubs
defined by owner process.
Expand Down Expand Up @@ -948,13 +958,14 @@ defmodule Mox do
end

defp add_expectations(owner_pid, mock, key_expectation_list) do
case ensure_pid_can_add_expectation(owner_pid, mock) do
:ok ->
update_fun = &{:ok, init_or_merge_expectations(&1, key_expectation_list)}
:ok = get_and_update!(owner_pid, mock, update_fun)
update_fun = &{:ok, init_or_merge_expectations(&1, key_expectation_list)}

case get_and_update(owner_pid, mock, update_fun) do
{:ok, _value} ->
:ok

{:error, reason} ->
{:error, reason}
{:error, error} ->
{:error, error}
end
end

Expand All @@ -981,17 +992,6 @@ defmodule Mox do
end
end

# Make sure that the owner_pid is either the owner or that the mock
# isn't owned yet.
defp ensure_pid_can_add_expectation(owner_pid, mock) do
case NimbleOwnership.fetch_owner(@this, [owner_pid], mock, @timeout) do
:error -> :ok
{tag, ^owner_pid} when tag in [:ok, :shared_owner] -> :ok
{:shared_owner, other_owner} -> {:error, {:not_shared_owner, other_owner}}
{:ok, other_owner} -> {:error, {:currently_allowed, other_owner}}
end
end

defp fetch_owner_from_callers(caller_pids, mock) do
# If the mock doesn't have an owner, it can't have expectations so we return :no_expectation.
case NimbleOwnership.fetch_owner(@this, caller_pids, mock, @timeout) do
Expand All @@ -1000,8 +1000,12 @@ defmodule Mox do
end
end

defp get_and_update(owner_pid, mock, update_fun) do
NimbleOwnership.get_and_update(@this, owner_pid, mock, update_fun, @timeout)
end

defp get_and_update!(owner_pid, mock, update_fun) do
case NimbleOwnership.get_and_update(@this, owner_pid, mock, update_fun, @timeout) do
case get_and_update(owner_pid, mock, update_fun) do
{:ok, return} -> return
{:error, %NimbleOwnership.Error{} = error} -> raise error
end
Expand Down

0 comments on commit 26dd4cb

Please sign in to comment.