From 0c2ee37b6b2e033aad9536f3509a718ec27d36e6 Mon Sep 17 00:00:00 2001 From: Parker Selbert Date: Mon, 12 Feb 2024 15:34:29 -0600 Subject: [PATCH] Skip taking advisory locks in testing modes Advisory locks are global and apply across transactions. That can break async tests with overlapping unique jobs because the lock is held in a concurrent, sandboxed test. --- lib/oban/engines/basic.ex | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/oban/engines/basic.ex b/lib/oban/engines/basic.ex index 5d5a8dfe..db059c37 100644 --- a/lib/oban/engines/basic.ex +++ b/lib/oban/engines/basic.ex @@ -423,19 +423,18 @@ defmodule Oban.Engines.Basic do where(query, [j], field(j, ^timestamp) >= ^seconds_from_now(-period)) end - defp acquire_lock(conf, base_key) do + defp acquire_lock(%{testing: :disabled} = conf, base_key) do pref_key = :erlang.phash2(conf.prefix) lock_key = pref_key + base_key case Repo.query(conf, "SELECT pg_try_advisory_xact_lock($1)", [lock_key]) do - {:ok, %{rows: [[true]]}} -> - :ok - - _ -> - {:error, :locked} + {:ok, %{rows: [[true]]}} -> :ok + _ -> {:error, :locked} end end + defp acquire_lock(_conf, _key), do: :ok + defp fetch_job(conf, query, opts) do case Repo.one(conf, query, Keyword.put(opts, :prepare, :unnamed)) do nil -> nil