diff --git a/Makefile b/Makefile index 80a3dafbd..ce0a282fa 100644 --- a/Makefile +++ b/Makefile @@ -35,10 +35,10 @@ EUNIT_OPTS = verbose # CT_SUITES = emqx_stats ## emqx_trie emqx_router emqx_frame emqx_mqtt_compat -CT_SUITES = emqx emqx_access emqx_base62 emqx_broker emqx_client emqx_cm emqx_frame emqx_guid emqx_inflight \ +CT_SUITES = emqx emqx_session emqx_access emqx_base62 emqx_broker emqx_client emqx_cm emqx_frame emqx_guid emqx_inflight \ emqx_json emqx_keepalive emqx_lib emqx_metrics emqx_misc emqx_mod emqx_mqtt_caps \ emqx_mqtt_compat emqx_mqtt_properties emqx_mqueue emqx_net emqx_pqueue emqx_router emqx_sm \ - emqx_stats emqx_tables emqx_time emqx_topic emqx_trie emqx_vm emqx_zone + emqx_stats emqx_tables emqx_time emqx_topic emqx_trie emqx_vm emqx_zone emqx_mountpoint CT_OPTS = -cover test/ct.cover.spec -erl_args -name emqxct@127.0.0.1 diff --git a/test/emqx_mock_client.erl b/test/emqx_mock_client.erl index 164b6d4ea..0536b8bac 100644 --- a/test/emqx_mock_client.erl +++ b/test/emqx_mock_client.erl @@ -16,13 +16,15 @@ -behaviour(gen_server). --export([start_link/1, open_session/3, close_session/2, stop/1]). +-export([start_link/1, open_session/3, close_session/2, stop/1, get_last_message/0]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -record(state, {clean_start, client_id, client_pid}). +-define(TAB, messages). + start_link(ClientId) -> gen_server:start_link(?MODULE, [ClientId], []). @@ -35,13 +37,25 @@ close_session(ClientPid, SessPid) -> stop(CPid) -> gen_server:call(CPid, stop). +get_last_message() -> + [{last_message, Msg}] = ets:lookup(?TAB, last_message), + Msg. + init([ClientId]) -> - {ok, #state{clean_start = true, client_id = ClientId}}. + Result = lists:member(?TAB, ets:all()), + if Result == false -> + ets:new(?TAB, [set, named_table]); + true -> ok + end, + {ok, + #state{clean_start = true, + client_id = ClientId} + }. handle_call({start_session, ClientPid, ClientId, Zone}, _From, State) -> Attrs = #{ zone => Zone, client_id => ClientId, - conn_pid => ClientPid, + conn_pid => ClientPid, clean_start => true, username => undefined, conn_props => undefined @@ -49,7 +63,7 @@ handle_call({start_session, ClientPid, ClientId, Zone}, _From, State) -> {ok, SessPid} = emqx_sm:open_session(Attrs), {reply, {ok, SessPid}, State#state{ clean_start = true, - client_id = ClientId, + client_id = ClientId, client_pid = ClientPid }}; @@ -66,6 +80,9 @@ handle_call(_Request, _From, State) -> handle_cast(_Msg, State) -> {noreply, State}. +handle_info({_, Msg}, State) -> + ets:insert(?TAB, {last_message, Msg}), + {noreply, State}; handle_info(_Info, State) -> {noreply, State}. diff --git a/test/emqx_mountpoint_SUITE.erl b/test/emqx_mountpoint_SUITE.erl new file mode 100644 index 000000000..61d8d3652 --- /dev/null +++ b/test/emqx_mountpoint_SUITE.erl @@ -0,0 +1,36 @@ +%% Copyright (c) 2018 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(emqx_mountpoint_SUITE). + +-compile(export_all). +-compile(nowarn_export_all). + +-include("emqx.hrl"). +-include("emqx_mqtt.hrl"). + +-include_lib("eunit/include/eunit.hrl"). + +all() -> [t_mount_unmount, t_replvar]. + +t_mount_unmount(_) -> + Msg = emqx_message:make(<<"clientid">>, <<"topic">>, <<"payload">>), + Msg2 = emqx_mountpoint:mount(<<"mount">>, Msg), + ?assertEqual(<<"mounttopic">>, Msg2#message.topic), + TopicFilter = [{<<"mounttopic">>, #{qos => ?QOS2}}], + TopicFilter = emqx_mountpoint:mount(<<"mount">>, [{<<"topic">>, #{qos => ?QOS2}}]), + Msg = emqx_mountpoint:unmount(<<"mount">>, Msg2). + +t_replvar(_) -> + <<"mount/test/clientid">> = emqx_mountpoint:replvar(<<"mount/%u/%c">>, #{client_id => <<"clientid">>, username => <<"test">>}). diff --git a/test/emqx_session_SUITE.erl b/test/emqx_session_SUITE.erl new file mode 100644 index 000000000..f2da10b74 --- /dev/null +++ b/test/emqx_session_SUITE.erl @@ -0,0 +1,57 @@ +%% Copyright (c) 2018 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(emqx_session_SUITE). + +-compile(export_all). +-compile(nowarn_export_all). + +-include_lib("common_test/include/ct.hrl"). + +all() -> [t_session_all]. + +t_session_all(_) -> + emqx_ct_broker_helpers:run_setup_steps(), + ClientId = <<"ClientId">>, + {ok, ConnPid} = emqx_mock_client:start_link(ClientId), + {ok, SPid} = emqx_mock_client:open_session(ConnPid, ClientId, internal), + Message1 = emqx_message:make(<<"ClientId">>, 2, <<"topic">>, <<"hello">>), + emqx_session:subscribe(SPid, [{<<"topic">>, #{qos => 2}}]), + emqx_session:subscribe(SPid, [{<<"topic">>, #{qos => 1}}]), + timer:sleep(200), + [{<<"topic">>, _}] = emqx:subscriptions({SPid, <<"ClientId">>}), + emqx_session:publish(SPid, 1, Message1), + timer:sleep(200), + {publish, 1, _} = emqx_mock_client:get_last_message(), + emqx_session:puback(SPid, 2), + emqx_session:puback(SPid, 3, reasoncode), + emqx_session:pubrec(SPid, 4), + emqx_session:pubrec(SPid, 5, reasoncode), + emqx_session:pubrel(SPid, 6, reasoncode), + emqx_session:pubcomp(SPid, 7, reasoncode), + timer:sleep(200), + 2 = emqx_metrics:val('packets/puback/missed'), + 2 = emqx_metrics:val('packets/pubrec/missed'), + 1 = emqx_metrics:val('packets/pubrel/missed'), + 1 = emqx_metrics:val('packets/pubcomp/missed'), + Attrs = emqx_session:attrs(SPid), + Info = emqx_session:info(SPid), + Stats = emqx_session:stats(SPid), + ClientId = proplists:get_value(client_id, Attrs), + ClientId = proplists:get_value(client_id, Info), + 1 = proplists:get_value(subscriptions_count, Stats), + emqx_session:unsubscribe(SPid, [<<"topic">>]), + timer:sleep(200), + [] = emqx:subscriptions({SPid, <<"clientId">>}), + emqx_mock_client:close_session(ConnPid, SPid). diff --git a/test/emqx_sm_SUITE.erl b/test/emqx_sm_SUITE.erl index 5bc096cd8..24999f2a0 100644 --- a/test/emqx_sm_SUITE.erl +++ b/test/emqx_sm_SUITE.erl @@ -26,7 +26,7 @@ t_open_close_session(_) -> {ok, ClientPid} = emqx_mock_client:start_link(<<"client">>), Attrs = #{clean_start => true, client_id => <<"client">>, conn_pid => ClientPid, zone => internal, username => <<"zhou">>, conn_props => #{}}, - {ok, _SPid} = emqx_sm:open_session(Attrs), + {ok, SPid} = emqx_sm:open_session(Attrs), [{<<"client">>, SPid}] = emqx_sm:lookup_session(<<"client">>), SPid = emqx_sm:lookup_session_pid(<<"client">>), {ok, NewConnPid} = emqx_mock_client:start_link(<<"client">>),