fix: guard connection monitor stats lookup

This commit is contained in:
Graham McIntire 2026-03-22 17:08:48 -05:00
parent 37c5932f96
commit 1631352786
No known key found for this signature in database
2 changed files with 13 additions and 1 deletions

View file

@ -77,7 +77,7 @@ defmodule Aprsme.ConnectionMonitor do
Get current node statistics
"""
def get_stats do
if cluster_enabled?() do
if cluster_enabled?() and Process.whereis(__MODULE__) != nil do
GenServer.call(__MODULE__, :get_stats)
else
%{connections: 0, cpu: 0.0, memory: 0.0}

View file

@ -17,6 +17,18 @@ defmodule Aprsme.ConnectionMonitorTest do
end
end
describe "cluster enabled without a running monitor" do
test "get_stats/0 returns default stats instead of crashing" do
Application.put_env(:aprsme, :cluster_enabled, true)
on_exit(fn ->
Application.put_env(:aprsme, :cluster_enabled, false)
end)
assert ConnectionMonitor.get_stats() == %{connections: 0, cpu: 0.0, memory: 0.0}
end
end
describe "cluster enabled" do
setup do
Application.put_env(:aprsme, :cluster_enabled, true)