sorentwo / oban

💎 Robust job processing in Elixir, backed by modern PostgreSQL and SQLite3
https://getoban.pro
Apache License 2.0
3.17k stars 297 forks source link

with_testing_mode is ignored if enqueuing within another process #1067

Closed benschenker closed 2 months ago

benschenker commented 2 months ago

Environment

  {:oban, "~> 2.17.3"},
  {:oban_pro, "~> 1.3.4", repo: "oban"},
  {:oban_web, "~> 2.10.1", repo: "oban"},

Current Behavior

Given a test with the code:

Oban.Testing.with_testing_mode(:inline, fn ->
  list
    |> Task.async_stream(
      fn el -> do_thing_and_enqueue_job(el) end,
      max_concurrency: 5
    )
    |> Enum.map(fn {:ok, res} -> res end)
end)

and a config that sets the default testing to :manual then the test fails with a timeout because the job never runs (it runs in :manual mode, ignoring the with_testing_mode)

Expected Behavior

Enqueuing a job within Task.async_stream would honor the testing mode specified in with_testing_mode. This issue would also be relevant with browser testing where the test process is different from the process that enqueues the job.

Workaround

Oban.Testing.with_testing_mode(:inline, fn ->
  oban_testing_state = Process.get(:oban_testing)

  list
  |> Task.async_stream(
    fn el -> 
      Process.put(:oban_testing, oban_testing_state)
      do_thing_and_enqueue_job(el) 
     end,
    max_concurrency: 5
  )
  |> Enum.map(fn {:ok, res} -> res end)
end)

Similarly it also works correctly when the work is done in the same process:

Oban.Testing.with_testing_mode(:inline, fn ->
  list
  |> Enum.map(fn el -> do_thing_and_enqueue_job(el) end)
end)
sorentwo commented 2 months ago

@benschenker Great callout! This is handled now.