freewebwithme

freewebwithme

** (DBConnection.Error) owner #PID<> exited while testing async function

I need some help to understand writing test for async function

For example I have a function

def send_notification({:ok, nessage}, to, title, body) do
    Task.Supervisor.async_nolink(MyApp.TaskSupervisor, fn -> 
        send(to, title, body)
    end)
end

def create(to, title, body) do
  to
  |> create_message(title, body)
  |> send(to, title, body)
  |> log_message()
end

If I run test for create function, I got an error

13:19:41.877 [error] Postgrex.Protocol (#PID<0.1313.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.4033.0> exited

Client #PID<0.4035.0> is still using a connection from owner at location:

    :prim_inet.recv0/3
    (postgrex 0.16.5) lib/postgrex/protocol.ex:3171: Postgrex.Protocol.msg_recv/4
    (postgrex 0.16.5) lib/postgrex/protocol.ex:2871: Postgrex.Protocol.recv_transaction/4
    (postgrex 0.16.5) lib/postgrex/protocol.ex:2180: Postgrex.Protocol.rebind_execute/4
    (ecto_sql 3.9.1) lib/ecto/adapters/sql/sandbox.ex:375: Ecto.Adapters.SQL.Sandbox.Connection.proxy/3
    (db_connection 2.4.3) lib/db_connection/holder.ex:354: DBConnection.Holder.holder_apply/4
    (db_connection 2.4.3) lib/db_connection.ex:1413: DBConnection.run_execute/5
    (db_connection 2.4.3) lib/db_connection.ex:1508: DBConnection.run/6
    (db_connection 2.4.3) lib/db_connection.ex:701: DBConnection.execute/4
    (ecto_sql 3.9.1) lib/ecto/adapters/postgres/connection.ex:102: Ecto.Adapters.Postgres.Connection.execute/4
    (ecto_sql 3.9.1) lib/ecto/adapters/sql.ex:858: Ecto.Adapters.SQL.execute!/5
    (ecto_sql 3.9.1) lib/ecto/adapters/sql.ex:828: Ecto.Adapters.SQL.execute/6
    (ecto 3.9.4) lib/ecto/repo/queryable.ex:229: Ecto.Repo.Queryable.execute/4
    (ecto 3.9.4) lib/ecto/repo/queryable.ex:19: Ecto.Repo.Queryable.all/3
    (ecto 3.9.4) lib/ecto/repo/preloader.ex:272: Ecto.Repo.Preloader.fetch_query/8
    (elixir 1.14.2) lib/enum.ex:1658: Enum."-map/2-lists^map/1-0-"/2
    (ecto 3.9.4) lib/ecto/repo/preloader.ex:72: Ecto.Repo.Preloader.preload_each/4
    (ecto 3.9.4) lib/ecto/repo/preloader.ex:51: Ecto.Repo.Preloader.normalize_and_preload_each/5
    (ecto 3.9.4) lib/ecto/repo/preloader.ex:46: Ecto.Repo.Preloader.preload/4

And I read the doc But I can’t figure out how to write test for this async function

And sandbox is :manual mode

Ecto.Adapters.SQL.Sandbox.mode(MyAppRepo, :manual)

Most Liked

josevalim

josevalim

Creator of Elixir

Nowadays we have proper abstractions in place so this doesn’t happen as often.

First, for managing connections, make sure you are using the start_owner API: todo_trek/test/support/data_case.ex at main · chrismccord/todo_trek · GitHub

When you are starting processes under test, use start_supervisor! from ExUnit, which guarantees it will be shut down before the test process.

Finally, if you are starting tasks or processes under a supervisor dynamically, add an on_exit callback that gets all children of said supervisor and wait for them to terminate. Something like:

on_exit(fn ->
  for {_, pid, _, _} <- DynamicSupervisor.which_children(YourSupName) do
    ref = Process.monitor(pid)
    assert_receive {:DOWN, ^ref, _, _, _}, :infinity
  end
end)
Joep

Joep

When searching for start_supervisor! in the docs I get no results: Search — ExUnit v1.16.0!

Sorry, we couldn’t find anything for start_supervisor!.

I suppose you mean start_supervised!/2 ?

dimitarvp

dimitarvp

Yep, that’s the one.

Where Next?

Popular in Questions 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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42842 311
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
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30840 112
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement