From 594bcd2be8e16dd01f658b689a96ae3f266a78d7 Mon Sep 17 00:00:00 2001 From: William Yang Date: Thu, 28 Oct 2021 09:18:35 +0200 Subject: [PATCH] fix(test): adapt to emqx 5.0 master branch --- include/emqtt.hrl | 3 ++ rebar.config | 6 ++-- test/emqtt_SUITE.erl | 4 +-- test/emqtt_frame_SUITE.erl | 1 - test/emqtt_props_SUITE.erl | 10 ++++-- test/emqtt_quic_SUITE.erl | 4 ++- test/emqtt_request_response_SUITE.erl | 4 +-- test/emqtt_sock_SUITE.erl | 12 +++++-- test/emqtt_test_lib.erl | 47 +++++++++++++++++++++++++++ 9 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 test/emqtt_test_lib.erl diff --git a/include/emqtt.hrl b/include/emqtt.hrl index 1fd4a764..b4937d07 100644 --- a/include/emqtt.hrl +++ b/include/emqtt.hrl @@ -532,4 +532,7 @@ -define(PACKET(Type), #mqtt_packet{header = #mqtt_packet_header{type = Type}}). +-define(catch_error(Error, Exp), + try (Exp) catch error:Error -> ok end). + -endif. diff --git a/rebar.config b/rebar.config index 23301ec6..554a77f9 100644 --- a/rebar.config +++ b/rebar.config @@ -22,9 +22,9 @@ {profiles, [{test, [{deps, - [{meck, "0.9.2"}, - {emqx, {git_subdir, "https://github.com/qzhuyan/emqx", {branch, "dev/william/5.0-quic-support-2"}, "apps/emqx"}}, - {emqx_ct_helpers, {git, "https://github.com/emqx/emqx-ct-helpers", {branch, "hocon"}}} + [ {meck, "0.9.2"} + , {emqx, {git_subdir, "https://github.com/emqx/emqx", {branch, "master"}, "apps/emqx"}} + , {proper, "1.4.0"} ]}, {erl_opts, [debug_info]}, %% Define `TEST' in emqx to get empty `foreign_refereced_schema_apps' diff --git a/test/emqtt_SUITE.erl b/test/emqtt_SUITE.erl index 84b237d1..c91df5c4 100644 --- a/test/emqtt_SUITE.erl +++ b/test/emqtt_SUITE.erl @@ -71,11 +71,11 @@ groups() -> retain_as_publish_test]}]. init_per_suite(Config) -> - emqx_ct_helpers:start_apps([]), + ok = emqtt_test_lib:start_emqx(), Config. end_per_suite(_Config) -> - emqx_ct_helpers:stop_apps([]). + emqtt_test_lib:stop_emqx(). receive_messages(Count) -> receive_messages(Count, []). diff --git a/test/emqtt_frame_SUITE.erl b/test/emqtt_frame_SUITE.erl index 062f9603..9a407041 100644 --- a/test/emqtt_frame_SUITE.erl +++ b/test/emqtt_frame_SUITE.erl @@ -23,7 +23,6 @@ -include_lib("proper/include/proper.hrl"). -include_lib("eunit/include/eunit.hrl"). -include_lib("common_test/include/ct.hrl"). --include_lib("emqx_ct_helpers/include/emqx_ct.hrl"). %%-define(PROPTEST(F), ?assert(proper:quickcheck(F()))). -define(PROPTEST(F), ?assert(proper:quickcheck(F(), [{to_file, user}]))). diff --git a/test/emqtt_props_SUITE.erl b/test/emqtt_props_SUITE.erl index 41457fb6..95a4913f 100644 --- a/test/emqtt_props_SUITE.erl +++ b/test/emqtt_props_SUITE.erl @@ -21,9 +21,15 @@ -include("emqtt.hrl"). -include_lib("eunit/include/eunit.hrl"). --include_lib("emqx_ct_helpers/include/emqx_ct.hrl"). -all() -> emqx_ct:all(?MODULE). +all() -> emqx_common_test_helpers:all(?MODULE). + +init_per_suite(Config) -> + emqtt_test_lib:ensure_test_module(emqx_common_test_helpers), + Config. + +end_per_suite(_Config) -> + ok. t_id(_) -> foreach_prop( diff --git a/test/emqtt_quic_SUITE.erl b/test/emqtt_quic_SUITE.erl index 82c89394..a7b6e8ce 100644 --- a/test/emqtt_quic_SUITE.erl +++ b/test/emqtt_quic_SUITE.erl @@ -20,13 +20,15 @@ -include_lib("eunit/include/eunit.hrl"). -all() -> emqx_ct:all(?MODULE). +all() -> emqx_common_test_helpers:all(?MODULE). init_per_suite(Config) -> + emqtt_test_lib:start_emqx(), application:ensure_all_started(quicer), Config. end_per_suite(_) -> + emqtt_test_lib:stop_emqx(), ok. t_quic_sock(Config) -> diff --git a/test/emqtt_request_response_SUITE.erl b/test/emqtt_request_response_SUITE.erl index ad280c30..c77b785e 100644 --- a/test/emqtt_request_response_SUITE.erl +++ b/test/emqtt_request_response_SUITE.erl @@ -24,11 +24,11 @@ -include_lib("common_test/include/ct.hrl"). init_per_suite(Config) -> - emqx_ct_helpers:start_apps([]), + ok = emqtt_test_lib:start_emqx(), Config. end_per_suite(_Config) -> - emqx_ct_helpers:stop_apps([]). + emqtt_test_lib:stop_emqx(). all() -> [request_response]. diff --git a/test/emqtt_sock_SUITE.erl b/test/emqtt_sock_SUITE.erl index 6fe9ddf2..42eedb42 100644 --- a/test/emqtt_sock_SUITE.erl +++ b/test/emqtt_sock_SUITE.erl @@ -21,15 +21,21 @@ -include_lib("eunit/include/eunit.hrl"). -all() -> emqx_ct:all(?MODULE). +all() -> emqx_common_test_helpers:all(?MODULE). +init_per_suite(Config) -> + emqtt_test_lib:ensure_test_module(emqx_common_test_helpers), + Config. + +end_per_suite(_Config) -> + ok. %%-------------------------------------------------------------------- %% Test cases %%-------------------------------------------------------------------- t_tcp_sock(_) -> - Server = tcp_server:start_link(4000), - {ok, Sock} = emqtt_sock:connect("127.0.0.1", 4000, [], 3000), + Server = tcp_server:start_link(4001), + {ok, Sock} = emqtt_sock:connect("127.0.0.1", 4001, [], 3000), send_and_recv_with(Sock), ok = emqtt_sock:close(Sock), ok = tcp_server:stop(Server). diff --git a/test/emqtt_test_lib.erl b/test/emqtt_test_lib.erl new file mode 100644 index 00000000..8f28cb52 --- /dev/null +++ b/test/emqtt_test_lib.erl @@ -0,0 +1,47 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2020 EMQ Technologies Co., Ltd. All Rights Reserved. +%% +%% Licensed under the Apache License, Version 2.0 (the "License"); +%% you may not use this file except in compliance with the License. +%% You may obtain a copy of the License at +%% +%% http://www.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, software +%% distributed under the License is distributed on an "AS IS" BASIS, +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +%% See the License for the specific language governing permissions and +%% limitations under the License. +%%-------------------------------------------------------------------- + +-module(emqtt_test_lib). + +-export([ start_emqx/0 + , stop_emqx/0 + , ensure_test_module/1 + ]). + +-spec start_emqx() -> ok. +start_emqx() -> + ensure_test_module(emqx_common_test_helpers), + emqx_common_test_helpers:start_apps([]), + ok. + +-spec stop_emqx() -> ok. +stop_emqx() -> + ensure_test_module(emqx_common_test_helpers), + emqx_common_test_helpers:stop_apps([]). + +-spec ensure_test_module(M::atom()) -> ok. +ensure_test_module(M) -> + false == code:is_loaded(M) andalso + compile_emqx_test_module(M). + +-spec compile_emqx_test_module(M::atom()) -> ok. +compile_emqx_test_module(M) -> + EmqxDir = code:lib_dir(emqx), + EmqttDir = code:lib_dir(emqtt), + MFilename= filename:join([EmqxDir, "test", M]), + OutDir = filename:join([EmqttDir, "test"]), + {ok, _} = compile:file(MFilename, [{outdir, OutDir}]), + ok.