@@ -48,12 +48,6 @@ defmodule Ecto.Adapters.SQL.Sandbox do
48
48
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)
49
49
end
50
50
51
- setup tags do
52
- pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Repo, shared: not tags[:async])
53
- on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
54
- :ok
55
- end
56
-
57
51
test "create post" do
58
52
# Use the repository as usual
59
53
assert %Post{} = Repo.insert!(%Post{})
@@ -417,18 +411,27 @@ defmodule Ecto.Adapters.SQL.Sandbox do
417
411
end
418
412
419
413
@ doc """
420
- Starts a process that owns the connection and returns its pid.
414
+ Starts a process that will check out and own a connection, then returns that process's pid.
421
415
422
- The owner process is not linked to the caller, it is your responsibility to
423
- ensure it will be stopped. In tests, this is done by terminating the pool
424
- in an `ExUnit.Callbacks.on_exit/2` callback:
416
+ The process is not linked to the caller, so it is your responsibility to ensure that it will be
417
+ stopped with `stop_owner/1`. In tests, this is done in an `ExUnit.Callbacks.on_exit/2` callback:
425
418
426
419
setup tags do
427
420
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(MyApp.Repo, shared: not tags[:async])
428
421
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
429
422
:ok
430
423
end
431
424
425
+ ## `start_owner!/2` vs `checkout/2`
426
+
427
+ `start_owner!/2` should be used in place of `checkout/2`.
428
+
429
+ `start_owner!/2` solves the problem of unlinked processes started in a test outliving the test process and causing ownership errors.
430
+ For example, `LiveView`'s `live(...)` test helper starts a process linked to the LiveView supervisor, not the test process.
431
+ These errors can be eliminated by having the owner of the connection be a separate process from the test process.
432
+
433
+ Outside of that scenario, `checkout/2` involves less overhead than this function and so can be preferable.
434
+
432
435
## Options
433
436
434
437
* `:shared` - if `true`, the pool runs in the shared mode. Defaults to `false`
0 commit comments