prop/test/microwaveprop/application_test.exs
Graham McIntire 15ce50365f
perf(concurrency): partition Task.Supervisor to remove bottleneck on heavy async_stream paths
Wrap a Task.Supervisor in a PartitionSupervisor (one partition per
scheduler) and route the four sustained async_stream callers through
`{:via, PartitionSupervisor, {Microwaveprop.TaskSupervisor, self()}}`.
Concurrent work — the hourly PropagationGridWorker pulling f00-f18
HRRR range downloads, the GEFS worker scoring its grid, and the
Recalibrator's 20-way positive/negative fan-out — no longer funnels
through a single supervisor PID.

Light/one-shot Task.async + Task.start sites (hrrr_client surface/
pressure fan-out, application boot warmup, weather fire-and-forget)
are left alone; partitioning only helps under sustained concurrency.
2026-04-21 14:20:29 -05:00

83 lines
2.9 KiB
Elixir

defmodule Microwaveprop.ApplicationTest do
use ExUnit.Case, async: false
alias Microwaveprop.Application, as: App
describe "Microwaveprop.TaskSupervisor partition supervisor" do
test "is started and exposes schedulers_online partitions" do
# A partitioned Task.Supervisor removes contention on a single
# supervisor PID when many callers run Task.async_stream in parallel
# (recalibrator, hrrr_client range downloads, gefs worker, etc.).
%{active: active, specs: specs} = PartitionSupervisor.count_children(Microwaveprop.TaskSupervisor)
expected = System.schedulers_online()
assert active == expected
assert specs == expected
end
test "routes async_stream work through a partition keyed on the caller" do
results =
{:via, PartitionSupervisor, {Microwaveprop.TaskSupervisor, self()}}
|> Task.Supervisor.async_stream(1..5, fn n -> n * 2 end, max_concurrency: 2)
|> Enum.map(fn {:ok, v} -> v end)
assert results == [2, 4, 6, 8, 10]
end
end
describe "build_timestamp/0" do
setup do
original_env = System.get_env("DEPLOY_TIMESTAMP")
original_file = Application.get_env(:microwaveprop, :build_timestamp_file)
tmp = Path.join(System.tmp_dir!(), "build_timestamp_#{System.unique_integer([:positive])}")
on_exit(fn ->
if original_env do
System.put_env("DEPLOY_TIMESTAMP", original_env)
else
System.delete_env("DEPLOY_TIMESTAMP")
end
if original_file do
Application.put_env(:microwaveprop, :build_timestamp_file, original_file)
else
Application.delete_env(:microwaveprop, :build_timestamp_file)
end
File.rm(tmp)
end)
System.delete_env("DEPLOY_TIMESTAMP")
Application.put_env(:microwaveprop, :build_timestamp_file, tmp)
{:ok, tmp: tmp}
end
test "returns a DateTime when no sources are present" do
assert %DateTime{} = App.build_timestamp()
end
test "reads ISO8601 timestamp from the build-timestamp file", %{tmp: tmp} do
File.write!(tmp, "2026-04-15T12:30:00Z\n")
assert App.build_timestamp() == ~U[2026-04-15 12:30:00Z]
end
test "file takes precedence over DEPLOY_TIMESTAMP env", %{tmp: tmp} do
File.write!(tmp, "2026-04-15T12:30:00Z")
System.put_env("DEPLOY_TIMESTAMP", "2026-01-01T00:00:00Z")
assert App.build_timestamp() == ~U[2026-04-15 12:30:00Z]
end
test "falls back to DEPLOY_TIMESTAMP env when file is absent" do
System.put_env("DEPLOY_TIMESTAMP", "2026-02-02T02:02:02Z")
assert App.build_timestamp() == ~U[2026-02-02 02:02:02Z]
end
test "falls back to compile time when both are malformed", %{tmp: tmp} do
File.write!(tmp, "not-a-timestamp")
System.put_env("DEPLOY_TIMESTAMP", "also-garbage")
assert %DateTime{} = App.build_timestamp()
end
end
end