Can't checkout connection from Ecto Sandbox after restarting application in tests


I’m trying to figure out the best way to test my Elixir application using Ecto + a supervision tree. During the test the supervision tree dynamically starts processes which causes test pollution. The tests pass when run individually.

My initial thought was that I should restart the application, but that causes the following problem when trying to checkout a new connection from the Ecto sandbox.

** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 10999ms. This means requests are coming in and your connection pool cannot serve them fast enough. You can address this by:
       1. Ensuring your database is available and that you can connect to it
       2. Tracking down slow queries and making sure they are running fast enough
       3. Increasing the pool_size (albeit it increases resource consumption)
       4. Allowing requests to wait longer by increasing :queue_target and :queue_interval
     See DBConnection.start_link/2 for more information

I get the following error in my logs

17:44:49.279 [error] Exqlite.Connection (#PID<0.950.0>) failed to connect: ** (Exqlite.Error) got :database_open_failed while retrieving Exception.message/1 for %Exqlite.Error{message: :database_open_failed, statement: nil} (expected a string)

This is my custom data case template

defmodule Tai.TestSupport.DataCase do
  @moduledoc """
  This module defines the setup for tests requiring
  access to the application's data layer.

  You may define functions here to be used as helpers in
  your tests.

  Finally, if the test case interacts with the database,
  we enable the SQL sandbox, so changes done to the database
  are reverted at the end of every test. If you are using
  PostgreSQL, you can even run database tests asynchronously
  by setting `use Tai.DataCase, async: true`, although
  this option is not recommended for other databases.

  use ExUnit.CaseTemplate

  using do
    quote do
      alias Tai.NewOrders.OrderRepo
      alias Tai.TestSupport.Mocks

      import Ecto
      import Ecto.Changeset
      import Ecto.Query
      import Tai.TestSupport.DataCase
      import Tai.TestSupport.Mock
      import Tai.TestSupport.Factories.NewOrderSubmissionFactory
      import Tai.TestSupport.Factories.NewOrderFactory
      import Tai.TestSupport.Factories.OrderTransitionFactory
      import Tai.TestSupport.Factories.FailedOrderTransitionFactory

  setup tags do
    {:ok, _} = Application.ensure_all_started(:tai)

    :ok = Ecto.Adapters.SQL.Sandbox.checkout(Tai.NewOrders.OrderRepo)

    unless tags[:async] do
      Ecto.Adapters.SQL.Sandbox.mode(Tai.NewOrders.OrderRepo, {:shared, self()})



  @doc """
  A helper that transforms changeset errors into a map of messages.

      assert {:error, changeset} = Accounts.create_user(%{password: "short"})
      assert "password is too short" in errors_on(changeset).password
      assert %{password: ["password is too short"]} = errors_on(changeset)

  def errors_on(changeset) do
    Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
      Regex.replace(~r"%{(\w+)}", message, fn _, key ->
        opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
1 Like