You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2018/07/15 19:01:11 UTC

[couchdb] branch elixir-suite updated: Implement retry_until to fix racy assertions

This is an automated email from the ASF dual-hosted git repository.

jan pushed a commit to branch elixir-suite
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/elixir-suite by this push:
     new 6f7ce34  Implement retry_until to fix racy assertions
6f7ce34 is described below

commit 6f7ce341f6f6f66d6a05af7fbac2c4edf21a1d04
Author: Jay Doane <ja...@gmail.com>
AuthorDate: Sat Jul 14 11:27:12 2018 -0700

    Implement retry_until to fix racy assertions
---
 test/elixir/test/basics_test.exs | 19 ++++++++++++-------
 test/elixir/test/test_helper.exs | 22 ++++++++++++++++++++++
 2 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/test/elixir/test/basics_test.exs b/test/elixir/test/basics_test.exs
index 40ad4bd..565c27a 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 f84e1a0..141489a 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