Skip to content

Commit

Permalink
Merge pull request #139 from qzhuyan/dev/william/quicer-0.0.9
Browse files Browse the repository at this point in the history
support quicer 0.0.9, fix CT framework
  • Loading branch information
qzhuyan authored Oct 29, 2021
2 parents 25892ef + 594bcd2 commit b3c6cba
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 20 deletions.
3 changes: 3 additions & 0 deletions include/emqtt.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 3 additions & 3 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion rebar.config.script
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Profiles1 = lists:keystore(emqtt, 1, Profiles, {emqtt, NewRelx(emqtt, Profiles)}
Profiles2 = lists:keystore(emqtt_pkg, 1, Profiles1, {emqtt_pkg, NewRelx(emqtt_pkg, Profiles1)}),
NewConfig = lists:keystore(profiles, 1, CONFIG, {profiles, Profiles2}),

Quicer = {quicer, {git, "https://github.com/emqx/quic.git", {tag, "0.0.8"}}},
Quicer = {quicer, {git, "https://github.com/emqx/quic.git", {tag, "0.0.9"}}},
ExtraDeps = fun(C) ->
{deps, Deps0} = lists:keyfind(deps, 1, C),
Deps = Deps0 ++
Expand Down
26 changes: 21 additions & 5 deletions src/emqtt_quic.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,21 @@ connect(Host, Port, Opts, Timeout) ->
KeepAlive = proplists:get_value(keepalive, Opts, 60),
ConnOpts = [ {alpn, ["mqtt"]}
, {idle_timeout_ms, timer:seconds(KeepAlive * 3)}
, {handshake_idle_timeout_ms, 3000}
, {peer_unidi_stream_count, 1}
, {peer_bidi_stream_count, 10}
| Opts],
{ok, Conn} = quicer:connect(Host, Port, ConnOpts, Timeout),
quicer:start_stream(Conn, [{active, false}]).
, {peer_bidi_stream_count, 1}
| Opts] ++ local_addr(Opts),
case quicer:connect(Host, Port, ConnOpts, Timeout) of
{ok, Conn} ->
quicer:start_stream(Conn, [{active, false}]);
{error, transport_down, Reason} ->
{error, {transport_down, Reason}}
end.

send(Stream, IoData) when is_list(IoData) ->
send(Stream, iolist_to_binary(IoData));
send(Stream, Bin) ->
case quicer:send(Stream, Bin) of
case quicer:async_send(Stream, Bin) of
{ok, _Len} ->
ok;
Other ->
Expand All @@ -63,3 +68,14 @@ close(Stream) ->

sockname(H) ->
quicer:sockname(H).

local_addr(SOpts) ->
case { proplists:get_value(port, SOpts, 0),
proplists:get_value(ip, SOpts, undefined)} of
{0, undefined} ->
[];
{Port, undefined} ->
[{param_conn_local_address, ":" ++ integer_to_list(Port)}];
{Port, IpAddr} when is_tuple(IpAddr) ->
[{param_conn_local_address, inet:ntoa(IpAddr) ++ ":" ++integer_to_list(Port)}]
end.
4 changes: 2 additions & 2 deletions test/emqtt_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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, []).
Expand Down
1 change: 0 additions & 1 deletion test/emqtt_frame_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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}]))).
Expand Down
10 changes: 8 additions & 2 deletions test/emqtt_props_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion test/emqtt_quic_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand Down
4 changes: 2 additions & 2 deletions test/emqtt_request_response_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down
12 changes: 9 additions & 3 deletions test/emqtt_sock_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
47 changes: 47 additions & 0 deletions test/emqtt_test_lib.erl
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit b3c6cba

Please sign in to comment.