1%% @doc This stub module mimics part of folsom's API. It allows us to
2%% test the metrics instrumentation of pooler without introducing a
3%% dependency on folsom or another metrics application.
4%%
5-module(fake_metrics).
6-behaviour(gen_server).
7-define(SERVER, ?MODULE).
8
9-include_lib("eunit/include/eunit.hrl").
10%% ------------------------------------------------------------------
11%% API Function Exports
12%% ------------------------------------------------------------------
13
14-export([start_link/0,
15         notify/3,
16         get_metrics/0,
17         reset_metrics/0,
18         stop/0
19        ]).
20
21%% ------------------------------------------------------------------
22%% gen_server Function Exports
23%% ------------------------------------------------------------------
24
25-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
26         terminate/2, code_change/3]).
27
28%% ------------------------------------------------------------------
29%% API Function Definitions
30%% ------------------------------------------------------------------
31
32start_link() ->
33    gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
34
35notify(Name, Value, Type) ->
36    gen_server:cast(?SERVER, {Name, Value, Type}).
37
38reset_metrics() ->
39    gen_server:call(?SERVER, reset).
40
41stop() ->
42    gen_server:call(?SERVER, stop).
43
44get_metrics() ->
45    gen_server:call(?SERVER, get_metrics).
46
47%% ------------------------------------------------------------------
48%% gen_server Function Definitions
49%% ------------------------------------------------------------------
50-record(state, {
51          metrics = []
52         }).
53
54init(_) ->
55    {ok, #state{}}.
56
57handle_call(reset, _From, State) ->
58    {reply, ok, State#state{metrics = []}};
59handle_call(get_metrics, _From, #state{metrics = Metrics}=State) ->
60    {reply, Metrics, State};
61handle_call(stop, _From, State) ->
62    {stop, normal, stop_ok, State};
63handle_call(_Request, _From, State) ->
64    erlang:error({what, _Request}),
65    {noreply, ok, State}.
66
67handle_cast({_N, _V, _T}=M, #state{metrics = Metrics} = State) ->
68    {noreply, State#state{metrics = [M|Metrics]}};
69handle_cast(_Msg, State) ->
70    {noreply, State}.
71
72handle_info(_Info, State) ->
73    {noreply, State}.
74
75terminate(_Reason, _State) ->
76    ok.
77
78code_change(_OldVsn, State, _Extra) ->
79    {ok, State}.
80
81