wohali closed pull request #1725: Improve retry_until
URL: https://github.com/apache/couchdb/pull/1725
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/test/elixir/test/helper_test.exs b/test/elixir/test/helper_test.exs
new file mode 100644
index 0000000000..e2070c457a
--- /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 e8f3943454..7e685c8370 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()
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services