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

jiahuili430 pushed a commit to branch add-retry-until-for-search_test.exs
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit c9c5fa50abeb858bdc466eb68acf37833d9359a8
Author: Jiahui Li <[email protected]>
AuthorDate: Sat Apr 11 15:12:27 2026 -0500

    Add `retry_until()` to fix flaky elixir search tests
    
    When running Elixir search tests in Clouseau GitHub Actions CI, some tests 
got `(KeyError) key :status_code not found in: 
%HTTPotion.ErrorResponse{message: "req_timedout"}`. Adding `retry_until()` to 
mitigate this.
---
 test/elixir/test/partition_search_test.exs | 161 +++++++++++++----------
 test/elixir/test/search_test.exs           | 200 +++++++++++++++++------------
 2 files changed, 212 insertions(+), 149 deletions(-)

diff --git a/test/elixir/test/partition_search_test.exs 
b/test/elixir/test/partition_search_test.exs
index 9310e701d..f74efe093 100644
--- a/test/elixir/test/partition_search_test.exs
+++ b/test/elixir/test/partition_search_test.exs
@@ -56,16 +56,20 @@ defmodule PartitionSearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_partition/foo/_design/library/_search/books"
-    resp = Couch.get(url, query: %{q: "some:field"})
-    assert_on_status(resp, 200, "Fail to do partitioned search.")
-    ids = get_ids(resp)
-    assert ids == ["foo:10", "foo:2", "foo:4", "foo:6", "foo:8"]
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "some:field"})
+      assert_on_status(resp, 200, "Fail to do partitioned search.")
+      ids = get_ids(resp)
+      assert ids == ["foo:10", "foo:2", "foo:4", "foo:6", "foo:8"]
+    end)
 
     url = "/#{db_name}/_partition/bar/_design/library/_search/books"
-    resp = Couch.get(url, query: %{q: "some:field"})
-    assert_on_status(resp, 200, "Fail to do partitioned search.")
-    ids = get_ids(resp)
-    assert ids == ["bar:1", "bar:3", "bar:5", "bar:7", "bar:9"]
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "some:field"})
+      assert_on_status(resp, 200, "Fail to do partitioned search.")
+      ids = get_ids(resp)
+      assert ids == ["bar:1", "bar:3", "bar:5", "bar:7", "bar:9"]
+    end)
   end
 
   @tag :with_partitioned_db
@@ -75,10 +79,12 @@ defmodule PartitionSearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_partition/foo/_design/library/_search/books"
-    resp = Couch.get(url, query: %{q: "some:field"})
-    assert_on_status(resp, 200, "Fail to do partitioned search.")
-    ids = get_ids(resp)
-    assert ids == ["foo:10", "foo:2", "foo:4", "foo:6", "foo:8"]
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "some:field"})
+      assert_on_status(resp, 200, "Fail to do partitioned search.")
+      ids = get_ids(resp)
+      assert ids == ["foo:10", "foo:2", "foo:4", "foo:6", "foo:8"]
+    end)
   end
 
   @tag :with_partitioned_db
