From 3865ff96907157c84067b96c7e30a44fed43c919 Mon Sep 17 00:00:00 2001 From: Benjamin Milde Date: Sun, 7 Sep 2025 08:10:07 +0200 Subject: [PATCH 1/2] Update docs --- lib/ecto/adapters/sql/sandbox.ex | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/ecto/adapters/sql/sandbox.ex b/lib/ecto/adapters/sql/sandbox.ex index b2527653..790401b0 100644 --- a/lib/ecto/adapters/sql/sandbox.ex +++ b/lib/ecto/adapters/sql/sandbox.ex @@ -54,6 +54,10 @@ defmodule Ecto.Adapters.SQL.Sandbox do end end + If you run into issues with processes accessing your db after tests + complete – e.g. ones stopped in `on_exit` callbacks – consider using + `start_owner!/1` over `checkout/1`. + ## Collaborating processes The example above is straight-forward because we have only @@ -102,6 +106,9 @@ defmodule Ecto.Adapters.SQL.Sandbox do the parent's connection (i.e. the test process' connection) to the task. + Besides calling `allow/3` allowance can also be provided to processes + via [Caller Tracking](`m:Task#module-ancestor-and-caller-tracking`). + Because allowances use an explicit mechanism, their advantage is that you can still run your tests in async mode. The downside is that you need to explicitly control and allow every single @@ -148,7 +155,7 @@ defmodule Ecto.Adapters.SQL.Sandbox do There are two mechanisms for explicit ownerships: - * Using allowances - requires explicit allowances via `allow/3`. + * Using allowances - requires explicit allowances. Tests may run concurrently. * Using shared mode - does not require explicit allowances. From 65b457c9b8d5aa368beb89ebba7c1c0440d72ae6 Mon Sep 17 00:00:00 2001 From: Benjamin Milde Date: Sun, 7 Sep 2025 08:37:28 +0200 Subject: [PATCH 2/2] Update example with start_owner! --- lib/ecto/adapters/sql/sandbox.ex | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/ecto/adapters/sql/sandbox.ex b/lib/ecto/adapters/sql/sandbox.ex index 790401b0..59caf4a6 100644 --- a/lib/ecto/adapters/sql/sandbox.ex +++ b/lib/ecto/adapters/sql/sandbox.ex @@ -45,7 +45,9 @@ defmodule Ecto.Adapters.SQL.Sandbox do setup do # Explicitly get a connection before each test - :ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo) + pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Repo) + on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) + :ok end test "create post" do @@ -54,10 +56,6 @@ defmodule Ecto.Adapters.SQL.Sandbox do end end - If you run into issues with processes accessing your db after tests - complete – e.g. ones stopped in `on_exit` callbacks – consider using - `start_owner!/1` over `checkout/1`. - ## Collaborating processes The example above is straight-forward because we have only