fix(coap): trigger disconnect logic when the client manually disconnected

This commit is contained in:
firest 2022-03-23 14:19:00 +08:00
parent ab40036a45
commit ab51d8ab8e
3 changed files with 40 additions and 3 deletions

View File

@ -638,7 +638,8 @@ process_connection({open, Req}, Result,
Channel);
process_connection({close, Msg}, _, Channel, _) ->
Reply = emqx_coap_message:piggyback({ok, deleted}, Msg),
{shutdown, close, Reply, Channel}.
NChannel = ensure_disconnected(close_connection, Channel),
{shutdown, close, Reply, NChannel}.
process_subscribe({Sub, Msg}, Result, #channel{session = Session} = Channel, Iter) ->
Result2 = emqx_coap_session:process_subscribe(Sub, Msg, Result, Session),

View File

@ -292,12 +292,37 @@ t_clients_get_subscription_api(_) ->
end,
with_connection(Fun).
t_on_offline_event(_) ->
Fun = fun(Channel) ->
application:start(emqx_modules),
emqx_event_message_SUITE:load_config(),
timer:sleep(100),
emqx_event_message:enable(),
timer:sleep(200),
emqx_broker:subscribe(<<"$event/#">>),
Token = connection(Channel),
?assertMatch(#message{topic = <<"$event/client_connected">>}, receive_deliver(500)),
disconnection(Channel, Token),
?assertMatch(#message{topic = <<"$event/client_disconnected">>}, receive_deliver(500)),
emqx_broker:unsubscribe(<<"$event/#">>),
emqx_event_message:disable(),
application:stop(emqx_modules),
timer:sleep(200)
end,
do(Fun).
%%--------------------------------------------------------------------
%% helpers
connection(Channel) ->
URI = ?MQTT_PREFIX ++
"/connection?clientid=client1&username=admin&password=public",
"/connection?clientid=client1&username=admin&password=public",
Req = make_req(post),
{ok, created, Data} = do_request(Channel, URI, Req),
#coap_content{payload = BinToken} = Data,
@ -378,3 +403,11 @@ with_connection(Action) ->
timer:sleep(100)
end,
do(Fun).
receive_deliver(Wait) ->
receive
{deliver, _, Msg} ->
Msg
after Wait ->
{error, timeout}
end.

View File

@ -42,9 +42,12 @@ all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) ->
emqx_common_test_helpers:boot_modules(all),
emqx_common_test_helpers:start_apps([emqx_modules]),
ok = emqx_common_test_helpers:load_config(emqx_modules_schema, ?EVENT_MESSAGE),
load_config(),
Config.
load_config() ->
ok = emqx_common_test_helpers:load_config(emqx_modules_schema, ?EVENT_MESSAGE).
end_per_suite(_Config) ->
emqx_common_test_helpers:stop_apps([emqx_modules]).