7
votes

I am working through a phoenix tutorial but I have this error:

 ** (DBConnection.OwnershipError) cannot find ownership process for #PID<0.265.0>.

I am not using Task.start so nothing should be running asynchronously, and I thought that having the mode in the unless tag would be enough to prevent this error, in test/support/channel_case.ex:

  setup tags do
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(Watchlist.Repo)

    unless tags[:async] do
      Ecto.Adapters.SQL.Sandbox.mode(Watchlist.Repo, {:shared, self()})
    end

    :ok
  end

So, I am curious how I resolve this error.

This is how I run it:

mix test test/integration/listing_movies_test.exs

I am using elixir 1.3.2

UPDATE

defmodule ListingMoviesIntegrationTest do
  use ExUnit.Case, async: true
  use Plug.Test
  alias Watchlist.Router

  @opts Router.init([])
  test 'listing movies' do
    movie = %Movie{name: "Back to the future", rating: 5}
            |> Repo.insert!   <== error happens here

    conn = conn(:get, "/movies")
    response = Router.call(conn, @opts)

    assert response.status == 200
    assert response.resp_body == movie
  end

Full stack trace:

(db_connection) lib/db_connection.ex:718: DBConnection.checkout/2
       (db_connection) lib/db_connection.ex:619: DBConnection.run/3
       (db_connection) lib/db_connection.ex:463: DBConnection.prepare_execute/4
       (ecto) lib/ecto/adapters/postgres/connection.ex:91: Ecto.Adapters.Postgres.Connection.execute/4
       (ecto) lib/ecto/adapters/sql.ex:235: Ecto.Adapters.SQL.sql_call/6
       (ecto) lib/ecto/adapters/sql.ex:454: Ecto.Adapters.SQL.struct/6
       (ecto) lib/ecto/repo/schema.ex:397: Ecto.Repo.Schema.apply/4
       (ecto) lib/ecto/repo/schema.ex:193: anonymous fn/11 in Ecto.Repo.Schema.do_insert/4
       (ecto) lib/ecto/repo/schema.ex:124: Ecto.Repo.Schema.insert!/4
       test/integration/listing_movies_test.exs:13: (test)

and in test_helper which is actually being called as I put in a debug statement:

ExUnit.start

Ecto.Adapters.SQL.Sandbox.mode(Watchlist.Repo, :manual)
Ecto.Adapters.SQL.Sandbox.mode(Watchlist.Repo, {:shared, self()})
3
can you post the code from the test itself? - rozap
@rozap - added code - James Black
can you show us what you have in test helper? Also, could you please show us full error message. - ash
@ash - updated with more source code and full error message. - James Black

3 Answers

6
votes
use ExUnit.Case, async: true
use Plug.Test

You have code for setup hook in "test/support/channel_case.ex" but you are not using it anywhere in your test or at least it is not clear if you do use it. It would be helpful if you can add this:

IO.puts "#{inspect __MODULE__}: setup is getting called."

somewhere in your setup hook's code. This will make sure that the code actually runs. My suspicion from the comment you made in my previous answer this code is dead.

defmodule ListingMoviesIntegrationTest do
  use ExUnit.Case, async: true
  use Plug.Test
  alias Watchlist.Router

  setup do
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(Watchlist.Repo)
    Ecto.Adapters.SQL.Sandbox.mode(Watchlist.Repo, {:shared, self()})
    :ok
  end


  @opts Router.init([])
  test 'listing movies' do
    movie = %Movie{name: "Back to the future", rating: 5}
        |> Repo.insert!   <== error happens here

    conn = conn(:get, "/movies")
    response = Router.call(conn, @opts)

    assert response.status == 200
    assert response.resp_body == movie
  end
  ...
2
votes

I got the same error and in my case on Elixir 1.8.2, Phoenix 1.4.1: after looking at this Elixir forum thread, I changed my test_helpers.exs to have the line below to have the adapter pool mode from manual to auto.

Ecto.Adapters.SQL.Sandbox.mode(MyApp.Repo, :auto)

You can read more about this about the modes and pool checkout and ownership on the Hex Ecto docs

0
votes

I guess you are skipping this line

Ecto.Adapters.SQL.Sandbox.mode(Watchlist.Repo, {:shared, self()})

because

iex(1)> unless true do
...(1)>   IO.puts "test"
...(1)> end
nil
iex(2)> unless false do
...(2)>   IO.puts "test"
...(2)> end
test
:ok

Did you try:

if tags[:async] do
  Ecto.Adapters.SQL.Sandbox.mode(Watchlist.Repo, {:shared, self()})
end