6

I was happily following this advice on how to run a debugger inside my Phoenix controller tests:

  • require IEx in the target file
  • add IEx.pry to the desired line
  • run the tests inside IEx: iex -S mix test --trace

But after a few seconds this error always appeared:

16:51:08.108 [error] Postgrex.Protocol (#PID<0.250.0>) disconnected: 
** (DBConnection.ConnectionError) owner #PID<0.384.0> timed out because 
it owned the connection for longer than 15000ms

As the message says, the database connection appears to time out at this point and any commands that invoke the database connection will error out with a DBConnection.OwnershipError. How do I tell my database connection not to time out so I can debug my tests in peace?

Community
  • 1
  • 1
Topher Hunt
  • 4,404
  • 2
  • 27
  • 51

1 Answers1

7

The Ecto.Adapters.SQL.Sandbox FAQ mentions this issue and explains that you can add the :ownership_timeout setting to your Repo config to specify how long db connections should stay open before timing out. I set mine to 10 minutes (test environment only) so I never have to think about it again:

# config.test.exs

config :rumbl, Rumbl.Repo,
  # ...other settings...
  ownership_timeout: 10 * 60 * 1000 # long timeout so pry sessions don't break

As expected, I can now fool around in pry for 10 minutes before seeing that error.

Topher Hunt
  • 4,404
  • 2
  • 27
  • 51
  • 1
    Also: `ExUnit.configure(timeout: :infinity)` and/or `iex -S mix test --trace` are world-wide alternatives. – Aleksei Matiushkin Nov 30 '16 at 10:41
  • 5
    Thanks for that tip; I didn't try `ExUnit.configure(timeout: :infinity)`, however I can confirm that `iex -S mix test --trace` prevents *request* timeouts but doesn't prevent the *db connection* timeout (see steps I've tried in the question above). I wonder, should I treat this as a bug? – Topher Hunt Nov 30 '16 at 17:05
  • No idea. Probably yes, but I am not sure. – Aleksei Matiushkin Nov 30 '16 at 17:42
  • 1
    For reference,`ExUnit.configure(timeout: :infinity)` didn't work for me, but setting the `ownership_timeout` did. – John Hamelink Sep 27 '17 at 11:17