diff options
author | Jay Doane <jay.s.doane@gmail.com> | 2018-11-10 12:34:51 -0800 |
---|---|---|
committer | Joan Touzet <wohali@users.noreply.github.com> | 2018-11-10 15:34:51 -0500 |
commit | 844d6fe021121b89d98bf89cf4e9cc1d7e68a270 (patch) | |
tree | fa17162317014af91c77ab1eaef4f7e70557f310 | |
parent | 00b28c265d97df675b725cd68897dc371cbd7168 (diff) | |
download | couchdb-844d6fe021121b89d98bf89cf4e9cc1d7e68a270.tar.gz |
Improve retry_until (#1725)
* Fix bug in epoch millisecond calculation
* Include timeout milliseconds in exception message
* Support assertions in retry_until
Previously, retry_until would only work with boolean conditions.
This change supports assertions in code being retried, which requires
fewer changes to wrap existing code containing assertions.
-rw-r--r-- | test/elixir/test/helper_test.exs | 28 | ||||
-rw-r--r-- | test/elixir/test/test_helper.exs | 22 |
2 files changed, 42 insertions, 8 deletions
diff --git a/test/elixir/test/helper_test.exs b/test/elixir/test/helper_test.exs new file mode 100644 index 000000000..e2070c457 --- /dev/null +++ b/test/elixir/test/helper_test.exs @@ -0,0 +1,28 @@ +defmodule HelperTest do + use CouchTestCase + + @moduledoc """ + Test helper code + """ + + test "retry_until handles boolean conditions", _context do + retry_until fn -> + true + end + end + + test "retry_until handles assertions", _context do + retry_until fn -> + assert true + end + end + + test "retry_until times out", _context do + assert_raise RuntimeError, ~r/^timed out after \d+ ms$/, fn -> + retry_until fn -> + assert false + end, 1, 5 + end + end + +end diff --git a/test/elixir/test/test_helper.exs b/test/elixir/test/test_helper.exs index e8f394345..7e685c837 100644 --- a/test/elixir/test/test_helper.exs +++ b/test/elixir/test/test_helper.exs @@ -200,20 +200,26 @@ defmodule CouchTestCase do end defp retry_until(condition, start, sleep, timeout) do - if (now(:ms) > start + timeout) do - raise "timed out" + now = now(:ms) + if now > start + timeout do + raise "timed out after #{now - start} ms" else - if condition.() do - :ok - else - :timer.sleep(sleep) - retry_until(condition, start, sleep, timeout) + try do + if condition.() do + :ok + else + raise ExUnit.AssertionError + end + rescue + ExUnit.AssertionError -> + :timer.sleep(sleep) + retry_until(condition, start, sleep, timeout) end end end defp now(:ms) do - div(:erlang.system_time, 100000) + div(:erlang.system_time, 1000000) end @spec rev(map(), map()) :: map() |