fix tests

This commit is contained in:
Berenice Medel Sánchez 2021-12-14 16:14:37 -06:00 committed by Chris McCord
parent 84d4eead7a
commit 8dfb96c78a
2 changed files with 53 additions and 29 deletions

View file

@ -1,46 +1,70 @@
defmodule Phoenix.Presence.ClientTest do
use ExUnit.Case, async: true
use ExUnit.Case
alias Phoenix.Presence.Client.PresenceMock
alias Phoenix.Presence.Client
test "When a new process is tracked, a topic is created" do
@pubsub LiveBeats.PubSub
@client Phoenix.Presence.Client.Mock
@presence LiveBeatsWeb.Presence
@presence_client_opts [client: @client, pubsub: @pubsub, presence: @presence]
setup tags do
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(LiveBeats.Repo, shared: not tags[:async])
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
:ok
end
test "When a new process is tracked, a topic key is added to the topics state" do
presence_key = 1
topic_id = 100
topic = topic(100)
{:ok, pid} = PresenceMock.start_link(id: presence_key)
{:ok, presence_client} = Client.start_link(@presence_client_opts)
{:ok, presence_process} = 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
Phoenix.PubSub.subscribe(@pubsub, topic)
Process.monitor(presence_process)
assert %{topics: %{"mock_topic:100" => %{"1" => [%{phx_ref: _ref}]}}} =
GenServer.call(PresenceClient, :state)
PresenceMock.track(presence_client, presence_process, topic, presence_key)
send(pid, :quit)
:timer.sleep(1000)
refute Process.alive?(pid)
assert Process.alive?(presence_process)
assert_receive %{event: "presence_diff"}
client_state = :sys.get_state(presence_client)
assert %{topics: %{^topic => %{"1" => [%{phx_ref: _ref}]}}} = client_state
end
test "topic is removed from the topics state when there is no more presences" do
presence_key = 1
topic_id = 100
topic = topic(100)
{:ok, pid} = PresenceMock.start_link(id: presence_key)
{:ok, presence_client} = Client.start_link(@presence_client_opts)
{:ok, presence_process} = PresenceMock.start_link(id: presence_key)
PresenceMock.track(pid, topic(topic_id), presence_key)
assert Process.alive?(pid)
# _ = :sys.get_state(PresenceClient)
Phoenix.PubSub.subscribe(@pubsub, topic)
Process.monitor(presence_process)
:timer.sleep(1000)# not the best
PresenceMock.track(presence_client, presence_process, topic, presence_key)
assert %{topics: %{"mock_topic:100" => %{"1" => [%{phx_ref: _ref}]}}} =
GenServer.call(PresenceClient, :state)
assert Process.alive?(presence_process)
send(pid, :quit)
:timer.sleep(1000)
refute Process.alive?(pid)
assert %{topics: %{}} = GenServer.call(PresenceClient, :state)
assert_receive %{event: "presence_diff"}
client_state = :sys.get_state(presence_client)
assert %{topics: %{^topic => %{"1" => [%{phx_ref: _ref}]}}} = client_state
send(presence_process, :quit)
assert_receive {:DOWN, _ref, :process, ^presence_process, _reason}
client_state = :sys.get_state(presence_client)
assert %{topics: %{}} = client_state
end
defp topic(id) do

View file

@ -13,8 +13,8 @@ defmodule Phoenix.Presence.Client.PresenceMock do
{:ok, %{id: id}}
end
def track(pid, topic, key) do
GenServer.cast(pid, {:track, topic, key})
def track(client_pid, pid, topic, key) do
GenServer.cast(pid, {:track, client_pid, topic, key})
end
@impl true
@ -24,8 +24,8 @@ defmodule Phoenix.Presence.Client.PresenceMock do
end
@impl true
def handle_cast({:track, topic, key}, state) do
Client.track(topic, key, %{})
def handle_cast({:track, client_pid, topic, key}, state) do
Client.track(client_pid, topic, key, %{})
{:noreply, state}
end
end