You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by kx...@apache.org on 2014/06/03 06:50:41 UTC

[22/31] couchdb commit: updated refs/heads/1963-eunit to 16528f8

Port 100-ref-counter.t etap test suite to eunit


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

Branch: refs/heads/1963-eunit
Commit: ac9bc9b9f0b2298470a73bd173bea004e3bffd1d
Parents: 658a3de
Author: Alexander Shorin <kx...@gmail.com>
Authored: Mon May 26 20:59:45 2014 +0400
Committer: Alexander Shorin <kx...@gmail.com>
Committed: Tue Jun 3 03:46:07 2014 +0400

----------------------------------------------------------------------
 test/couchdb/Makefile.am                 |   1 +
 test/couchdb/couch_ref_counter_tests.erl | 107 ++++++++++++++++++++++++
 test/etap/100-ref-counter.t              | 114 --------------------------
 test/etap/Makefile.am                    |   1 -
 4 files changed, 108 insertions(+), 115 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/ac9bc9b9/test/couchdb/Makefile.am
----------------------------------------------------------------------
diff --git a/test/couchdb/Makefile.am b/test/couchdb/Makefile.am
index 7ae8c28..1e6ae04 100644
--- a/test/couchdb/Makefile.am
+++ b/test/couchdb/Makefile.am
@@ -35,6 +35,7 @@ eunit_files = \
     couchdb_file_compression_tests.erl \
     couch_config_tests.erl \
     couch_task_status_tests.erl \
+    couch_ref_counter_tests.erl \
     test_request.erl \
     couchdb_tests.hrl
 

http://git-wip-us.apache.org/repos/asf/couchdb/blob/ac9bc9b9/test/couchdb/couch_ref_counter_tests.erl
----------------------------------------------------------------------
diff --git a/test/couchdb/couch_ref_counter_tests.erl b/test/couchdb/couch_ref_counter_tests.erl
new file mode 100644
index 0000000..7400ac0
--- /dev/null
+++ b/test/couchdb/couch_ref_counter_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_ref_counter_tests).
+
+-include("../../src/couchdb/couch_db.hrl").
+-include("couchdb_tests.hrl").
+
+-define(TIMEOUT, 1000).
+
+
+setup() ->
+    {ok, RefCtr} = couch_ref_counter:start([]),
+    ChildPid = spawn(fun() -> loop() end),
+    {RefCtr, ChildPid}.
+
+teardown({_, ChildPid}) ->
+    erlang:monitor(process, ChildPid),
+    ChildPid ! close,
+    wait().
+
+
+couch_ref_counter_test_() ->
+    {
+        "CouchDB reference counter tests",
+        {
+            foreach,
+            fun setup/0, fun teardown/1,
+            [
+                fun should_initialize_with_calling_process_as_referrer/1,
+                fun should_ignore_unknown_pid/1,
+                fun should_increment_counter_on_pid_add/1,
+                fun should_not_increase_counter_on_readding_same_pid/1,
+                fun should_drop_ref_for_double_added_pid/1,
+                fun should_decrement_counter_on_pid_drop/1,
+                fun should_add_after_drop/1,
+                fun should_decrement_counter_on_process_exit/1
+
+            ]
+        }
+    }.
+
+
+should_initialize_with_calling_process_as_referrer({RefCtr, _}) ->
+    ?_assertEqual(1, couch_ref_counter:count(RefCtr)).
+
+should_ignore_unknown_pid({RefCtr, ChildPid}) ->
+    ?_assertEqual(ok, couch_ref_counter:drop(RefCtr, ChildPid)).
+
+should_increment_counter_on_pid_add({RefCtr, ChildPid}) ->
+    couch_ref_counter:add(RefCtr, ChildPid),
+    ?_assertEqual(2, couch_ref_counter:count(RefCtr)).
+
+should_not_increase_counter_on_readding_same_pid({RefCtr, ChildPid}) ->
+    couch_ref_counter:add(RefCtr, ChildPid),
+    couch_ref_counter:add(RefCtr, ChildPid),
+    ?_assertEqual(2, couch_ref_counter:count(RefCtr)).
+
+should_drop_ref_for_double_added_pid({RefCtr, ChildPid}) ->
+    couch_ref_counter:add(RefCtr, ChildPid),
+    couch_ref_counter:add(RefCtr, ChildPid),
+    couch_ref_counter:drop(RefCtr, ChildPid),
+    ?_assertEqual(2, couch_ref_counter:count(RefCtr)).
+
+should_decrement_counter_on_pid_drop({RefCtr, ChildPid}) ->
+    couch_ref_counter:add(RefCtr, ChildPid),
+    couch_ref_counter:drop(RefCtr, ChildPid),
+    ?_assertEqual(1, couch_ref_counter:count(RefCtr)).
+
+should_add_after_drop({RefCtr, ChildPid}) ->
+    couch_ref_counter:add(RefCtr, ChildPid),
+    couch_ref_counter:drop(RefCtr, ChildPid),
+    couch_ref_counter:add(RefCtr, ChildPid),
+    ?_assertEqual(2, couch_ref_counter:count(RefCtr)).
+
+should_decrement_counter_on_process_exit({RefCtr, ChildPid}) ->
+    ?_assertEqual(1,
+        begin
+            couch_ref_counter:add(RefCtr, ChildPid),
+            erlang:monitor(process, ChildPid),
+            ChildPid ! close,
+            wait(),
+            couch_ref_counter:count(RefCtr)
+        end).
+
+
+loop() ->
+    receive
+        close -> ok
+    end.
+
+wait() ->
+    receive
+        {'DOWN', _, _, _, _} ->
+            ok
+    after ?TIMEOUT ->
+        throw(timeout_error)
+    end.

