Skip to content

Commit

Permalink
Check process ancestry tree for test mode override
Browse files Browse the repository at this point in the history
Cascade the `with_testing_mode` block to nested processes that make use
of `:$ancestry` in the process dictionary, i.e. tasks. Now enqueuing a
job within spawned processes like `Task.async` or `Task.async_stream`
will honor the testing mode specified in `with_testing_mode/2`.

Closes #1067
  • Loading branch information
sorentwo committed Apr 16, 2024
1 parent 98e4b39 commit 49ae042
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lib/oban/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,24 @@ defmodule Oban.Config do

@doc false
@spec get_engine(t()) :: module()
def get_engine(%__MODULE__{engine: engine, testing: :disabled}), do: engine

def get_engine(%__MODULE__{engine: engine, testing: testing}) do
if Process.get(:oban_testing, testing) == :inline do
pids = [self() | Process.get(:"$ancestors", [])]

if Enum.any?(pids, &inline_testing?(&1, testing)) do
Oban.Engines.Inline
else
engine
end
end

defp inline_testing?(pid, default) do
{:dictionary, dictionary} = Process.info(pid, :dictionary)

Keyword.get(dictionary, :oban_testing, default) == :inline
end

@doc false
@spec node_name(%{optional(binary()) => binary()}) :: binary()
def node_name(env \\ System.get_env()) do
Expand Down
18 changes: 17 additions & 1 deletion test/oban/testing_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,23 @@ defmodule Oban.TestingTest do

assert_enqueued worker: Worker

refute_received {:ok, 1}
refute_received {:ok, 1}, 25
end)
end

test "the temporary mode cascades down to child processes" do
name = start_supervised_oban!(testing: :manual)

Testing.with_testing_mode(:inline, fn ->
fun = fn ->
Oban.insert!(name, Worker.new(%{ref: 1, action: "OK"}))

assert_received {:ok, 1}
end

fun
|> Task.async()
|> Task.await()
end)
end

Expand Down

0 comments on commit 49ae042

Please sign in to comment.