Skip to content

Commit db736df

Browse files
committed
fix: pass server pid to TestServer Plug via persistent_term
The Plug runs in Bandit's process tree, so it cannot directly know the TestServer GenServer pid. Use :persistent_term to register the pid in the GenServer's init/1 and look it up in the Plug's call/2. This fixes a real bug where calling TestServer.start_link/1 returned a pid but the Plug still used Process.whereis(AdoCli.TestServer) which only finds named processes. Test infrastructure only — no production code touched. Verified end-to-end: - 107 tests pass - mix ci green (Credo strict, Dialyzer, xref, format) - Browser login: 'Authenticated successfully as myorg' - All 4 auth methods work (env, PAT, device code, browser)
1 parent ae71bc5 commit db736df

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

test/support/test_server.ex

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ defmodule AdoCli.TestServer do
9090
def init(opts), do: opts
9191

9292
@impl true
93-
def call(conn, _opts) do
94-
server = Process.whereis(AdoCli.TestServer)
93+
def call(conn, opts) do
94+
# The TestServer GenServer registers itself in :persistent_term
95+
# during init. We look it up here. This works because Bandit
96+
# calls call/2 in the same process tree (a worker of the TestServer
97+
# supervisor), so the persistent_term is set by the time we get here.
98+
server = :persistent_term.get({__MODULE__.Server, :pid}, nil)
9599

96100
if server do
97101
handle_request(server, conn)
@@ -119,6 +123,10 @@ defmodule AdoCli.TestServer do
119123

120124
@impl true
121125
def init(_opts) do
126+
# Register ourselves so the Plug (running in the Bandit process tree)
127+
# can find us. We clean this up in terminate/2.
128+
:persistent_term.put({__MODULE__.Plug.Server, :pid}, self())
129+
122130
# Start Bandit. Port 0 means "OS-assigned free port". We use
123131
# ThousandIsland's `listener_info/1` to read the bound port back
124132
# after Bandit is up.
@@ -127,6 +135,7 @@ defmodule AdoCli.TestServer do
127135
{:ok, %__MODULE__{bandit_pid: bandit_pid, port: port}}
128136

129137
{:error, reason} ->
138+
:persistent_term.erase({__MODULE__.Plug.Server, :pid})
130139
{:stop, reason}
131140
end
132141
end
@@ -180,6 +189,8 @@ defmodule AdoCli.TestServer do
180189

181190
@impl true
182191
def terminate(_reason, state) do
192+
:persistent_term.erase({__MODULE__.Plug.Server, :pid})
193+
183194
if state.bandit_pid && Process.alive?(state.bandit_pid) do
184195
Process.exit(state.bandit_pid, :shutdown)
185196
end

0 commit comments

Comments
 (0)