diff --git a/src/quicer.erl b/src/quicer.erl index 36a25a2c..7597a5ce 100644 --- a/src/quicer.erl +++ b/src/quicer.erl @@ -551,7 +551,7 @@ listeners() -> -spec listener(quicer_listener:listener_name() | {quicer_listener:listener_name(), - quicer_listener:listen_on()}) -> pid(). + quicer_listener:listen_on()}) -> {ok, pid()} | {error, not_found}. listener(Name) -> quicer_listener_sup:listener(Name). diff --git a/src/quicer_listener_sup.erl b/src/quicer_listener_sup.erl index 3b124953..0e184b06 100644 --- a/src/quicer_listener_sup.erl +++ b/src/quicer_listener_sup.erl @@ -68,16 +68,19 @@ listeners() -> end end, supervisor:which_children(?MODULE)). --spec listener(atom() | {atom(), integer()|string()}) -> pid(). +-spec listener(atom() | {atom(), integer()|string()}) -> {ok, pid()}|{error, not_found}. listener({Name, _ListenOn}) when is_atom(Name) -> listener(Name); listener(Name) when is_atom(Name) -> - [Target] = lists:filtermap( + Targets = lists:filtermap( fun({?CHILD_ID(Id), Child, _Type, _Modules}) when Id =:= Name -> {true, Child}; (_) -> false end, supervisor:which_children(?MODULE)), - Target. + case Targets of + [Pid] -> {ok, Pid}; + [] -> {error, not_found} + end. %%%=================================================================== %%% Supervisor callbacks diff --git a/test/quicer_SUITE.erl b/test/quicer_SUITE.erl index f3040e36..ad232c37 100644 --- a/test/quicer_SUITE.erl +++ b/test/quicer_SUITE.erl @@ -300,12 +300,18 @@ tc_get_listener(Config) -> end, Listeners), lists:foreach(fun({Name, _} = NameListenON) -> - LPid = quicer:listener(Name), - LPid = quicer:listener(NameListenON), + {ok, LPid} = quicer:listener(Name), + {ok, LPid} = quicer:listener(NameListenON), true = is_process_alive(LPid) end, Listeners), - lists:foreach(fun({L, _}) -> ok = quicer:stop_listener(L) end, Listeners). + lists:foreach(fun({L, _}) -> ok = quicer:stop_listener(L) end, Listeners), + + lists:foreach(fun({Name, _} = NameListenON) -> + ?assertEqual({error, not_found}, quicer:listener(Name)), + ?assertEqual({error, not_found}, quicer:listener(NameListenON)) + end, Listeners), + ?assertEqual({error, not_found}, quicer:listener(bad_listen_name)). tc_conn_basic(Config)-> Port = select_port(),