Testing database operations in separate processes

I am sorry my problem maybe difficult to explain and I am not sure I understand how things are working.

My app uses some GenStage and GenServers to process data and sometimes write this data to the database as a side effect.

I wrote some tests to assert that the side effects are happening. I learned about the :name for start_link so I can start a separate GetStage or GenServer for a test. I am using Ecto.Adapters.SQL.Sandbox in my test env. All this is normal I think.

However what I see is that I can run individual tests (like mix test test/some/module.exs) and they pass, but when I do a full test run with mix test, sometimes tests fail. The first time works, but I notice that some data stays in the database. I can drop/create the database and run mix test (and they pass) but then when I look in the database I see some records got created.

How is this possible? I thought Sandbox used a transaction to wrap the database operations? Maybe I forgot Sandbox.checkout(Repo) somewhere? Or is there some other reason why records are staying in the database?

Thanks for any assistance!

I think I found the problem – I needed shared mode

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