Skip to content

Commit

Permalink
do not start service if not present in configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekgorny committed Apr 16, 2018
1 parent d241039 commit 2437ace
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/mongoose_service.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ start() ->
-spec stop() -> ok.
stop() -> catch ets:delete(?ETAB), ok.

-spec start_service(service(), options()) -> ok | {error, already_started}.
start_service(Service, Opts) ->
-spec start_service(service(), options() | undefined) -> ok | {error, already_started}.
start_service(Service, undefined) ->
error({service_not_configured, Service});
start_service(Service, Opts) when is_list(Opts) ->
case is_loaded(Service) of
true -> {error, already_started};
false -> run_start_service(Service, Opts)
Expand All @@ -74,7 +76,7 @@ ensure_loaded(Service) ->
ok;
false ->
Options = ejabberd_config:get_local_option_or_default(services, []),
start_service(Service, proplists:get_value(Service, Options, [])),
start_service(Service, proplists:get_value(Service, Options)),
ok
end.

Expand Down
21 changes: 20 additions & 1 deletion test/mongoose_service_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ all() ->
ensure,
verify_deps,
start_deps,
module_deps
module_deps,
misconfigured
].

services() -> [service_a, service_b, service_c, service_d, service_e, service_f, service_g,
Expand Down Expand Up @@ -50,6 +51,15 @@ service_deps() ->
{service_l, [service_k]}
].

init_per_testcase(misconfigured, C) ->
mongoose_service:start(),
meck:new(ejabberd_config, [passthrough]),
meck:expect(ejabberd_config, get_local_option_or_default,
fun(services, _) -> [{service_a, []}] end),
meck:new(service_a, [non_strict]),
meck:expect(service_a, deps, fun() -> [service_b] end),
C;

init_per_testcase(module_deps, C) ->
init_per_testcase(generic, C),
meck:expect(ejabberd_config, get_local_option, fun(_) -> undefined end),
Expand Down Expand Up @@ -79,6 +89,11 @@ init_per_testcase(_, C) ->
proplists:get_keys(service_deps())),
C.

end_per_testcase(misconfigured, C) ->
meck:unload(service_a),
meck:unload(ejabberd_config),
C;

end_per_testcase(module_deps, C) ->
meck:unload(module_a),
end_per_testcase(generic, C);
Expand Down Expand Up @@ -161,6 +176,10 @@ module_deps(_) ->
?assert(gen_mod:is_loaded(<<"localhost">>, module_a)),
ok.

misconfigured(_) ->
?assertError({service_not_configured, service_b}, mongoose_service:ensure_loaded(service_a)),
ok.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
Expand Down

0 comments on commit 2437ace

Please sign in to comment.