prop/test/microwaveprop/commercial/poll_worker_test.exs
Graham McIntire 0a9058bfa0
test: broaden coverage across parsers, schemas, and pure helpers
- SwpcClient: unparseable time_tag rows drop silently, integer / string
  numerics both cast, already-decoded list bodies (Req auto-decode) are
  accepted, non-JSON-array bodies return {:error, :not_a_list}, X-ray
  rows whose energy isn't the long wavelength are filtered, and the
  misspelled-upstream electron_contaminaton key flags
  electron_contaminated.
- UwyoSoundingClient: every month abbreviation routes to its correct
  number, bogus month tokens fall through to an empty result, and
  short-of-fixed-width lines are skipped without raising.
- Grid.wgrib2_grid_spec/0: lon/lat_start anchor to the SW corner,
  steps match Grid.step/0, cell counts span CONUS inclusive, and the
  spec's cell product equals length(Grid.conus_points/0).
- PollWorker: empty-link list is a no-op, and poll_fn receives host /
  community / radio_type straight from the link row.
- Contact submission_changeset: antenna heights are bounded to
  [0, 1000] ft, whitespace-only mode normalises without firing an
  invalid-mode error, plus a pair of StreamData properties — every
  sanctioned band validates and every out-of-list band string is
  rejected.

Suite: 2,380 tests + 148 properties (was 2,370 + 146); coverage
70.38% → 71.08% total; credo strict clean.
2026-04-23 13:56:29 -05:00

100 lines
2.8 KiB
Elixir

defmodule Microwaveprop.Commercial.PollWorkerTest do
use Microwaveprop.DataCase, async: false
alias Microwaveprop.Commercial
alias Microwaveprop.Commercial.PollWorker
alias Microwaveprop.Commercial.Sample
@link_attrs %{
label: "test-poll-link",
host: "10.0.0.1",
community: "public",
radio_type: "af11x",
weather_station: "KTKI",
enabled: true
}
describe "poll_and_record/2" do
test "creates samples for enabled links when SNMP succeeds" do
{:ok, link} = Commercial.create_link(@link_attrs)
snmp_result = %{
rx_power_0: -55,
tx_power: 20,
link_state: 1,
link_uptime: 86_400
}
PollWorker.poll_and_record([link], fn _host, _community, _type -> {:ok, snmp_result} end)
samples = Repo.all(Sample)
assert length(samples) == 1
assert hd(samples).rx_power_0 == -55.0
assert hd(samples).link_id == link.id
end
test "skips links when SNMP fails" do
{:ok, link} = Commercial.create_link(@link_attrs)
PollWorker.poll_and_record(
[link],
fn _host, _community, _type -> {:error, :snmp_failed} end
)
assert Repo.all(Sample) == []
end
test "continues polling remaining links after one failure" do
{:ok, link1} = Commercial.create_link(@link_attrs)
{:ok, link2} = Commercial.create_link(%{@link_attrs | label: "test-poll-link-2", host: "10.0.0.2"})
call_count = :counters.new(1, [:atomics])
poll_fn = fn _host, _community, _type ->
n = :counters.get(call_count, 1)
:counters.add(call_count, 1, 1)
if n == 0 do
{:error, :snmp_failed}
else
{:ok, %{rx_power_0: -60, link_state: 1}}
end
end
PollWorker.poll_and_record([link1, link2], poll_fn)
assert length(Repo.all(Sample)) == 1
end
test "no-ops cleanly on an empty link list" do
# Defensive: if the DB has no enabled links, the worker should
# still return without calling the poll fn or touching samples.
called? = :counters.new(1, [:atomics])
poll_fn = fn _host, _community, _type ->
:counters.add(called?, 1, 1)
{:ok, %{}}
end
PollWorker.poll_and_record([], poll_fn)
assert :counters.get(called?, 1) == 0
assert Repo.all(Sample) == []
end
test "dispatches poll_fn with host, community, and radio_type from the link" do
{:ok, _link} = Commercial.create_link(@link_attrs)
test_pid = self()
poll_fn = fn host, community, radio_type ->
send(test_pid, {:poll_args, host, community, radio_type})
{:error, :stop}
end
PollWorker.poll_and_record(Commercial.enabled_links(), poll_fn)
assert_receive {:poll_args, "10.0.0.1", "public", "af11x"}, 500
end
end
end