When starting to use DynamicSupervisor, the tests started to fail with the message:
09:09:55.169 [error] GenServer {:element_process_registry, "a4d69ba4-6f55-40fa-8c2e-eea9c5cecf27"} terminating
** (DBConnection.OwnershipError) cannot find ownership process for #PID<0.495.0>.
When using ownership, you must manage connections in one of the four ways:
- By explicitly checking out a connection
- By explicitly allowing a spawned process
- By running the pool in shared mode
- By using :caller option with allowed process
The first two options require every new process to explicitly check a connection out or be allowed by calling checkout or allow respectively.
The third option requires a {:shared, pid} mode to be set. If using shared mode in tests, make sure your tests are not async.
The fourth option requires [caller: pid] to be used when checking out a connection from the pool. The caller process should already be allowed on a connection.
If you are reading this error, it means you have not done one of the steps above or that the owner process has crashed.
See Ecto.Adapters.SQL.Sandbox docs for more information.
It seems that the problem is not complicated and known:
Ecto.Adapters.SQL.Sandbox — Ecto SQL v3.8.3 Ecto.Adapters.SQL.Sandbox — Ecto SQL v3.8.3
Problems with Ecto Sandbox · Issue #167 · antonmi/espec · GitHub
Tests are run using espec
The tests take into account recommendations from the documentation
spec_helper.rb
ESpec.configure fn(config) ->
config.before fn(_tags) ->
{:ok, _} = Application.ensure_all_started(:ex_machina)
end
config.finally fn(_shared) ->
:ok
end
end
ESpec.configure fn(config) ->
config.before fn (shared) ->
case Ecto.Adapters.SQL.Sandbox.checkout(Krake.Repo) do
:ok -> nil
{:already, :owner} -> nil
error -> IO.puts "Sandbox checkout error: #{ error }"
end
unless Map.has_key?(shared, :async) do
Ecto.Adapters.SQL.Sandbox.mode(Krake.Repo, {:shared, self()})
end
end
config.finally fn(_shared) ->
Ecto.Adapters.SQL.Sandbox.checkin(Krake.Repo, [])
end
end
example_test.exs:
use Krake.Web.ConnCase, async: true
Inside the test, the controller call, in the controller, the supervisor process is called::
DynamicSupervisor.start_child(__MODULE__, {ElementProcess, [element_id, ttl]})
Maybe there are some ideas?