Error "could not checkout the connection owned by #PID" When testing an umbrella using triplex database connections

I’m have been trying to solve this issue for more than a week but unsuccessfull, I need to run my tests in an umbrella application using Triplex database style. But every time I run a test that tries to create some other register in the triplex database, I have this issue.
I am using shared connections and async tests, here’s one of the tests modules failing and my conn_case.ex file that’s sahring the connection:

conn_case.ex

use Mix.Config

# Configure your database
config :loss_prevention, LossPrevention.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: "postgres",
  password: "postgres",
  database: "loss_prevention_test",
  hostname: "localhost",
  pool: Ecto.Adapters.SQL.Sandbox,
  pool_size: 5,
  queue_target: 1000

config :bcrypt_elixir, :log_rounds, 4

# Queue
config :exq,
  name: Exq,
  host: Map.get(System.get_env(), "REDIS_HOST", "redis"),
  port: Map.get(System.get_env(), "REDIS_PORT", "6379"),
  password: Map.get(System.get_env(), "REDIS_PASSWORD", "Lossprevention"),
  concurrency: 10,
  queues: [
    "email_test",
    "strategy_execution_test",
    "schedule_strategy_execution_test",
    "alert_bulk",
    "export_alerts",
    "alert",
    "notification",
    "api_audit_log",
    "monetary_conversion"
  ],
  scheduler_enable: true,
  max_retries: 0

config :loss_prevention, LossPrevention.Mailer, adapter: Bamboo.TestAdapter

term_list_controller_test.exs:

defmodule LossPreventionWeb.TermListControllerTest do
  use LossPreventionWeb.ConnCase
  use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
  import LossPreventionWeb.Gettext
  alias LossPrevention.Wrappers.Elastic.TermList
  alias LossPrevention.Wrappers.Elastic

  @moduletag company: :test

  setup_all do
    HTTPoison.start()
  end

  describe "index" do
    test "open term list index page without passing type", %{conn: conn} do
      conn =
        conn
        |> simulate_session()
        |> get(term_list_path(conn, :index, status: "active"))

      assert html_response(conn, 200) =~ gettext("Group of inclusion terms")
    end
  end
end

Thanks! :smiley:

1 Like

I vaguely recall having issues with test databases (not using triplex), so I set it up in sandbox mode.

Maybe adding this to test/test_helper.exs will help?

Ecto.Adapters.SQL.Sandbox.mode(LossPreventionWeb.Repo, :manual)
1 Like

Thanks for the reply,
Im using manual mode on the test_helper already.
This has to do with concurrency, but I’m following the ecto documentation for this so it should be working.

This may not be helpful but whenever I get this error it’s never been what I think it is and usually something pretty small and stupid. For example, I believe the last time I got it was because I forgot to call test and only had a describe (don’t quote me on that, though). I don’t see anything wrong with your code, though.

1 Like

Thanks for the insight haha Yea usually the issue is pretty stupid.
Thing is I have more than 170 tests failing (I just entered the project and my task is to fix all the tests)
And about 90 of these tests are failing with this error (multiple tests files)
So I figure some config is missing out. But thanks though.
If I find out the issue I’ll post here.
For now I set the Sandbox mode to :auto , and implemented on_exit to manipulate the data everytime the test case checks out. But this is not good practice and it’s impossible to predict every move of the database so in some cases it will fail and I’ll have to keep inserting or removing data in order for it to work. So I know there’s a better way out there to do that I just haven’t found out yet.

Update, I just found my umbrella app has only one repo, should I have another repo for each app in the umbrella? How do I start each repo?