You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ch...@apache.org on 2014/07/22 01:57:37 UTC

[24/43] couchdb commit: updated refs/heads/1963-eunit-bigcouch to 424dca5

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/e897be86
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/e897be86
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/e897be86

Branch: refs/heads/1963-eunit-bigcouch
Commit: e897be8684d1c5ea39a956cbde8afb5bf601cbeb
Parents: 9f8911d
Author: Alexander Shorin <kx...@apache.org>
Authored: Mon May 26 23:57:38 2014 +0400
Committer: Russell Branca <ch...@apache.org>
Committed: Mon Jul 21 16:43:55 2014 -0700

----------------------------------------------------------------------
 test/couchdb/couch_stats_tests.erl | 193 ++++++++++++++++++++++++++++++++
 test/etap/120-stats-collect.t      | 150 -------------------------
 2 files changed, 193 insertions(+), 150 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/e897be86/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..aaaa687
--- /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("couch_eunit.hrl").
+-include_lib("couchdb/couch_db.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/e897be86/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).