Skip to content

Commit 5f144a9

Browse files
authored
Add specs to Ecto.Adapters.SQL.Sandbox (#598)
1 parent 95e2fff commit 5f144a9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lib/ecto/adapters/sql/sandbox.ex

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ defmodule Ecto.Adapters.SQL.Sandbox do
436436
The remaining options are passed to `checkout/2`.
437437
"""
438438
@doc since: "3.4.4"
439+
@spec start_owner!(Ecto.Repo.t() | pid(), keyword()) :: pid()
439440
def start_owner!(repo, opts \\ []) do
440441
parent = self()
441442

@@ -489,7 +490,15 @@ defmodule Ecto.Adapters.SQL.Sandbox do
489490
connections are checked in. Therefore, it is recommend to set those
490491
modes before your test suite starts, as otherwise you will check in
491492
connections being used in any other test running concurrently.
493+
494+
If successful, returns `:ok` (this is always successful for `:auto`
495+
and `:manual` modes). It may return `:not_owner` or `:not_found`
496+
when setting `{:shared, pid}` and the given `pid` does not own any
497+
connection for the repo. May return `:already_shared` if another
498+
process set the ownership mode to `{:shared, _}` and is still alive.
492499
"""
500+
@spec mode(Ecto.Repo.t() | pid(), :auto | :manual | {:shared, pid()}) ::
501+
:ok | :already_shared | :now_owner | :not_found
493502
def mode(repo, mode)
494503
when (is_atom(repo) or is_pid(repo)) and mode in [:auto, :manual]
495504
when (is_atom(repo) or is_pid(repo)) and elem(mode, 0) == :shared and is_pid(elem(mode, 1)) do
@@ -504,6 +513,9 @@ defmodule Ecto.Adapters.SQL.Sandbox do
504513
until it calls `checkin/2` or until it crashes in which case
505514
the connection will be automatically reclaimed by the pool.
506515
516+
If successful, returns `:ok`. If the caller already has a
517+
connection, it returns `{:already, :owner | :allowed}`.
518+
507519
## Options
508520
509521
* `:sandbox` - when true the connection is wrapped in
@@ -519,6 +531,7 @@ defmodule Ecto.Adapters.SQL.Sandbox do
519531
be bumped whenever necessary.
520532
521533
"""
534+
@spec checkout(Ecto.Repo.t() | pid(), keyword()) :: :ok | {:already, :owner | :allowed}
522535
def checkout(repo, opts \\ []) when is_atom(repo) or is_pid(repo) do
523536
%{pid: pool, opts: pool_opts} = lookup_meta!(repo)
524537

@@ -564,6 +577,7 @@ defmodule Ecto.Adapters.SQL.Sandbox do
564577
@doc """
565578
Checks in the connection back into the sandbox pool.
566579
"""
580+
@spec checkin(Ecto.Repo.t() | pid()) :: :ok | :not_owner | :not_found
567581
def checkin(repo, _opts \\ []) when is_atom(repo) or is_pid(repo) do
568582
%{pid: pool, opts: opts} = lookup_meta!(repo)
569583
DBConnection.Ownership.ownership_checkin(pool, opts)
@@ -573,7 +587,13 @@ defmodule Ecto.Adapters.SQL.Sandbox do
573587
Allows the `allow` process to use the same connection as `parent`.
574588
575589
`allow` may be a PID or a locally registered name.
590+
591+
If the allowance is successful, this function returns `:ok`. If `allow` is already an
592+
owner or already allowed, it returns `{:already, :owner | :allowed}`. If `parent` has not
593+
checked out a connection from the repo, it returns `:not_found`.
576594
"""
595+
@spec allow(Ecto.Repo.t() | pid(), pid(), term()) ::
596+
:ok | {:already, :owner | :allowed} | :not_found
577597
def allow(repo, parent, allow, _opts \\ []) when is_atom(repo) or is_pid(repo) do
578598
case GenServer.whereis(allow) do
579599
pid when is_pid(pid) ->
@@ -591,6 +611,7 @@ defmodule Ecto.Adapters.SQL.Sandbox do
591611
@doc """
592612
Runs a function outside of the sandbox.
593613
"""
614+
@spec unboxed_run(Ecto.Repo.t() | pid(), (-> result)) :: result when result: var
594615
def unboxed_run(repo, fun) when is_atom(repo) or is_pid(repo) do
595616
checkin(repo)
596617
checkout(repo, sandbox: false)

0 commit comments

Comments
 (0)