Skip to content

Commit

Permalink
harden monitor test
Browse files Browse the repository at this point in the history
  • Loading branch information
InoMurko committed Mar 22, 2021
1 parent 538b420 commit 304595f
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions apps/omg_watcher/test/omg_watcher/integration/monitor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule OMG.Watcher.MonitorTest do

test "that a child process gets restarted after alarm is cleared" do
child = ChildProcess.prepare_child()
{:ok, monitor_pid} = Monitor.start_link([Alarm, child])
{:ok, monitor_pid} = start_and_attach_a_child([Alarm, child])
app_alarm = Alarm.ethereum_connection_error(__MODULE__)

# the monitor is now started, we raise an alarm and kill it's child
Expand Down Expand Up @@ -85,7 +85,7 @@ defmodule OMG.Watcher.MonitorTest do

test "that a child process does not get restarted if an alarm is cleared but it was not down" do
child = ChildProcess.prepare_child()
{:ok, monitor_pid} = Monitor.start_link([Alarm, child])
{:ok, monitor_pid} = start_and_attach_a_child([Alarm, child])
app_alarm = Alarm.ethereum_connection_error(__MODULE__)
:ok = :alarm_handler.set_alarm(app_alarm)
:erlang.trace(monitor_pid, true, [:receive])
Expand All @@ -102,6 +102,17 @@ defmodule OMG.Watcher.MonitorTest do
assert Process.info(monitor_pid, :links) == {:links, links}
end

defp start_and_attach_a_child(opts) do
case Monitor.start_link(opts) do
{:ok, monitor_pid} ->
{:ok, monitor_pid}

{:error, {{:badmatch, {:error, {:already_started, _}}}, _}} ->
Process.sleep(500)
start_and_attach_a_child(opts)
end
end

defmodule ChildProcess do
@moduledoc """
Mocking a child process to Monitor
Expand Down

0 comments on commit 304595f

Please sign in to comment.