icecap
** (DBConnection.OwnershipError) cannot find ownership process for #PID<0.XXX.0>
In order to update a last_seen field when a User leaves their UserChannel, I am starting a GenServer, on join, that monitors the User’s UserChannel process, and makes the db call on reception of :DOWN signal.
I’m trying to test this.
test "joins succesfully", %{user_a: user} do
assert {:ok, _, _socket} =
UserSocket
|> socket("user_socket:#{user.id}", %{user_id: user.id})
|> subscribe_and_join(UserChannel, "user:#{user.id}")
end
Currently getting this error
[error] GenServer #PID<0.629.0> terminating
** (DBConnection.OwnershipError) cannot find ownership process for #PID<0.629.0>.
The module that tracks the user channel
defmodule ChataWeb.UserTracker.Process do
use GenServer, restart: :transient
alias Chata.Accounts
def start_link(%{user_id: user_id, user_channel_pid: user_channel_pid}, opts \\ []) do
init_args = %{user_id: user_id, user_channel_pid: user_channel_pid}
GenServer.start_link(__MODULE__, init_args, opts)
end
def init(init_args) do
Process.monitor(init_args.user_channel_pid)
{:ok, init_args}
end
def handle_info({:DOWN, _ref, :process, _pid, _}, state) do
%{user_id: user_id} = state
Accounts.update_last_seen(user_id)
{:stop, :normal, state}
end
end
I suppose this is happening because the test process completes before the db call does.
Any ideas how to get this to work?
Most Liked
josevalim
Two quick things to address this:
-
Make sure that you are starting the sandbox with the new start_owner! function. Here is how it is used in Phoenix: Use Ecto.Adapters.SQL.Sandbox.start_owner!/2 in generators by wojtekmach · Pull Request #3856 · phoenixframework/phoenix · GitHub
-
Use
start_supervised!to start your GenServer
This will make it so ExUnit first terminates your GenServer before it terminates the process owning the DB connection.
If you can’t control how the channel monitor process is started, you can alternatively start them under a supervisor and use a code like this to wait until they are done: Use Ecto.Adapters.SQL.Sandbox.start_owner!/2 in generators by wojtekmach · Pull Request #3856 · phoenixframework/phoenix · GitHub
Nicd
I haven’t tried it but feels like this doc page speaks about your issue: Ecto.Adapters.SQL.Sandbox — Ecto SQL v3.14.0
axelson
You might need to trap exits in the test processes to prevent the test process from dying when the channel goes down. From there you can assert that the channel has indeed died and then you can check in the DB for the update.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









