diff --git a/lib/aprsme/device_cache.ex b/lib/aprsme/device_cache.ex index d58946f..c65b011 100644 --- a/lib/aprsme/device_cache.ex +++ b/lib/aprsme/device_cache.ex @@ -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 diff --git a/test/aprsme/device_cache_test.exs b/test/aprsme/device_cache_test.exs new file mode 100644 index 0000000..c05de12 --- /dev/null +++ b/test/aprsme/device_cache_test.exs @@ -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