diff --git a/lib/parrhesia/application.ex b/lib/parrhesia/application.ex index e232308..13b0b62 100644 --- a/lib/parrhesia/application.ex +++ b/lib/parrhesia/application.ex @@ -5,19 +5,6 @@ defmodule Parrhesia.Application do @impl true def start(_type, _args) do - children = [ - Parrhesia.Telemetry, - Parrhesia.Config, - Parrhesia.Storage.Supervisor, - Parrhesia.Subscriptions.Supervisor, - Parrhesia.Auth.Supervisor, - Parrhesia.Sync.Supervisor, - Parrhesia.Policy.Supervisor, - Parrhesia.Web.Endpoint, - Parrhesia.Tasks.Supervisor - ] - - opts = [strategy: :one_for_one, name: Parrhesia.Supervisor] - Supervisor.start_link(children, opts) + Parrhesia.Runtime.start_link(name: Parrhesia.Supervisor) end end diff --git a/lib/parrhesia/runtime.ex b/lib/parrhesia/runtime.ex new file mode 100644 index 0000000..77bd2f4 --- /dev/null +++ b/lib/parrhesia/runtime.ex @@ -0,0 +1,29 @@ +defmodule Parrhesia.Runtime do + @moduledoc false + + use Supervisor + + def start_link(opts \\ []) do + name = Keyword.get(opts, :name, Parrhesia.Supervisor) + Supervisor.start_link(__MODULE__, opts, name: name) + end + + @impl true + def init(_opts) do + Supervisor.init(children(), strategy: :one_for_one) + end + + def children do + [ + Parrhesia.Telemetry, + Parrhesia.Config, + Parrhesia.Storage.Supervisor, + Parrhesia.Subscriptions.Supervisor, + Parrhesia.Auth.Supervisor, + Parrhesia.Sync.Supervisor, + Parrhesia.Policy.Supervisor, + Parrhesia.Web.Endpoint, + Parrhesia.Tasks.Supervisor + ] + end +end diff --git a/mix.exs b/mix.exs index 75a3c0d..cf5137f 100644 --- a/mix.exs +++ b/mix.exs @@ -6,6 +6,7 @@ defmodule Parrhesia.MixProject do app: :parrhesia, version: "0.5.0", elixir: "~> 1.18", + elixirc_paths: elixirc_paths(Mix.env()), start_permanent: Mix.env() == :prod, deps: deps(), aliases: aliases() @@ -20,6 +21,9 @@ defmodule Parrhesia.MixProject do ] end + defp elixirc_paths(:test), do: ["lib", "test/support"] + defp elixirc_paths(_env), do: ["lib"] + def cli do [preferred_envs: [precommit: :test, bench: :test]] end diff --git a/test/parrhesia/api/acl_test.exs b/test/parrhesia/api/acl_test.exs index edbaf66..46f521d 100644 --- a/test/parrhesia/api/acl_test.exs +++ b/test/parrhesia/api/acl_test.exs @@ -1,14 +1,10 @@ defmodule Parrhesia.API.ACLTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: true - alias Ecto.Adapters.SQL.Sandbox alias Parrhesia.API.ACL alias Parrhesia.API.RequestContext - alias Parrhesia.Repo setup do - :ok = Sandbox.checkout(Repo) - previous_acl = Application.get_env(:parrhesia, :acl, []) Application.put_env( diff --git a/test/parrhesia/api/events_test.exs b/test/parrhesia/api/events_test.exs index 09f8bd3..6d8f166 100644 --- a/test/parrhesia/api/events_test.exs +++ b/test/parrhesia/api/events_test.exs @@ -1,16 +1,9 @@ defmodule Parrhesia.API.EventsTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: true - alias Ecto.Adapters.SQL.Sandbox alias Parrhesia.API.Events alias Parrhesia.API.RequestContext alias Parrhesia.Protocol.EventValidator - alias Parrhesia.Repo - - setup do - :ok = Sandbox.checkout(Repo) - :ok - end test "publish stores valid events through the shared API" do event = valid_event() diff --git a/test/parrhesia/api/identity_test.exs b/test/parrhesia/api/identity_test.exs index 966693a..1c1e33f 100644 --- a/test/parrhesia/api/identity_test.exs +++ b/test/parrhesia/api/identity_test.exs @@ -1,5 +1,5 @@ defmodule Parrhesia.API.IdentityTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false alias Parrhesia.API.Auth alias Parrhesia.API.Identity diff --git a/test/parrhesia/api/stream_test.exs b/test/parrhesia/api/stream_test.exs index 941686b..ba4165a 100644 --- a/test/parrhesia/api/stream_test.exs +++ b/test/parrhesia/api/stream_test.exs @@ -1,19 +1,10 @@ defmodule Parrhesia.API.StreamTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: true - alias Ecto.Adapters.SQL.Sandbox alias Parrhesia.API.Events alias Parrhesia.API.RequestContext alias Parrhesia.API.Stream alias Parrhesia.Protocol.EventValidator - alias Parrhesia.Repo - - setup do - ensure_repo_started() - ensure_stream_runtime_started() - :ok = Sandbox.checkout(Repo) - :ok - end test "subscribe streams catch-up events followed by eose" do event = valid_event() @@ -79,24 +70,4 @@ defmodule Parrhesia.API.StreamTest do defp recalculate_event_id(event) do Map.put(event, "id", EventValidator.compute_id(event)) end - - defp ensure_stream_runtime_started do - if is_nil(Process.whereis(Parrhesia.API.Stream.Supervisor)) do - case start_supervised({Parrhesia.Subscriptions.Supervisor, []}) do - {:ok, _pid} -> :ok - {:error, {:already_started, _pid}} -> :ok - end - else - :ok - end - end - - defp ensure_repo_started do - if is_nil(Process.whereis(Repo)) do - _ = start_supervised(Repo) - :ok - else - :ok - end - end end diff --git a/test/parrhesia/api/sync_test.exs b/test/parrhesia/api/sync_test.exs index 3b4eacf..5d498a4 100644 --- a/test/parrhesia/api/sync_test.exs +++ b/test/parrhesia/api/sync_test.exs @@ -1,16 +1,9 @@ defmodule Parrhesia.API.SyncTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: true - alias Ecto.Adapters.SQL.Sandbox alias Parrhesia.API.Admin alias Parrhesia.API.Sync alias Parrhesia.API.Sync.Manager - alias Parrhesia.Repo - - setup do - :ok = Sandbox.checkout(Repo) - :ok - end test "put_server stores normalized config and persists it across restart" do {manager, path, pid} = start_sync_manager() diff --git a/test/parrhesia/application_test.exs b/test/parrhesia/application_test.exs index 9834cf5..3c9043a 100644 --- a/test/parrhesia/application_test.exs +++ b/test/parrhesia/application_test.exs @@ -1,5 +1,5 @@ defmodule Parrhesia.ApplicationTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false test "starts the core supervision tree" do assert is_pid(Process.whereis(Parrhesia.Supervisor)) diff --git a/test/parrhesia/e2e/nak_cli_test.exs b/test/parrhesia/e2e/nak_cli_test.exs index 96986fb..704e276 100644 --- a/test/parrhesia/e2e/nak_cli_test.exs +++ b/test/parrhesia/e2e/nak_cli_test.exs @@ -1,5 +1,5 @@ defmodule Parrhesia.E2E.NakCliTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false @moduletag :nak_e2e diff --git a/test/parrhesia/fanout/multi_node_test.exs b/test/parrhesia/fanout/multi_node_test.exs index 9581e82..8518b57 100644 --- a/test/parrhesia/fanout/multi_node_test.exs +++ b/test/parrhesia/fanout/multi_node_test.exs @@ -1,5 +1,5 @@ defmodule Parrhesia.Fanout.MultiNodeTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false alias Parrhesia.Fanout.MultiNode alias Parrhesia.Subscriptions.Index diff --git a/test/parrhesia/fault_injection_group_flow_test.exs b/test/parrhesia/fault_injection_group_flow_test.exs index 78d2b5a..c4a070e 100644 --- a/test/parrhesia/fault_injection_group_flow_test.exs +++ b/test/parrhesia/fault_injection_group_flow_test.exs @@ -1,17 +1,13 @@ defmodule Parrhesia.FaultInjectionGroupFlowTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: true - alias Ecto.Adapters.SQL.Sandbox alias Parrhesia.Protocol.EventValidator - alias Parrhesia.Repo alias Parrhesia.Storage alias Parrhesia.TestSupport.FailingEvents alias Parrhesia.TestSupport.PermissiveModeration alias Parrhesia.Web.Connection setup do - :ok = Sandbox.checkout(Repo) - previous_storage = Application.get_env(:parrhesia, :storage, []) Application.put_env( diff --git a/test/parrhesia/fault_injection_test.exs b/test/parrhesia/fault_injection_test.exs index d338186..5d976ac 100644 --- a/test/parrhesia/fault_injection_test.exs +++ b/test/parrhesia/fault_injection_test.exs @@ -1,5 +1,5 @@ defmodule Parrhesia.FaultInjectionTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false alias Parrhesia.Protocol.EventValidator alias Parrhesia.Web.Connection diff --git a/test/parrhesia/groups/flow_test.exs b/test/parrhesia/groups/flow_test.exs index ce1463e..151e160 100644 --- a/test/parrhesia/groups/flow_test.exs +++ b/test/parrhesia/groups/flow_test.exs @@ -1,16 +1,9 @@ defmodule Parrhesia.Groups.FlowTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: true - alias Ecto.Adapters.SQL.Sandbox alias Parrhesia.Groups.Flow - alias Parrhesia.Repo alias Parrhesia.Storage - setup do - :ok = Sandbox.checkout(Repo) - :ok - end - test "handles membership request kinds by upserting group memberships" do event = %{ "kind" => 8_000, diff --git a/test/parrhesia/negentropy/sessions_test.exs b/test/parrhesia/negentropy/sessions_test.exs index f1b8542..53449b4 100644 --- a/test/parrhesia/negentropy/sessions_test.exs +++ b/test/parrhesia/negentropy/sessions_test.exs @@ -1,7 +1,6 @@ defmodule Parrhesia.Negentropy.SessionsTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: true - alias Ecto.Adapters.SQL.Sandbox alias Parrhesia.Negentropy.Engine alias Parrhesia.Negentropy.Message alias Parrhesia.Negentropy.Sessions @@ -9,20 +8,6 @@ defmodule Parrhesia.Negentropy.SessionsTest do alias Parrhesia.Repo alias Parrhesia.Storage.Adapters.Postgres.Events - setup_all do - if is_nil(Process.whereis(Repo)) do - start_supervised!(Repo) - end - - Sandbox.mode(Repo, :manual) - :ok - end - - setup do - :ok = Sandbox.checkout(Repo) - :ok - end - test "opens, responds, advances and closes sessions" do server = start_supervised!({Sessions, name: nil}) Sandbox.allow(Repo, self(), server) diff --git a/test/parrhesia/performance/load_soak_test.exs b/test/parrhesia/performance/load_soak_test.exs index a6a267b..a93004a 100644 --- a/test/parrhesia/performance/load_soak_test.exs +++ b/test/parrhesia/performance/load_soak_test.exs @@ -1,14 +1,10 @@ defmodule Parrhesia.Performance.LoadSoakTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: true - alias Ecto.Adapters.SQL.Sandbox - alias Parrhesia.Repo alias Parrhesia.Web.Connection @tag :performance test "fanout enqueue/drain stays within relaxed p95 budget" do - :ok = Sandbox.checkout(Repo) - {:ok, state} = Connection.init(subscription_index: nil, max_outbound_queue: 10_000) req_payload = JSON.encode!(["REQ", "sub-load", %{"kinds" => [1]}]) diff --git a/test/parrhesia/policy/event_policy_test.exs b/test/parrhesia/policy/event_policy_test.exs index 772e826..18d479e 100644 --- a/test/parrhesia/policy/event_policy_test.exs +++ b/test/parrhesia/policy/event_policy_test.exs @@ -1,5 +1,5 @@ defmodule Parrhesia.Policy.EventPolicyTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false alias Parrhesia.Policy.EventPolicy diff --git a/test/parrhesia/protocol/event_validator_signature_test.exs b/test/parrhesia/protocol/event_validator_signature_test.exs index 8655151..c10918a 100644 --- a/test/parrhesia/protocol/event_validator_signature_test.exs +++ b/test/parrhesia/protocol/event_validator_signature_test.exs @@ -1,5 +1,5 @@ defmodule Parrhesia.Protocol.EventValidatorSignatureTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false alias Parrhesia.Protocol.EventValidator diff --git a/test/parrhesia/storage/adapters/memory/adapter_test.exs b/test/parrhesia/storage/adapters/memory/adapter_test.exs index b504415..4b99760 100644 --- a/test/parrhesia/storage/adapters/memory/adapter_test.exs +++ b/test/parrhesia/storage/adapters/memory/adapter_test.exs @@ -1,5 +1,5 @@ defmodule Parrhesia.Storage.Adapters.Memory.AdapterTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false alias Parrhesia.Storage.Adapters.Memory.ACL alias Parrhesia.Storage.Adapters.Memory.Admin diff --git a/test/parrhesia/storage/adapters/postgres/adapter_contract_test.exs b/test/parrhesia/storage/adapters/postgres/adapter_contract_test.exs index f0e17e6..81d65de 100644 --- a/test/parrhesia/storage/adapters/postgres/adapter_contract_test.exs +++ b/test/parrhesia/storage/adapters/postgres/adapter_contract_test.exs @@ -1,26 +1,11 @@ defmodule Parrhesia.Storage.Adapters.Postgres.AdapterContractTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: true - alias Ecto.Adapters.SQL.Sandbox - alias Parrhesia.Repo alias Parrhesia.Storage.Adapters.Postgres.ACL alias Parrhesia.Storage.Adapters.Postgres.Admin alias Parrhesia.Storage.Adapters.Postgres.Groups alias Parrhesia.Storage.Adapters.Postgres.Moderation - setup_all do - if is_nil(Process.whereis(Repo)) do - start_supervised!(Repo) - end - - Sandbox.mode(Repo, :manual) - :ok - end - - setup do - :ok = Sandbox.checkout(Repo) - end - test "moderation adapter persists pubkey/event/ip block state" do pubkey = String.duplicate("a", 64) event_id = String.duplicate("b", 64) diff --git a/test/parrhesia/storage/adapters/postgres/events_lifecycle_test.exs b/test/parrhesia/storage/adapters/postgres/events_lifecycle_test.exs index ee16b1e..53c384f 100644 --- a/test/parrhesia/storage/adapters/postgres/events_lifecycle_test.exs +++ b/test/parrhesia/storage/adapters/postgres/events_lifecycle_test.exs @@ -1,16 +1,9 @@ defmodule Parrhesia.Storage.Adapters.Postgres.EventsLifecycleTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: true - alias Ecto.Adapters.SQL.Sandbox alias Parrhesia.Protocol.EventValidator - alias Parrhesia.Repo alias Parrhesia.Storage.Adapters.Postgres.Events - setup do - :ok = Sandbox.checkout(Repo) - :ok - end - test "event tags round-trip without truncation" do tagged_event = event(%{ diff --git a/test/parrhesia/storage/adapters/postgres/events_query_count_test.exs b/test/parrhesia/storage/adapters/postgres/events_query_count_test.exs index 2cb4f6f..1b38a08 100644 --- a/test/parrhesia/storage/adapters/postgres/events_query_count_test.exs +++ b/test/parrhesia/storage/adapters/postgres/events_query_count_test.exs @@ -1,24 +1,9 @@ defmodule Parrhesia.Storage.Adapters.Postgres.EventsQueryCountTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: true - alias Ecto.Adapters.SQL.Sandbox alias Parrhesia.Protocol.EventValidator - alias Parrhesia.Repo alias Parrhesia.Storage.Adapters.Postgres.Events - setup_all do - if is_nil(Process.whereis(Repo)) do - start_supervised!(Repo) - end - - Sandbox.mode(Repo, :manual) - :ok - end - - setup do - :ok = Sandbox.checkout(Repo) - end - test "query/3 translates NIP filters including tag filters" do author = String.duplicate("a", 64) other_author = String.duplicate("b", 64) diff --git a/test/parrhesia/storage/adapters/postgres/events_test.exs b/test/parrhesia/storage/adapters/postgres/events_test.exs index 334c155..327c513 100644 --- a/test/parrhesia/storage/adapters/postgres/events_test.exs +++ b/test/parrhesia/storage/adapters/postgres/events_test.exs @@ -1,5 +1,5 @@ defmodule Parrhesia.Storage.Adapters.Postgres.EventsTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false alias Parrhesia.Protocol.EventValidator alias Parrhesia.Storage.Adapters.Postgres.Events diff --git a/test/parrhesia/storage/adapters/postgres/query_plan_regression_test.exs b/test/parrhesia/storage/adapters/postgres/query_plan_regression_test.exs index 0dc57ac..83931ec 100644 --- a/test/parrhesia/storage/adapters/postgres/query_plan_regression_test.exs +++ b/test/parrhesia/storage/adapters/postgres/query_plan_regression_test.exs @@ -1,22 +1,11 @@ defmodule Parrhesia.Storage.Adapters.Postgres.QueryPlanRegressionTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: true - alias Ecto.Adapters.SQL.Sandbox alias Parrhesia.Protocol.EventValidator alias Parrhesia.Repo alias Parrhesia.Storage.Adapters.Postgres.Events - setup_all do - if is_nil(Process.whereis(Repo)) do - start_supervised!(Repo) - end - - Sandbox.mode(Repo, :manual) - :ok - end - setup do - :ok = Sandbox.checkout(Repo) :ok = Repo.query!("SET enable_seqscan TO off") |> then(fn _ -> :ok end) end diff --git a/test/parrhesia/storage/partitions_test.exs b/test/parrhesia/storage/partitions_test.exs index 2f19ddf..7ae9c35 100644 --- a/test/parrhesia/storage/partitions_test.exs +++ b/test/parrhesia/storage/partitions_test.exs @@ -1,15 +1,8 @@ defmodule Parrhesia.Storage.PartitionsTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: true - alias Ecto.Adapters.SQL.Sandbox - alias Parrhesia.Repo alias Parrhesia.Storage.Partitions - setup do - :ok = Sandbox.checkout(Repo) - :ok - end - test "list_partitions returns partition tables" do partitions = Partitions.list_partitions() assert is_list(partitions) diff --git a/test/parrhesia/storage_test.exs b/test/parrhesia/storage_test.exs index 467bb07..048fc4a 100644 --- a/test/parrhesia/storage_test.exs +++ b/test/parrhesia/storage_test.exs @@ -1,5 +1,5 @@ defmodule Parrhesia.StorageTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false alias Parrhesia.Storage diff --git a/test/parrhesia/sync/worker_test.exs b/test/parrhesia/sync/worker_test.exs index 72f01dd..0a87ee0 100644 --- a/test/parrhesia/sync/worker_test.exs +++ b/test/parrhesia/sync/worker_test.exs @@ -1,29 +1,16 @@ defmodule Parrhesia.Sync.WorkerTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: :shared - alias Ecto.Adapters.SQL.Sandbox alias Parrhesia.API.ACL alias Parrhesia.API.Events alias Parrhesia.API.Identity alias Parrhesia.API.RequestContext alias Parrhesia.API.Sync alias Parrhesia.Protocol.EventValidator - alias Parrhesia.Repo alias Parrhesia.Sync.Supervisor alias Parrhesia.TestSupport.SyncFakeRelay.Plug alias Parrhesia.TestSupport.SyncFakeRelay.Server - setup do - :ok = Sandbox.checkout(Repo) - Sandbox.mode(Repo, {:shared, self()}) - - on_exit(fn -> - Sandbox.mode(Repo, :manual) - end) - - :ok - end - test "req_stream worker verifies remote identity, authenticates, syncs catch-up, streams live, and sync_now reruns catch-up" do {:ok, %{pubkey: local_pubkey}} = Identity.ensure() remote_pubkey = String.duplicate("b", 64) diff --git a/test/parrhesia/tasks/expiration_worker_test.exs b/test/parrhesia/tasks/expiration_worker_test.exs index 2263d99..4cce0e0 100644 --- a/test/parrhesia/tasks/expiration_worker_test.exs +++ b/test/parrhesia/tasks/expiration_worker_test.exs @@ -1,5 +1,5 @@ defmodule Parrhesia.Tasks.ExpirationWorkerTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false alias Parrhesia.Tasks.ExpirationWorker alias Parrhesia.TestSupport.ExpirationStubEvents diff --git a/test/parrhesia/tasks/partition_retention_worker_test.exs b/test/parrhesia/tasks/partition_retention_worker_test.exs index cea7d35..7a0dcab 100644 --- a/test/parrhesia/tasks/partition_retention_worker_test.exs +++ b/test/parrhesia/tasks/partition_retention_worker_test.exs @@ -1,5 +1,5 @@ defmodule Parrhesia.Tasks.PartitionRetentionWorkerTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false alias Parrhesia.Tasks.PartitionRetentionWorker alias Parrhesia.TestSupport.PartitionRetentionStubPartitions diff --git a/test/parrhesia/web/conformance_test.exs b/test/parrhesia/web/conformance_test.exs index 6faadb3..0fb08d1 100644 --- a/test/parrhesia/web/conformance_test.exs +++ b/test/parrhesia/web/conformance_test.exs @@ -1,17 +1,10 @@ defmodule Parrhesia.Web.ConformanceTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: true - alias Ecto.Adapters.SQL.Sandbox alias Parrhesia.Protocol.EventValidator - alias Parrhesia.Repo alias Parrhesia.Storage alias Parrhesia.Web.Connection - setup do - :ok = Sandbox.checkout(Repo) - :ok - end - test "REQ -> EOSE emitted once and CLOSE emits CLOSED" do {:ok, state} = Connection.init(subscription_index: nil) diff --git a/test/parrhesia/web/connection_test.exs b/test/parrhesia/web/connection_test.exs index a87520e..2882618 100644 --- a/test/parrhesia/web/connection_test.exs +++ b/test/parrhesia/web/connection_test.exs @@ -1,23 +1,14 @@ defmodule Parrhesia.Web.ConnectionTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: true - alias Ecto.Adapters.SQL.Sandbox alias Parrhesia.API.ACL alias Parrhesia.API.Events alias Parrhesia.API.RequestContext alias Parrhesia.Negentropy.Engine alias Parrhesia.Negentropy.Message alias Parrhesia.Protocol.EventValidator - alias Parrhesia.Repo alias Parrhesia.Web.Connection - setup do - ensure_repo_started() - ensure_stream_runtime_started() - :ok = Sandbox.checkout(Repo) - :ok - end - test "REQ registers subscription, streams initial events and replies with EOSE" do state = connection_state() @@ -876,26 +867,6 @@ defmodule Parrhesia.Web.ConnectionTest do state end - defp ensure_stream_runtime_started do - if is_nil(Process.whereis(Parrhesia.API.Stream.Supervisor)) do - case start_supervised({Parrhesia.Subscriptions.Supervisor, []}) do - {:ok, _pid} -> :ok - {:error, {:already_started, _pid}} -> :ok - end - else - :ok - end - end - - defp ensure_repo_started do - if is_nil(Process.whereis(Repo)) do - _ = start_supervised(Repo) - :ok - else - :ok - end - end - defp listener(overrides) do base = %{ id: :test, diff --git a/test/parrhesia/web/proxy_ip_e2e_test.exs b/test/parrhesia/web/proxy_ip_e2e_test.exs index ef490e7..5dba3dc 100644 --- a/test/parrhesia/web/proxy_ip_e2e_test.exs +++ b/test/parrhesia/web/proxy_ip_e2e_test.exs @@ -1,9 +1,7 @@ defmodule Parrhesia.Web.ProxyIpE2ETest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: :shared alias __MODULE__.TestClient - alias Ecto.Adapters.SQL.Sandbox - alias Parrhesia.Repo setup_all do {:ok, _apps} = Application.ensure_all_started(:websockex) @@ -11,14 +9,10 @@ defmodule Parrhesia.Web.ProxyIpE2ETest do end setup do - :ok = Sandbox.checkout(Repo) - Sandbox.mode(Repo, {:shared, self()}) - previous_trusted_proxies = Application.get_env(:parrhesia, :trusted_proxies, []) on_exit(fn -> Application.put_env(:parrhesia, :trusted_proxies, previous_trusted_proxies) - Sandbox.mode(Repo, :manual) end) {:ok, port: free_port()} diff --git a/test/parrhesia/web/router_test.exs b/test/parrhesia/web/router_test.exs index 5b8fcaf..7996897 100644 --- a/test/parrhesia/web/router_test.exs +++ b/test/parrhesia/web/router_test.exs @@ -1,21 +1,14 @@ defmodule Parrhesia.Web.RouterTest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: true import Plug.Conn import Plug.Test - alias Ecto.Adapters.SQL.Sandbox alias Parrhesia.API.Sync alias Parrhesia.Protocol.EventValidator - alias Parrhesia.Repo alias Parrhesia.Web.Listener alias Parrhesia.Web.Router - setup do - :ok = Sandbox.checkout(Repo) - :ok - end - test "GET /health returns ok" do conn = conn(:get, "/health") |> Router.call([]) diff --git a/test/parrhesia/web/tls_e2e_test.exs b/test/parrhesia/web/tls_e2e_test.exs index 707c1c1..355e4a6 100644 --- a/test/parrhesia/web/tls_e2e_test.exs +++ b/test/parrhesia/web/tls_e2e_test.exs @@ -1,16 +1,11 @@ defmodule Parrhesia.Web.TLSE2ETest do - use ExUnit.Case, async: false + use Parrhesia.IntegrationCase, async: false, sandbox: :shared - alias Ecto.Adapters.SQL.Sandbox - alias Parrhesia.Repo alias Parrhesia.Sync.Transport.WebSockexClient alias Parrhesia.TestSupport.TLSCerts alias Parrhesia.Web.Endpoint setup do - :ok = Sandbox.checkout(Repo) - Sandbox.mode(Repo, {:shared, self()}) - tmp_dir = Path.join( System.tmp_dir!(), @@ -20,7 +15,6 @@ defmodule Parrhesia.Web.TLSE2ETest do File.mkdir_p!(tmp_dir) on_exit(fn -> - Sandbox.mode(Repo, :manual) _ = File.rm_rf(tmp_dir) end) diff --git a/test/support/integration_case.ex b/test/support/integration_case.ex new file mode 100644 index 0000000..5d41e96 --- /dev/null +++ b/test/support/integration_case.ex @@ -0,0 +1,43 @@ +defmodule Parrhesia.IntegrationCase do + @moduledoc false + + use ExUnit.CaseTemplate + + alias Ecto.Adapters.SQL.Sandbox + alias ExUnit.Case + alias Parrhesia.Repo + alias Parrhesia.TestSupport.Runtime + + using opts do + quote bind_quoted: [opts: opts] do + use Case, async: Keyword.get(opts, :async, false) + + alias Ecto.Adapters.SQL.Sandbox + alias Parrhesia.Repo + + @moduletag parrhesia_sandbox: Keyword.get(opts, :sandbox, false) + end + end + + setup tags do + Runtime.ensure_started!() + + case tags[:parrhesia_sandbox] do + false -> + :ok + + true -> + :ok = Sandbox.checkout(Repo) + + :shared -> + :ok = Sandbox.checkout(Repo) + Sandbox.mode(Repo, {:shared, self()}) + + on_exit(fn -> + Sandbox.mode(Repo, :manual) + end) + end + + :ok + end +end diff --git a/test/support/runtime.ex b/test/support/runtime.ex new file mode 100644 index 0000000..4d71765 --- /dev/null +++ b/test/support/runtime.ex @@ -0,0 +1,34 @@ +defmodule Parrhesia.TestSupport.Runtime do + @moduledoc false + + @required_processes [ + Parrhesia.Supervisor, + Parrhesia.Config, + Parrhesia.Repo, + Parrhesia.Subscriptions.Supervisor, + Parrhesia.Web.Endpoint + ] + + def ensure_started! do + if healthy?() do + :ok + else + restart!() + end + end + + defp healthy? do + Enum.all?(@required_processes, &is_pid(Process.whereis(&1))) + end + + defp restart! do + case Application.stop(:parrhesia) do + :ok -> :ok + {:error, {:not_started, :parrhesia}} -> :ok + {:error, {:not_started, _app}} -> :ok + end + + {:ok, _apps} = Application.ensure_all_started(:parrhesia) + :ok + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs index bd54632..eaf59ee 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -5,6 +5,4 @@ exclude_tags = [:nak_e2e] end -{:ok, _apps} = Application.ensure_all_started(:parrhesia) - ExUnit.start(exclude: exclude_tags)