|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 2 | +# use this file except in compliance with the License. You may obtain a copy of |
| 3 | +# the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations under |
| 11 | +# the License. |
| 12 | + |
| 13 | +defmodule PaginatedResultsTest do |
| 14 | + alias Couch.Test.Utils |
| 15 | + use CouchTestCase |
| 16 | + |
| 17 | + @db_name "paginated-results" |
| 18 | + # Great enough to make faster systems busy while running the |
| 19 | + # query. |
| 20 | + @num_docs 9_999 |
| 21 | + @updates 25 |
| 22 | + |
| 23 | + setup do |
| 24 | + MangoDatabase.recreate(@db_name) |
| 25 | + fields = [ |
| 26 | + %{"name" => "_id", "type" => "string"}, |
| 27 | + %{"name" => "name", "type" => "string"} |
| 28 | + ] |
| 29 | + MangoDatabase.create_text_index( |
| 30 | + @db_name, |
| 31 | + analyzer: "keyword", |
| 32 | + default_field: %{}, |
| 33 | + selector: %{}, |
| 34 | + fields: fields, |
| 35 | + index_array_lengths: true |
| 36 | + ) |
| 37 | + |
| 38 | + docs = |
| 39 | + 0..@num_docs |
| 40 | + |> Enum.map(fn doc_id -> |
| 41 | + %{ |
| 42 | + "_id" => hex8(doc_id), |
| 43 | + "name" => Utils.random_name("db") |
| 44 | + } |
| 45 | + end) |
| 46 | + |
| 47 | + MangoDatabase.save_docs(@db_name, docs) |
| 48 | + :ok |
| 49 | + end |
| 50 | + |
| 51 | + defp hex8(i) do |
| 52 | + :io_lib.format("~8.16.0B", [i]) |> IO.iodata_to_binary() |
| 53 | + end |
| 54 | + |
| 55 | + defp do_query(delay, selector, opts \\ []) do |
| 56 | + Task.async(fn -> |
| 57 | + Process.sleep(delay) |
| 58 | + MangoDatabase.find(@db_name, selector, opts) |
| 59 | + end) |
| 60 | + end |
| 61 | + |
| 62 | + defp do_updates(pause, doc_id) do |
| 63 | + Task.async(fn -> |
| 64 | + for i <- 0..@updates do |
| 65 | + doc = MangoDatabase.open_doc(@db_name, doc_id) |
| 66 | + updated_doc = %{ |
| 67 | + "_id" => doc_id, |
| 68 | + "_rev" => doc["_rev"], |
| 69 | + "update" => i, |
| 70 | + "name" => "foobar" |
| 71 | + } |
| 72 | + |
| 73 | + MangoDatabase.save_doc(@db_name, updated_doc) |
| 74 | + Process.sleep(pause) |
| 75 | + end |
| 76 | + end) |
| 77 | + end |
| 78 | + |
| 79 | + test "query with lot of results" do |
| 80 | + selector = %{"_id" => %{"$lte": hex8(1000)}} |
| 81 | + # 200 is the maximum for `text` searches. |
| 82 | + {:ok, docs} = MangoDatabase.find(@db_name, selector, limit: 200) |
| 83 | + assert length(docs) == 200 |
| 84 | + end |
| 85 | + |
| 86 | + test "no duplicates on interleaved updates" do |
| 87 | + # Give ~500 ms head start for the updates before running the |
| 88 | + # query. |
| 89 | + query = do_query(500, %{"name" => "foobar"}) |
| 90 | + # Keep updating the target document in every 200 ms. |
| 91 | + do_updates(200, hex8(2)) |
| 92 | + |
| 93 | + {:ok, docs} = Task.await(query, :infinity) |
| 94 | + assert length(docs) == 1 |
| 95 | + end |
| 96 | + |
| 97 | + test "no duplicates on interleaved updates heavy" do |
| 98 | + query = do_query(500, %{"name" => "foobar"}) |
| 99 | + do_updates(50, hex8(2)) |
| 100 | + do_updates(200, hex8(3)) |
| 101 | + do_updates(300, hex8(4)) |
| 102 | + do_updates(150, hex8(5)) |
| 103 | + do_updates(100, hex8(6)) |
| 104 | + |
| 105 | + {:ok, docs} = Task.await(query, :infinity) |
| 106 | + ids = Enum.sort(Enum.map(docs, fn doc -> doc["_id"] end)) |
| 107 | + assert ids == [ |
| 108 | + hex8(2), |
| 109 | + hex8(3), |
| 110 | + hex8(4), |
| 111 | + hex8(5), |
| 112 | + hex8(6) |
| 113 | + ] |
| 114 | + end |
| 115 | + |
| 116 | + test "no duplicates on interleaved updates with limit skip" do |
| 117 | + query = do_query(500, %{"name" => "foobar"}, limit: 1, skip: 3) |
| 118 | + do_updates(50, hex8(2)) |
| 119 | + do_updates(200, hex8(3)) |
| 120 | + do_updates(300, hex8(4)) |
| 121 | + do_updates(150, hex8(5)) |
| 122 | + do_updates(100, hex8(6)) |
| 123 | + |
| 124 | + {:ok, docs} = Task.await(query, :infinity) |
| 125 | + assert length(docs) == 1 |
| 126 | + end |
| 127 | +end |
0 commit comments