From 1b115f22ae65a0c3992c82de9edb255c586e74ad Mon Sep 17 00:00:00 2001 From: Alex Martsinovich Date: Tue, 3 Dec 2024 02:29:08 -0800 Subject: [PATCH] Correctly fetch owners if metadata is falsey (#9) --- lib/nimble_ownership.ex | 8 ++++---- test/nimble_ownership_test.exs | 11 +++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/nimble_ownership.ex b/lib/nimble_ownership.ex index a263ef5..b41bc10 100644 --- a/lib/nimble_ownership.ex +++ b/lib/nimble_ownership.ex @@ -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 diff --git a/test/nimble_ownership_test.exs b/test/nimble_ownership_test.exs index 8e8b66e..6303286 100644 --- a/test/nimble_ownership_test.exs +++ b/test/nimble_ownership_test.exs @@ -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})