Add LeaderElection tests for dead-pid cleanup and registered leader pid status

This commit is contained in:
Graham McIntire 2026-05-08 17:15:26 -05:00
parent eeec857d01
commit 4560dc0c6f
No known key found for this signature in database
GPG key ID: F4ABF488E6029E59

View file

@ -353,6 +353,45 @@ defmodule Aprsme.Cluster.LeaderElectionTest do
assert Map.has_key?(status.cluster_info, :connected_nodes)
end
end
test "attempt_election triggers cleanup_stale_registrations for dead-local-process" do
# Spawn a process that exits immediately, then register it globally so
# cleanup_stale_registrations sees a dead pid and cleans it up.
Application.put_env(:aprsme, :cluster_enabled, false)
:global.unregister_name(@election_key)
dead_pid = spawn(fn -> :ok end)
Process.sleep(20)
refute Process.alive?(dead_pid)
try do
:global.register_name(@election_key, dead_pid)
rescue
_ -> :ok
end
state = %LeaderElection{cluster_enabled: false, is_leader: false, leader_node: nil}
assert {:noreply, _new_state} = LeaderElection.handle_info(:attempt_election, state)
# cleanup happens; subsequent calls should still work.
:global.unregister_name(@election_key)
end
test "clustered get_cluster_aprs_status with registered leader pid hits get_leader_node_name pid branch" do
Application.put_env(:aprsme, :cluster_enabled, true)
# Pre-register a real pid under the @election_key so the get_leader_node_name
# branch (line 382: pid when is_pid(pid) -> pid |> node() |> to_string()).
:global.unregister_name(@election_key)
:global.register_name(@election_key, self())
try do
status = LeaderElection.get_cluster_aprs_status()
assert is_map(status)
after
:global.unregister_name(@election_key)
end
end
end
describe "handle_info(:check_cluster_and_elect) direct callback" do