39 lines
1013 B
Elixir
39 lines
1013 B
Elixir
defmodule Microprints.TestPubSub do
|
|
@moduledoc """
|
|
Test helper for starting a Phoenix.PubSub server.
|
|
|
|
Provides `start_link/1` to start a PubSub server and returns the
|
|
server name for use with MicroprintCache.
|
|
"""
|
|
|
|
@doc """
|
|
Starts a test PubSub server and returns its name.
|
|
"""
|
|
def start_link(opts \\ []) do
|
|
name = opts[:name] || Microprints.TestPubSub
|
|
|
|
child_spec = Phoenix.PubSub.child_spec(name: name, adapter: Phoenix.PubSub.PG2)
|
|
|
|
# Start a temporary supervisor to host the PubSub server
|
|
case Supervisor.start_link([child_spec], strategy: :one_for_one) do
|
|
{:ok, sup_pid} ->
|
|
Process.put(:test_pubsub_supervisor, sup_pid)
|
|
|
|
{:error, {:already_started, sup_pid}} ->
|
|
Process.put(:test_pubsub_supervisor, sup_pid)
|
|
end
|
|
|
|
name
|
|
end
|
|
|
|
@doc """
|
|
Stops the test PubSub server.
|
|
"""
|
|
def stop do
|
|
if sup_pid = Process.get(:test_pubsub_supervisor) do
|
|
Supervisor.stop(sup_pid)
|
|
Process.delete(:test_pubsub_supervisor)
|
|
end
|
|
end
|
|
end
|