couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bbast...@apache.org
Subject [2/6] couch-index commit: updated refs/heads/8409-view-cache to 5a9f05c
Date Wed, 08 Mar 2017 20:39:26 GMT
Add basic test for index cache monitor


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/commit/19a3a497
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/tree/19a3a497
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/diff/19a3a497

Branch: refs/heads/8409-view-cache
Commit: 19a3a4977761bebf510c24cb4b680b2167f7f6ec
Parents: 4f184e4
Author: Benjamin Bastian <benjamin.bastian@gmail.com>
Authored: Wed Mar 1 16:26:50 2017 -0800
Committer: Benjamin Bastian <benjamin.bastian@gmail.com>
Committed: Thu Mar 2 10:07:12 2017 -0800

----------------------------------------------------------------------
 test/couch_index_monitor_tests.erl | 107 ++++++++++++++++++++++++++++++++
 1 file changed, 107 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/blob/19a3a497/test/couch_index_monitor_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_index_monitor_tests.erl b/test/couch_index_monitor_tests.erl
new file mode 100644
index 0000000..d4fd2c9
--- /dev/null
+++ b/test/couch_index_monitor_tests.erl
@@ -0,0 +1,107 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couch_index_monitor_tests).
+
+-include("couch_index.hrl").
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+
+-define(DDOC, {[{
+    <<"views">>, {[
+        {<<"test_view">>, {[{<<"map">>, <<"function(doc) {emit(null,
1);}">>}]}
+    }]}
+}]}).
+
+
+setup() ->
+    DbName = ?tempdb(),
+    {ok, Db} = couch_db:create(DbName, [?ADMIN_CTX]),
+    DDocName = <<"_design/", DbName/binary>>,
+    DDoc = #doc{id = DDocName, body = ?DDOC},
+    {ok, _} = couch_db:update_doc(Db, DDoc, [?ADMIN_CTX]),
+    {Db, DDoc}.
+
+
+teardown({Db, _DDoc}) ->
+    couch_db:close(Db),
+    ok.
+
+
+monitoring_test_() ->
+    {
+        "Test index monitoring",
+        {
+            setup,
+            fun() -> test_util:start_couch([]) end, fun test_util:stop_couch/1,
+            {
+                foreach,
+                fun setup/0, fun teardown/1,
+                [
+                    fun test_basic/1
+                ]
+            }
+        }
+    }.
+
+
+test_basic({Db, DDoc}) ->
+    ?_test(begin
+        {ok, Pid} = couch_index_server:get_index(couch_mrview_index, Db#db.name, DDoc#doc.id),
+        {ok, IdxState} = couch_mrview_index:init(Db, DDoc),
+        DbName = couch_mrview_index:get(db_name, IdxState),
+        Sig = couch_mrview_index:get(signature, IdxState),
+        [{_, {Pid, Monitor, SysOwned}}] = ets:lookup(?BY_SIG, {DbName, Sig}),
+        ?assert(is_pid(Pid)),
+        ?assert(is_pid(Monitor)),
+        ?assertNot(SysOwned),
+        ?assertEqual(1, get_count({DbName, Sig})),
+        Acq1 = spawn_monitor_acquirer(DbName, DDoc#doc.id),
+        ?assertEqual(2, get_count({DbName, Sig})),
+        Acq2 = spawn_monitor_acquirer(DbName, DDoc#doc.id),
+        ?assertEqual(3, get_count({DbName, Sig})),
+        wait_down(Acq2),
+        ?assertEqual(2, get_count({DbName, Sig})),
+        wait_down(Acq1),
+        ?assertEqual(1, get_count({DbName, Sig})),
+        couch_index_monitor:cancel({DbName, Sig}, {self(), Monitor, SysOwned}),
+        ?assertEqual(0, get_count({DbName, Sig})),
+        ?assertEqual(1, length(ets:lookup(?BY_IDLE, {DbName, Sig}))),
+        ok
+    end).
+
+
+wait_down({Pid, Ref}) ->
+    Pid ! close,
+    receive {'DOWN', Ref, process, Pid, normal} ->
+        ok
+    end.
+
+
+spawn_monitor_acquirer(DbName, DDocID) ->
+    {Pid, Ref} = spawn_monitor(fun() ->
+        receive {acquire, From} ->
+            {ok, _} = couch_index_server:get_index(couch_mrview_index, DbName, DDocID),
+            From ! acquired
+        end,
+        receive close -> ok end
+    end),
+    Pid ! {acquire, self()},
+    receive acquired -> ok end,
+    {Pid, Ref}.
+
+
+get_count(NameSig) ->
+    case ets:lookup(?BY_COUNTERS, NameSig) of
+        [] -> 0;
+        [{_, Count}] -> Count
+    end.


Mime
View raw message