freewebwithme
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)
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
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
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 6 of 6 Posts
al2o3cr
In
manualmode, the sandbox works with Task behind the scenes to ensure that the task process getsallowed to share the connection with the caller.HOWEVER
That means that the task cannot outlive the test process, or you’ll get the error you’re seeing.
A quick way to check if this might be causing your issue would be to add
Process.sleep(5000)or similar to the end of the test that’s failing. That will ensure the call tosendcompletes before the test does.bolducp
@al2o3cr Do you have advice about the best way to handle this in a test? I’ve got some code that uses
Task.startto call some side effect functions that I don’t care about the results of. But my tests that uses the parent function are returning this error now, because the Task is trying to outlive the test.Is the best way to insert
Process.sleep/1statements? Or is there a better way?tim.jarratt
I’ve been fighting this exact same problem in some of the tests in a project at work. I’m not using the Task API, but I see the same problem when testing GenServers and with some async LiveViews.
Our current approach is to have our tests safely kill the spawned process, and to ensure it has stopped before the test ends. The reason for this is that the fundamental problem is that the spawned process is killed while it is still expecting to receive data from the Postgrex process – which rightly complains when the process on the other end disappears.
I’d love for this to be easier, but right now we need to spend a lot of time identifying which tests are failing, and then building custom ways to ensure our process is either stopped cleanly or isn’t in the middle of a query.
josevalim
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_exitcallback that gets all children of said supervisor and wait for them to terminate. Something like:Joep
When searching for
start_supervisor!in the docs I get no results: Search — ExUnit v1.20.2!I suppose you mean start_supervised!/2 ?
dimitarvp
Yep, that’s the one.