kura_sandbox (kura v2.31.3)

View Source

Test sandbox that wraps each test in a rolled-back transaction.

Supports three modes:

Single-process (default) - the test process owns the connection:

init_per_testcase(_TestCase, Config) ->
    kura_sandbox:checkout(my_repo),
    Config.

end_per_testcase(_TestCase, _Config) ->
    kura_sandbox:checkin(my_repo),
    ok.

Allow - let spawned processes share the sandbox connection:

kura_sandbox:checkout(my_repo),
kura_sandbox:allow(my_repo, self(), WorkerPid).

Under a shared checkout allow/3 is not needed, and an allowed process uses the shared connection. An allowed process whose owner has no checkout raises {kura_sandbox, owner_not_checked_out} on its first query instead of running outside the sandbox.

Shared - all processes use a single sandbox connection (simplest for tests that spawn many processes):

kura_sandbox:checkout(my_repo, #{shared => true}).

All database operations within the sandbox are rolled back on checkin. No test data persists.

A kura_repo_worker:transaction/2 inside the sandbox runs under a SAVEPOINT on the sandbox connection, as a nested transaction does outside it: a raise undoes only that transaction's work, and a statement that fails inside it leaves the sandbox usable. Processes that share one sandbox connection (allow/3 or shared) share its savepoints too, so their transactions must run one after another: a transaction opened while another process has one open on the same connection raises {kura_sandbox, overlapping_transactions}.

A statement that fails outside a transaction, such as an insert that breaks a unique index, aborts the sandbox's transaction on PostgreSQL: every later statement in the test fails with 25P02 (in_failed_sql_transaction) until checkin. Statements are not wrapped in a savepoint each, since that is two more round trips per statement and processes sharing the connection would interleave them. To keep going after an expected failure, run it in a transaction that is rolled back:

rolled_back(Fun) ->
    try
        kura_repo_worker:transaction(my_repo, fun() -> throw({rolled_back, Fun()}) end)
    catch
        throw:{rolled_back, Result} -> Result
    end.

{error, CS} = rolled_back(fun() -> kura_repo_worker:insert(my_repo, Dup) end).

A COMMIT, END or ROLLBACK run through a query ends the sandbox's transaction, and what it committed stays in the database. With a driver that checks for it (see kura_driver:query_on/4), the statement raises {DriverMod, transaction_ended_by_statement}. So does every statement on the connection that starts after that error, from any process sharing it, without being run. On SQLite, a statement another process started while the COMMIT ran can still run in autocommit and keep its work, or raise transaction_rolled_back. With a driver that does not check, the statements after it run in autocommit and their work is kept. On kura_postgres, a RESET ALL in the test also reads as the end of the transaction.

Summary

Functions

Allow ChildPid to use OwnerPid's sandbox connection.

Roll back the sandbox transaction and return the connection to the pool.

Check out a sandbox connection with default options.

Check out a sandbox connection.

Note that this process opens a transaction on sandbox connection Conn.

Look up a sandbox connection for the current process and pool.

Note that this process closed a transaction it opened on Conn.

Create the sandbox ETS table. Call once before tests run.

Functions

allow(RepoMod, OwnerPid, ChildPid)

-spec allow(module(), pid(), pid()) -> ok.

Allow ChildPid to use OwnerPid's sandbox connection.

Call it before ChildPid makes any database queries. When OwnerPid holds a shared checkout, ChildPid uses the shared connection. When OwnerPid has no checkout on the repo and no shared checkout exists, ChildPid's queries raise {kura_sandbox, owner_not_checked_out}.

checkin(RepoMod)

-spec checkin(module()) -> ok.

Roll back the sandbox transaction and return the connection to the pool.

checkout(RepoMod)

-spec checkout(module()) -> ok.

Check out a sandbox connection with default options.

checkout(RepoMod, Opts)

-spec checkout(module(), map()) -> ok.

Check out a sandbox connection.

Options:

  • shared - when true, all processes use this connection (default: false)

enter_transaction(Conn)

-spec enter_transaction(kura_pool:conn()) -> ok.

Note that this process opens a transaction on sandbox connection Conn.

Called by kura_db:transaction/2. A process may nest its own transactions. Raises {kura_sandbox, overlapping_transactions} when another live process has one open on Conn, since their savepoints would interleave.

get_conn(Pool)

-spec get_conn(atom()) -> {ok, term()} | not_found.

Look up a sandbox connection for the current process and pool.

Returns {ok, Conn} if a sandbox connection exists (owned, allowed, or shared), not_found otherwise. Raises {kura_sandbox, owner_not_checked_out} for an allowed process whose owner has no checkout while no shared checkout exists. Called from kura_repo_worker.

leave_transaction(Conn)

-spec leave_transaction(kura_pool:conn()) -> ok.

Note that this process closed a transaction it opened on Conn.

start()

-spec start() -> ok.

Create the sandbox ETS table. Call once before tests run.