Skip to content

Commit

Permalink
Add unit test for the :paused status
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelbej committed Oct 4, 2024
1 parent bfc083a commit f95e791
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/ask/runtime/channel_status_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ defmodule Ask.Runtime.ChannelStatusServer do
Logger.info("ChannelStatusServer: #{message}")
end

defp process_channel_status_change({:down, _messages}, %{status: :paused}, _timestamp, _channel) do
nil
end

defp process_channel_status_change({:down, _messages}, %{status: :down}, _timestamp, _channel) do
nil
end
Expand All @@ -86,6 +90,10 @@ defmodule Ask.Runtime.ChannelStatusServer do
})
end

defp process_channel_status_change({:error, _code}, %{status: :paused}, _timestamp, _channel) do
nil
end

defp process_channel_status_change({:error, _code}, %{status: :error}, _timestamp, _channel) do
nil
end
Expand Down
48 changes: 48 additions & 0 deletions test/ask/runtime/channel_status_server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ defmodule Ask.Runtime.ChannelStatusServerTest do
assert t2
end

test "update channel status" do
{:ok, _pid} = ChannelStatusServer.start_link()
user = insert(:user)

channel =
TestChannel.create_channel(user, "test", TestChannel.settings(TestChannel.new(), 1))

assert ChannelStatusServer.get_channel_status(channel.id) == :unknown

ChannelStatusServer.update_channel_status(channel.id, %{status: :new_channel_status})

assert ChannelStatusServer.get_channel_status(channel.id) == %{status: :new_channel_status}
end

test "sends email when a channel is down and its status was previously :unknown" do
{:ok, pid} = ChannelStatusServer.start_link()
Process.register(self(), :mail_target)
Expand Down Expand Up @@ -144,4 +158,38 @@ defmodule Ask.Runtime.ChannelStatusServerTest do
ChannelStatusServer.poll(pid)
refute_receive [:email, ^email]
end

test "doesn't send email when a channel is down but status was previously :paused" do
{:ok, pid} = ChannelStatusServer.start_link()
Process.register(self(), :mail_target)
user = insert(:user)
survey = insert(:survey, state: :running)

channel =
TestChannel.create_channel(user, "test", TestChannel.settings(TestChannel.new(), 1, :down))

setup_surveys_with_channels([survey], [channel])

ChannelStatusServer.update_channel_status(channel.id, %{status: :paused})

ChannelStatusServer.poll(pid)
refute_receive [:email, _]
end

test "doesn't send email when :error is received but status was previously :paused" do
{:ok, pid} = ChannelStatusServer.start_link()
Process.register(self(), :mail_target)
user = insert(:user)
survey = insert(:survey, state: :running)

channel =
TestChannel.create_channel(user, "test", TestChannel.settings(TestChannel.new(), 1, :error))

setup_surveys_with_channels([survey], [channel])

ChannelStatusServer.update_channel_status(channel.id, %{status: :paused})

ChannelStatusServer.poll(pid)
refute_receive [:email, _]
end
end

0 comments on commit f95e791

Please sign in to comment.