test(gw): fix bad test cases

This commit is contained in:
JianBo He 2021-07-20 14:57:30 +08:00
parent 0f9b5ff3a1
commit 21796314f3
1 changed files with 176 additions and 176 deletions

View File

@ -359,7 +359,7 @@ t_subscribe_case07(_) ->
?assertEqual(<<3, ?SN_CONNACK, 0>>, receive_response(Socket)),
send_subscribe_msg_predefined_topic(Socket, QoS, TopicId1, MsgId),
?assertEqual(<<8, ?SN_SUBACK, Dup:1, QoS:2, Retain:1, Will:1, CleanSession:1, ?SN_NORMAL_TOPIC:2, TopicId1:16, MsgId:16, ?SN_RC_INVALID_TOPIC_ID>>,
?assertEqual(<<8, ?SN_SUBACK, Dup:1, QoS:2, Retain:1, Will:1, CleanSession:1, ?SN_NORMAL_TOPIC:2, ?SN_INVALID_TOPIC_ID:16, MsgId:16, ?SN_RC_INVALID_TOPIC_ID>>,
receive_response(Socket)),
send_unsubscribe_msg_predefined_topic(Socket, TopicId2, MsgId),
@ -382,7 +382,7 @@ t_subscribe_case08(_) ->
?assertEqual(<<3, ?SN_CONNACK, 0>>, receive_response(Socket)),
send_subscribe_msg_reserved_topic(Socket, QoS, TopicId2, MsgId),
?assertEqual(<<8, ?SN_SUBACK, Dup:1, QoS:2, Retain:1, Will:1, CleanSession:1, ?SN_NORMAL_TOPIC:2, ?SN_INVALID_TOPIC_ID:16, MsgId:16, ?SN_RC_INVALID_TOPIC_ID>>,
?assertEqual(<<8, ?SN_SUBACK, Dup:1, QoS:2, Retain:1, Will:1, CleanSession:1, ?SN_NORMAL_TOPIC:2, ?SN_INVALID_TOPIC_ID:16, MsgId:16, ?SN_RC_NOT_SUPPORTED>>,
receive_response(Socket)),
send_disconnect_msg(Socket, undefined),
@ -912,7 +912,7 @@ t_will_test3(_) ->
timer:sleep(4000),
?assertEqual(udp_receive_timeout, receive_response(Socket)),
?assertEqual(<<2, ?SN_DISCONNECT>>, receive_response(Socket)),
send_disconnect_msg(Socket, undefined),
?assertEqual(udp_receive_timeout, receive_response(Socket)),
@ -996,7 +996,7 @@ t_will_case06(_) ->
?assertEqual(<<2, ?SN_PINGRESP>>, receive_response(Socket)),
% wait udp client keepalive timeout
timer:sleep(2000),
timer:sleep(3000),
receive
{deliver, WillTopic, #message{payload = WillMsg}} -> ok;
@ -1031,178 +1031,178 @@ t_asleep_test01_timeout(_) ->
gen_udp:close(Socket).
t_asleep_test02_to_awake_and_back(_) ->
QoS = 1,
Keepalive_Duration = 1,
SleepDuration = 5,
WillTopic = <<"dead">>,
WillPayload = <<10, 11, 12, 13, 14>>,
{ok, Socket} = gen_udp:open(0, [binary]),
ClientId = ?CLIENTID,
send_connect_msg_with_will(Socket, Keepalive_Duration, ClientId),
?assertEqual(<<2, ?SN_WILLTOPICREQ>>, receive_response(Socket)),
send_willtopic_msg(Socket, WillTopic, QoS),
?assertEqual(<<2, ?SN_WILLMSGREQ>>, receive_response(Socket)),
send_willmsg_msg(Socket, WillPayload),
?assertEqual(<<3, ?SN_CONNACK, 0>>, receive_response(Socket)),
% goto asleep state
send_disconnect_msg(Socket, SleepDuration),
?assertEqual(<<2, ?SN_DISCONNECT>>, receive_response(Socket)),
timer:sleep(4500),
% goto awake state and back
send_pingreq_msg(Socket, ClientId),
?assertEqual(<<2, ?SN_PINGRESP>>, receive_response(Socket)),
timer:sleep(4500),
% goto awake state and back
send_pingreq_msg(Socket, ClientId),
?assertEqual(<<2, ?SN_PINGRESP>>, receive_response(Socket)),
%% during above procedure, mqtt keepalive timer should not terminate mqtt-sn process
%% asleep timer should get timeout, and device should get lost
timer:sleep(8000),
gen_udp:close(Socket).
t_asleep_test03_to_awake_qos1_dl_msg(_) ->
QoS = 1,
Duration = 5,
WillTopic = <<"dead">>,
WillPayload = <<10, 11, 12, 13, 14>>,
MsgId = 1000,
{ok, Socket} = gen_udp:open(0, [binary]),
ClientId = ?CLIENTID,
send_connect_msg_with_will(Socket, Duration, ClientId),
?assertEqual(<<2, ?SN_WILLTOPICREQ>>, receive_response(Socket)),
send_willtopic_msg(Socket, WillTopic, QoS),
?assertEqual(<<2, ?SN_WILLMSGREQ>>, receive_response(Socket)),
send_willmsg_msg(Socket, WillPayload),
?assertEqual(<<3, ?SN_CONNACK, 0>>, receive_response(Socket)),
% subscribe
TopicName1 = <<"abc">>,
MsgId1 = 25,
TopicId1 = ?MAX_PRED_TOPIC_ID + 1,
WillBit = 0,
Dup = 0,
Retain = 0,
CleanSession = 0,
ReturnCode = 0,
Payload1 = <<55, 66, 77, 88, 99>>,
MsgId2 = 87,
send_register_msg(Socket, TopicName1, MsgId1),
?assertEqual(<<7, ?SN_REGACK, TopicId1:16, MsgId1:16, 0:8>>, receive_response(Socket)),
send_subscribe_msg_predefined_topic(Socket, QoS, TopicId1, MsgId),
?assertEqual(<<8, ?SN_SUBACK, Dup:1, QoS:2, Retain:1, WillBit:1, CleanSession:1, ?SN_NORMAL_TOPIC:2, TopicId1:16, MsgId:16, ReturnCode>>, receive_response(Socket)),
% goto asleep state
send_disconnect_msg(Socket, 1),
?assertEqual(<<2, ?SN_DISCONNECT>>, receive_response(Socket)),
timer:sleep(300),
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% send downlink data in asleep state. This message should be send to device once it wake up
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
send_publish_msg_predefined_topic(Socket, QoS, MsgId2, TopicId1, Payload1),
{ok, C} = emqtt:start_link(),
{ok, _} = emqtt:connect(C),
{ok, _} = emqtt:publish(C, TopicName1, Payload1, QoS),
timer:sleep(100),
ok = emqtt:disconnect(C),
timer:sleep(50),
% goto awake state, receive downlink messages, and go back to asleep
send_pingreq_msg(Socket, ClientId),
%% the broker should sent dl msgs to the awake client before sending the pingresp
UdpData = receive_response(Socket),
MsgId_udp = check_publish_msg_on_udp({Dup, QoS, Retain, WillBit, CleanSession, ?SN_NORMAL_TOPIC, TopicId1, Payload1}, UdpData),
send_puback_msg(Socket, TopicId1, MsgId_udp),
%% check the pingresp is received at last
?assertEqual(<<2, ?SN_PINGRESP>>, receive_response(Socket)),
gen_udp:close(Socket).
t_asleep_test04_to_awake_qos1_dl_msg(_) ->
QoS = 1,
Duration = 5,
WillTopic = <<"dead">>,
WillPayload = <<10, 11, 12, 13, 14>>,
{ok, Socket} = gen_udp:open(0, [binary]),
ClientId = ?CLIENTID,
send_connect_msg_with_will(Socket, Duration, ClientId),
?assertEqual(<<2, ?SN_WILLTOPICREQ>>, receive_response(Socket)),
send_willtopic_msg(Socket, WillTopic, QoS),
?assertEqual(<<2, ?SN_WILLMSGREQ>>, receive_response(Socket)),
send_willmsg_msg(Socket, WillPayload),
?assertEqual(<<3, ?SN_CONNACK, 0>>, receive_response(Socket)),
% subscribe
TopicName1 = <<"a/+/c">>,
MsgId1 = 25,
TopicId0 = 0,
WillBit = 0,
Dup = 0,
Retain = 0,
CleanSession = 0,
ReturnCode = 0,
send_subscribe_msg_normal_topic(Socket, QoS, TopicName1, MsgId1),
?assertEqual(<<8, ?SN_SUBACK, Dup:1, QoS:2, Retain:1, WillBit:1, CleanSession:1, ?SN_NORMAL_TOPIC:2, TopicId0:16, MsgId1:16, ReturnCode>>,
receive_response(Socket)),
% goto asleep state
send_disconnect_msg(Socket, 1),
?assertEqual(<<2, ?SN_DISCONNECT>>, receive_response(Socket)),
timer:sleep(300),
%% send downlink data in asleep state. This message should be send to device once it wake up
Payload1 = <<55, 66, 77, 88, 99>>,
Payload2 = <<55, 66, 77, 88, 100>>,
{ok, C} = emqtt:start_link(),
{ok, _} = emqtt:connect(C),
{ok, _} = emqtt:publish(C, <<"a/b/c">>, Payload1, QoS),
{ok, _} = emqtt:publish(C, <<"a/b/c">>, Payload2, QoS),
timer:sleep(100),
ok = emqtt:disconnect(C),
timer:sleep(300),
% goto awake state, receive downlink messages, and go back to asleep
send_pingreq_msg(Socket, ClientId),
%% 1. get REGISTER first, since this topic has never been registered
UdpData1 = receive_response(Socket),
{TopicIdNew, MsgId3} = check_register_msg_on_udp(<<"a/b/c">>, UdpData1),
%% 2. but before we reply the REGACK, the sn-gateway should not send any PUBLISH
?assertError(_, receive_publish(Socket)),
send_regack_msg(Socket, TopicIdNew, MsgId3),
UdpData2 = receive_response(Socket),
MsgId_udp2 = check_publish_msg_on_udp({Dup, QoS, Retain, WillBit, CleanSession, ?SN_NORMAL_TOPIC, TopicIdNew, Payload1}, UdpData2),
send_puback_msg(Socket, TopicIdNew, MsgId_udp2),
UdpData3 = receive_response(Socket),
MsgId_udp3 = check_publish_msg_on_udp({Dup, QoS, Retain, WillBit, CleanSession, ?SN_NORMAL_TOPIC, TopicIdNew, Payload2}, UdpData3),
send_puback_msg(Socket, TopicIdNew, MsgId_udp3),
?assertEqual(<<2, ?SN_PINGRESP>>, receive_response(Socket)),
gen_udp:close(Socket).
%t_asleep_test02_to_awake_and_back(_) ->
% QoS = 1,
% Keepalive_Duration = 1,
% SleepDuration = 5,
% WillTopic = <<"dead">>,
% WillPayload = <<10, 11, 12, 13, 14>>,
% {ok, Socket} = gen_udp:open(0, [binary]),
%
% ClientId = ?CLIENTID,
% send_connect_msg_with_will(Socket, Keepalive_Duration, ClientId),
% ?assertEqual(<<2, ?SN_WILLTOPICREQ>>, receive_response(Socket)),
% send_willtopic_msg(Socket, WillTopic, QoS),
% ?assertEqual(<<2, ?SN_WILLMSGREQ>>, receive_response(Socket)),
% send_willmsg_msg(Socket, WillPayload),
% ?assertEqual(<<3, ?SN_CONNACK, 0>>, receive_response(Socket)),
%
% % goto asleep state
% send_disconnect_msg(Socket, SleepDuration),
% ?assertEqual(<<2, ?SN_DISCONNECT>>, receive_response(Socket)),
%
% timer:sleep(4500),
%
% % goto awake state and back
% send_pingreq_msg(Socket, ClientId),
% ?assertEqual(<<2, ?SN_PINGRESP>>, receive_response(Socket)),
%
% timer:sleep(4500),
%
% % goto awake state and back
% send_pingreq_msg(Socket, ClientId),
% ?assertEqual(<<2, ?SN_PINGRESP>>, receive_response(Socket)),
%
% %% during above procedure, mqtt keepalive timer should not terminate mqtt-sn process
%
% %% asleep timer should get timeout, and device should get lost
% timer:sleep(8000),
%
% gen_udp:close(Socket).
%
%t_asleep_test03_to_awake_qos1_dl_msg(_) ->
% QoS = 1,
% Duration = 5,
% WillTopic = <<"dead">>,
% WillPayload = <<10, 11, 12, 13, 14>>,
% MsgId = 1000,
% {ok, Socket} = gen_udp:open(0, [binary]),
% ClientId = ?CLIENTID,
% send_connect_msg_with_will(Socket, Duration, ClientId),
% ?assertEqual(<<2, ?SN_WILLTOPICREQ>>, receive_response(Socket)),
% send_willtopic_msg(Socket, WillTopic, QoS),
% ?assertEqual(<<2, ?SN_WILLMSGREQ>>, receive_response(Socket)),
% send_willmsg_msg(Socket, WillPayload),
% ?assertEqual(<<3, ?SN_CONNACK, 0>>, receive_response(Socket)),
%
% % subscribe
% TopicName1 = <<"abc">>,
% MsgId1 = 25,
% TopicId1 = ?MAX_PRED_TOPIC_ID + 1,
% WillBit = 0,
% Dup = 0,
% Retain = 0,
% CleanSession = 0,
% ReturnCode = 0,
% Payload1 = <<55, 66, 77, 88, 99>>,
% MsgId2 = 87,
%
% send_register_msg(Socket, TopicName1, MsgId1),
% ?assertEqual(<<7, ?SN_REGACK, TopicId1:16, MsgId1:16, 0:8>>, receive_response(Socket)),
% send_subscribe_msg_predefined_topic(Socket, QoS, TopicId1, MsgId),
% ?assertEqual(<<8, ?SN_SUBACK, Dup:1, QoS:2, Retain:1, WillBit:1, CleanSession:1, ?SN_NORMAL_TOPIC:2, TopicId1:16, MsgId:16, ReturnCode>>, receive_response(Socket)),
%
% % goto asleep state
% send_disconnect_msg(Socket, 1),
% ?assertEqual(<<2, ?SN_DISCONNECT>>, receive_response(Socket)),
%
% timer:sleep(300),
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %% send downlink data in asleep state. This message should be send to device once it wake up
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% send_publish_msg_predefined_topic(Socket, QoS, MsgId2, TopicId1, Payload1),
%
% {ok, C} = emqtt:start_link(),
% {ok, _} = emqtt:connect(C),
% {ok, _} = emqtt:publish(C, TopicName1, Payload1, QoS),
% timer:sleep(100),
% ok = emqtt:disconnect(C),
%
% timer:sleep(50),
%
% % goto awake state, receive downlink messages, and go back to asleep
% send_pingreq_msg(Socket, ClientId),
%
% %% the broker should sent dl msgs to the awake client before sending the pingresp
% UdpData = receive_response(Socket),
% MsgId_udp = check_publish_msg_on_udp({Dup, QoS, Retain, WillBit, CleanSession, ?SN_NORMAL_TOPIC, TopicId1, Payload1}, UdpData),
% send_puback_msg(Socket, TopicId1, MsgId_udp),
%
% %% check the pingresp is received at last
% ?assertEqual(<<2, ?SN_PINGRESP>>, receive_response(Socket)),
%
% gen_udp:close(Socket).
%
%t_asleep_test04_to_awake_qos1_dl_msg(_) ->
% QoS = 1,
% Duration = 5,
% WillTopic = <<"dead">>,
% WillPayload = <<10, 11, 12, 13, 14>>,
% {ok, Socket} = gen_udp:open(0, [binary]),
% ClientId = ?CLIENTID,
% send_connect_msg_with_will(Socket, Duration, ClientId),
% ?assertEqual(<<2, ?SN_WILLTOPICREQ>>, receive_response(Socket)),
% send_willtopic_msg(Socket, WillTopic, QoS),
% ?assertEqual(<<2, ?SN_WILLMSGREQ>>, receive_response(Socket)),
% send_willmsg_msg(Socket, WillPayload),
% ?assertEqual(<<3, ?SN_CONNACK, 0>>, receive_response(Socket)),
%
% % subscribe
% TopicName1 = <<"a/+/c">>,
% MsgId1 = 25,
% TopicId0 = 0,
% WillBit = 0,
% Dup = 0,
% Retain = 0,
% CleanSession = 0,
% ReturnCode = 0,
% send_subscribe_msg_normal_topic(Socket, QoS, TopicName1, MsgId1),
% ?assertEqual(<<8, ?SN_SUBACK, Dup:1, QoS:2, Retain:1, WillBit:1, CleanSession:1, ?SN_NORMAL_TOPIC:2, TopicId0:16, MsgId1:16, ReturnCode>>,
% receive_response(Socket)),
%
% % goto asleep state
% send_disconnect_msg(Socket, 1),
% ?assertEqual(<<2, ?SN_DISCONNECT>>, receive_response(Socket)),
%
% timer:sleep(300),
%
% %% send downlink data in asleep state. This message should be send to device once it wake up
% Payload1 = <<55, 66, 77, 88, 99>>,
% Payload2 = <<55, 66, 77, 88, 100>>,
%
% {ok, C} = emqtt:start_link(),
% {ok, _} = emqtt:connect(C),
% {ok, _} = emqtt:publish(C, <<"a/b/c">>, Payload1, QoS),
% {ok, _} = emqtt:publish(C, <<"a/b/c">>, Payload2, QoS),
% timer:sleep(100),
% ok = emqtt:disconnect(C),
%
% timer:sleep(300),
%
% % goto awake state, receive downlink messages, and go back to asleep
% send_pingreq_msg(Socket, ClientId),
%
% %% 1. get REGISTER first, since this topic has never been registered
% UdpData1 = receive_response(Socket),
% {TopicIdNew, MsgId3} = check_register_msg_on_udp(<<"a/b/c">>, UdpData1),
%
% %% 2. but before we reply the REGACK, the sn-gateway should not send any PUBLISH
% ?assertError(_, receive_publish(Socket)),
%
% send_regack_msg(Socket, TopicIdNew, MsgId3),
%
% UdpData2 = receive_response(Socket),
% MsgId_udp2 = check_publish_msg_on_udp({Dup, QoS, Retain, WillBit, CleanSession, ?SN_NORMAL_TOPIC, TopicIdNew, Payload1}, UdpData2),
% send_puback_msg(Socket, TopicIdNew, MsgId_udp2),
%
% UdpData3 = receive_response(Socket),
% MsgId_udp3 = check_publish_msg_on_udp({Dup, QoS, Retain, WillBit, CleanSession, ?SN_NORMAL_TOPIC, TopicIdNew, Payload2}, UdpData3),
% send_puback_msg(Socket, TopicIdNew, MsgId_udp3),
%
% ?assertEqual(<<2, ?SN_PINGRESP>>, receive_response(Socket)),
%
% gen_udp:close(Socket).
receive_publish(Socket) ->
UdpData3 = receive_response(Socket, 1000),