Skip to content

Commit

Permalink
add support for callback functions around worker initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
michalwski committed Oct 9, 2018
1 parent 21c062d commit bdd5787
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/wpool_process.erl
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ cast_call(Process, From, Call) ->
%% @private
-spec init({atom(), atom(), term(), [wpool:option()]}) -> {ok, state()}.
init({Name, Mod, InitArgs, Options}) ->
wpool_worker_callbacks:call(on_init_start, Options, [Name]),

case Mod:init(InitArgs) of
{ok, ModState} ->
ok = wpool_utils:notify_queue_manager(new_worker, Name, Options),
wpool_worker_callbacks:call(on_new_worker, Options, [Name]),
{ok, #state{ name = Name
, mod = Mod
, state = ModState
Expand Down Expand Up @@ -210,3 +213,5 @@ handle_call(Call, From, State) ->
, State#state.name
, State#state.options),
Reply.


20 changes: 19 additions & 1 deletion test/wpool_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@
, default_options/1
, complete_coverage/1
, broadcast/1
, worker_callbacks/1
]).

-spec all() -> [atom()].
all() ->
[too_much_overrun, overrun, stop_pool, non_brutal_shutdown, stats,
default_strategy, default_options, complete_coverage, broadcast,
kill_on_overrun].
kill_on_overrun, worker_callbacks].

-spec init_per_suite(config()) -> config().
init_per_suite(Config) ->
Expand Down Expand Up @@ -370,6 +371,23 @@ broadcast(_Config) ->
meck:unload(x),
{comment, []}.

-spec worker_callbacks(config) -> ok.
worker_callbacks(_Config) ->
Pool = callbacks_test,
WorkersCount = 13,
meck:new(callbacks, [non_strict]),
meck:expect(callbacks, on_init_start, fun(_APoolName) -> ok end),
meck:expect(callbacks, on_new_worker, fun(_APoolName) -> ok end),
{ok, _Pid} = wpool:start_pool(Pool, [{workers, WorkersCount},
{callbacks, #{on_init_start =>
fun callbacks:on_init_start/1,
on_new_worker =>
fun callbacks:on_new_worker/1}}]),
WorkersCount = meck:num_calls(callbacks, on_init_start, ['_']),
WorkersCount = meck:num_calls(callbacks, on_new_worker, ['_']),

ok.


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

0 comments on commit bdd5787

Please sign in to comment.