initial tests added

This commit is contained in:
Berenice Medel Sánchez 2021-12-13 12:33:13 -06:00 committed by Chris McCord
parent bdb4319f16
commit 6b5e820130
7 changed files with 103 additions and 3 deletions

View file

@ -58,6 +58,8 @@ config :logger, :console,
# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason
config :live_beats, :presence_client, LiveBeats.PresenceClient
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{config_env()}.exs"

View file

@ -31,3 +31,5 @@ config :logger, level: :warn
# Initialize plugs at runtime for faster test compilation
config :phoenix, :plug_init_mode, :runtime
config :live_beats, :presence_client, Phoenix.Presence.Client.Mock

View file

@ -23,7 +23,7 @@ defmodule LiveBeats.Application do
LiveBeatsWeb.Endpoint,
# Start a worker by calling: LiveBeats.Worker.start_link(arg)
# {LiveBeats.Worker, arg}
{Phoenix.Presence.Client, client: LiveBeats.PresenceClient, pubsub: LiveBeats.PubSub, presence: LiveBeatsWeb.Presence}
{Phoenix.Presence.Client, client: Application.get_env(:live_beats, :presence_client), pubsub: LiveBeats.PubSub, presence: LiveBeatsWeb.Presence}
]
# See https://hexdocs.pm/elixir/Supervisor.html

View file

@ -42,8 +42,7 @@ defmodule Phoenix.Presence.Client do
end
def handle_call(:state, _from, state) do
IO.inspect(state.topics, label: :state_topics)
{:reply, :ok, state}
{:reply, state, state}
end
def handle_call({:track, pid, topic, key, meta}, _from, state) do

View file

@ -0,0 +1,49 @@
defmodule Phoenix.Presence.ClientTest do
use ExUnit.Case, async: true
alias Phoenix.Presence.Client.PresenceMock
test "When a new process is tracked, a topic is created" do
presence_key = 1
topic_id = 100
{:ok, pid} = PresenceMock.start_link(id: presence_key)
PresenceMock.track(pid, topic(topic_id), presence_key)
assert Process.alive?(pid)
# _ = :sys.get_state(PresenceClient)
:timer.sleep(1000)# not the best
assert %{topics: %{"mock_topic:100" => %{"1" => [%{phx_ref: _ref}]}}} =
GenServer.call(PresenceClient, :state)
send(pid, :quit)
:timer.sleep(1000)
refute Process.alive?(pid)
end
test "topic is removed from the topics state when there is no more presences" do
presence_key = 1
topic_id = 100
{:ok, pid} = PresenceMock.start_link(id: presence_key)
PresenceMock.track(pid, topic(topic_id), presence_key)
assert Process.alive?(pid)
# _ = :sys.get_state(PresenceClient)
:timer.sleep(1000)# not the best
assert %{topics: %{"mock_topic:100" => %{"1" => [%{phx_ref: _ref}]}}} =
GenServer.call(PresenceClient, :state)
send(pid, :quit)
:timer.sleep(1000)
refute Process.alive?(pid)
assert %{topics: %{}} = GenServer.call(PresenceClient, :state)
end
defp topic(id) do
"mock_topic:#{id}"
end
end

View file

@ -0,0 +1,17 @@
defmodule Phoenix.Presence.Client.Mock do
def init(_opts) do
{:ok, %{}}
end
def handle_join(_topic, _key, _meta, state) do
IO.inspect(:handle_join)
{:ok, state}
end
def handle_leave(_topic, _key, _meta, state) do
IO.inspect(:handle_leave)
{:ok, state}
end
end

View file

@ -0,0 +1,31 @@
defmodule Phoenix.Presence.Client.PresenceMock do
use GenServer
alias Phoenix.Presence.Client
def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, opts[:id], opts)
end
@impl true
def init(id) do
{:ok, %{id: id}}
end
def track(pid, topic, key) do
GenServer.cast(pid, {:track, topic, key})
end
@impl true
def handle_info(:quit, state) do
IO.inspect(:quit)
{:stop, :normal, state}
end
@impl true
def handle_cast({:track, topic, key}, state) do
Client.track(topic, key, %{})
{:noreply, state}
end
end