icecap

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

josevalim

Creator of Elixir

Two quick things to address this:

  1. 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

  2. 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

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

axelson

Scenic Core Team

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.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement