Skip to content

Commit

Permalink
Add a test example of how to start a pool of supervisors
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Sep 22, 2024
1 parent eab9100 commit 0ef6a31
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
5 changes: 5 additions & 0 deletions test/echo_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
-behaviour(gen_server).

%% gen_server callbacks
-export([start_link/1]).
-export([init/1, terminate/2, code_change/3, handle_call/3, handle_cast/2, handle_info/2,
handle_continue/2, format_status/1]).

Expand All @@ -26,6 +27,10 @@

-export_type([from/0]).

-spec start_link(term()) -> gen_server:start_ret().
start_link(Something) ->
gen_server:start_link(?MODULE, Something, []).

%%%===================================================================
%%% callbacks
%%%===================================================================
Expand Down
23 changes: 23 additions & 0 deletions test/echo_supervisor.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-module(echo_supervisor).

-behaviour(supervisor).

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

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, noargs).

init(noargs) ->
Children =
#{id => undefined,
start => {echo_server, start_link, []},
restart => transient,
shutdown => 5000,
type => worker,
modules => [echo_server]},
Strategy =
#{strategy => simple_one_for_one,
intensity => 5,
period => 60},
{ok, {Strategy, [Children]}}.
29 changes: 27 additions & 2 deletions test/wpool_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
-export([stats/1, stop_pool/1, non_brutal_shutdown/1, brutal_worker_shutdown/1, overrun/1,
kill_on_overrun/1, too_much_overrun/1, default_strategy/1, overrun_handler1/1,
overrun_handler2/1, default_options/1, complete_coverage/1, child_spec/1, broadcall/1,
broadcast/1, send_request/1, worker_killed_stats/1, accepts_maps_and_lists_as_opts/1]).
broadcast/1, send_request/1, worker_killed_stats/1, accepts_maps_and_lists_as_opts/1,
pool_of_supervisors/1]).

-elvis([{elvis_style, no_block_expressions, disable}]).

Expand All @@ -51,7 +52,8 @@ all() ->
send_request,
kill_on_overrun,
worker_killed_stats,
accepts_maps_and_lists_as_opts].
accepts_maps_and_lists_as_opts,
pool_of_supervisors].

-spec init_per_suite(config()) -> config().
init_per_suite(Config) ->
Expand Down Expand Up @@ -515,6 +517,29 @@ accepts_maps_and_lists_as_opts(_Config) ->

{comment, []}.

-spec pool_of_supervisors(config()) -> {comment, string()}.
pool_of_supervisors(_Config) ->
Opts =
#{workers => 3,
worker_shutdown => infinity,
worker => {supervisor, {echo_supervisor, echo_supervisor, noargs}}},

{ok, Pid} = wpool:start_sup_pool(pool_of_supervisors, Opts),
true = erlang:is_process_alive(Pid),

[begin
Run = fun(Sup) -> supervisor:start_child(Sup, [{ok, #{}}]) end,
{ok, EchoServer} = wpool:run(pool_of_supervisors, Run, next_worker),
true = erlang:is_process_alive(EchoServer)
end
|| _N <- lists:seq(1, 9)],

Supervisors = wpool:get_workers(pool_of_supervisors),
[3 = proplists:get_value(active, supervisor:count_children(Supervisor))
|| Supervisor <- Supervisors],

{comment, "Nicely load-balanced childrens across supervisors"}.

%% =============================================================================
%% Helpers
%% =============================================================================
Expand Down

0 comments on commit 0ef6a31

Please sign in to comment.