couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kxe...@apache.org
Subject [28/31] couchdb commit: updated refs/heads/1963-eunit to 16528f8
Date Tue, 03 Jun 2014 04:50:47 GMT
Port 120-stats-collect.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/97edff30
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/97edff30
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/97edff30

Branch: refs/heads/1963-eunit
Commit: 97edff30034ed988401a8e3e3985e3d124b4a520
Parents: ac9bc9b
Author: Alexander Shorin <kxepal@gmail.com>
Authored: Mon May 26 23:57:38 2014 +0400
Committer: Alexander Shorin <kxepal@gmail.com>
Committed: Tue Jun 3 03:46:07 2014 +0400

----------------------------------------------------------------------
 test/couchdb/Makefile.am           |   1 +
 test/couchdb/couch_stats_tests.erl | 193 ++++++++++++++++++++++++++++++++
 test/etap/120-stats-collect.t      | 150 -------------------------
 test/etap/Makefile.am              |   1 -
 4 files changed, 194 insertions(+), 151 deletions(-)
----------------------------------------------------------------------


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

http://git-wip-us.apache.org/repos/asf/couchdb/blob/97edff30/test/couchdb/couch_stats_tests.erl
----------------------------------------------------------------------
diff --git a/test/couchdb/couch_stats_tests.erl b/test/couchdb/couch_stats_tests.erl
new file mode 100644
index 0000000..2086d7b
--- /dev/null
+++ b/test/couchdb/couch_stats_tests.erl
@@ -0,0 +1,193 @@
+% 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_stats_tests).
+
+-include("../../src/couchdb/couch_db.hrl").
+-include("couchdb_tests.hrl").
+
+-define(TIMEOUT, 1000).
+-define(SLEEPTIME, 100).
+
+
+setup_collector() ->
+    couch_stats_collector:start(),
+    ok.
+
+teardown_collector(_) ->
+    couch_stats_collector:stop(),
+    ok.
+
+
+couch_stats_collector_test_() ->
+    {
+        "CouchDB stats collector tests",
+        {
+            foreach,
+            fun setup_collector/0, fun teardown_collector/1,
+            [
+                should_increment_counter(),
+                should_decrement_counter(),
+                should_increment_and_decrement_counter(),
+                should_record_absolute_values(),
+                should_clear_absolute_values(),
+                should_track_process_count(),
+                should_increment_counter_multiple_times_per_pid(),
+                should_decrement_counter_on_process_exit(),
+                should_decrement_for_each_track_process_count_call_on_exit(),
+                should_return_all_counters_and_absolute_values(),
+                should_return_incremental_counters(),
+                should_return_absolute_values()
+            ]
+        }
+    }.
+
+
+should_increment_counter() ->
+    ?_assertEqual(100,
+        begin
+            AddCount = fun() -> couch_stats_collector:increment(foo) end,
+            repeat(AddCount, 100),
+            couch_stats_collector:get(foo)
+        end).
+
+should_decrement_counter() ->
+    ?_assertEqual(67,
+        begin
+            AddCount = fun() -> couch_stats_collector:increment(foo) end,
+            RemCount = fun() -> couch_stats_collector:decrement(foo) end,
+            repeat(AddCount, 100),
+            repeat(RemCount, 33),
+            couch_stats_collector:get(foo)
+        end).
+
+should_increment_and_decrement_counter() ->
+    ?_assertEqual(0,
+        begin
+            AddCount = fun() -> couch_stats_collector:increment(foo) end,
+            RemCount = fun() -> couch_stats_collector:decrement(foo) end,
+            repeat(AddCount, 100),
+            repeat(RemCount, 25),
+            repeat(AddCount, 10),
+            repeat(RemCount, 5),
+            repeat(RemCount, 80),
+            couch_stats_collector:get(foo)
+        end).
+
+should_record_absolute_values() ->
+    ?_assertEqual(lists:seq(1, 15),
+        begin
+            lists:map(fun(Val) ->
+                couch_stats_collector:record(bar, Val)
+            end, lists:seq(1, 15)),
+            couch_stats_collector:get(bar)
+        end).
+
+should_clear_absolute_values() ->
+    ?_assertEqual(nil,
+        begin
+            lists:map(fun(Val) ->
+                couch_stats_collector:record(bar, Val)
+            end, lists:seq(1, 15)),
+            couch_stats_collector:clear(bar),
+            couch_stats_collector:get(bar)
+        end).
+
+should_track_process_count() ->
+    ?_assertMatch({_, 1}, spawn_and_count(1)).
+
+should_increment_counter_multiple_times_per_pid() ->
+    ?_assertMatch({_, 3}, spawn_and_count(3)).
+
+should_decrement_counter_on_process_exit() ->
+    ?_assertEqual(2,
+        begin
+            {Pid, 1} = spawn_and_count(1),
+            spawn_and_count(2),
+            RefMon = erlang:monitor(process, Pid),
+            Pid ! sepuku,
+            receive
+                {'DOWN', RefMon, _, _, _} -> ok
+            after ?TIMEOUT ->
+                throw(timeout)
+            end,
+            % sleep for awhile to let collector handle the updates
+            % suddenly, it couldn't notice process death instantly
+            timer:sleep(?SLEEPTIME),
+            couch_stats_collector:get(hoopla)
+        end).
+
+should_decrement_for_each_track_process_count_call_on_exit() ->
+    ?_assertEqual(2,
+        begin
+            {_, 2} = spawn_and_count(2),
+            {Pid, 6} = spawn_and_count(4),
+            RefMon = erlang:monitor(process, Pid),
+            Pid ! sepuku,
+            receive
+                {'DOWN', RefMon, _, _, _} -> ok
+            after ?TIMEOUT ->
+                throw(timeout)
+            end,
+            timer:sleep(?SLEEPTIME),
+            couch_stats_collector:get(hoopla)
+        end).
+
+should_return_all_counters_and_absolute_values() ->
+    ?_assertEqual([{bar,[1.0,0.0]}, {foo,1}],
+        begin
+            couch_stats_collector:record(bar, 0.0),
+            couch_stats_collector:record(bar, 1.0),
+            couch_stats_collector:increment(foo),
+            lists:sort(couch_stats_collector:all())
+        end).
+
+should_return_incremental_counters() ->
+    ?_assertEqual([{foo,1}],
+        begin
+            couch_stats_collector:record(bar, 0.0),
+            couch_stats_collector:record(bar, 1.0),
+            couch_stats_collector:increment(foo),
+            lists:sort(couch_stats_collector:all(incremental))
+        end).
+
+should_return_absolute_values() ->
+    ?_assertEqual([{bar,[1.0,0.0]}, {zing, "Z"}],
+        begin
+            couch_stats_collector:record(bar, 0.0),
+            couch_stats_collector:record(bar, 1.0),
+            couch_stats_collector:record(zing, 90),
+            couch_stats_collector:increment(foo),
+            lists:sort(couch_stats_collector:all(absolute))
+        end).
+
+
+spawn_and_count(N) ->
+    Self = self(),
+    Pid = spawn(fun() ->
+        lists:foreach(
+            fun(_) ->
+                couch_stats_collector:track_process_count(hoopla)
+            end, lists:seq(1,N)),
+        Self ! reporting,
+        receive
+            sepuku -> ok
+        end
+    end),
+    receive reporting -> ok end,
+    {Pid, couch_stats_collector:get(hoopla)}.
+
+repeat(_, 0) ->
+    ok;
+repeat(Fun, Count) ->
+    Fun(),
+    repeat(Fun, Count-1).

