Browse Source

EMQX failed to start when the listening port was occupied (#3354)

张奇怪 5 years atrás
parent
commit
9ba938b9d9
2 changed files with 8 additions and 8 deletions
  1. 5 6
      src/emqx_listeners.erl
  2. 3 2
      test/emqx_listeners_SUITE.erl

+ 5 - 6
src/emqx_listeners.erl

@@ -44,17 +44,16 @@
 start() ->
     lists:foreach(fun start_listener/1, emqx:get_env(listeners, [])).
 
--spec(start_listener(listener()) -> {ok, pid()} | {error, term()}).
+-spec(start_listener(listener()) -> ok).
 start_listener({Proto, ListenOn, Options}) ->
-    StartRet = start_listener(Proto, ListenOn, Options),
-    case StartRet of
+    case start_listener(Proto, ListenOn, Options) of
         {ok, _} -> io:format("Start mqtt:~s listener on ~s successfully.~n",
                              [Proto, format(ListenOn)]);
         {error, Reason} ->
             io:format(standard_error, "Failed to start mqtt:~s listener on ~s - ~0p~n!",
-                      [Proto, format(ListenOn), Reason])
-    end,
-    StartRet.
+                      [Proto, format(ListenOn), Reason]),
+            error(Reason)
+    end.
 
 %% Start MQTT/TCP listener
 -spec(start_listener(esockd:proto(), esockd:listen_on(), [esockd:option()])

+ 3 - 2
test/emqx_listeners_SUITE.erl

@@ -21,6 +21,7 @@
 
 -include("emqx.hrl").
 -include("emqx_mqtt.hrl").
+-include_lib("eunit/include/eunit.hrl").
 
 all() -> emqx_ct:all(?MODULE).
 
@@ -37,7 +38,7 @@ end_per_suite(_Config) ->
 
 t_start_stop_listeners(_) ->
     ok = emqx_listeners:start(),
-    {error, _} = emqx_listeners:start_listener({ws,{"127.0.0.1", 8083}, []}),
+    ?assertException(error, _, emqx_listeners:start_listener({ws,{"127.0.0.1", 8083}, []})),
     ok = emqx_listeners:stop().
 
 t_restart_listeners(_) ->
@@ -90,4 +91,4 @@ get_base_dir(Module) ->
 
 get_base_dir() ->
     get_base_dir(?MODULE).
-    
+