Is it possible to have Oban running the jobs normally with Ecto.Sandbox?

Hello! I have some integration/acceptance? tests where I need the system to run pretty much normally and just assert that the final result is correct. The result, though, depends on some Oban tasks running normally (without calling Oban.drain_queue directly in a few different points.

I found the advice at the end of the article here suggesting that was possible in the past by starting Oban supervisors in the test setup, but unfortunately it seems the internals have changed so the snippets don’t work anymore. Here’s my last try:

  def start_oban_supervised!(opts) do
    default_opts = [
      name: make_ref(),
      repo: MyApp.Repo,
      plugins: [],
      poll_interval: :timer.seconds(1),
      shutdown_grace_period: 25

    opts = Keyword.merge(default_opts, opts)
    name = opts[:name]

    Callbacks.start_supervised!({Oban, opts})


  def setup_oban_supervised(_) do
    start_oban_supervised!(queues: [tasks: 10])

I’ve tried a few variations of the config poll_interval, plugins etc and none worked. It looks like Repeater was deprecated too.

I’ll keep trying stuff anyway and post if I find out how.

Hey @lurodrigo no it is fundamentally impossible to make the sandbox work with Oban normally. The issue is this: Sandbox transactions never commit. If they never commit, then the rows are never visible to other processes, and that means they are never visible to Oban.

You can write tests that use Oban normally, but you have to run an ecto repo that is configured normally, and essentially require separate tag (exluded by default) so that you can run them separately and handle all the various database resetting and so forth between tests yourself.

EDIT: I’m wrong, Oban is fancy! see: Is it possible to have Oban running the jobs normally with Ecto.Sandbox? - #4 by sorentwo

1 Like

Yeah, I’ve suspected that, thanks for the confirmation! I already have a separate mix env for integration tests so that should be doable.

1 Like

It’s possible to run Oban normally in acceptance tests using a setup similar to the one you showed above, provided you’re doing everything within the same transaction.

Oban Pro has a dedicated start_supervised_oban!/1 helper to facilitate acceptance testing within the sandbox.

start_supervised_oban!(repo: MyApp.Repo, stage_interval: 10, queues: [alpha: 10])

Note that stage_interval was added in Oban v2.14, and the undocumented poll_interval was removed.