http://git-wip-us.apache.org/repos/asf/couchdb/blob/97edff30/test/etap/120-stats-collect.t
----------------------------------------------------------------------
diff --git a/test/etap/120-stats-collect.t b/test/etap/120-stats-collect.t
deleted file mode 100755
index a30f9ac..0000000
--- a/test/etap/120-stats-collect.t
+++ /dev/null
@@ -1,150 +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(11),
-    case (catch test()) of
-        ok ->
-            etap:end_tests();
-        Other ->
-            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
-            etap:bail()
-    end,
-    ok.
-
-test() ->
-    couch_stats_collector:start(),
-    ok = test_counters(),
-    ok = test_abs_values(),
-    ok = test_proc_counting(),
-    ok = test_all(),
-    ok.
-
-test_counters() ->
-    AddCount = fun() -> couch_stats_collector:increment(foo) end,
-    RemCount = fun() -> couch_stats_collector:decrement(foo) end,
-    repeat(AddCount, 100),
-    repeat(RemCount, 25),
-    repeat(AddCount, 10),
-    repeat(RemCount, 5),
-    etap:is(
-        couch_stats_collector:get(foo),
-        80,
-        "Incrememnt tracks correctly."
-    ),
-
-    repeat(RemCount, 80),
-    etap:is(
-        couch_stats_collector:get(foo),
-        0,
-        "Decremented to zaro."
-    ),
-    ok.
-
-test_abs_values() ->
-    lists:map(fun(Val) ->
-        couch_stats_collector:record(bar, Val)
-    end, lists:seq(1, 15)),
-    etap:is(
-        couch_stats_collector:get(bar),
-        lists:seq(1, 15),
-        "Absolute values are recorded correctly."
-    ),
-    
-    couch_stats_collector:clear(bar),
-    etap:is(
-        couch_stats_collector:get(bar),
-        nil,
-        "Absolute values are cleared correctly."
-    ),
-    ok.
-
-test_proc_counting() ->
-    Self = self(),
-    OnePid = spawn(fun() ->
-        couch_stats_collector:track_process_count(hoopla),
-        Self ! reporting,
-        receive sepuku -> ok end
-    end),
-    R1 = erlang:monitor(process, OnePid),
-    receive reporting -> ok end,
-    etap:is(
-        couch_stats_collector:get(hoopla),
-        1,
-        "track_process_count increments the counter."
-    ),
-    
-    TwicePid = spawn(fun() ->
-        couch_stats_collector:track_process_count(hoopla),
-        couch_stats_collector:track_process_count(hoopla),
-        Self ! reporting,
-        receive sepuku -> ok end
-    end),
-    R2 = erlang:monitor(process, TwicePid),
-    receive reporting -> ok end,
-    etap:is(
-        couch_stats_collector:get(hoopla),
-        3,
-        "track_process_count allows more than one incrememnt per Pid"
-    ),
-    
-    OnePid ! sepuku,
-    receive {'DOWN', R1, _, _, _} -> ok end,
-    timer:sleep(250),
-    etap:is(
-        couch_stats_collector:get(hoopla),
-        2,
-        "Process count is decremented when process exits."
-    ),
-    
-    TwicePid ! sepuku,
-    receive {'DOWN', R2, _, _, _} -> ok end,
-    timer:sleep(250),
-    etap:is(
-        couch_stats_collector:get(hoopla),
-        0,
-        "Process count is decremented for each call to track_process_count."
-    ),
-    ok.
-
-test_all() ->
-    couch_stats_collector:record(bar, 0.0),
-    couch_stats_collector:record(bar, 1.0),
-    etap:is(
-        lists:sort(couch_stats_collector:all()),
-        [ {bar,[1.0,0.0]}, {foo,0}, { hoopla,0} ],
-        "all/0 returns all counters and absolute values."
-    ),
-    
-    etap:is(
-        lists:sort(couch_stats_collector:all(incremental)),
-        [ {foo, 0}, {hoopla, 0} ],
-        "all/1 returns only the specified type."
-    ),
-    
-    couch_stats_collector:record(zing, 90),
-    etap:is(
-        lists:sort(couch_stats_collector:all(absolute)),
-        [ {bar,[1.0,0.0]}, {zing,"Z"} ],
-        "all/1 returns only the specified type."
-    ),
-    ok.
-
-repeat(_, 0) ->
-    ok;
-repeat(Fun, Count) ->
-    Fun(),
-    repeat(Fun, Count-1).

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


Mime
View raw message