Skip to content

Commit

Permalink
feat: make quicer:listener/1 don't crash when listener not found
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongwencool committed Apr 20, 2022
1 parent 8950d3a commit ff55d12
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/quicer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
9 changes: 6 additions & 3 deletions src/quicer_listener_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions test/quicer_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit ff55d12

Please sign in to comment.