Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

** (DBConnection.OwnershipError) cannot find ownership process for #PID #128

Open
nelsonic opened this issue May 22, 2019 · 5 comments
Open

Comments

@nelsonic
Copy link
Member

When attempting to create/run a test that inserts data:

defmodule UseragentTest do
  use ExUnit.Case
  alias Hits.Useragent

  test "Useragent.insert" do
    useragent = "Mozilla/5.0 (Windows; Windows NT 5.0; rv:1.1) Gecko/20020826"
    ip = "127.0.0.1"
    useragent_id = Useragent.insert(%Useragent{name: useragent, ip: ip})

    IO.inspect(useragent_id, label: "useragent_id")
  end

end

attempting to run it with the command:

mix test test/useragent_test.exs

I get the following error:

  1) test Useragent.insert (UseragentTest)
     test/useragent_test.exs:8
     ** (DBConnection.OwnershipError) cannot find ownership process for #PID<0.390.0>.

     When using ownership, you must manage connections in one
     of the four ways:

     * By explicitly checking out a connection
     * By explicitly allowing a spawned process
     * By running the pool in shared mode
     * By using :caller option with allowed process

     The first two options require every new process to explicitly
     check a connection out or be allowed by calling checkout or
     allow respectively.

     The third option requires a {:shared, pid} mode to be set.
     If using shared mode in tests, make sure your tests are not
     async.

     The fourth option requires [caller: pid] to be used when
     checking out a connection from the pool. The caller process
     should already be allowed on a connection.

     If you are reading this error, it means you have not done one
     of the steps above or that the owner process has crashed.

     See Ecto.Adapters.SQL.Sandbox docs for more information.
     code: useragent_id = Useragent.insert(%Useragent{name: useragent, ip: ip})
     stacktrace:
       (ecto_sql) lib/ecto/adapters/sql.ex:602: Ecto.Adapters.SQL.raise_sql_call_error/1
       (ecto_sql) lib/ecto/adapters/sql.ex:538: Ecto.Adapters.SQL.execute/5
       (ecto) lib/ecto/repo/queryable.ex:153: Ecto.Repo.Queryable.execute/4
       (ecto) lib/ecto/repo/queryable.ex:18: Ecto.Repo.Queryable.all/3
       (ecto) lib/ecto/repo/queryable.ex:67: Ecto.Repo.Queryable.one/3
       (hits) lib/hits/useragent.ex:31: Hits.Useragent.insert/1
       test/useragent_test.exs:11: (test)

Obviously the Useragent.insert works when I run the app mix phx.server ...
But the test does not run because it does not have ownership of the connection ... 😞

@nelsonic
Copy link
Member Author

After reading: https://hexdocs.pm/ecto_sql/Ecto.Adapters.SQL.Sandbox.html

Add the following code to the top of the test:

  setup do
    # Explicitly get a connection before each test
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(YourApp.Repo)
  end

Now the test runs as expected:

Finished in 0.05 seconds
1 test, 0 failures

@HoffsMH
Copy link

HoffsMH commented May 27, 2019

@nelsonic
I wonder if the intended path is to
use MyApp.Datacase instead of use ExUnit.Case as defined in test/support/data_case.ex

Had this problem myself what do you think?

EDIT: oops I guess its model_case.ex for older versions of phoenix

@nelsonic
Copy link
Member Author

@HoffsMH good thinking! 💡

@romenigld
Copy link

Thank's @nelsonic !

@jarrodmoldrich
Copy link

jarrodmoldrich commented Jan 3, 2021

I wonder if the intended path is to
use MyApp.Datacase instead of use ExUnit.Case as defined in test/support/data_case.ex

Because I ran into an intermittent error with this, it's worth noting: when you're implicitly affecting a repo controlled by another module in a test, you'll still need to do an explicit checkout(..).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants