prop/test/microwaveprop/workers/era5_fetch_worker_test.exs
Graham McIntire 6476bc8045 Dedupe ERA5 fetch jobs by (lat, lon, valid_time)
The Era5FetchWorker now declares an Oban unique constraint on
{lat, lon, valid_time} for any job in available/scheduled/executing/
retryable, so two backfill runs targeting the same contact grid point
can no longer spawn parallel CDS requests for the same hour.

Because Oban OSS insert_all doesn't honor unique, ERA5 jobs are now
routed through Oban.insert/1 from ContactWeatherEnqueueWorker and the
era5_backfill mix task. Other worker types still use insert_all.
2026-04-08 15:52:53 -05:00

59 lines
2.2 KiB
Elixir

defmodule Microwaveprop.Workers.Era5FetchWorkerTest do
use Microwaveprop.DataCase
alias Microwaveprop.Workers.Era5FetchWorker
describe "unique constraint" do
test "deduplicates identical (lat, lon, valid_time) jobs across inserts" do
args = %{"lat" => 32.75, "lon" => -97.0, "valid_time" => "2014-01-01T00:00:00Z"}
Oban.Testing.with_testing_mode(:manual, fn ->
{:ok, job1} = Oban.insert(Era5FetchWorker.new(args))
{:ok, job2} = Oban.insert(Era5FetchWorker.new(args))
# Second insert should hit the unique constraint and return the
# existing job (same id) instead of creating a new one.
assert job1.id == job2.id
count = Repo.aggregate(Oban.Job, :count, :id)
assert count == 1
end)
end
test "allows distinct hours on the same date" do
args_00 = %{"lat" => 32.75, "lon" => -97.0, "valid_time" => "2014-01-01T00:00:00Z"}
args_06 = %{"lat" => 32.75, "lon" => -97.0, "valid_time" => "2014-01-01T06:00:00Z"}
Oban.Testing.with_testing_mode(:manual, fn ->
{:ok, job1} = Oban.insert(Era5FetchWorker.new(args_00))
{:ok, job2} = Oban.insert(Era5FetchWorker.new(args_06))
assert job1.id != job2.id
assert Repo.aggregate(Oban.Job, :count, :id) == 2
end)
end
test "allows distinct grid points for the same valid_time" do
args_a = %{"lat" => 32.75, "lon" => -97.0, "valid_time" => "2014-01-01T00:00:00Z"}
args_b = %{"lat" => 33.00, "lon" => -97.0, "valid_time" => "2014-01-01T00:00:00Z"}
Oban.Testing.with_testing_mode(:manual, fn ->
{:ok, job1} = Oban.insert(Era5FetchWorker.new(args_a))
{:ok, job2} = Oban.insert(Era5FetchWorker.new(args_b))
assert job1.id != job2.id
assert Repo.aggregate(Oban.Job, :count, :id) == 2
end)
end
test "deduplicates across repeated Oban.insert calls" do
args = %{"lat" => 32.75, "lon" => -97.0, "valid_time" => "2014-01-01T00:00:00Z"}
Oban.Testing.with_testing_mode(:manual, fn ->
Enum.each(1..5, fn _ -> Oban.insert(Era5FetchWorker.new(args)) end)
assert Repo.aggregate(Oban.Job, :count, :id) == 1
end)
end
end
end