Skip to content

Commit

Permalink
Correctly fetch owners if metadata is falsey (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
martosaur authored Dec 3, 2024
1 parent 48ee441 commit 1b115f2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/nimble_ownership.ex
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,10 @@ defmodule NimbleOwnership do
state = revalidate_lazy_calls(state)

Enum.find_value(callers, {:reply, :error, state}, fn caller ->
cond do
owner_pid = state.allowances[caller][key] -> {:reply, {:ok, owner_pid}, state}
_meta = state.owners[caller][key] -> {:reply, {:ok, caller}, state}
true -> nil
case state do
%{allowances: %{^caller => %{^key => owner_pid}}} -> {:reply, {:ok, owner_pid}, state}
%{owners: %{^caller => %{^key => _meta}}} -> {:reply, {:ok, caller}, state}
_ -> nil
end
end)
end
Expand Down
11 changes: 11 additions & 0 deletions test/nimble_ownership_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ defmodule NimbleOwnershipTest do
assert owner_pid == self()
end

test "nil as metadata is ok", %{key: key} do
assert {:ok, nil} =
NimbleOwnership.get_and_update(@server, self(), key, fn arg ->
assert arg == nil
{nil, arg}
end)

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

test "updates the metadata with the returned value from the function", %{key: key} do
test_pid = self()
init_key(test_pid, key, %{counter: 1})
Expand Down

0 comments on commit 1b115f2

Please sign in to comment.