fix: guard device cache refresh when stopped

This commit is contained in:
Graham McIntire 2026-03-22 17:31:43 -05:00
parent 59aee0416e
commit 35962c195b
No known key found for this signature in database
2 changed files with 33 additions and 1 deletions

View file

@ -46,7 +46,10 @@ defmodule Aprsme.DeviceCache do
Refreshes the device cache from the database.
"""
def refresh_cache do
GenServer.call(__MODULE__, :refresh_cache)
case Process.whereis(__MODULE__) do
nil -> :ok
_pid -> GenServer.call(__MODULE__, :refresh_cache)
end
end
# Server callbacks

View file

@ -0,0 +1,29 @@
defmodule Aprsme.DeviceCacheTest do
use ExUnit.Case, async: false
alias Aprsme.DeviceCache
setup do
pid = Process.whereis(DeviceCache)
if pid do
:erlang.unregister(DeviceCache)
end
on_exit(fn ->
if pid && !Process.whereis(DeviceCache) do
true = Process.register(pid, DeviceCache)
end
end)
:ok
end
test "refresh_cache/0 returns :ok when the cache server is not running" do
assert DeviceCache.refresh_cache() == :ok
end
test "lookup_device/1 returns nil on cache miss without crashing when server is not running" do
assert DeviceCache.lookup_device("TEST-DEVICE") == nil
end
end