I basically have a similar setup like the one described in the sandbox docs for a shared mode connection, which looks like this:
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)
Ecto.Adapters.SQL.Sandbox.mode(Repo, {:shared, self()})
end
test "create two posts, one sync, another async" do
task = Task.async(fn ->
Repo.insert!(%Post{title: "async"})
end)
assert %Post{} = Repo.insert!(%Post{title: "sync"})
assert %Post{} = Task.await(task)
end
Which runs fine.
Now when I change this example slightly and wrap both inserts in a transaction:
test "create two posts, one sync, another async" do
Repo.transaction(fn ->
task = Task.async(fn ->
Repo.insert!(%Post{title: "async"})
end)
assert %Post{} = Repo.insert!(%Post{title: "sync"})
assert %Post{} = Task.await(task)
end)
end
This leads to a timeout:
** (EXIT from #PID<0.478.0>) exited in: GenServer.call(#PID<0.479.0>, {:checkout, #Reference<0.0.3.2012>, true, 15000}, 5000)
** (EXIT) time out
.10:04:12.191 [error] Postgrex.Protocol (#PID<0.383.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.478.0> exited while client #PID<0.478.0> is still running with: exited in: GenServer.call(#PID<0.479.0>, {:checkout, #Reference<0.0.3.2012>, true, 15000}, 5000)
** (EXIT) time out
I am wondering if this is because I can’t use transactions with the shared mode or if I am missing something else. I am using PostgreSQL 9.6.2, Ecto 2.1.4 and Elixir 1.4.2