shigoto_testing (shigoto v1.9.10)

View Source

Test helpers for Shigoto: run workers with no database, and assert on the jobs your code enqueued.

Running a worker (shigoto:perform_job/2,3)

shigoto:perform_job/2,3 runs a worker through the exact perform path production uses (the middleware chain and deps_results injection) and returns the raw worker result, with no database, no queue, no retry, and no resilience gating (rate limits, circuit breakers, concurrency caps are skipped). It is a pure, in-process unit-test runner — it must never be used on a production path.

ok = shigoto:perform_job(email_worker, #{~"to" => ~"a@b.com"}),
{ok, Total} = shigoto:perform_job(sum_worker, #{~"xs" => [1, 2, 3]}),
%% predecessors' results, exactly as a real dependent would see them:
{ok, _} = shigoto:perform_job(rollup_worker, #{}, #{deps_results => #{41 => #{~"n" => 7}}}).

Testing modes

Set a testing mode so insert/1,2 and insert_all/1,2 stop persisting. Both keys are required — see shigoto_config:testing_mode/0 for why:

application:set_env(shigoto, testing, manual),
application:set_env(shigoto, testing_confirm_no_persistence, true).
  • inline runs each inserted job synchronously and keeps no row. A job that returns {error, _} raises {shigoto_inline_job_failed, Worker, Reason} so failures surface loudly in the test.
  • manual captures inserted jobs in a process-local buffer without running or persisting them, for the assertions below.

manual capture is per-process: it sees enqueues made by the calling process, not by other processes it spawned. For cross-process expectations, run against a real test pool (the assertions fall back to querying the pool when not in manual mode) and clean the table between tests.

Assertions

my_code_that_enqueues(),
shigoto_testing:assert_enqueued(#{worker => email_worker}),
shigoto_testing:refute_enqueued(#{queue => ~"sms"}),
[_ | _] = shigoto_testing:all_enqueued().

Filters: worker, queue, args (containment), tags (containment), state. Call reset/0 in your test setup to clear the manual buffer.

Summary

Functions

All enqueued jobs (manual buffer, or the configured pool otherwise).

Enqueued jobs matching a filter map.

Assert at least one enqueued job matches the filter; raises otherwise.

Assert with options; #{pool => Pool} overrides the pool for the DB-backed path.

Run a worker with default job context. See the module doc.

Run a worker with extra job context in Opts: deps_results, attempt, max_attempts, queue, id, meta, tags, priority, timeout.

Assert NO enqueued job matches the filter; raises with the matches otherwise.

Clear the manual-mode capture buffer for the calling process.

Functions

all_enqueued()

-spec all_enqueued() -> [map()].

All enqueued jobs (manual buffer, or the configured pool otherwise).

all_enqueued(Filters)

-spec all_enqueued(map()) -> [map()].

Enqueued jobs matching a filter map.

assert_enqueued(Filters)

-spec assert_enqueued(map()) -> ok.

Assert at least one enqueued job matches the filter; raises otherwise.

assert_enqueued(Filters, Opts)

-spec assert_enqueued(map(), map()) -> ok.

Assert with options; #{pool => Pool} overrides the pool for the DB-backed path.

perform_job(Worker, Args)

-spec perform_job(module(), map()) -> ok | {ok, term()} | {error, term()} | {snooze, pos_integer()}.

Run a worker with default job context. See the module doc.

perform_job(Worker, Args, Opts)

-spec perform_job(module(), map(), map()) ->
                     ok | {ok, term()} | {error, term()} | {snooze, pos_integer()}.

Run a worker with extra job context in Opts: deps_results, attempt, max_attempts, queue, id, meta, tags, priority, timeout.

refute_enqueued(Filters)

-spec refute_enqueued(map()) -> ok.

Assert NO enqueued job matches the filter; raises with the matches otherwise.

reset()

-spec reset() -> ok.

Clear the manual-mode capture buffer for the calling process.