diff --git a/test/supavisor/prom_ex_test.exs b/test/supavisor/prom_ex_test.exs index df684260..e399071b 100644 --- a/test/supavisor/prom_ex_test.exs +++ b/test/supavisor/prom_ex_test.exs @@ -2,6 +2,8 @@ defmodule Supavisor.PromExTest do use ExUnit.Case, async: true use Supavisor.DataCase + import Supavisor.Asserts + alias Supavisor.Monitoring.PromEx @tenant "prom_tenant" @@ -33,7 +35,7 @@ defmodule Supavisor.PromExTest do Process.sleep(3000) - refute PromEx.get_metrics() =~ "tenant=\"#{@tenant}\"" + refute_eventually(fn -> PromEx.get_metrics() =~ "tenant=\"#{@tenant}\"" end) end test "clean_string/1 removes extra spaces from metric string" do diff --git a/test/support/asserts.ex b/test/support/asserts.ex new file mode 100644 index 00000000..d0e720b9 --- /dev/null +++ b/test/support/asserts.ex @@ -0,0 +1,41 @@ +defmodule Supavisor.Asserts do + @doc """ + Asserts that `function` will eventually success. Fails otherwise. + + It performs `repeats` checks with `delay` milliseconds between each check. + """ + def assert_eventually(repeats \\ 5, delay \\ 1000, function) + + def assert_eventually(0, _, _) do + raise ExUnit.AssertionError, message: "Expected function to return truthy value" + end + + def assert_eventually(n, delay, func) do + if func.() do + :ok + else + Process.sleep(delay) + assert_eventually(n - 1, delay, func) + end + end + + @doc """ + Asserts that `function` will eventually fail. Fails otherwise. + + It performs `repeats` checks with `delay` milliseconds between each check. + """ + def refute_eventually(repeats \\ 5, delay \\ 1000, function) + + def refute_eventually(0, _, _) do + raise ExUnit.AssertionError, message: "Expected function to return falsey value" + end + + def refute_eventually(n, delay, func) do + if func.() do + Process.sleep(delay) + refute_eventually(n - 1, delay, func) + else + :ok + end + end +end