From 0ef6a316efb50a24dc4e4d688a28a91fa6363d5f Mon Sep 17 00:00:00 2001 From: Nelson Vides Date: Sun, 22 Sep 2024 13:08:35 +0200 Subject: [PATCH] Add a test example of how to start a pool of supervisors --- test/echo_server.erl | 5 +++++ test/echo_supervisor.erl | 23 +++++++++++++++++++++++ test/wpool_SUITE.erl | 29 +++++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 test/echo_supervisor.erl diff --git a/test/echo_server.erl b/test/echo_server.erl index 82d5012..d3b0d18 100644 --- a/test/echo_server.erl +++ b/test/echo_server.erl @@ -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]). @@ -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 %%%=================================================================== diff --git a/test/echo_supervisor.erl b/test/echo_supervisor.erl new file mode 100644 index 0000000..66b3df7 --- /dev/null +++ b/test/echo_supervisor.erl @@ -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]}}. diff --git a/test/wpool_SUITE.erl b/test/wpool_SUITE.erl index 07a1ae1..99ae2e3 100644 --- a/test/wpool_SUITE.erl +++ b/test/wpool_SUITE.erl @@ -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}]). @@ -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) -> @@ -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 %% =============================================================================