towerops/test/towerops/workers/job_cleanup_task_test.exs
Graham McIntire c50dc08ad4 test: fix slow + flaky tests (Mox global, env pollution, pool starvation)
- Remove Mox.set_mox_global() in DPW extra test; rely on $callers
  instead so concurrent vendor SNMP tests stop intermittently failing
  with VerificationError.
- Make ToweropsWeb.Plugs.GraphQLIntrospectionTest async: false. It
  mutates Application.put_env(:towerops, :env, :prod), which polluted
  every async vendor test that reads :env via
  Towerops.Snmp.Client.phoenix_snmp_disabled/0.
- Bump test pool queue_target/queue_interval. AgentChannelTest spawns
  many `:proc_lib` channel processes that hold sandbox connections;
  the default 50ms queue caused intermittent
  'could not checkout the connection' errors.
- Tag real-System.cmd("ping") tests as :network so they're excluded
  by default. Saves ~20s on every full run.
- Make JobCleanupTask's internal 1s settle sleep configurable via
  :job_cleanup_settle_ms; the test overrides it to 0.

Plus the pending CoverageLive.Form edit-save / EIRP recompute and
TraceLive deep-link tests from before the compact.
2026-05-08 14:43:30 -05:00

80 lines
2.5 KiB
Elixir

defmodule Towerops.Workers.JobCleanupTaskTest do
use Towerops.DataCase, async: false
import Towerops.AccountsFixtures
alias Towerops.Organizations
alias Towerops.Sites
alias Towerops.Workers.JobCleanupTask
setup do
previous_env = Application.get_env(:towerops, :env)
previous_settle = Application.get_env(:towerops, :job_cleanup_settle_ms)
# Make the internal "wait for cancellations to process" sleep a no-op
# in tests so the prod-path test runs in <1ms instead of >1s.
Application.put_env(:towerops, :job_cleanup_settle_ms, 0)
on_exit(fn ->
if previous_env do
Application.put_env(:towerops, :env, previous_env)
else
Application.delete_env(:towerops, :env)
end
if previous_settle do
Application.put_env(:towerops, :job_cleanup_settle_ms, previous_settle)
else
Application.delete_env(:towerops, :job_cleanup_settle_ms)
end
end)
:ok
end
describe "run/0" do
test "skips work in non-prod environment" do
Application.put_env(:towerops, :env, :test)
assert :ok == JobCleanupTask.run() || JobCleanupTask.run() == nil
end
test "skips work in :dev environment" do
Application.put_env(:towerops, :env, :dev)
assert JobCleanupTask.run() == :ok || JobCleanupTask.run() == nil
end
test "in :prod, cancels existing jobs and reschedules SNMP-enabled devices" do
user = user_fixture()
{:ok, org} = Organizations.create_organization(%{name: "JC Org"}, user.id)
{:ok, site} = Sites.create_site(%{name: "JC Site", organization_id: org.id})
{:ok, _device_snmp} =
Towerops.Devices.create_device(%{
name: "JC dev 1",
ip_address: "10.7.7.1",
site_id: site.id,
organization_id: org.id,
snmp_enabled: true
})
{:ok, _device_off} =
Towerops.Devices.create_device(%{
name: "JC dev 2",
ip_address: "10.7.7.2",
site_id: site.id,
organization_id: org.id,
snmp_enabled: false
})
Application.put_env(:towerops, :env, :prod)
# Speed up: shorten the internal sleep by spawning concurrently isn't possible,
# but the function returns once Sleep + reschedule complete. The sleep is 1s;
# acceptable in this single test.
assert JobCleanupTask.run() in [:ok, nil]
# We don't assert exact job counts (other tests may run concurrently), just
# confirm the function completed without raising and the prod path executed.
end
end
end