@@ -88,25 +94,26 @@ defmodule PartitionSearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_partition/foo/_design/library/_search/books"
-    resp = Couch.get(url, query: %{q: "some:field", limit: 3})
-    assert_on_status(resp, 200, "Fail to do partitioned search.")
-    ids = get_ids(resp)
-    assert ids == ["foo:10", "foo:2", "foo:4"]
-
-    %{:body => %{"bookmark" => bookmark}} = resp
-
-    resp = Couch.get(url, query: %{q: "some:field", limit: 3, bookmark: 
bookmark})
-    assert_on_status(resp, 200, "Fail to do partitioned search with a 
bookmark.")
-    ids = get_ids(resp)
-    assert ids == ["foo:6", "foo:8"]
-
-    resp = Couch.get(url, query: %{q: "some:field", limit: 2000, bookmark: 
bookmark})
-    assert_on_status(resp, 200, "Fail to do partition search with an upper 
bound on the limit.")
-    ids = get_ids(resp)
-    assert ids == ["foo:6", "foo:8"]
-
-    resp = Couch.get(url, query: %{q: "some:field", limit: 2001, bookmark: 
bookmark})
-    assert_on_status(resp, 400, "Should fail to do partition search with over 
limit.")
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "some:field", limit: 3})
+      assert_on_status(resp, 200, "Fail to do partitioned search.")
+      ids = get_ids(resp)
+      assert ids == ["foo:10", "foo:2", "foo:4"]
+      %{:body => %{"bookmark" => bookmark}} = resp
+
+      resp = Couch.get(url, query: %{q: "some:field", limit: 3, bookmark: 
bookmark})
+      assert_on_status(resp, 200, "Fail to do partitioned search with a 
bookmark.")
+      ids = get_ids(resp)
+      assert ids == ["foo:6", "foo:8"]
+
+      resp = Couch.get(url, query: %{q: "some:field", limit: 2000, bookmark: 
bookmark})
+      assert_on_status(resp, 200, "Fail to do partition search with an upper 
bound on the limit.")
+      ids = get_ids(resp)
+      assert ids == ["foo:6", "foo:8"]
+
+      resp = Couch.get(url, query: %{q: "some:field", limit: 2001, bookmark: 
bookmark})
+      assert_on_status(resp, 400, "Should fail to do partition search with 
over limit.")
+    end)
   end
 
   @tag :with_db
@@ -116,8 +123,10 @@ defmodule PartitionSearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_design/library/_search/books"
-    resp = Couch.post(url, body: %{:q => "some:field", :limit => 1})
-    assert_on_status(resp, 200, "Fail to do POST for non-partitioned db with 
limit.")
+    retry_until(fn ->
+      resp = Couch.post(url, body: %{:q => "some:field", :limit => 1})
+      assert_on_status(resp, 200, "Fail to do POST for non-partitioned db with 
limit.")
+    end)
   end
 
   @tag :with_partitioned_db
@@ -127,8 +136,10 @@ defmodule PartitionSearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_partition/foo/_design/library/_search/books"
-    resp = Couch.post(url, body: %{:q => "some:field", :limit => 1})
-    assert_on_status(resp, 200, "Fail to do POST for partitioned db with 
limit.")
+    retry_until(fn ->
+      resp = Couch.post(url, body: %{:q => "some:field", :limit => 1})
+      assert_on_status(resp, 200, "Fail to do POST for partitioned db with 
limit.")
+    end)
   end
 
   @tag :with_partitioned_db
@@ -138,10 +149,12 @@ defmodule PartitionSearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_design/library/_search/books"
-    resp = Couch.get(url, query: %{q: "some:field"})
-    assert_on_status(resp, 400, "Expected a failure to do a global query on 
partitioned view.")
-    %{:body => %{"reason" => reason}} = resp
-    assert Regex.match?(~r/mandatory for queries to this index./, reason)
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "some:field"})
+      assert_on_status(resp, 400, "Expected a failure to do a global query on 
partitioned view.")
+      %{:body => %{"reason" => reason}} = resp
+      assert Regex.match?(~r/mandatory for queries to this index./, reason)
+    end)
   end
 
   @tag :with_partitioned_db
@@ -151,10 +164,12 @@ defmodule PartitionSearchTest do
     create_ddoc(db_name, options: %{partitioned: false})
 
     url = "/#{db_name}/_partition/foo/_design/library/_search/books"
-    resp = Couch.get(url, query: %{q: "some:field"})
-    assert_on_status(resp, 400, "Expected a failure to do a query with a 
global search ddoc.")
-    %{:body => %{"reason" => reason}} = resp
-    assert reason == "`partition` not supported on this index"
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "some:field"})
+      assert_on_status(resp, 400, "Expected a failure to do a query with a 
global search ddoc.")
+      %{:body => %{"reason" => reason}} = resp
+      assert reason == "`partition` not supported on this index"
+    end)
   end
 
   @tag :with_db
@@ -164,10 +179,12 @@ defmodule PartitionSearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_design/library/_search/books"
-    resp = Couch.get(url, query: %{q: "some:field"})
-    assert_on_status(resp, 200, "Failed to search on non-partitioned dbs.")
-    ids = get_ids(resp)
-    assert Enum.sort(ids) == Enum.sort(["bar:1", "bar:5", "bar:9", "foo:2", 
"bar:3", "foo:4", "foo:6", "bar:7", "foo:8", "foo:10"])
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "some:field"})
+      assert_on_status(resp, 200, "Failed to search on non-partitioned dbs.")
+      ids = get_ids(resp)
+      assert Enum.sort(ids) == Enum.sort(["bar:1", "bar:5", "bar:9", "foo:2", 
"bar:3", "foo:4", "foo:6", "bar:7", "foo:8", "foo:10"])
+    end)
   end
 
   @tag :with_db
@@ -177,10 +194,12 @@ defmodule PartitionSearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_design/library/_search/books"
-    resp = Couch.get(url, query: %{q: "some:field"})
-    assert_on_status(resp, 200, "Failed to search on non-partitioned dbs 
without the limit.")
-    ids = get_ids(resp)
-    assert Enum.sort(ids) == Enum.sort(["bar:1", "bar:5", "bar:9", "foo:2", 
"bar:3", "foo:4", "foo:6", "bar:7", "foo:8", "foo:10"])
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "some:field"})
+      assert_on_status(resp, 200, "Failed to search on non-partitioned dbs 
without the limit.")
+      ids = get_ids(resp)
+      assert Enum.sort(ids) == Enum.sort(["bar:1", "bar:5", "bar:9", "foo:2", 
"bar:3", "foo:4", "foo:6", "bar:7", "foo:8", "foo:10"])
+    end)
   end
 
   @tag :with_db
@@ -192,14 +211,16 @@ defmodule PartitionSearchTest do
     url = "/#{db_name}/_design/library/_search/books"
 
     # score order varies by Lucene version, so captured this order first.
-    resp = Couch.get(url, query: %{q: "some:field"})
-    assert_on_status(resp, 200, "Failed to search on non-partitioned dbs 
without the limit.")
-    expected_ids = get_ids(resp)
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "some:field"})
+      assert_on_status(resp, 200, "Failed to search on non-partitioned dbs 
without the limit.")
+      expected_ids = get_ids(resp)
 
     # Assert that the limit:3 results are the first 3 results from the 
unlimited search
-    resp = Couch.get(url, query: %{q: "some:field", limit: 3})
-    assert_on_status(resp, 200, "Failed to search on non-partitioned dbs with 
the limit.")
-    assert List.starts_with?(expected_ids, get_ids(resp))
+      resp = Couch.get(url, query: %{q: "some:field", limit: 3})
+      assert_on_status(resp, 200, "Failed to search on non-partitioned dbs 
with the limit.")
+      assert List.starts_with?(expected_ids, get_ids(resp))
+    end)
   end
 
   @tag :with_db
@@ -209,8 +230,10 @@ defmodule PartitionSearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_design/library/_search/books"
-    resp = Couch.get(url, query: %{q: "some:field", limit: 201})
-    assert_on_status(resp, 400, "Expected a failure on non-partitioned dbs 
with over limit.")
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "some:field", limit: 201})
+      assert_on_status(resp, 400, "Expected a failure on non-partitioned dbs 
with over limit.")
+    end)
   end
 
   @tag :with_partitioned_db
@@ -220,8 +243,10 @@ defmodule PartitionSearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_partition/foo/_design/library/_search/books"
-    resp = Couch.post(url, body: %{q: "some:field", partition: "bar"})
-    assert_on_status(resp, 400, "Expected a failure on conflicting partition 
values.")
+    retry_until(fn ->
+      resp = Couch.post(url, body: %{q: "some:field", partition: "bar"})
+      assert_on_status(resp, 400, "Expected a failure on conflicting partition 
values.")
+    end)
   end
 
   @tag :with_partitioned_db
@@ -242,12 +267,14 @@ defmodule PartitionSearchTest do
       fn {key, value} ->
         url = "/#{db_name}/_partition/foo/_design/library/_search/books"
         bannedparam = Map.put(body, key, value)
-        get_resp = Couch.get(url, query: bannedparam)
-        %{:body => %{"reason" => get_reason}} = get_resp
-        assert Regex.match?(~r/are incompatible/, get_reason)
-        post_resp = Couch.post(url, body: bannedparam)
-        %{:body => %{"reason" => post_reason}} = post_resp
-        assert Regex.match?(~r/are incompatible/, post_reason)
+        retry_until(fn ->
+          get_resp = Couch.get(url, query: bannedparam)
+          %{:body => %{"reason" => get_reason}} = get_resp
+          assert Regex.match?(~r/are incompatible/, get_reason)
+          post_resp = Couch.post(url, body: bannedparam)
+          %{:body => %{"reason" => post_reason}} = post_resp
+          assert Regex.match?(~r/are incompatible/, post_reason)
+        end)
       end
     )
   end
diff --git a/test/elixir/test/search_test.exs b/test/elixir/test/search_test.exs
index edf6858cf..cb5801671 100644
--- a/test/elixir/test/search_test.exs
+++ b/test/elixir/test/search_test.exs
@@ -9,19 +9,18 @@ defmodule SearchTest do
   """
 
   def create_search_docs(db_name) do
-    resp = Couch.post("/#{db_name}/_bulk_docs",
-      headers: ["Content-Type": "application/json"],
-      body: %{:docs => [
-                %{"item" => "apple",  "place" => "kitchen", "state" => "new",  
   "price" => 0.99},
-                %{"item" => "banana", "place" => "kitchen", "state" => "new",  
   "price" => 1.49},
-                %{"item" => "carrot", "place" => "kitchen", "state" => "old",  
   "price" => 0.75},
-                %{"item" => "date",   "place" => "lobby",   "state" => 
"unknown", "price" => 1.25},
-      ]}
-    )
-    assert resp.status_code in [201, 202],
-      "Cannot create search docs. " <>
-      "Expected one of [201, 202], got: #{resp.status_code}, body: #{inspect 
resp.body}"
-
+      resp = Couch.post("/#{db_name}/_bulk_docs",
+        headers: ["Content-Type": "application/json"],
+        body: %{:docs => [
+                  %{"item" => "apple",  "place" => "kitchen", "state" => 
"new",     "price" => 0.99},
+                  %{"item" => "banana", "place" => "kitchen", "state" => 
"new",     "price" => 1.49},
+                  %{"item" => "carrot", "place" => "kitchen", "state" => 
"old",     "price" => 0.75},
+                  %{"item" => "date",   "place" => "lobby",   "state" => 
"unknown", "price" => 1.25},
+        ]}
+      )
+      assert resp.status_code in [201, 202],
+             "Cannot create search docs. " <>
+             "Expected one of [201, 202], got: #{resp.status_code}, body: 
#{inspect resp.body}"
   end
 
   def create_ddoc(db_name, opts \\ %{}) do
@@ -74,10 +73,13 @@ defmodule SearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_design/inventory/_search/fruits"
-    resp = Couch.get(url, query: %{q: "*:*", include_docs: true})
-    assert_on_status(resp, 200, "Fail to do search.")
-    ids = get_items(resp)
-    assert Enum.sort(ids) == Enum.sort(["apple", "banana", "carrot", "date"])
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "*:*", include_docs: true})
+      assert_on_status(resp, 200, "Fail to do search.")
+
+      ids = get_items(resp)
+      assert Enum.sort(ids) == Enum.sort(["apple", "banana", "carrot", "date"])
+    end)
   end
 
   @tag :with_db
@@ -87,11 +89,13 @@ defmodule SearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_design/inventory/_search/fruits"
-    resp = Couch.get(url, query: %{q: "*:*", drilldown: 
:jiffy.encode(["place", "kitchen"]), include_docs: true})
-    assert_on_status(resp, 200, "Fail to do search.")
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "*:*", drilldown: 
:jiffy.encode(["place", "kitchen"]), include_docs: true})
+      assert_on_status(resp, 200, "Fail to do search.")
 
-    ids = get_items(resp)
-    assert Enum.sort(ids) == Enum.sort(["apple", "banana", "carrot"])
+      ids = get_items(resp)
+      assert Enum.sort(ids) == Enum.sort(["apple", "banana", "carrot"])
+    end)
   end
 
   @tag :with_db
@@ -101,8 +105,10 @@ defmodule SearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_design/inventory/_search/fruits"
-    resp = Couch.get(url, query: %{q: "*:*", drilldown: 
:jiffy.encode(["state", "new", "unknown"]), include_docs: true})
-    assert_on_status(resp, 200, "Fail to do search.")
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "*:*", drilldown: 
:jiffy.encode(["state", "new", "unknown"]), include_docs: true})
+      assert_on_status(resp, 200, "Fail to do search.")
+    end)
 
     ids = get_items(resp)
     assert Enum.sort(ids) == Enum.sort(["apple", "banana", "date"])
@@ -115,11 +121,13 @@ defmodule SearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_design/inventory/_search/fruits"
-    resp = Couch.get(url, query: %{q: "*:*", drilldown: 
:jiffy.encode([["state", "old"], ["item", "apple"]]), include_docs: true})
-    assert_on_status(resp, 200, "Fail to do search.")
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "*:*", drilldown: 
:jiffy.encode([["state", "old"], ["item", "apple"]]), include_docs: true})
+      assert_on_status(resp, 200, "Fail to do search.")
 
-    ids = get_items(resp)
-    assert Enum.sort(ids) == []
+      ids = get_items(resp)
+      assert Enum.sort(ids) == []
+    end)
   end
 
   @tag :with_db
@@ -129,8 +137,10 @@ defmodule SearchTest do
     create_ddoc(db_name)
 
     url = 
"/#{db_name}/_design/inventory/_search/fruits?q=*:*&drilldown=[\"state\",\"old\"]&drilldown=[\"item\",\"apple\"]&include_docs=true"
-    resp = Couch.get(url)
-    assert_on_status(resp, 200, "Fail to do search.")
+    retry_until(fn ->
+      resp = Couch.get(url)
+      assert_on_status(resp, 200, "Fail to do search.")
+    end)
 
     ids = get_items(resp)
     assert Enum.sort(ids) == []
@@ -144,11 +154,13 @@ defmodule SearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_design/inventory/_search/fruits"
-    resp = Couch.post(url, body: %{q: "*:*", include_docs: true})
-    assert_on_status(resp, 200, "Fail to do search.")
+    retry_until(fn ->
+      resp = Couch.post(url, body: %{q: "*:*", include_docs: true})
+      assert_on_status(resp, 200, "Fail to do search.")
 
-    ids = get_items(resp)
-    assert Enum.sort(ids) == Enum.sort(["apple", "banana", "carrot", "date"])
+      ids = get_items(resp)
+      assert Enum.sort(ids) == Enum.sort(["apple", "banana", "carrot", "date"])
+    end)
   end
 
   @tag :with_db
@@ -158,8 +170,10 @@ defmodule SearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_design/inventory/_search/fruits"
-    resp = Couch.post(url, body: %{query: "*:*", drilldown: ["place", 
"kitchen"], include_docs: true})
-    assert_on_status(resp, 200, "Fail to do search.")
+    retry_until(fn ->
+      resp = Couch.post(url, body: %{query: "*:*", drilldown: ["place", 
"kitchen"], include_docs: true})
+      assert_on_status(resp, 200, "Fail to do search.")
+    end)
 
     ids = get_items(resp)
     assert Enum.sort(ids) == Enum.sort(["apple", "banana", "carrot"])
@@ -172,8 +186,10 @@ defmodule SearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_design/inventory/_search/fruits"
-    resp = Couch.post(url, body: %{query: "*:*", drilldown: ["state", "new", 
"unknown"], include_docs: true})
-    assert_on_status(resp, 200, "Fail to do search.")
+    retry_until(fn ->
+      resp = Couch.post(url, body: %{query: "*:*", drilldown: ["state", "new", 
"unknown"], include_docs: true})
+      assert_on_status(resp, 200, "Fail to do search.")
+    end)
 
     ids = get_items(resp)
     assert Enum.sort(ids) == Enum.sort(["apple", "banana", "date"])
@@ -186,11 +202,13 @@ defmodule SearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_design/inventory/_search/fruits"
-    resp = Couch.post(url, body: %{q: "*:*", drilldown: [["state", "old"], 
["item", "apple"]], include_docs: true})
-    assert_on_status(resp, 200, "Fail to do search.")
+    retry_until(fn ->
+      resp = Couch.post(url, body: %{q: "*:*", drilldown: [["state", "old"], 
["item", "apple"]], include_docs: true})
+      assert_on_status(resp, 200, "Fail to do search.")
 
-    ids = get_items(resp)
-    assert Enum.sort(ids) == []
+      ids = get_items(resp)
+      assert Enum.sort(ids) == []
+    end)
   end
 
   @tag :with_db
@@ -200,11 +218,13 @@ defmodule SearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_design/inventory/_search/fruits"
-    resp = Couch.post(url, body: %{q: "*:*", drilldown: [["place", "kitchen"], 
["state", "new"], ["item", "apple"]], include_docs: true})
-    assert_on_status(resp, 200, "Fail to do search.")
+    retry_until(fn ->
+      resp = Couch.post(url, body: %{q: "*:*", drilldown: [["place", 
"kitchen"], ["state", "new"], ["item", "apple"]], include_docs: true})
+      assert_on_status(resp, 200, "Fail to do search.")
 
-    ids = get_items(resp)
-    assert Enum.sort(ids) == ["apple"]
+      ids = get_items(resp)
+      assert Enum.sort(ids) == ["apple"]
+    end)
   end
 
   @tag :with_db
@@ -214,11 +234,13 @@ defmodule SearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_design/inventory/_search/fruits"
-    resp = Couch.post(url, body: %{q: "*:*", drilldown: [["state", "old", 
"new"], ["item", "apple"]], include_docs: true})
-    assert_on_status(resp, 200, "Fail to do search.")
+    retry_until(fn ->
+      resp = Couch.post(url, body: %{q: "*:*", drilldown: [["state", "old", 
"new"], ["item", "apple"]], include_docs: true})
+      assert_on_status(resp, 200, "Fail to do search.")
 
-    ids = get_items(resp)
-    assert Enum.sort(ids) == ["apple"]
+      ids = get_items(resp)
+      assert Enum.sort(ids) == ["apple"]
+    end)
   end
 
   @tag :with_db
@@ -228,11 +250,13 @@ defmodule SearchTest do
     create_ddoc(db_name)
 
     url = "/#{db_name}/_design/inventory/_search/fruits"
-    resp = Couch.post(url, body: "{\"include_docs\": true, \"q\": \"*:*\", 
\"drilldown\": [\"state\", \"old\"], \"drilldown\": [\"item\", \"apple\"]}")
-    assert_on_status(resp, 200, "Fail to do search.")
+    retry_until(fn ->
+      resp = Couch.post(url, body: "{\"include_docs\": true, \"q\": \"*:*\", 
\"drilldown\": [\"state\", \"old\"], \"drilldown\": [\"item\", \"apple\"]}")
+      assert_on_status(resp, 200, "Fail to do search.")
 
-    ids = get_items(resp)
-    assert Enum.sort(ids) == ["apple"]
+      ids = get_items(resp)
+      assert Enum.sort(ids) == ["apple"]
+    end)
   end
 
   @tag :with_db
@@ -242,8 +266,10 @@ defmodule SearchTest do
     create_ddoc(db_name)
     create_invalid_ddoc(db_name)
 
-    resp = Couch.post("/#{db_name}/_search_cleanup")
-    assert_on_status(resp, [201, 202], "Fail to do a _search_cleanup.")
+    retry_until(fn ->
+      resp = Couch.post("/#{db_name}/_search_cleanup")
+      assert_on_status(resp, [201, 202], "Fail to do a _search_cleanup.")
+    end)
   end
 
   @tag :with_db
@@ -254,11 +280,13 @@ defmodule SearchTest do
 
     url = "/#{db_name}/_design/inventory/_search/fruits"
     counts = ["place"]
-    resp = Couch.get(url, query: %{q: "*:*", limit: 0, counts: 
:jiffy.encode(counts)})
-    assert_on_status(resp, 200, "Fail to do search.")
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "*:*", limit: 0, counts: 
:jiffy.encode(counts)})
+      assert_on_status(resp, 200, "Fail to do search.")
 
-    %{:body => %{"counts" => counts}} = resp
-    assert counts == %{"place" => %{"kitchen" => 3, "lobby" => 1}}
+      %{:body => %{"counts" => counts}} = resp
+      assert counts == %{"place" => %{"kitchen" => 3, "lobby" => 1}}
+    end)
   end
 
   @tag :with_db
@@ -269,11 +297,13 @@ defmodule SearchTest do
 
     url = "/#{db_name}/_design/inventory/_search/fruits"
     counts = ["place"]
-    resp = Couch.get(url, query: %{q: "item:tomato", limit: 0, counts: 
:jiffy.encode(counts)})
-    assert_on_status(resp, 200, "Fail to do search.")
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "item:tomato", limit: 0, counts: 
:jiffy.encode(counts)})
+      assert_on_status(resp, 200, "Fail to do search.")
 
-    %{:body => %{"counts" => counts}} = resp
-    assert counts == %{"place" => %{}}
+      %{:body => %{"counts" => counts}} = resp
+      assert counts == %{"place" => %{}}
+    end)
   end
 
   @tag :with_db
@@ -284,11 +314,13 @@ defmodule SearchTest do
 
     url = "/#{db_name}/_design/inventory/_search/fruits"
     ranges = %{"price" => %{"cheap" => "[0 TO 0.99]", "expensive" => "[1.00 TO 
Infinity]"}}
-    resp = Couch.get(url, query: %{q: "*:*", limit: 0, ranges: 
:jiffy.encode(ranges)})
-    assert_on_status(resp, 200, "Fail to do search.")
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "*:*", limit: 0, ranges: 
:jiffy.encode(ranges)})
+      assert_on_status(resp, 200, "Fail to do search.")
 
-    %{:body => %{"ranges" => ranges}} = resp
-    assert ranges == %{"price" => %{"cheap" => 2, "expensive" => 2}}
+      %{:body => %{"ranges" => ranges}} = resp
+      assert ranges == %{"price" => %{"cheap" => 2, "expensive" => 2}}
+    end)
   end
 
   @tag :with_db
@@ -299,11 +331,13 @@ defmodule SearchTest do
 
     url = "/#{db_name}/_design/inventory/_search/fruits"
     ranges = %{"price" => %{}}
-    resp = Couch.get(url, query: %{q: "*:*", limit: 0, ranges: 
:jiffy.encode(ranges)})
-    assert_on_status(resp, 200, "Fail to do search.")
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "*:*", limit: 0, ranges: 
:jiffy.encode(ranges)})
+      assert_on_status(resp, 200, "Fail to do search.")
 
-    %{:body => %{"ranges" => ranges}} = resp
-    assert ranges == %{"price" => %{}}
+      %{:body => %{"ranges" => ranges}} = resp
+      assert ranges == %{"price" => %{}}
+    end)
   end
 
   @tag :with_db
@@ -314,17 +348,19 @@ defmodule SearchTest do
 
     url = "/#{db_name}/_design/inventory/_search/fruits"
     ranges = %{"price" => %{}}
-    resp = Couch.get(url, query: %{q: "*:*", group_field: "state"})
-    assert_on_status(resp, 200, "Fail to do search.")
-
-    %{:body => %{"groups" => groups}} = resp
-    assert length(groups) == 3
-    Enum.each(groups, fn g ->
-      case g["by"] do
-        "new" -> assert g["total_rows"] == 2
-        "old" -> assert g["total_rows"] == 1
-        "unknown" -> assert g["total_rows"] == 1
-      end
+    retry_until(fn ->
+      resp = Couch.get(url, query: %{q: "*:*", group_field: "state"})
+      assert_on_status(resp, 200, "Fail to do search.")
+
+      %{:body => %{"groups" => groups}} = resp
+      assert length(groups) == 3
+      Enum.each(groups, fn g ->
+        case g["by"] do
+          "new" -> assert g["total_rows"] == 2
+          "old" -> assert g["total_rows"] == 1
+          "unknown" -> assert g["total_rows"] == 1
+        end
+      end)
     end)
   end
 

Reply via email to