Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide committed Mar 8, 2024
1 parent ba35017 commit 0d60735
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/nimble_ownership.ex
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ defmodule NimbleOwnership do
state =
if not Map.has_key?(state.owner_cleanup, owner_pid) do
_ref = Process.monitor(owner_pid)
state
put_in(state.owner_cleanup[owner_pid], :auto)
else
state
end
Expand Down Expand Up @@ -514,11 +514,11 @@ defmodule NimbleOwnership do
# An owner went down, so we need to clean up all of its allowances as well as all its keys.
def handle_info({:DOWN, _ref, _, down_pid, _}, state)
when is_map_key(state.owners, down_pid) do
case state.owner_cleanup[down_pid] do
case state.owner_cleanup[down_pid] || :auto do
:manual ->
{:noreply, state}

nil ->
:auto ->
state = pop_owner_and_clean_up_allowances(state, down_pid)
{:noreply, state}
end
Expand Down
18 changes: 18 additions & 0 deletions test/nimble_ownership_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,24 @@ defmodule NimbleOwnershipTest do

assert :error = NimbleOwnership.fetch_owner(@server, [child_pid, owner_pid], key)
end

test "if a child shuts down, the deps of that child are cleaned up but not the whole allowance",
%{key: key} do
{owner_pid, _owner_monitor_ref} = spawn_monitor(fn -> Process.sleep(:infinity) end)
{child_pid1, child_monitor_ref1} = spawn_monitor(fn -> Process.sleep(:infinity) end)
{child_pid2, _child_monitor_ref2} = spawn_monitor(fn -> Process.sleep(:infinity) end)

init_key(owner_pid, key, %{counter: 1})

assert :ok = NimbleOwnership.allow(@server, owner_pid, child_pid1, key)
assert :ok = NimbleOwnership.allow(@server, child_pid1, child_pid2, key)

Process.exit(child_pid1, :kill)
assert_receive {:DOWN, ^child_monitor_ref1, _, _, _}

assert {:ok, ^owner_pid} =
NimbleOwnership.fetch_owner(@server, [child_pid1, owner_pid], key)
end
end

describe "set_owner_to_manual_cleanup/2" do
Expand Down

0 comments on commit 0d60735

Please sign in to comment.