http://git-wip-us.apache.org/repos/asf/couchdb/blob/ac9bc9b9/test/etap/100-ref-counter.t
----------------------------------------------------------------------
diff --git a/test/etap/100-ref-counter.t b/test/etap/100-ref-counter.t
deleted file mode 100755
index 8f996d0..0000000
--- a/test/etap/100-ref-counter.t
+++ /dev/null
@@ -1,114 +0,0 @@
-#!/usr/bin/env escript
-%% -*- erlang -*-
-
-% 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.
-
-main(_) ->
-    test_util:init_code_path(),
-    etap:plan(8),
-    case (catch test()) of
-        ok ->
-            etap:end_tests();
-        Other ->
-            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
-            etap:bail(Other)
-    end,
-    ok.
-
-loop() ->
-    receive
-        close -> ok
-    end.
-
-wait() ->
-    receive
-        {'DOWN', _, _, _, _} -> ok
-    after 1000 ->
-        throw(timeout_error)
-    end.
-
-test() ->
-    {ok, RefCtr} = couch_ref_counter:start([]),
-
-    etap:is(
-        couch_ref_counter:count(RefCtr),
-        1,
-        "A ref_counter is initialized with the calling process as a referer."
-    ),
-
-    ChildPid1 = spawn(fun() -> loop() end),
-
-    % This is largely implicit in that nothing else breaks
-    % as ok is just returned from gen_server:cast()
-    etap:is(
-        couch_ref_counter:drop(RefCtr, ChildPid1),
-        ok,
-        "Dropping an unknown Pid is ignored."
-    ),
-
-    couch_ref_counter:add(RefCtr, ChildPid1),
-    etap:is(
-        couch_ref_counter:count(RefCtr),
-        2,
-        "Adding a Pid to the ref_counter increases it's count."
-    ),
-
-    couch_ref_counter:add(RefCtr, ChildPid1),
-    etap:is(
-        couch_ref_counter:count(RefCtr),
-        2,
-        "Readding the same Pid maintains the count but increments it's refs."
-    ),
-
-    couch_ref_counter:drop(RefCtr, ChildPid1),
-    etap:is(
-        couch_ref_counter:count(RefCtr),
-        2,
-        "Droping the doubly added Pid only removes a ref, not a referer."
-    ),
-
-    couch_ref_counter:drop(RefCtr, ChildPid1),
-    etap:is(
-        couch_ref_counter:count(RefCtr),
-        1,
-        "Dropping the second ref drops the referer."
-    ),
-
-    couch_ref_counter:add(RefCtr, ChildPid1),
-    etap:is(
-        couch_ref_counter:count(RefCtr),
-        2,
-        "Sanity checking that the Pid was re-added."
-    ),
-
-    erlang:monitor(process, ChildPid1),
-    ChildPid1 ! close,
-    wait(),
-    
-    CheckFun = fun
-        (Iter, nil) ->
-            case couch_ref_counter:count(RefCtr) of
-                1 -> Iter;
-                _ -> nil
-            end;
-        (_, Acc) ->
-            Acc
-    end,
-    Result = lists:foldl(CheckFun, nil, lists:seq(1, 10000)),
-    etap:isnt(
-        Result,
-        nil,
-        "The referer count was decremented automatically on process exit."
-    ),
-
-    ok.

http://git-wip-us.apache.org/repos/asf/couchdb/blob/ac9bc9b9/test/etap/Makefile.am
----------------------------------------------------------------------
diff --git a/test/etap/Makefile.am b/test/etap/Makefile.am
index c2d9ad4..93c8579 100644
--- a/test/etap/Makefile.am
+++ b/test/etap/Makefile.am
@@ -36,7 +36,6 @@ fixture_files = \
     fixtures/test.couch
 
 tap_files = \\
-    100-ref-counter.t \
     120-stats-collect.t \
     121-stats-aggregates.cfg \
     121-stats-aggregates.ini \