Skip to content

Commit

Permalink
Skip taking advisory locks in testing modes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sorentwo committed Feb 12, 2024
1 parent 63d778a commit 0c2ee37
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/oban/engines/basic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0c2ee37

Please sign in to comment.