** (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)

In manual mode, the sandbox works with Task behind the scenes to ensure that the task process gets allowed 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 to send completes before the test does.