I’m using 2 repo in my project.
For testing some logic that uses both repos, I tried to use sandbox mode with both repos.
defmodule MyApp.DataCase do
...
setup tags do
repos = tags[:repos] || [tags[:repo]] || [MyApp.Repo1]
pids =
repos
|> Enum.map(fn repo ->
Ecto.Adapters.SQL.Sandbox.start_owner!(repo, shared: not tags[:async])
end)
on_exit(fn ->
pids
|> Enum.map(fn pid ->
Ecto.Adapters.SQL.Sandbox.stop_owner(pid)
end)
end)
:ok
end
...
end
But when I set repos
option [MyApp.Repo1, MyApp.Repo2]
, an error occurs.
defmodule MyApp.SampleTest do
use MyApp.DataCase, async: true
@moduletag repos: [MyApp.Repo1, MyApp.Repo2]
...
end
23:49:07.982 mfa=DBConnection.Connection.disconnect/2 [error] Postgrex.Protocol (#PID<0.1735.0>) disconnected: ** (DBConnection.ConnectionError) client #PID<0.1845.0> timed out because it queued and checked out the connection for longer than 30000ms
Client #PID<0.1845.0> is still using a connection from owner at location:
:prim_inet.recv0/3
(postgrex 0.16.5) lib/postgrex/protocol.ex:3171: Postgrex.Protocol.msg_recv/4
(postgrex 0.16.5) lib/postgrex/protocol.ex:2201: Postgrex.Protocol.recv_bind/3
(postgrex 0.16.5) lib/postgrex/protocol.ex:2126: Postgrex.Protocol.bind_execute/4
(ecto_sql 3.9.2) lib/ecto/adapters/sql/sandbox.ex:375: Ecto.Adapters.SQL.Sandbox.Connection.proxy/3
(db_connection 2.4.3) lib/db_connection/holder.ex:354: DBConnection.Holder.holder_apply/4
(db_connection 2.4.3) lib/db_connection.ex:1413: DBConnection.run_execute/5
(db_connection 2.4.3) lib/db_connection.ex:1508: DBConnection.run/6
(db_connection 2.4.3) lib/db_connection.ex:644: DBConnection.parsed_prepare_execute/5
(db_connection 2.4.3) lib/db_connection.ex:636: DBConnection.prepare_execute/4
(postgrex 0.16.5) lib/postgrex.ex:340: Postgrex.query/4
(ecto_sql 3.9.2) lib/ecto/adapters/sql.ex:955: Ecto.Adapters.SQL.struct/10
(ecto 3.9.5) lib/ecto/repo/schema.ex:756: Ecto.Repo.Schema.apply/4
(ecto 3.9.5) lib/ecto/repo/schema.ex:369: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
(ecto 3.9.5) lib/ecto/repo/schema.ex:265: Ecto.Repo.Schema.insert!/4
test/my_app/sample_test.exs:14: MyApp.SampleTest.__ex_unit_setup_0_0/1
MyApp.SampleTest.__ex_unit_describe_0/1
(ex_unit 1.14.2) lib/ex_unit/runner.ex:505: ExUnit.Runner.exec_test_setup/2
(ex_unit 1.14.2) lib/ex_unit/runner.ex:464: anonymous fn/2 in ExUnit.Runner.spawn_test_monitor/4
(stdlib 4.1.1) timer.erl:235: :timer.tc/1
The connection itself was checked out by #PID<0.1845.0> at location:
(postgrex 0.16.5) lib/postgrex.ex:340: Postgrex.query/4
(ecto_sql 3.9.2) lib/ecto/adapters/sql.ex:955: Ecto.Adapters.SQL.struct/10
(ecto 3.9.5) lib/ecto/repo/schema.ex:756: Ecto.Repo.Schema.apply/4
(ecto 3.9.5) lib/ecto/repo/schema.ex:369: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
(ecto 3.9.5) lib/ecto/repo/schema.ex:265: Ecto.Repo.Schema.insert!/4
test/my_app/sample_test.exs:14: MyApp.SampleTest.__ex_unit_setup_0_0/1
MyApp.SampleTest.__ex_unit_describe_0/1
(ex_unit 1.14.2) lib/ex_unit/runner.ex:505: ExUnit.Runner.exec_test_setup/2
(ex_unit 1.14.2) lib/ex_unit/runner.ex:464: anonymous fn/2 in ExUnit.Runner.spawn_test_monitor/4
(stdlib 4.1.1) timer.erl:235: :timer.tc/1
(ex_unit 1.14.2) lib/ex_unit/runner.ex:463: anonymous fn/4 in ExUnit.Runner.spawn_test_monitor/4
...
1) test sample (MyApp.SampleTest)
...
** (DBConnection.ConnectionError) tcp send: closed (the connection was closed by the pool, possibly due to a timeout or because the pool has been terminated)
stacktrace:
(ecto_sql 3.9.2) lib/ecto/adapters/sql.ex:913: Ecto.Adapters.SQL.raise_sql_call_error/1
(ecto 3.9.5) lib/ecto/repo/schema.ex:756: Ecto.Repo.Schema.apply/4
(ecto 3.9.5) lib/ecto/repo/schema.ex:369: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
(ecto 3.9.5) lib/ecto/repo/schema.ex:265: Ecto.Repo.Schema.insert!/4
test/my_app/sample_test.exs:14: MyApp.SampleTest.__ex_unit_setup_0_0/1
MyApp.SampleTest.__ex_unit_describe_0/1
Is it possible to use sandbox mode with multiple repos?