1%%%
2%%% Copyright 2011, Boundary
3%%%
4%%% Licensed under the Apache License, Version 2.0 (the "License");
5%%% you may not use this file except in compliance with the License.
6%%% You may obtain a copy of the License at
7%%%
8%%%     http://www.apache.org/licenses/LICENSE-2.0
9%%%
10%%% Unless required by applicable law or agreed to in writing, software
11%%% distributed under the License is distributed on an "AS IS" BASIS,
12%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13%%% See the License for the specific language governing permissions and
14%%% limitations under the License.
15%%%
16
17
18%%%-------------------------------------------------------------------
19%%% File:      folsom_tests.erl
20%%% @author    joe williams <j@boundary.com>
21%%% @doc
22%%% @end
23%%%------------------------------------------------------------------
24
25-module(folsom_tests).
26
27-include_lib("eunit/include/eunit.hrl").
28
29run_test_() ->
30    {setup,
31     fun () -> folsom:start() end,
32     fun (_) -> folsom:stop() end,
33     [{"creating metrics",
34       fun folsom_erlang_checks:create_metrics/0},
35      {"tagging metrics",
36       fun folsom_erlang_checks:tag_metrics/0},
37      {"populating metrics",
38       {timeout, 30, fun folsom_erlang_checks:populate_metrics/0}},
39      {"checking metrics",
40       fun folsom_erlang_checks:check_metrics/0},
41      {"checking counter metric",
42       fun () ->
43               folsom_erlang_checks:counter_metric(10000, testcounter)
44       end},
45      {"checking group metrics",
46       fun folsom_erlang_checks:check_group_metrics/0},
47      {"checking erlang vm metrics",
48       fun folsom_erlang_checks:vm_metrics/0},
49      {"deleting metrics",
50       fun folsom_erlang_checks:delete_metrics/0},
51      {"cpu topology test",
52       fun folsom_erlang_checks:cpu_topology/0},
53      {"c compiler test",
54       fun folsom_erlang_checks:c_compiler_used/0},
55      {"create and delete tests",
56       fun folsom_erlang_checks:create_delete_metrics/0}]}.
57
58configure_test_() ->
59    {foreach, fun setup_app/0, fun cleanup_app/1,
60     [{"start with configured metrics",
61       fun() ->
62               ?assertMatch(ok, application:start(folsom)),
63               [counter, slide, <<"gauge">>, <<"uniform">>] =
64                   lists:sort(folsom_metrics:get_metrics())
65       end}]}.
66
67setup_app() ->
68    application:unload(folsom),
69    Env = [{counter, counter},
70           {gauge, <<"gauge">>},
71           {histogram, [[<<"uniform">>, uniform, 5000],
72                        [slide, slide_uniform, {60, 1028}]]}],
73    application:load({application, folsom, [{mod, {folsom, []}}, {env, Env}]}),
74    ok.
75
76cleanup_app(ok) ->
77    lists:foreach(fun folsom_metrics:delete_metric/1,
78                  [counter, slide, <<"gauge">>, <<"uniform">>]),
79    application:stop(folsom),
80    application:unload(folsom),
81    ok.
82
83update_counter_test() ->
84    Tid = ets:new(sometable, [public, set]),
85    Workers = [spawn_monitor(fun() -> timer:sleep(100-N), folsom_utils:update_counter(Tid, hello, N) end) || N <- lists:seq(1, 100)],
86    wait_for_results(Workers),
87    ?assertEqual([{hello, 5050}], ets:lookup(Tid, hello)).
88
89wait_for_results([]) ->
90    ok;
91wait_for_results(Workers) ->
92    receive
93        {'DOWN', _, _, Pid, Reason} ->
94            case lists:keyfind(Pid, 1, Workers) of
95                false ->
96                    wait_for_results(Workers);
97                _ ->
98                    case Reason of
99                        normal ->
100                            wait_for_results(lists:keydelete(Pid, 1, Workers));
101                        _ ->
102                            erlang:error(Reason)
103                    end
104            end
105    end.
106