diff options
author | Alexander Shorin <kxepal@apache.org> | 2014-05-26 23:57:38 +0400 |
---|---|---|
committer | Alexander Shorin <kxepal@apache.org> | 2014-06-16 00:51:18 +0400 |
commit | 8bc52a1171f2eb87a8523c8746399b1fd739a33c (patch) | |
tree | 8936bbad4a7325a8cfbfab2a82210c201987a0e7 | |
parent | fad32ab98af0abde6ea01694136ee1f79844f914 (diff) | |
download | couchdb-8bc52a1171f2eb87a8523c8746399b1fd739a33c.tar.gz |
Port 120-stats-collect.t etap test suite to eunit
-rw-r--r-- | test/couchdb/Makefile.am | 1 | ||||
-rw-r--r-- | test/couchdb/couch_stats_tests.erl | 193 | ||||
-rwxr-xr-x | test/etap/120-stats-collect.t | 150 | ||||
-rw-r--r-- | test/etap/Makefile.am | 1 |
4 files changed, 194 insertions, 151 deletions
diff --git a/test/couchdb/Makefile.am b/test/couchdb/Makefile.am index 59a366ccc..25e4639f6 100644 --- a/test/couchdb/Makefile.am +++ b/test/couchdb/Makefile.am @@ -29,6 +29,7 @@ eunit_files = \ couch_key_tree_tests.erl \ couch_ref_counter_tests.erl \ couch_stream_tests.erl \ + couch_stats_tests.erl \ couch_task_status_tests.erl \ couch_util_tests.erl \ couch_uuids_tests.erl \ diff --git a/test/couchdb/couch_stats_tests.erl b/test/couchdb/couch_stats_tests.erl new file mode 100644 index 000000000..aaaa687e0 --- /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). diff --git a/test/etap/120-stats-collect.t b/test/etap/120-stats-collect.t deleted file mode 100755 index a30f9ac5d..000000000 --- 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). diff --git a/test/etap/Makefile.am b/test/etap/Makefile.am index 2e38ee51b..216aa78e0 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 \ |