Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Prevent Salts server from dirty postgres disconnects during tests (#5968)

Adam Rutkowski6d72c183d248
diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex
index 1a0565ee8f..99432208bf 100644
--- a/test/support/conn_case.ex
+++ b/test/support/conn_case.ex
@@ -40,6 +40,8 @@ defmodule PlausibleWeb.ConnCase do
Ecto.Adapters.SQL.Sandbox.mode(Plausible.Repo, {:shared, self()})
end
+ Plausible.Test.Support.Sandbox.allow_salts_process()
+
# randomize client ip to avoid accidentally hitting
# rate limiting during tests
conn =
diff --git a/test/support/data_case.ex b/test/support/data_case.ex
index 54fc41685a..e379994e68 100644
--- a/test/support/data_case.ex
+++ b/test/support/data_case.ex
@@ -37,6 +37,8 @@ defmodule Plausible.DataCase do
Ecto.Adapters.SQL.Sandbox.mode(Plausible.Repo, {:shared, self()})
end
+ Plausible.Test.Support.Sandbox.allow_salts_process()
+
:ok
end
end
diff --git a/test/support/plugins_api_case.ex b/test/support/plugins_api_case.ex
index 6eab811344..f0eec2e631 100644
--- a/test/support/plugins_api_case.ex
+++ b/test/support/plugins_api_case.ex
@@ -41,6 +41,8 @@ defmodule PlausibleWeb.PluginsAPICase do
Ecto.Adapters.SQL.Sandbox.mode(Plausible.Repo, {:shared, self()})
end
+ Plausible.Test.Support.Sandbox.allow_salts_process()
+
conn = Phoenix.ConnTest.build_conn()
site = Plausible.Teams.Test.new_site()
diff --git a/test/support/sandbox.ex b/test/support/sandbox.ex
new file mode 100644
index 0000000000..1c75890a3a
--- /dev/null
+++ b/test/support/sandbox.ex
@@ -0,0 +1,15 @@
+defmodule Plausible.Test.Support.Sandbox do
+ @moduledoc """
+ Prevent background processes from interfering with tests
+ (e.g. exiting before properly returning the connection) by allowing them to access sandbox
+ """
+ def allow_salts_process do
+ case Process.whereis(Plausible.Session.Salts) do
+ nil ->
+ :ok
+
+ pid ->
+ Ecto.Adapters.SQL.Sandbox.allow(Plausible.Repo, self(), pid)
+ end
+ end
+end