test(listeners): try to stop all listeners after each test

- Reduce the `eaddrinuse` flakiness
- Use ?FUNCTION_NAME as listener name to avoid name clashing between
  tests
- Call emqx:remove_config for adhoc listeners created for testing
  tombestone is designed for default listeners
This commit is contained in:
Zaiming (Stone) Shi 2024-02-16 14:33:15 +01:00
parent 28d9e09504
commit 3fa262f9ca
1 changed files with 19 additions and 14 deletions

View File

@ -206,7 +206,8 @@ t_ssl_update_opts(Config) ->
{verify, verify_peer}, {verify, verify_peer},
{customize_hostname_check, [{match_fun, fun(_, _) -> true end}]} {customize_hostname_check, [{match_fun, fun(_, _) -> true end}]}
], ],
with_listener(ssl, updated, Conf, fun() -> Name = ?FUNCTION_NAME,
with_listener(ssl, Name, Conf, fun() ->
%% Client connects successfully. %% Client connects successfully.
C1 = emqtt_connect_ssl(Host, Port, [ C1 = emqtt_connect_ssl(Host, Port, [
{cacertfile, filename:join(PrivDir, "ca.pem")} | ClientSSLOpts {cacertfile, filename:join(PrivDir, "ca.pem")} | ClientSSLOpts
@ -214,7 +215,7 @@ t_ssl_update_opts(Config) ->
%% Change the listener SSL configuration: another set of cert/key files. %% Change the listener SSL configuration: another set of cert/key files.
{ok, _} = emqx:update_config( {ok, _} = emqx:update_config(
[listeners, ssl, updated], [listeners, ssl, Name],
{update, #{ {update, #{
<<"ssl_options">> => #{ <<"ssl_options">> => #{
<<"cacertfile">> => filename:join(PrivDir, "ca-next.pem"), <<"cacertfile">> => filename:join(PrivDir, "ca-next.pem"),
@ -238,7 +239,7 @@ t_ssl_update_opts(Config) ->
%% Change the listener SSL configuration: require peer certificate. %% Change the listener SSL configuration: require peer certificate.
{ok, _} = emqx:update_config( {ok, _} = emqx:update_config(
[listeners, ssl, updated], [listeners, ssl, Name],
{update, #{ {update, #{
<<"ssl_options">> => #{ <<"ssl_options">> => #{
<<"verify">> => verify_peer, <<"verify">> => verify_peer,
@ -292,7 +293,8 @@ t_wss_update_opts(Config) ->
{verify, verify_peer}, {verify, verify_peer},
{customize_hostname_check, [{match_fun, fun(_, _) -> true end}]} {customize_hostname_check, [{match_fun, fun(_, _) -> true end}]}
], ],
with_listener(wss, updated, Conf, fun() -> Name = ?FUNCTION_NAME,
with_listener(wss, Name, Conf, fun() ->
%% Start a client. %% Start a client.
C1 = emqtt_connect_wss(Host, Port, [ C1 = emqtt_connect_wss(Host, Port, [
{cacertfile, filename:join(PrivDir, "ca.pem")} {cacertfile, filename:join(PrivDir, "ca.pem")}
@ -303,7 +305,7 @@ t_wss_update_opts(Config) ->
%% 1. Another set of (password protected) cert/key files. %% 1. Another set of (password protected) cert/key files.
%% 2. Require peer certificate. %% 2. Require peer certificate.
{ok, _} = emqx:update_config( {ok, _} = emqx:update_config(
[listeners, wss, updated], [listeners, wss, Name],
{update, #{ {update, #{
<<"ssl_options">> => #{ <<"ssl_options">> => #{
<<"cacertfile">> => filename:join(PrivDir, "ca-next.pem"), <<"cacertfile">> => filename:join(PrivDir, "ca-next.pem"),
@ -327,7 +329,7 @@ t_wss_update_opts(Config) ->
%% Change the listener SSL configuration: require peer certificate. %% Change the listener SSL configuration: require peer certificate.
{ok, _} = emqx:update_config( {ok, _} = emqx:update_config(
[listeners, wss, updated], [listeners, wss, Name],
{update, #{ {update, #{
<<"ssl_options">> => #{ <<"ssl_options">> => #{
<<"verify">> => verify_peer, <<"verify">> => verify_peer,
@ -384,7 +386,8 @@ t_quic_update_opts(Config) ->
{verify, verify_peer}, {verify, verify_peer},
{customize_hostname_check, [{match_fun, fun(_, _) -> true end}]} {customize_hostname_check, [{match_fun, fun(_, _) -> true end}]}
], ],
with_listener(ListenerType, updated, Conf, fun() -> Name = ?FUNCTION_NAME,
with_listener(ListenerType, Name, Conf, fun() ->
%% Client connects successfully. %% Client connects successfully.
C1 = ConnectFun(Host, Port, [ C1 = ConnectFun(Host, Port, [
{cacertfile, filename:join(PrivDir, "ca.pem")} | ClientSSLOpts {cacertfile, filename:join(PrivDir, "ca.pem")} | ClientSSLOpts
@ -392,7 +395,7 @@ t_quic_update_opts(Config) ->
%% Change the listener SSL configuration: another set of cert/key files. %% Change the listener SSL configuration: another set of cert/key files.
{ok, _} = emqx:update_config( {ok, _} = emqx:update_config(
[listeners, ListenerType, updated], [listeners, ListenerType, Name],
{update, #{ {update, #{
<<"ssl_options">> => #{ <<"ssl_options">> => #{
<<"cacertfile">> => filename:join(PrivDir, "ca-next.pem"), <<"cacertfile">> => filename:join(PrivDir, "ca-next.pem"),
@ -419,7 +422,7 @@ t_quic_update_opts(Config) ->
%% Change the listener SSL configuration: require peer certificate. %% Change the listener SSL configuration: require peer certificate.
{ok, _} = emqx:update_config( {ok, _} = emqx:update_config(
[listeners, ListenerType, updated], [listeners, ListenerType, Name],
{update, #{ {update, #{
<<"ssl_options">> => #{ <<"ssl_options">> => #{
<<"verify">> => verify_peer, <<"verify">> => verify_peer,
@ -447,7 +450,7 @@ t_quic_update_opts(Config) ->
%% Change the listener port %% Change the listener port
NewPort = emqx_common_test_helpers:select_free_port(ListenerType), NewPort = emqx_common_test_helpers:select_free_port(ListenerType),
{ok, _} = emqx:update_config( {ok, _} = emqx:update_config(
[listeners, ListenerType, updated], [listeners, ListenerType, Name],
{update, #{ {update, #{
<<"bind">> => format_bind({Host, NewPort}) <<"bind">> => format_bind({Host, NewPort})
}} }}
@ -506,7 +509,8 @@ t_quic_update_opts_fail(Config) ->
{verify, verify_peer}, {verify, verify_peer},
{customize_hostname_check, [{match_fun, fun(_, _) -> true end}]} {customize_hostname_check, [{match_fun, fun(_, _) -> true end}]}
], ],
with_listener(ListenerType, updated, Conf, fun() -> Name = ?FUNCTION_NAME,
with_listener(ListenerType, Name, Conf, fun() ->
%% GIVEN: an working Listener that client could connect to. %% GIVEN: an working Listener that client could connect to.
C1 = ConnectFun(Host, Port, [ C1 = ConnectFun(Host, Port, [
{cacertfile, filename:join(PrivDir, "ca.pem")} | ClientSSLOpts {cacertfile, filename:join(PrivDir, "ca.pem")} | ClientSSLOpts
@ -514,7 +518,7 @@ t_quic_update_opts_fail(Config) ->
%% WHEN: reload the listener with invalid SSL options (certfile and keyfile missmatch). %% WHEN: reload the listener with invalid SSL options (certfile and keyfile missmatch).
UpdateResult1 = emqx:update_config( UpdateResult1 = emqx:update_config(
[listeners, ListenerType, updated], [listeners, ListenerType, Name],
{update, #{ {update, #{
<<"ssl_options">> => #{ <<"ssl_options">> => #{
<<"cacertfile">> => filename:join(PrivDir, "ca-next.pem"), <<"cacertfile">> => filename:join(PrivDir, "ca-next.pem"),
@ -537,7 +541,7 @@ t_quic_update_opts_fail(Config) ->
%% WHEN: Change the listener SSL configuration again %% WHEN: Change the listener SSL configuration again
UpdateResult2 = emqx:update_config( UpdateResult2 = emqx:update_config(
[listeners, ListenerType, updated], [listeners, ListenerType, Name],
{update, #{ {update, #{
<<"ssl_options">> => #{ <<"ssl_options">> => #{
<<"cacertfile">> => filename:join(PrivDir, "ca-next.pem"), <<"cacertfile">> => filename:join(PrivDir, "ca-next.pem"),
@ -581,7 +585,8 @@ with_listener(Type, Name, Config, Then) ->
try try
Then() Then()
after after
emqx:update_config([listeners, Type, Name], ?TOMBSTONE_CONFIG_CHANGE_REQ) ok = emqx_listeners:stop(),
emqx:remove_config([listeners, Type, Name])
end. end.
emqtt_connect_ssl(Host, Port, SSLOpts) -> emqtt_connect_ssl(Host, Port, SSLOpts) ->