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

vatamane pushed a commit to branch reshard
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/reshard by this push:
     new c106248  [fixup|tests] Elixir all_docs and changes feed tests
c106248 is described below

commit c106248d64ee0be229334f27c7a8ce868ffae787
Author: Nick Vatamaniuc <[email protected]>
AuthorDate: Wed Mar 13 19:11:39 2019 -0400

    [fixup|tests] Elixir all_docs and changes feed tests
---
 test/elixir/test/reshard_all_docs_test.exs | 79 +++++++++++++++++++++++++++++
 test/elixir/test/reshard_basic_test.exs    | 23 +++++----
 test/elixir/test/reshard_changes_feed.exs  | 81 ++++++++++++++++++++++++++++++
 test/elixir/test/reshard_helpers.exs       |  4 ++
 4 files changed, 177 insertions(+), 10 deletions(-)

diff --git a/test/elixir/test/reshard_all_docs_test.exs 
b/test/elixir/test/reshard_all_docs_test.exs
new file mode 100644
index 0000000..550c7a4
--- /dev/null
+++ b/test/elixir/test/reshard_all_docs_test.exs
@@ -0,0 +1,79 @@
+defmodule ReshardAllDocsTest do
+  use CouchTestCase
+  import ReshardHelpers
+
+  @moduledoc """
+  Test _all_docs interaction with resharding
+  """
+
+  setup do
+    db = random_db_name()
+    {:ok, _} = create_db(db, query: %{q: 2})
+
+    on_exit(fn ->
+      reset_reshard_state()
+      delete_db(db)
+    end)
+
+    {:ok, [db: db]}
+  end
+
+  test "all_docs after splitting all shards on node1", context do
+    db = context[:db]
+    node1 = get_node1()
+    docs = add_docs(1..100, db)
+
+    before_split_all_docs = all_docs(db)
+    assert docs == before_split_all_docs
+
+    resp = post_job_node(db, node1)
+    assert resp.status_code == 201
+    jobid = hd(resp.body)["id"]
+    wait_job_completed(jobid)
+
+    assert before_split_all_docs == all_docs(db)
+
+    assert remove_job(jobid).status_code == 200
+  end
+
+  test "all_docs after splitting the same range on all nodes", context do
+    db = context[:db]
+    docs = add_docs(1..100, db)
+
+    before_split_all_docs = all_docs(db)
+    assert docs == before_split_all_docs
+
+    resp = post_job_range(db, "00000000-7fffffff")
+    assert resp.status_code == 201
+
+    resp.body
+    |> Enum.map(fn j -> j["id"] end)
+    |> Enum.each(fn id -> wait_job_completed(id) end)
+
+    assert before_split_all_docs == all_docs(db)
+
+    get_jobs()
+    |> Enum.map(fn j -> j["id"] end)
+    |> Enum.each(fn id -> remove_job(id) end)
+  end
+
+  defp add_docs(range, db) do
+    docs = create_docs(range)
+    w3 = %{:w => 3}
+    resp = Couch.post("/#{db}/_bulk_docs", body: %{docs: docs}, query: w3)
+    assert resp.status_code == 201
+    assert length(resp.body) == length(docs)
+
+    docs
+    |> rev(resp.body)
+    |> Enum.into(%{}, fn %{:_id => id, :_rev => rev} -> {id, rev} end)
+  end
+
+  defp all_docs(db, query \\ %{}) do
+    resp = Couch.get("/#{db}/_all_docs", query: query)
+    assert resp.status_code == 200
+
+    resp.body["rows"]
+    |> Enum.into(%{}, fn %{"id" => id, "value" => v} -> {id, v["rev"]} end)
+  end
+end
diff --git a/test/elixir/test/reshard_basic_test.exs 
b/test/elixir/test/reshard_basic_test.exs
index 99cdc47..787554e 100644
--- a/test/elixir/test/reshard_basic_test.exs
+++ b/test/elixir/test/reshard_basic_test.exs
@@ -64,11 +64,11 @@ defmodule ReshardBasicTest do
     assert get_state() == %{"state" => "running", "reason" => :null}
   end
 
-  test "split db1 (q=1) shards on node1", context do
-    db1 = context[:db1]
+  test "split q=1 db shards on node1 (1 job)", context do
+    db = context[:db1]
     node1 = get_node1()
 
-    resp = post_job_node(db1, node1)
+    resp = post_job_node(db, node1)
     assert resp.status_code == 201
 
     body = resp.body
@@ -106,7 +106,7 @@ defmodule ReshardBasicTest do
     assert body["job_state"] == "completed"
     assert body["split_state"] == "completed"
 
-    resp = Couch.get("/#{db1}/_shards")
+    resp = Couch.get("/#{db}/_shards")
     assert resp.status_code == 200
     shards = resp.body["shards"]
     assert node1 not in shards["00000000-ffffffff"]
@@ -127,11 +127,11 @@ defmodule ReshardBasicTest do
     assert summary["completed"] == 0
   end
 
