You've already forked tribes-plugin-sender
forked from tribes/tribes-plugin-template
f8e2bfaada
Move Sender modules under TribeOne.TribesPlugin.Sender and replace the Aether-specific chat integration with the public chat@1 surface contract.
77 lines
2.0 KiB
Elixir
77 lines
2.0 KiB
Elixir
defmodule TribeOne.TribesPlugin.Sender.DataCase do
|
|
@moduledoc false
|
|
|
|
use ExUnit.CaseTemplate
|
|
|
|
using do
|
|
quote do
|
|
import TribeOne.TribesPlugin.Sender.DataCase
|
|
end
|
|
end
|
|
|
|
setup tags do
|
|
setup_sandbox(tags)
|
|
:ok
|
|
end
|
|
|
|
def setup_sandbox(tags) do
|
|
tribes_owner = Ecto.Adapters.SQL.Sandbox.start_owner!(Tribes.Repo, shared: not tags[:async])
|
|
|
|
parrhesia_owner =
|
|
Ecto.Adapters.SQL.Sandbox.start_owner!(Parrhesia.Repo, shared: not tags[:async])
|
|
|
|
allow_background_processes(tribes_owner, parrhesia_owner)
|
|
|
|
on_exit(fn ->
|
|
drain_background_processes()
|
|
Ecto.Adapters.SQL.Sandbox.stop_owner(parrhesia_owner)
|
|
Ecto.Adapters.SQL.Sandbox.stop_owner(tribes_owner)
|
|
end)
|
|
end
|
|
|
|
defp allow_background_processes(tribes_owner, parrhesia_owner) do
|
|
Enum.each(background_process_allowances(), fn {name, repos} ->
|
|
allow_if_running(name, &allow_repos(&1, repos, tribes_owner, parrhesia_owner))
|
|
end)
|
|
end
|
|
|
|
defp allow_repos(pid, repos, tribes_owner, parrhesia_owner) do
|
|
Enum.each(repos, fn
|
|
:tribes -> allow_repo(Tribes.Repo, tribes_owner, pid)
|
|
:parrhesia -> allow_repo(Parrhesia.Repo, parrhesia_owner, pid)
|
|
end)
|
|
end
|
|
|
|
defp drain_background_processes do
|
|
Enum.each(background_process_allowances(), fn {name, _repos} ->
|
|
case Process.whereis(name) do
|
|
nil -> :ok
|
|
pid -> _ = :sys.get_state(pid)
|
|
end
|
|
end)
|
|
end
|
|
|
|
defp background_process_allowances do
|
|
[
|
|
{AshNostrSync.Consumer, [:tribes, :parrhesia]},
|
|
{AshNostrSync.Reconciler, [:tribes, :parrhesia]},
|
|
{Parrhesia.Tasks.ExpirationWorker, [:parrhesia]},
|
|
{Parrhesia.Tasks.PartitionRetentionWorker, [:parrhesia]}
|
|
]
|
|
end
|
|
|
|
defp allow_if_running(name, callback) when is_function(callback, 1) do
|
|
case Process.whereis(name) do
|
|
nil -> :ok
|
|
pid -> callback.(pid)
|
|
end
|
|
end
|
|
|
|
defp allow_repo(repo, owner, pid) do
|
|
case Ecto.Adapters.SQL.Sandbox.allow(repo, owner, pid) do
|
|
:ok -> :ok
|
|
{:already, :allowed} -> :ok
|
|
end
|
|
end
|
|
end
|