We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Testing Oban Workers
Deankinyua
Background processing refers to the execution of tasks as separate, detached processes. Oban is a robust background job framework that is widely used in Elixir projects. Oban relies heavily on what we call workers to execute jobs. These workers are just modules that implement the Oban.Worker behaviour.
Because anything that can go wrong will, it is imperative that we test this workers. We can do this through Oban.Testing. To understand how we test workers, we first have to know which testing mode Oban operates on. We have two modes available to us:
- Inline - which means jobs will execute immediately and without touching the database.
- Manual - which means jobs will be inserted into the database and wait for you to execute them.
You may run into some problems when executing in inline mode because if jobs are executed immediately, it will not be possible to assert that they were enqueued.
describe "enqueue_job/1" do
test "enqueues a transcribing job if successful" do
# enqueus a job
enqueue_job(job_params)
assert_enqueued(
worker: TranscribingWorker,
args: %{
"audio_url" => "https://skeptic-bot-dev.fly.storage.tigris.dev/song.mp3",
"id" => @id
}
)
end
end
If we do this in inline mode, the job in transcribing worker will be executed immediately and hence we will not be able to assert that it was enqueued. To solve this, we have to switch to manual mode :
# config/test.exs
config :skeptic_bot, Oban, testing: :manual
# if inline mode do :
# config :skeptic_bot, Oban, testing: :inline
Now we will be able to assert that the job was enqueued. In case subsequent processes need job completion results to finish successfully, execute the job manually using the perform_job/2 function:
assert :ok =
perform_job(TranscribingWorker, %{
"audio_url" => "https://skeptic-bot-dev.fly.storage.tigris.dev/song.mp3",
"id" => @id
})
Copy link
copied to clipboard