-  test "split db2 (q=2) shards on node1", context do
-    db2 = context[:db2]
+  test "split q=2 shards on node1 (2 jobs)", context do
+    db = context[:db2]
     node1 = get_node1()
 
-    resp = post_job_node(db2, node1)
+    resp = post_job_node(db, node1)
     assert resp.status_code == 201
 
     body = resp.body
@@ -153,7 +153,7 @@ defmodule ReshardBasicTest do
     summary = get_summary()
     assert summary["completed"] == 2
 
-    resp = Couch.get("/#{db2}/_shards")
+    resp = Couch.get("/#{db}/_shards")
     assert resp.status_code == 200
     shards = resp.body["shards"]
     assert node1 not in shards["00000000-7fffffff"]
@@ -163,8 +163,11 @@ defmodule ReshardBasicTest do
     assert shards["80000000-bfffffff"] == [node1]
     assert shards["c0000000-ffffffff"] == [node1]
 
-    remove_job(id1)
-    remove_job(id2)
+    # deleting the source db should remove the jobs
+    delete_db(db)
+    wait_job_removed(id1)
+    wait_job_removed(id2)
+
     summary = get_summary()
     assert summary["total"] == 0
   end
diff --git a/test/elixir/test/reshard_changes_feed.exs 
b/test/elixir/test/reshard_changes_feed.exs
new file mode 100644
index 0000000..a4a39fe
--- /dev/null
+++ b/test/elixir/test/reshard_changes_feed.exs
@@ -0,0 +1,81 @@
+defmodule ReshardChangesFeedTest do
+  use CouchTestCase
+  import ReshardHelpers
+
+  @moduledoc """
+  Test _changes interaction with resharding
+  """
+
+  setup do
+    db = random_db_name()
+    {:ok, _} = create_db(db, query: %{q: 2})
+
+    on_exit(fn ->
+      reset_reshard_state()
+      delete_db(db)
+    end)
+
+    {:ok, [db: db]}
+  end
+
+  test "all_docs after splitting all shards on node1", context do
+    db = context[:db]
+    add_docs(1..3, db)
+
+    all_before = changes(db)
+    first_seq = hd(all_before["results"])["seq"]
+    last_seq = all_before["last_seq"]
+    since_1_before = docset(changes(db, %{:since => first_seq}))
+    since_last_before = docset(changes(db, %{:since => last_seq}))
+
+    resp = post_job_range(db, "00000000-7fffffff")
+    assert resp.status_code == 201
+
+    resp.body
+    |> Enum.map(fn j -> j["id"] end)
+    |> Enum.each(fn id -> wait_job_completed(id) end)
+
+    all_after = changes(db)
+    since_1_after = docset(changes(db, %{:since => first_seq}))
+    since_last_after = docset(changes(db, %{:since => last_seq}))
+
+    assert docset(all_before) == docset(all_after)
+    assert MapSet.subset?(since_1_before, since_1_after)
+    assert MapSet.subset?(since_last_before, since_last_after)
+
+    get_jobs()
+    |> Enum.map(fn j -> j["id"] end)
+    |> Enum.each(fn id -> remove_job(id) end)
+  end
+
+  defp docset(changes) do
+    changes["results"]
+    |> Enum.map(fn %{"id" => id} -> id end)
+    |> MapSet.new()
+  end
+
+  defp changes(db, query \\ %{}) do
+    resp = Couch.get("/#{db}/_changes", query: query)
+    assert resp.status_code == 200
+    resp.body
+  end
+
+  defp add_docs(range, db) do
+    docs = create_docs(range)
+    w3 = %{:w => 3}
+    resp = Couch.post("/#{db}/_bulk_docs", body: %{docs: docs}, query: w3)
+    assert resp.status_code == 201
+    assert length(resp.body) == length(docs)
+
+    docs
+    |> rev(resp.body)
+    |> Enum.into(%{}, fn %{:_id => id, :_rev => rev} -> {id, rev} end)
+  end
+
+  # (Keep for debugging)
+  # defp unpack_seq(seq) when is_binary(seq) do
+  #   [_, opaque] = String.split(seq, "-")
+  #   {:ok, binblob} = Base.url_decode64(opaque, padding: false)
+  #   :erlang.binary_to_term(binblob)
+  # end
+end
diff --git a/test/elixir/test/reshard_helpers.exs 
b/test/elixir/test/reshard_helpers.exs
index 66a2f7c..4e4c8a1 100644
--- a/test/elixir/test/reshard_helpers.exs
+++ b/test/elixir/test/reshard_helpers.exs
@@ -88,6 +88,10 @@ defmodule ReshardHelpers do
     node1
   end
 
+  def wait_job_removed(id) do
+    retry_until(fn -> get_job(id).status_code == 404 end, 200, 10_000)
+  end
+
   def wait_job_completed(id) do
     wait_job_state(id, "completed")
   end

Reply via email to