You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2018/07/15 19:01:10 UTC

[GitHub] janl closed pull request #1444: Implement retry_until to fix racy assertions

janl closed pull request #1444: Implement retry_until to fix racy assertions
URL: https://github.com/apache/couchdb/pull/1444
 
 
   

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/basics_test.exs b/test/elixir/test/basics_test.exs
index 40ad4bda12..565c27a5b9 100644
--- a/test/elixir/test/basics_test.exs
+++ b/test/elixir/test/basics_test.exs
@@ -94,7 +94,9 @@ defmodule BasicsTest do
     assert Couch.post("/#{db_name}", [body: %{:_id => "1", :a => 2, :b => 4}]).body["ok"]
     assert Couch.post("/#{db_name}", [body: %{:_id => "2", :a => 3, :b => 9}]).body["ok"]
     assert Couch.post("/#{db_name}", [body: %{:_id => "3", :a => 4, :b => 16}]).body["ok"]
-    assert Couch.get("/#{db_name}").body["doc_count"] == 3
+    retry_until(fn ->
+      Couch.get("/#{db_name}").body["doc_count"] == 3
+    end)
   end
 
   @tag :pending
@@ -145,14 +147,16 @@ defmodule BasicsTest do
     doc0 = Couch.get("/#{db_name}/0").body
     doc0 = Map.put(doc0, :a, 4)
     assert Couch.put("/#{db_name}/0", [body: doc0]).body["ok"]
-    resp = Couch.get("/#{db_name}/_design/foo/_view/baz")
-    assert resp.body["total_rows"] == 2
+    retry_until(fn ->
+      Couch.get("/#{db_name}/_design/foo/_view/baz").body["total_rows"] == 2
+    end)
 
     # Write 2 more docs and test for updated view results
     assert Couch.post("/#{db_name}", [body: %{:a => 3, :b => 9}]).body["ok"]
     assert Couch.post("/#{db_name}", [body: %{:a => 4, :b => 16}]).body["ok"]
-    resp = Couch.get("/#{db_name}/_design/foo/_view/baz")
-    assert resp.body["total_rows"] == 3
+    retry_until(fn ->
+      Couch.get("/#{db_name}/_design/foo/_view/baz").body["total_rows"] == 3
+    end)
     assert Couch.get("/#{db_name}").body["doc_count"] == 8
 
     # Test reduce function
@@ -162,8 +166,9 @@ defmodule BasicsTest do
     # Delete doc and test for updated view results
     doc0 = Couch.get("/#{db_name}/0").body
     assert Couch.delete("/#{db_name}/0?rev=#{doc0["_rev"]}").body["ok"]
-    resp = Couch.get("/#{db_name}/_design/foo/_view/baz")
-    assert resp.body["total_rows"] == 2
+    retry_until(fn ->
+      Couch.get("/#{db_name}/_design/foo/_view/baz").body["total_rows"] == 2
+    end)
     assert Couch.get("/#{db_name}").body["doc_count"] == 7
     assert Couch.get("/#{db_name}/0").status_code == 404
     refute Couch.get("/#{db_name}/0?rev=#{doc0["_rev"]}").status_code == 404
diff --git a/test/elixir/test/test_helper.exs b/test/elixir/test/test_helper.exs
index f84e1a0745..141489a0fe 100644
--- a/test/elixir/test/test_helper.exs
+++ b/test/elixir/test/test_helper.exs
@@ -189,6 +189,28 @@ defmodule CouchTestCase do
           }
         end
       end
+
+      def retry_until(condition, sleep \\ 100, timeout \\ 5000) do
+        retry_until(condition, now(:ms), sleep, timeout)
+      end
+
+      defp retry_until(condition, start, sleep, timeout) do
+        if (now(:ms) > start + timeout) do
+          raise "timed out"
+        else
+          if condition.() do
+            :ok
+          else
+            :timer.sleep(sleep)
+            retry_until(condition, start, sleep, timeout)
+          end
+        end
+      end
+
+      defp now(:ms) do
+        div(:erlang.system_time, 100000)
+      end
+
     end
   end
 end


 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services