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

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

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement