diff options
author | Eric Avdey <eiri@eiri.ca> | 2018-12-10 20:22:47 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-10 20:22:47 -0400 |
commit | b1893658fc035cbe6a821f77025920a8c506e4da (patch) | |
tree | 058d6e80703335bf3d459d93ecbcf16fd90f5055 | |
parent | 445e232d5539faf644c80cec495c0665671af3f9 (diff) | |
parent | 5350907e123f63243596b8b6e3e34267ec15def5 (diff) | |
download | couchdb-b1893658fc035cbe6a821f77025920a8c506e4da.tar.gz |
Merge pull request #1796 from cloudant/tests/port-delayed_commits-to-elixir
Port delayed_commits test to Elixir
-rw-r--r-- | test/elixir/README.md | 2 | ||||
-rw-r--r-- | test/elixir/lib/couch.ex | 4 | ||||
-rw-r--r-- | test/elixir/lib/couch/db_test.ex | 53 | ||||
-rw-r--r-- | test/elixir/test/delayed_commits_test.exs | 31 |
4 files changed, 89 insertions, 1 deletions
diff --git a/test/elixir/README.md b/test/elixir/README.md index e80df1f31..54de35929 100644 --- a/test/elixir/README.md +++ b/test/elixir/README.md @@ -33,7 +33,7 @@ X means done, - means partially - [ ] Port conflicts.js - [ ] Port cookie_auth.js - [ ] Port copy_doc.js - - [ ] Port delayed_commits.js + - [X] Port delayed_commits.js - [ ] Port design_docs.js - [ ] Port design_options.js - [ ] Port design_paths.js diff --git a/test/elixir/lib/couch.ex b/test/elixir/lib/couch.ex index 302b8276a..934262216 100644 --- a/test/elixir/lib/couch.ex +++ b/test/elixir/lib/couch.ex @@ -50,6 +50,10 @@ defmodule Couch do CouchDB library to power test suite. """ + def process_url("http://" <> _ = url) do + url + end + def process_url(url) do "http://127.0.0.1:15984" <> url end diff --git a/test/elixir/lib/couch/db_test.ex b/test/elixir/lib/couch/db_test.ex index 930090b51..3bbfb7eaa 100644 --- a/test/elixir/lib/couch/db_test.ex +++ b/test/elixir/lib/couch/db_test.ex @@ -239,4 +239,57 @@ defmodule Couch.DBTest do opts = [pretty: true, width: 20, limit: :infinity, printable_limit: :infinity] inspect(resp, opts) end + + def restart_cluster() do + resp = Couch.get("/_membership") + assert resp.status_code == 200 + nodes = resp.body["all_nodes"] + + nodes_ports = + Enum.reduce(nodes, [], fn node, acc -> + port = node_to_port(node) + [{node, port} | acc] + end) + + tasks = + Enum.map(nodes_ports, fn {node, port} -> + Task.async(fn -> restart_node(node, port) end) + end) + + Task.yield_many(tasks, length(nodes) * 5000) + end + + def restart_node(node \\ "node1@127.0.0.1") do + port = node_to_port(node) + restart_node(node, port) + end + + defp restart_node(node, port) do + url = "http://127.0.0.1:#{port}/_node/#{node}/_restart" + resp = Couch.post(url) + assert HTTPotion.Response.success?(resp) + assert resp.body["ok"] + # make sure node went down. we assuming the node can't bounce quick + # enough to inroduce a race here + retry_until(fn -> !node_is_running(port) end) + # wait utill node is back + retry_until(fn -> node_is_running(port) end, 500, 10_000) + end + + defp node_is_running(port) do + url = "http://127.0.0.1:#{port}/_up" + resp = Couch.get(url) + + case HTTPotion.Response.success?(resp) do + true -> resp.status_code in 200..399 + false -> false + end + end + + defp node_to_port(node) do + url = "/_node/#{node}/_config/chttpd/port" + resp = Couch.get(url) + assert HTTPotion.Response.success?(resp) + resp.body + end end diff --git a/test/elixir/test/delayed_commits_test.exs b/test/elixir/test/delayed_commits_test.exs new file mode 100644 index 000000000..e80d0bdfb --- /dev/null +++ b/test/elixir/test/delayed_commits_test.exs @@ -0,0 +1,31 @@ +defmodule DelayedCommitsTest do + use CouchTestCase + + @moduledoc """ + Test CouchDB delayed commits + This is a port of the delayed_commits.js suite + + Note that delayed_commits is deprecated in 2.0, so this is a minimal + test to show it still works. delayed_commits will be removed in 3.0. + """ + + @tag config: [ + {"couchdb", "delayed_commits", "true"} + ] + @tag :with_db + test "delayed commit", context do + db_name = context[:db_name] + doc_id = "doc-1" + resp = Couch.put("/#{db_name}/#{doc_id}", body: %{a: 2, b: 4}) + assert resp.status_code in 201..204 + assert resp.body["ok"] + + resp = Couch.get("/#{db_name}/#{doc_id}") + assert resp.status_code == 200, "The new doc should be in the database" + + restart_cluster() + + resp = Couch.get("/#{db_name}/#{doc_id}") + assert resp.status_code == 404, "The new doc should be missing" + end +end |