Skip to content

Commit 5b12420

Browse files
Support locally registered processes in allow/3 (#312)
1 parent a1f28f9 commit 5b12420

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

integration_test/sql/sandbox.exs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ defmodule Ecto.Integration.SandboxTest do
1919
Sandbox.mode(UnknownRepo, :manual)
2020
end
2121
end
22-
22+
2323
test "raises if repo is not started" do
2424
assert_raise RuntimeError, ~r"could not lookup Ecto repo #{inspect DynamicRepo} because it was not started", fn ->
2525
Sandbox.mode(DynamicRepo, :manual)
@@ -75,6 +75,27 @@ defmodule Ecto.Integration.SandboxTest do
7575
assert TestRepo.all(Post) == []
7676
end
7777

78+
test "uses the repository when allowed from another process by registered name" do
79+
assert_raise DBConnection.OwnershipError, ~r"cannot find ownership process", fn ->
80+
TestRepo.all(Post)
81+
end
82+
83+
parent = self()
84+
Process.register(parent, __MODULE__)
85+
86+
Task.start_link fn ->
87+
Sandbox.checkout(TestRepo)
88+
Sandbox.allow(TestRepo, self(), __MODULE__)
89+
send(parent, :allowed)
90+
Process.sleep(:infinity)
91+
end
92+
93+
assert_receive :allowed
94+
assert TestRepo.all(Post) == []
95+
96+
Process.unregister(__MODULE__)
97+
end
98+
7899
test "uses the repository when shared from another process" do
79100
assert_raise DBConnection.OwnershipError, ~r"cannot find ownership process", fn ->
80101
TestRepo.all(Post)
@@ -94,17 +115,17 @@ defmodule Ecto.Integration.SandboxTest do
94115
after
95116
Sandbox.mode(TestRepo, :manual)
96117
end
97-
118+
98119
test "works with a dynamic repo" do
99120
repo_pid = start_supervised!({DynamicRepo, name: nil})
100121
DynamicRepo.put_dynamic_repo(repo_pid)
101-
122+
102123
assert Sandbox.mode(DynamicRepo, :manual) == :ok
103-
124+
104125
assert_raise DBConnection.OwnershipError, ~r"cannot find ownership process", fn ->
105126
DynamicRepo.all(Post)
106127
end
107-
128+
108129
Sandbox.checkout(DynamicRepo)
109130
assert DynamicRepo.all(Post) == []
110131
end

lib/ecto/adapters/sql/sandbox.ex

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,10 +530,21 @@ defmodule Ecto.Adapters.SQL.Sandbox do
530530

531531
@doc """
532532
Allows the `allow` process to use the same connection as `parent`.
533+
534+
`allow` may be a PID or a locally registered name.
533535
"""
534536
def allow(repo, parent, allow, _opts \\ []) when is_atom(repo) or is_pid(repo) do
535-
%{pid: pool, opts: opts} = lookup_meta!(repo)
536-
DBConnection.Ownership.ownership_allow(pool, parent, allow, opts)
537+
case GenServer.whereis(allow) do
538+
pid when is_pid(pid) ->
539+
%{pid: pool, opts: opts} = lookup_meta!(repo)
540+
DBConnection.Ownership.ownership_allow(pool, parent, pid, opts)
541+
542+
other ->
543+
raise """
544+
only PID or a locally registered process can be allowed to \
545+
use the same connection as parent but the lookup returned #{inspect(other)}
546+
"""
547+
end
537548
end
538549

539550
@doc """

0 commit comments

Comments
 (0)