From 6b5e82013090a6a6061ee2dda0e268a977a00779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berenice=20Medel=20S=C3=A1nchez?= Date: Mon, 13 Dec 2021 12:33:13 -0600 Subject: [PATCH] initial tests added --- config/config.exs | 2 + config/test.exs | 2 + lib/live_beats/application.ex | 2 +- .../presence/phoenix_presence_client.ex | 3 +- .../presence/presence_client_test.exs | 49 +++++++++++++++++++ test/support/presence/client_mock.ex | 17 +++++++ test/support/presence/presence_mock.ex | 31 ++++++++++++ 7 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 test/live_beats/presence/presence_client_test.exs create mode 100644 test/support/presence/client_mock.ex create mode 100644 test/support/presence/presence_mock.ex diff --git a/config/config.exs b/config/config.exs index b937f88..e1ab7f1 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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" diff --git a/config/test.exs b/config/test.exs index 043e354..63469dc 100644 --- a/config/test.exs +++ b/config/test.exs @@ -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 diff --git a/lib/live_beats/application.ex b/lib/live_beats/application.ex index e46cbfe..067f171 100644 --- a/lib/live_beats/application.ex +++ b/lib/live_beats/application.ex @@ -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 diff --git a/lib/live_beats/presence/phoenix_presence_client.ex b/lib/live_beats/presence/phoenix_presence_client.ex index 82c66d1..9baea08 100644 --- a/lib/live_beats/presence/phoenix_presence_client.ex +++ b/lib/live_beats/presence/phoenix_presence_client.ex @@ -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 diff --git a/test/live_beats/presence/presence_client_test.exs b/test/live_beats/presence/presence_client_test.exs new file mode 100644 index 0000000..2ac01e5 --- /dev/null +++ b/test/live_beats/presence/presence_client_test.exs @@ -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 diff --git a/test/support/presence/client_mock.ex b/test/support/presence/client_mock.ex new file mode 100644 index 0000000..6ac611b --- /dev/null +++ b/test/support/presence/client_mock.ex @@ -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 diff --git a/test/support/presence/presence_mock.ex b/test/support/presence/presence_mock.ex new file mode 100644 index 0000000..91126e5 --- /dev/null +++ b/test/support/presence/presence_mock.ex @@ -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