Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional process to monitor amoc users #153

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/amoc_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@ init([]) ->
?CHILD(amoc_cluster, worker),
?CHILD(amoc_scenario, worker),
?CHILD(amoc_throttle_controller, worker)
]}}.
] ++ maybe_add_worker(amoc_users_monitor)}}.

maybe_add_worker(amoc_users_monitor) ->
case amoc_users_monitor:is_users_monitor_enabled() of
enabled -> [?CHILD(amoc_users_monitor, worker)];
disabled -> []
end.

4 changes: 4 additions & 0 deletions src/amoc_user.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ stop(Pid, Force) when is_pid(Pid) ->
-spec init(pid(), amoc:scenario(), amoc_scenario:user_id(), state()) ->
no_return().
init(Parent, Scenario, Id, State) ->
case amoc_users_monitor:is_users_monitor_enabled() of
enabled -> {ok, _} = amoc_users_monitor:monitor_user(self());
disabled -> []
end,
proc_lib:init_ack(Parent, {ok, self()}),
process_flag(trap_exit, true),
ScenarioFun = fun() -> perform_scenario(Scenario, Id, State) end,
Expand Down
61 changes: 61 additions & 0 deletions src/amoc_users_monitor.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
-module(amoc_users_monitor).

-behaviour(gen_server).

-export([is_users_monitor_enabled/0]).
-export([start_link/0]).
-export([monitor_user/1, get_all_users_pids/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2]).

is_users_monitor_enabled() ->
application:get_env(amoc, amoc_users_monitor, disabled).

get_all_users_pids() ->
case whereis(?MODULE) of
undefined -> "Amoc users monitor is disabled.";
_ -> gen_server:call(?MODULE, all_users_pids)
end.

start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

monitor_user(UserPid) ->
gen_server:call(?MODULE, {monitor_user, UserPid}).


init(_Args) ->
{ok, #{
monitored_users => sets:new([{version, 2}]),
children_which_died_normally => [],
children_which_died_unnormaly => []
}}.

handle_call(all_users_pids, _From, State) ->
{reply, State, State};
handle_call({monitor_user, UserPid}, _From, State = #{monitored_users := MonitoredUsers}) ->
MonitorRef = monitor(process, UserPid),
NewState = State#{monitored_users => sets:add_element(UserPid, MonitoredUsers)},
{reply, {ok, MonitorRef}, NewState}.

handle_cast(_, State) ->
{noreply, State}.

handle_info({'DOWN', _Ref, process, Pid, normal}, State = #{
monitored_users := MonitoredUsers,
children_which_died_normally := ProcessesDiendNormally}) ->
NewState = State#{
monitored_users => sets:del_element(Pid, MonitoredUsers),
children_which_died_normally => [Pid | ProcessesDiendNormally]
},
logger:info("Process ~p died normally", [Pid]),
{noreply, NewState};
handle_info({'DOWN', _Ref, process, Pid, Reason}, State = #{
monitored_users := MonitoredUsers,
children_which_died_unnormaly := ProcessesDiendUnnormally}) ->
NewState = State#{
monitored_users => sets:del_element(Pid, MonitoredUsers),
children_which_died_unnormaly => [{Pid, Reason} | ProcessesDiendUnnormally]
},
logger:warning("Process ~p died with reason ~p", [Pid, Reason]),
{noreply, NewState}.

57 changes: 57 additions & 0 deletions test/amoc_users_monitor_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
-module(amoc_users_monitor_SUITE).

-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").

-compile(export_all).

all() ->
[
error_when_amoc_users_monitor_is_disabled,
amoc_users_monitor_keeps_track_of_users
].

%% Setup and teardown

init_per_suite(Config) ->
Config.

end_per_suite(_Config) ->
ok.

init_per_testcase(error_when_amoc_users_monitor_is_disabled, Config) ->
application:set_env(amoc, amoc_users_monitor, disabled),
common_init_per_testcase(Config);
init_per_testcase(amoc_users_monitor_keeps_track_of_users, Config) ->
application:set_env(amoc, amoc_users_monitor, enabled),
common_init_per_testcase(Config).

common_init_per_testcase(Config) ->
application:ensure_all_started(amoc),
meck:new(amoc_dist, []),
Config.

end_per_testcase(_TC, _Config) ->
meck:unload(amoc_dist),
application:stop(amoc).

%% Test cases

error_when_amoc_users_monitor_is_disabled(_Config) ->
?assertEqual(disabled, amoc_users_monitor:is_users_monitor_enabled()),
?assertEqual("Amoc users monitor is disabled.", amoc_users_monitor:get_all_users_pids()).

amoc_users_monitor_keeps_track_of_users(_Config) ->
?assertEqual(enabled, amoc_users_monitor:is_users_monitor_enabled()),
ok = amoc:do(dummy_scenario_test, 10, []),

timer:sleep(1_000),

#{monitored_users := MonitoredUsers,
children_which_died_normally := DiedNormally,
children_which_died_unnormaly := DiedUnnormally} =
amoc_users_monitor:get_all_users_pids(),
?assertEqual(6, sets:size(MonitoredUsers)),
?assertEqual(2, length(DiedNormally)),
?assertEqual(2, length(DiedUnnormally)),
?assertMatch([{_, test_crush_reason} | _], DiedUnnormally).
20 changes: 20 additions & 0 deletions test/dummy_scenario_test.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-module(dummy_scenario_test).

-behaviour(amoc_scenario).

-export([start/1]).
-export([init/0]).

-spec init() -> ok.
init() ->
ok.

start(Id) when Id == 2 ->
1 = 2;
start(Id) when Id == 3 ->
exit(test_crush_reason);
start(Id) when Id == 5; Id == 6 ->
amoc_user:stop();
start(_Id) ->
timer:sleep(timer:minutes(1)),
amoc_user:stop().
22 changes: 0 additions & 22 deletions test/scenario_template.hrl

This file was deleted.

Loading