mirror of
https://github.com/fly-apps/live_beats.git
synced 2024-11-24 08:51:00 +00:00
fix tests
This commit is contained in:
parent
84d4eead7a
commit
8dfb96c78a
2 changed files with 53 additions and 29 deletions
|
@ -1,46 +1,70 @@
|
||||||
defmodule Phoenix.Presence.ClientTest do
|
defmodule Phoenix.Presence.ClientTest do
|
||||||
use ExUnit.Case, async: true
|
use ExUnit.Case
|
||||||
|
|
||||||
alias Phoenix.Presence.Client.PresenceMock
|
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
|
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)
|
Phoenix.PubSub.subscribe(@pubsub, topic)
|
||||||
assert Process.alive?(pid)
|
Process.monitor(presence_process)
|
||||||
# _ = :sys.get_state(PresenceClient)
|
|
||||||
:timer.sleep(1000)# not the best
|
|
||||||
|
|
||||||
assert %{topics: %{"mock_topic:100" => %{"1" => [%{phx_ref: _ref}]}}} =
|
PresenceMock.track(presence_client, presence_process, topic, presence_key)
|
||||||
GenServer.call(PresenceClient, :state)
|
|
||||||
|
|
||||||
send(pid, :quit)
|
assert Process.alive?(presence_process)
|
||||||
:timer.sleep(1000)
|
|
||||||
refute Process.alive?(pid)
|
assert_receive %{event: "presence_diff"}
|
||||||
|
|
||||||
|
client_state = :sys.get_state(presence_client)
|
||||||
|
|
||||||
|
assert %{topics: %{^topic => %{"1" => [%{phx_ref: _ref}]}}} = client_state
|
||||||
end
|
end
|
||||||
|
|
||||||
test "topic is removed from the topics state when there is no more presences" do
|
test "topic is removed from the topics state when there is no more presences" do
|
||||||
presence_key = 1
|
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)
|
Phoenix.PubSub.subscribe(@pubsub, topic)
|
||||||
assert Process.alive?(pid)
|
Process.monitor(presence_process)
|
||||||
# _ = :sys.get_state(PresenceClient)
|
|
||||||
|
|
||||||
:timer.sleep(1000)# not the best
|
PresenceMock.track(presence_client, presence_process, topic, presence_key)
|
||||||
|
|
||||||
assert %{topics: %{"mock_topic:100" => %{"1" => [%{phx_ref: _ref}]}}} =
|
assert Process.alive?(presence_process)
|
||||||
GenServer.call(PresenceClient, :state)
|
|
||||||
|
|
||||||
send(pid, :quit)
|
assert_receive %{event: "presence_diff"}
|
||||||
:timer.sleep(1000)
|
|
||||||
refute Process.alive?(pid)
|
client_state = :sys.get_state(presence_client)
|
||||||
assert %{topics: %{}} = GenServer.call(PresenceClient, :state)
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
defp topic(id) do
|
defp topic(id) do
|
||||||
|
|
|
@ -13,8 +13,8 @@ defmodule Phoenix.Presence.Client.PresenceMock do
|
||||||
{:ok, %{id: id}}
|
{:ok, %{id: id}}
|
||||||
end
|
end
|
||||||
|
|
||||||
def track(pid, topic, key) do
|
def track(client_pid, pid, topic, key) do
|
||||||
GenServer.cast(pid, {:track, topic, key})
|
GenServer.cast(pid, {:track, client_pid, topic, key})
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
@ -24,8 +24,8 @@ defmodule Phoenix.Presence.Client.PresenceMock do
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_cast({:track, topic, key}, state) do
|
def handle_cast({:track, client_pid, topic, key}, state) do
|
||||||
Client.track(topic, key, %{})
|
Client.track(client_pid, topic, key, %{})
|
||||||
{:noreply, state}
|
{:noreply, state}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue