Skip to content

Commit

Permalink
Test async runner death
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Jan 18, 2024
1 parent 8670b75 commit 1e25a29
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test/throttle_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ groups() ->
change_rate_gradually,
send_and_wait,
just_wait,
wait_for_process_to_die_sends_a_kill,
async_runner_dies_while_waiting_raises_exit,
run_with_interval_zero_limits_only_number_of_parallel_executions,
pause_and_resume,
get_state
Expand Down Expand Up @@ -149,6 +151,17 @@ just_wait(_) ->
amoc_throttle:send(?FUNCTION_NAME, receive_this),
?assertMatch({error, not_received_yet}, ?RECV(receive_this, 200)).

wait_for_process_to_die_sends_a_kill(_) ->
erlang:process_flag(trap_exit, true),
?assertMatch({ok, started}, amoc_throttle:start(?FUNCTION_NAME, 100, 10, 1)),
amoc_throttle:run(?FUNCTION_NAME, fun() -> exit(?FUNCTION_NAME) end),
?assertMatch(ok, ?RECV({'EXIT', _, ?FUNCTION_NAME}, 100)).

async_runner_dies_while_waiting_raises_exit(_) ->
?assertMatch({ok, started}, amoc_throttle:start(?FUNCTION_NAME, 1, 1, 1)),
find_new_link_and_kill_it(self()),
?assertExit({throttle_wait_died, _, killed}, amoc_throttle:wait(?FUNCTION_NAME)).

run_with_interval_zero_limits_only_number_of_parallel_executions(_) ->
%% Start 10 actions at once in 10 processes
?assertMatch({ok, started}, amoc_throttle:start(?FUNCTION_NAME, 10, 0, 1)),
Expand Down Expand Up @@ -213,6 +226,20 @@ fill_throttle(Name, Num) ->
continue -> ok
end.

find_new_link_and_kill_it(Self) ->
erlang:process_flag(trap_exit, true),
{links, OriginalLinks} = erlang:process_info(Self, links),
spawn(?MODULE, kill_async_runner, [Self, OriginalLinks]).

kill_async_runner(Pid, OriginalLinks) ->
GetLinksFun = fun() ->
{links, Links} = erlang:process_info(Pid, links),
Links -- OriginalLinks
end,
Validator = fun(Res) -> 1 =:= length(Res) end,
{ok, [AsyncRunner]} = wait_helper:wait_until(GetLinksFun, ok, #{validator => Validator}),
exit(AsyncRunner, kill).

%% Helpers
amoc_do(Scenario) ->
amoc_do(Scenario, 0, test_helpers:all_vars()).
Expand Down

0 comments on commit 1e25a29

Please sign in to comment.