-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathv5_SUITE.erl
2146 lines (1961 loc) · 98.3 KB
/
v5_SUITE.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
%% This test suite covers MQTT 5.0 features.
-module(v5_SUITE).
-compile([export_all,
nowarn_export_all]).
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
-include_lib("amqp_client/include/amqp_client.hrl").
-import(util,
[all_connection_pids/1,
start_client/4,
connect/2, connect/3, connect/4,
assert_message_expiry_interval/2,
non_clean_sess_opts/0,
expect_publishes/3
]).
-import(rabbit_ct_broker_helpers,
[rpc/4]).
-import(rabbit_ct_helpers,
[eventually/1]).
-define(APP, rabbitmq_mqtt).
-define(QUEUE_TTL_KEY, <<"x-expires">>).
%% defined in MQTT v5 (not in v4 or v3)
-define(RC_SUCCESS, 16#00).
-define(RC_NORMAL_DISCONNECTION, 16#00).
-define(RC_DISCONNECT_WITH_WILL, 16#04).
-define(RC_NO_SUBSCRIPTION_EXISTED, 16#11).
-define(RC_UNSPECIFIED_ERROR, 16#80).
-define(RC_PROTOCOL_ERROR, 16#82).
-define(RC_SERVER_SHUTTING_DOWN, 16#8B).
-define(RC_SESSION_TAKEN_OVER, 16#8E).
-define(RC_TOPIC_ALIAS_INVALID, 16#94).
-define(TIMEOUT, 30_000).
all() ->
[{group, mqtt}].
groups() ->
[
{mqtt, [],
[{cluster_size_1, [shuffle], cluster_size_1_tests()},
{cluster_size_3, [shuffle], cluster_size_3_tests()}
]}
].
cluster_size_1_tests() ->
[
client_set_max_packet_size_publish,
client_set_max_packet_size_connack,
client_set_max_packet_size_invalid,
message_expiry,
message_expiry_will_message,
message_expiry_retained_message,
session_expiry_classic_queue_disconnect_decrease,
session_expiry_quorum_queue_disconnect_decrease,
session_expiry_disconnect_zero_to_non_zero,
session_expiry_disconnect_non_zero_to_zero,
session_expiry_disconnect_infinity_to_zero,
session_expiry_disconnect_to_infinity,
session_expiry_reconnect_non_zero,
session_expiry_reconnect_zero,
session_expiry_reconnect_infinity_to_zero,
client_publish_qos2,
client_rejects_publish,
client_receive_maximum_min,
client_receive_maximum_large,
unsubscribe_success,
unsubscribe_topic_not_found,
subscription_option_no_local,
subscription_option_no_local_wildcards,
subscription_option_retain_as_published,
subscription_option_retain_as_published_wildcards,
subscription_option_retain_handling,
subscription_identifier,
subscription_identifier_amqp091,
subscription_identifier_at_most_once_dead_letter,
at_most_once_dead_letter_detect_cycle,
subscription_options_persisted,
subscription_options_modify,
subscription_options_modify_qos1,
subscription_options_modify_qos0,
session_upgrade_v3_v5_qos1,
session_upgrade_v3_v5_qos0,
session_upgrade_v3_v5_amqp091_pub,
compatibility_v3_v5,
session_upgrade_v3_v5_unsubscribe,
session_upgrade_v4_v5_no_queue_bind_permission,
amqp091_cc_header,
publish_property_content_type,
publish_property_payload_format_indicator,
publish_property_response_topic_correlation_data,
publish_property_user_property,
disconnect_with_will,
will_qos2,
will_delay_greater_than_session_expiry,
will_delay_less_than_session_expiry,
will_delay_equals_session_expiry,
will_delay_session_expiry_zero,
will_delay_reconnect_no_will,
will_delay_reconnect_with_will,
will_delay_session_takeover,
will_delay_message_expiry,
will_delay_message_expiry_publish_properties,
will_delay_properties,
will_properties,
retain_properties,
topic_alias_client_to_server,
topic_alias_server_to_client,
topic_alias_bidirectional,
topic_alias_invalid,
topic_alias_unknown,
topic_alias_disallowed,
topic_alias_retained_message,
topic_alias_disallowed_retained_message,
extended_auth,
headers_exchange,
consistent_hash_exchange
].
cluster_size_3_tests() ->
[session_migrate_v3_v5,
session_takeover_v3_v5,
will_delay_node_restart
].
suite() ->
[{timetrap, {minutes, 10}}].
%% -------------------------------------------------------------------
%% Testsuite setup/teardown.
%% -------------------------------------------------------------------
init_per_suite(Config) ->
rabbit_ct_helpers:log_environment(),
rabbit_ct_helpers:run_setup_steps(Config).
end_per_suite(Config) ->
rabbit_ct_helpers:run_teardown_steps(Config).
init_per_group(mqtt, Config) ->
rabbit_ct_helpers:set_config(Config, {websocket, false});
init_per_group(Group, Config0) ->
Nodes = case Group of
cluster_size_1 -> 1;
cluster_size_3 -> 3
end,
Suffix = rabbit_ct_helpers:testcase_absname(Config0, "", "-"),
Config1 = rabbit_ct_helpers:set_config(
Config0,
[{mqtt_version, v5},
{rmq_nodes_count, Nodes},
{rmq_nodename_suffix, Suffix}]),
Config = rabbit_ct_helpers:merge_app_env(
Config1,
{rabbit, [{quorum_tick_interval, 200}]}),
rabbit_ct_helpers:run_steps(
Config,
rabbit_ct_broker_helpers:setup_steps() ++
rabbit_ct_client_helpers:setup_steps()).
end_per_group(G, Config)
when G =:= cluster_size_1;
G =:= cluster_size_3 ->
rabbit_ct_helpers:run_steps(
Config,
rabbit_ct_client_helpers:teardown_steps() ++
rabbit_ct_broker_helpers:teardown_steps());
end_per_group(_, Config) ->
Config.
init_per_testcase(T, Config)
when T =:= session_expiry_disconnect_infinity_to_zero;
T =:= session_expiry_disconnect_to_infinity;
T =:= session_expiry_reconnect_infinity_to_zero ->
Par = max_session_expiry_interval_seconds,
{ok, Default} = rpc(Config, application, get_env, [?APP, Par]),
ok = rpc(Config, application, set_env, [?APP, Par, infinity]),
Config1 = rabbit_ct_helpers:set_config(Config, {Par, Default}),
init_per_testcase0(T, Config1);
init_per_testcase(T, Config) ->
init_per_testcase0(T, Config).
init_per_testcase0(Testcase, Config) ->
rabbit_ct_helpers:testcase_started(Config, Testcase).
end_per_testcase(T, Config)
when T =:= session_expiry_disconnect_infinity_to_zero;
T =:= session_expiry_disconnect_to_infinity;
T =:= session_expiry_reconnect_infinity_to_zero ->
Par = max_session_expiry_interval_seconds,
Default = ?config(Par, Config),
ok = rpc(Config, application, set_env, [?APP, Par, Default]),
end_per_testcase0(T, Config);
end_per_testcase(T, Config) ->
end_per_testcase0(T, Config).
end_per_testcase0(Testcase, Config) ->
%% Assert that every testcase cleaned up their MQTT sessions.
eventually(?_assertEqual([], rpc(Config, rabbit_amqqueue, list, []))),
rabbit_ct_helpers:testcase_finished(Config, Testcase).
%% -------------------------------------------------------------------
%% Testsuite cases
%% -------------------------------------------------------------------
client_set_max_packet_size_publish(Config) ->
NumRejectedBefore = dead_letter_metric(messages_dead_lettered_rejected_total, Config),
Topic = ClientId = atom_to_binary(?FUNCTION_NAME),
MaxPacketSize = 500,
C = connect(ClientId, Config, [{properties, #{'Maximum-Packet-Size' => MaxPacketSize}}]),
{ok, _, [1]} = emqtt:subscribe(C, Topic, qos1),
PayloadTooLarge = binary:copy(<<"x">>, MaxPacketSize + 1),
%% We expect the PUBLISH from client to server to succeed.
?assertMatch({ok, _}, emqtt:publish(C, Topic, PayloadTooLarge, [{qos, 1}])),
%% We expect the server to drop the PUBLISH packet prior to sending to the client
%% because the packet is larger than what the client is able to receive.
assert_nothing_received(),
NumRejected = dead_letter_metric(messages_dead_lettered_rejected_total, Config) - NumRejectedBefore,
?assertEqual(1, NumRejected),
ok = emqtt:disconnect(C),
ok.
client_set_max_packet_size_connack(Config) ->
{C, Connect} = start_client(?FUNCTION_NAME, Config, 0,
[{properties, #{'Maximum-Packet-Size' => 2}},
{connect_timeout, 1}]),
unlink(C),
%% We expect the server to drop the CONNACK packet because it's larger than 2 bytes.
?assertEqual({error, connack_timeout}, Connect(C)).
%% "It is a Protocol Error to include the Receive Maximum
%% value more than once or for it to have the value 0."
client_set_max_packet_size_invalid(Config) ->
{C, Connect} = start_client(?FUNCTION_NAME, Config, 0,
[{properties, #{'Maximum-Packet-Size' => 0}}]),
unlink(C),
?assertMatch({error, _}, Connect(C)).
message_expiry(Config) ->
NumExpiredBefore = dead_letter_metric(messages_dead_lettered_expired_total, Config),
Topic = ClientId = atom_to_binary(?FUNCTION_NAME),
Pub = connect(<<"publisher">>, Config),
Sub1 = connect(ClientId, Config, non_clean_sess_opts()),
{ok, _, [1]} = emqtt:subscribe(Sub1, Topic, qos1),
ok = emqtt:disconnect(Sub1),
{ok, _} = emqtt:publish(Pub, Topic, #{'Message-Expiry-Interval' => 1}, <<"m1">>, [{qos, 1}]),
{ok, _} = emqtt:publish(Pub, Topic, #{}, <<"m2">>, [{qos, 1}]),
{ok, _} = emqtt:publish(Pub, Topic, #{'Message-Expiry-Interval' => 10}, <<"m3">>, [{qos, 1}]),
{ok, _} = emqtt:publish(Pub, Topic, #{'Message-Expiry-Interval' => 2}, <<"m4">>, [{qos, 1}]),
timer:sleep(2001),
Sub2 = connect(ClientId, Config, non_clean_sess_opts()),
receive {publish, #{client_pid := Sub2,
topic := Topic,
payload := <<"m2">>,
properties := Props}}
when map_size(Props) =:= 0 -> ok
after ?TIMEOUT -> ct:fail("did not receive m2")
end,
receive {publish, #{client_pid := Sub2,
topic := Topic,
payload := <<"m3">>,
%% "The PUBLISH packet sent to a Client by the Server MUST contain a Message
%% Expiry Interval set to the received value minus the time that the
%% Application Message has been waiting in the Server" [MQTT-3.3.2-6]
properties := #{'Message-Expiry-Interval' := MEI}}} ->
assert_message_expiry_interval(10 - 2, MEI)
after ?TIMEOUT -> ct:fail("did not receive m3")
end,
assert_nothing_received(),
NumExpired = dead_letter_metric(messages_dead_lettered_expired_total, Config) - NumExpiredBefore,
?assertEqual(2, NumExpired),
ok = emqtt:disconnect(Pub),
ok = emqtt:disconnect(Sub2),
Sub3 = connect(ClientId, Config, [{clean_start, true}]),
ok = emqtt:disconnect(Sub3).
message_expiry_will_message(Config) ->
NumExpiredBefore = dead_letter_metric(messages_dead_lettered_expired_total, Config),
Topic = ClientId = atom_to_binary(?FUNCTION_NAME),
Opts = [{will_topic, Topic},
{will_payload, <<"will payload">>},
{will_qos, 1},
{will_props, #{'Message-Expiry-Interval' => 1}}
],
Pub = connect(<<"will-publisher">>, Config, Opts),
Sub1 = connect(ClientId, Config, non_clean_sess_opts()),
{ok, _, [1]} = emqtt:subscribe(Sub1, Topic, qos1),
ok = emqtt:disconnect(Sub1),
unlink(Pub),
erlang:exit(Pub, trigger_will_message),
%% Wait for will message to expire.
timer:sleep(1100),
NumExpired = dead_letter_metric(messages_dead_lettered_expired_total, Config) - NumExpiredBefore,
?assertEqual(1, NumExpired),
Sub2 = connect(ClientId, Config, [{clean_start, true}]),
assert_nothing_received(),
ok = emqtt:disconnect(Sub2).
message_expiry_retained_message(Config) ->
Pub = connect(<<"publisher">>, Config),
{ok, _} = emqtt:publish(Pub, <<"topic1">>, #{'Message-Expiry-Interval' => 100},
<<"m1.1">>, [{retain, true}, {qos, 1}]),
{ok, _} = emqtt:publish(Pub, <<"topic2">>, #{'Message-Expiry-Interval' => 2},
<<"m2">>, [{retain, true}, {qos, 1}]),
{ok, _} = emqtt:publish(Pub, <<"topic3">>, #{'Message-Expiry-Interval' => 100},
<<"m3.1">>, [{retain, true}, {qos, 1}]),
{ok, _} = emqtt:publish(Pub, <<"topic4">>, #{'Message-Expiry-Interval' => 100},
<<"m4">>, [{retain, true}, {qos, 1}]),
{ok, _} = emqtt:publish(Pub, <<"topic1">>, #{'Message-Expiry-Interval' => 2},
<<"m1.2">>, [{retain, true}, {qos, 1}]),
{ok, _} = emqtt:publish(Pub, <<"topic2">>, #{'Message-Expiry-Interval' => 2},
<<>>, [{retain, true}, {qos, 1}]),
{ok, _} = emqtt:publish(Pub, <<"topic3">>, #{},
<<"m3.2">>, [{retain, true}, {qos, 1}]),
timer:sleep(2001),
%% Expectations:
%% topic1 expired because 2 seconds elapsed
%% topic2 is not retained because it got deleted
%% topic3 is retained because its new message does not have an Expiry-Interval set
%% topic4 is retained because 100 seconds have not elapsed
Sub = connect(<<"subscriber">>, Config),
{ok, _, [1,1,1,1]} = emqtt:subscribe(Sub, [{<<"topic1">>, qos1},
{<<"topic2">>, qos1},
{<<"topic3">>, qos1},
{<<"topic4">>, qos1}]),
receive {publish, #{client_pid := Sub,
retain := true,
topic := <<"topic3">>,
payload := <<"m3.2">>,
properties := Props}}
when map_size(Props) =:= 0 -> ok
after ?TIMEOUT -> ct:fail("did not topic3")
end,
receive {publish, #{client_pid := Sub,
retain := true,
topic := <<"topic4">>,
payload := <<"m4">>,
properties := #{'Message-Expiry-Interval' := MEI}}} ->
assert_message_expiry_interval(100 - 2, MEI)
after ?TIMEOUT -> ct:fail("did not receive topic4")
end,
assert_nothing_received(),
ok = emqtt:disconnect(Pub),
ok = emqtt:disconnect(Sub).
session_expiry_classic_queue_disconnect_decrease(Config) ->
ok = session_expiry_disconnect_decrease(rabbit_classic_queue, Config).
session_expiry_quorum_queue_disconnect_decrease(Config) ->
ok = rpc(Config, application, set_env, [?APP, durable_queue_type, quorum]),
ok = session_expiry_disconnect_decrease(rabbit_quorum_queue, Config),
ok = rpc(Config, application, unset_env, [?APP, durable_queue_type]).
session_expiry_disconnect_decrease(QueueType, Config) ->
ClientId = ?FUNCTION_NAME,
C1 = connect(ClientId, Config, [{properties, #{'Session-Expiry-Interval' => 100}}]),
{ok, _, [1]} = emqtt:subscribe(C1, <<"t/1">>, qos1),
[Q1] = rpc(Config, rabbit_amqqueue, list, []),
?assertEqual(QueueType,
amqqueue:get_type(Q1)),
?assertEqual({long, 100_000},
rabbit_misc:table_lookup(amqqueue:get_arguments(Q1), ?QUEUE_TTL_KEY)),
%% DISCONNECT decreases Session Expiry Interval from 100 seconds to 1 second.
ok = emqtt:disconnect(C1, ?RC_NORMAL_DISCONNECTION, #{'Session-Expiry-Interval' => 1}),
%% Wait a bit since DISCONNECT is async.
timer:sleep(50),
assert_queue_ttl(1, 1, Config),
timer:sleep(1500),
C2 = connect(ClientId, Config, [{clean_start, false}]),
%% Server should reply in CONNACK that it does not have session state for our client ID.
?assertEqual({session_present, 0},
proplists:lookup(session_present, emqtt:info(C2))),
ok = emqtt:disconnect(C2).
session_expiry_disconnect_zero_to_non_zero(Config) ->
ClientId = ?FUNCTION_NAME,
C1 = connect(ClientId, Config, [{properties, #{'Session-Expiry-Interval' => 0}}]),
{ok, _, [1]} = emqtt:subscribe(C1, <<"t/1">>, qos1),
%% "If the Session Expiry Interval in the CONNECT packet was zero, then it is a Protocol
%% Error to set a non-zero Session Expiry Interval in the DISCONNECT packet sent by the Client.
ok = emqtt:disconnect(C1, ?RC_NORMAL_DISCONNECTION, #{'Session-Expiry-Interval' => 60}),
C2 = connect(ClientId, Config, [{clean_start, false}]),
%% Due to the prior protocol error, we expect the requested session expiry interval of
%% 60 seconds not to be applied. Therefore, the server should reply in CONNACK that
%% it does not have session state for our client ID.
?assertEqual({session_present, 0},
proplists:lookup(session_present, emqtt:info(C2))),
ok = emqtt:disconnect(C2).
session_expiry_disconnect_non_zero_to_zero(Config) ->
ClientId = ?FUNCTION_NAME,
C1 = connect(ClientId, Config, [{properties, #{'Session-Expiry-Interval' => 60}}]),
{ok, _, [0, 1]} = emqtt:subscribe(C1, [{<<"t/0">>, qos0},
{<<"t/1">>, qos1}]),
?assertEqual(2, rpc(Config, rabbit_amqqueue, count, [])),
ok = emqtt:disconnect(C1, ?RC_NORMAL_DISCONNECTION, #{'Session-Expiry-Interval' => 0}),
eventually(?_assertEqual(0, rpc(Config, rabbit_amqqueue, count, []))),
C2 = connect(ClientId, Config, [{clean_start, false}]),
?assertEqual({session_present, 0},
proplists:lookup(session_present, emqtt:info(C2))),
ok = emqtt:disconnect(C2).
session_expiry_disconnect_infinity_to_zero(Config) ->
ClientId = ?FUNCTION_NAME,
C1 = connect(ClientId, Config, [{properties, #{'Session-Expiry-Interval' => 16#FFFFFFFF}}]),
{ok, _, [1, 0]} = emqtt:subscribe(C1, [{<<"t/1">>, qos1},
{<<"t/0">>, qos0}]),
assert_no_queue_ttl(2, Config),
ok = emqtt:disconnect(C1, ?RC_NORMAL_DISCONNECTION, #{'Session-Expiry-Interval' => 0}),
eventually(?_assertEqual(0, rpc(Config, rabbit_amqqueue, count, []))).
session_expiry_disconnect_to_infinity(Config) ->
ClientId = ?FUNCTION_NAME,
%% Connect with a non-zero and non-infinity Session Expiry Interval.
C1 = connect(ClientId, Config, [{properties, #{'Session-Expiry-Interval' => 1}}]),
{ok, _, [0, 1]} = emqtt:subscribe(C1, [{<<"t/0">>, qos0},
{<<"t/1">>, qos1}]),
assert_queue_ttl(1, 2, Config),
%% Disconnect with infinity should remove queue TTL from both queues.
ok = emqtt:disconnect(C1, ?RC_NORMAL_DISCONNECTION, #{'Session-Expiry-Interval' => 16#FFFFFFFF}),
timer:sleep(100),
assert_no_queue_ttl(2, Config),
C2 = connect(ClientId, Config, [{clean_start, true}]),
ok = emqtt:disconnect(C2).
session_expiry_reconnect_non_zero(Config) ->
ClientId = ?FUNCTION_NAME,
C1 = connect(ClientId, Config, [{properties, #{'Session-Expiry-Interval' => 60}}]),
{ok, _, [0, 1]} = emqtt:subscribe(C1, [{<<"t/0">>, qos0},
{<<"t/1">>, qos1}]),
assert_queue_ttl(60, 2, Config),
ok = emqtt:disconnect(C1),
C2 = connect(ClientId, Config, [{clean_start, false},
{properties, #{'Session-Expiry-Interval' => 1}}]),
?assertEqual({session_present, 1},
proplists:lookup(session_present, emqtt:info(C2))),
assert_queue_ttl(1, 2, Config),
ok = emqtt:disconnect(C2),
C3 = connect(ClientId, Config, [{clean_start, true}]),
ok = emqtt:disconnect(C3).
session_expiry_reconnect_zero(Config) ->
ClientId = ?FUNCTION_NAME,
C1 = connect(ClientId, Config, [{properties, #{'Session-Expiry-Interval' => 60}}]),
{ok, _, [0, 1]} = emqtt:subscribe(C1, [{<<"t/0">>, qos0},
{<<"t/1">>, qos1}]),
assert_queue_ttl(60, 2, Config),
ok = emqtt:disconnect(C1),
C2 = connect(ClientId, Config, [{clean_start, false},
{properties, #{'Session-Expiry-Interval' => 0}}]),
?assertEqual({session_present, 1},
proplists:lookup(session_present, emqtt:info(C2))),
assert_queue_ttl(0, 2, Config),
ok = emqtt:disconnect(C2).
session_expiry_reconnect_infinity_to_zero(Config) ->
ClientId = ?FUNCTION_NAME,
C1 = connect(ClientId, Config, [{properties, #{'Session-Expiry-Interval' => 16#FFFFFFFF}}]),
{ok, _, [0, 1]} = emqtt:subscribe(C1, [{<<"t/0">>, qos0},
{<<"t/1">>, qos1}]),
assert_no_queue_ttl(2, Config),
ok = emqtt:disconnect(C1),
C2 = connect(ClientId, Config, [{clean_start, false}]),
?assertEqual({session_present, 1},
proplists:lookup(session_present, emqtt:info(C2))),
assert_queue_ttl(0, 2, Config),
ok = emqtt:disconnect(C2).
client_publish_qos2(Config) ->
Topic = ClientId = atom_to_binary(?FUNCTION_NAME),
{C, Connect} = start_client(ClientId, Config, 0, []),
?assertMatch({ok, #{'Maximum-QoS' := 1}}, Connect(C)),
unlink(C),
?assertEqual({error, {disconnected, _RcQosNotSupported = 155, #{}}},
emqtt:publish(C, Topic, <<"msg">>, qos2)).
client_rejects_publish(Config) ->
NumRejectedBefore = dead_letter_metric(messages_dead_lettered_rejected_total, Config),
Payload = Topic = ClientId = atom_to_binary(?FUNCTION_NAME),
C = connect(ClientId, Config, [{auto_ack, false}]),
{ok, _, [1]} = emqtt:subscribe(C, Topic, qos1),
{ok, _} = emqtt:publish(C, Topic, Payload, qos1),
receive {publish, #{payload := Payload,
packet_id := PacketId}} ->
%% Negatively ack the PUBLISH.
emqtt:puback(C, PacketId, ?RC_UNSPECIFIED_ERROR)
after ?TIMEOUT ->
ct:fail("did not receive PUBLISH")
end,
%% Even though we nacked the PUBLISH, we expect the server to not re-send the same message:
%% "If PUBACK [...] is received containing a Reason Code of 0x80 or greater the corresponding
%% PUBLISH packet is treated as acknowledged, and MUST NOT be retransmitted" [MQTT-4.4.0-2].
assert_nothing_received(),
%% However, we expect RabbitMQ to dead letter negatively acknowledged messages.
NumRejected = dead_letter_metric(messages_dead_lettered_rejected_total, Config) - NumRejectedBefore,
?assertEqual(1, NumRejected),
ok = emqtt:disconnect(C).
client_receive_maximum_min(Config) ->
Topic = ClientId = atom_to_binary(?FUNCTION_NAME),
C = connect(ClientId, Config, [{auto_ack, false},
%% Minimum allowed Receive Maximum is 1.
{properties, #{'Receive-Maximum' => 1}}]),
{ok, _, [1]} = emqtt:subscribe(C, Topic, qos1),
{ok, _} = emqtt:publish(C, Topic, <<"m1">>, qos1),
{ok, _} = emqtt:publish(C, Topic, <<"m2">>, qos1),
%% Since client set Receive Maximum is 1, at most 1 QoS 1 message should be in
%% flight from server to client at any given point in time.
PacketId1 = receive {publish, #{payload := <<"m1">>,
packet_id := Id}} ->
Id
after ?TIMEOUT ->
ct:fail("did not receive m1")
end,
assert_nothing_received(),
%% Only when we ack the 1st message, we expect to receive the 2nd message.
emqtt:puback(C, PacketId1),
ok = expect_publishes(C, Topic, [<<"m2">>]),
ok = emqtt:disconnect(C).
client_receive_maximum_large(Config) ->
Topic = ClientId = atom_to_binary(?FUNCTION_NAME),
C = connect(ClientId, Config, [{auto_ack, false},
{properties, #{'Receive-Maximum' => 1_000}}]),
{ok, _, [1]} = emqtt:subscribe(C, Topic, qos1),
%% We know that the configured mqtt.prefetch is 10.
Prefetch = 10,
Payloads = [integer_to_binary(N) || N <- lists:seq(1, Prefetch)],
[{ok, _} = emqtt:publish(C, Topic, P, qos1) || P <- Payloads],
{ok, _} = emqtt:publish(C, Topic, <<"I wait in the queue">>, qos1),
ok = expect_publishes(C, Topic, Payloads),
%% We expect the server to cap the number of in flight QoS 1 messages sent to the
%% client to the configured mqtt.prefetch value even though the client set a larger
%% Receive Maximum value.
assert_nothing_received(),
ok = emqtt:disconnect(C).
unsubscribe_success(Config) ->
C = connect(?FUNCTION_NAME, Config),
{ok, _, [1]} = emqtt:subscribe(C, <<"topic/1">>, qos1),
{ok, _, [0]} = emqtt:subscribe(C, <<"topic/0">>, qos0),
?assertMatch({ok, _, [?RC_SUCCESS, ?RC_SUCCESS]},
emqtt:unsubscribe(C, [<<"topic/1">>, <<"topic/0">>])),
ok = emqtt:disconnect(C).
unsubscribe_topic_not_found(Config) ->
C = connect(?FUNCTION_NAME, Config),
{ok, _, [1]} = emqtt:subscribe(C, <<"topic/1">>, qos1),
?assertMatch({ok, _, [?RC_SUCCESS, ?RC_NO_SUBSCRIPTION_EXISTED]},
emqtt:unsubscribe(C, [<<"topic/1">>, <<"topic/0">>])),
ok = emqtt:disconnect(C).
subscription_option_no_local(Config) ->
C = connect(?FUNCTION_NAME, Config),
Other = connect(<<"other">>, Config),
{ok, _, [0, 0]} = emqtt:subscribe(C, [{<<"t/1">>, [{nl, true}]},
{<<"t/2">>, [{nl, false}]}]),
{ok, _, [0]} = emqtt:subscribe(Other, [{<<"t/1">>, [{nl, true}]}]),
ok = emqtt:publish(C, <<"t/1">>, <<"m1">>),
ok = emqtt:publish(C, <<"t/2">>, <<"m2">>),
ok = expect_publishes(Other, <<"t/1">>, [<<"m1">>]),
ok = expect_publishes(C, <<"t/2">>, [<<"m2">>]),
%% We expect C to not receive m1.
assert_nothing_received(),
ok = emqtt:disconnect(C),
ok = emqtt:disconnect(Other).
subscription_option_no_local_wildcards(Config) ->
C = connect(?FUNCTION_NAME, Config),
{ok, _, [0, 0, 0, 0]} =
emqtt:subscribe(C, [{<<"+/1">>, [{nl, true}]},
{<<"t/1/#">>, [{nl, false}]},
{<<"+/2">>, [{nl, true}]},
{<<"t/2/#">>, [{nl, true}]}]),
%% Matches the first two subscriptions.
%% Not all matching subscriptions have the No Local option set.
%% Therefore, we should receive m1.
ok = emqtt:publish(C, <<"t/1">>, <<"m1">>),
ok = expect_publishes(C, <<"t/1">>, [<<"m1">>]),
%% Matches the last two subscriptions.
%% All matching subscriptions have the No Local option set.
%% Therefore, we should not receive m2.
ok = emqtt:publish(C, <<"t/2">>, <<"m2">>),
assert_nothing_received(),
ok = emqtt:disconnect(C).
subscription_option_retain_as_published(Config) ->
C1 = connect(<<"c1">>, Config),
C2 = connect(<<"c2">>, Config),
{ok, _, [0, 0]} = emqtt:subscribe(C1, [{<<"t/1">>, [{rap, true}]},
{<<"t/2">>, [{rap, false}]}]),
{ok, _, [0]} = emqtt:subscribe(C2, [{<<"t/1">>, [{rap, true}]}]),
ok = emqtt:publish(C1, <<"t/1">>, <<"m1">>, [{retain, true}]),
ok = emqtt:publish(C1, <<"t/2">>, <<"m2">>, [{retain, true}]),
receive {publish, #{client_pid := C1,
topic := <<"t/1">>,
payload := <<"m1">>,
retain := true}} -> ok
after ?TIMEOUT -> ct:fail("did not receive m1")
end,
receive {publish, #{client_pid := C1,
topic := <<"t/2">>,
payload := <<"m2">>,
retain := false}} -> ok
after ?TIMEOUT -> ct:fail("did not receive m2")
end,
receive {publish, #{client_pid := C2,
topic := <<"t/1">>,
payload := <<"m1">>,
retain := true}} -> ok
after ?TIMEOUT -> ct:fail("did not receive m1")
end,
{ok, _} = emqtt:publish(C1, <<"t/1">>, <<>>, [{retain, true}, {qos, 1}]),
{ok, _} = emqtt:publish(C1, <<"t/2">>, <<>>, [{retain, true}, {qos, 1}]),
ok = emqtt:disconnect(C1),
ok = emqtt:disconnect(C2).
subscription_option_retain_as_published_wildcards(Config) ->
C = connect(?FUNCTION_NAME, Config),
{ok, _, [0, 0, 0, 0]} = emqtt:subscribe(C, [{<<"+/1">>, [{rap, false}]},
{<<"t/1/#">>, [{rap, false}]},
{<<"+/2">>, [{rap, false}]},
{<<"t/2/#">>, [{rap, true}]}]),
%% Matches the first two subscriptions.
ok = emqtt:publish(C, <<"t/1">>, <<"m1">>, [{retain, true}]),
%% Matches the last two subscriptions.
ok = emqtt:publish(C, <<"t/2">>, <<"m2">>, [{retain, true}]),
receive {publish, #{topic := <<"t/1">>,
payload := <<"m1">>,
%% No matching subscription has the
%% Retain As Published option set.
retain := false}} -> ok
after ?TIMEOUT -> ct:fail("did not receive m1")
end,
receive {publish, #{topic := <<"t/2">>,
payload := <<"m2">>,
%% (At least) one matching subscription has the
%% Retain As Published option set.
retain := true}} -> ok
after ?TIMEOUT -> ct:fail("did not receive m2")
end,
{ok, _} = emqtt:publish(C, <<"t/1">>, <<>>, [{retain, true}, {qos, 1}]),
{ok, _} = emqtt:publish(C, <<"t/2">>, <<>>, [{retain, true}, {qos, 1}]),
ok = emqtt:disconnect(C).
subscription_option_retain_handling(Config) ->
ClientId = ?FUNCTION_NAME,
C1 = connect(ClientId, Config, non_clean_sess_opts()),
{ok, _} = emqtt:publish(C1, <<"t/1">>, <<"m1">>, [{retain, true}, {qos, 1}]),
{ok, _} = emqtt:publish(C1, <<"t/2">>, <<"m2">>, [{retain, true}, {qos, 1}]),
{ok, _} = emqtt:publish(C1, <<"t/3">>, <<"m3">>, [{retain, true}, {qos, 1}]),
{ok, _, [1, 1, 1]} = emqtt:subscribe(C1, [{<<"t/1">>, [{rh, 0}, {qos, 1}]},
%% Subscription does not exist.
{<<"t/2">>, [{rh, 1}, {qos, 1}]},
{<<"t/3">>, [{rh, 2}, {qos, 1}]}]),
ok = expect_publishes(C1, <<"t/1">>, [<<"m1">>]),
ok = expect_publishes(C1, <<"t/2">>, [<<"m2">>]),
assert_nothing_received(),
{ok, _, [1, 1, 1]} = emqtt:subscribe(C1, [{<<"t/1">>, [{rh, 0}, {qos, 1}]},
%% Subscription exists.
{<<"t/2">>, [{rh, 1}, {qos, 1}]},
{<<"t/3">>, [{rh, 2}, {qos, 1}]}]),
ok = expect_publishes(C1, <<"t/1">>, [<<"m1">>]),
assert_nothing_received(),
{ok, _, [0, 0, 0]} = emqtt:subscribe(C1, [{<<"t/1">>, [{rh, 0}, {qos, 0}]},
%% That specific subscription does not exist.
{<<"t/2">>, [{rh, 1}, {qos, 0}]},
{<<"t/3">>, [{rh, 2}, {qos, 0}]}]),
ok = expect_publishes(C1, <<"t/1">>, [<<"m1">>]),
ok = expect_publishes(C1, <<"t/2">>, [<<"m2">>]),
assert_nothing_received(),
ok = emqtt:disconnect(C1),
C2 = connect(ClientId, Config, [{clean_start, false}]),
{ok, _, [0, 0, 0]} = emqtt:subscribe(C2, [{<<"t/1">>, [{rh, 0}, {qos, 0}]},
%% Subscription exists.
{<<"t/2">>, [{rh, 1}, {qos, 0}]},
{<<"t/3">>, [{rh, 2}, {qos, 0}]}]),
ok = expect_publishes(C2, <<"t/1">>, [<<"m1">>]),
assert_nothing_received(),
{ok, _} = emqtt:publish(C2, <<"t/1">>, <<>>, [{retain, true}, {qos, 1}]),
{ok, _} = emqtt:publish(C2, <<"t/2">>, <<>>, [{retain, true}, {qos, 1}]),
{ok, _} = emqtt:publish(C2, <<"t/3">>, <<>>, [{retain, true}, {qos, 1}]),
ok = emqtt:disconnect(C2).
subscription_identifier(Config) ->
C1 = connect(<<"c1">>, Config),
C2 = connect(<<"c2">>, Config),
{ok, _, [0, 0]} = emqtt:subscribe(C1, #{'Subscription-Identifier' => 1}, [{<<"t/1">>, []},
{<<"+/1">>, []}]),
{ok, _, [0]} = emqtt:subscribe(C2, #{'Subscription-Identifier' => 1}, [{<<"t/2">>, []}]),
{ok, _, [0, 0]} = emqtt:subscribe(C2, #{'Subscription-Identifier' => 16#fffffff}, [{<<"+/2">>, []},
{<<"t/3">>, []}]),
{ok, _, [0]} = emqtt:subscribe(C2, #{}, [{<<"t/2/#">>, []}]),
ok = emqtt:publish(C1, <<"t/1">>, <<"m1">>),
ok = emqtt:publish(C1, <<"t/2">>, <<"m2">>),
ok = emqtt:publish(C1, <<"t/3">>, <<"m3">>),
ok = emqtt:publish(C1, <<"t/2/xyz">>, <<"m4">>),
receive {publish,
#{client_pid := C1,
topic := <<"t/1">>,
payload := <<"m1">>,
%% "It is possible that the Client made several subscriptions which match a publication
%% and that it used the same identifier for more than one of them. In this case the
%% PUBLISH packet will carry multiple identical Subscription Identifiers." [v5 3.3.4]
properties := #{'Subscription-Identifier' := [1, 1]}}} -> ok
after ?TIMEOUT -> ct:fail("did not receive m1")
end,
receive {publish,
#{client_pid := C2,
topic := <<"t/2">>,
payload := <<"m2">>,
properties := #{'Subscription-Identifier' := Ids}}} ->
%% "If the Server sends a single copy of the message it MUST include in the PUBLISH
%% packet the Subscription Identifiers for all matching subscriptions which have a
%% Subscription Identifiers, their order is not significant [MQTT-3.3.4-4]." [v5 3.3.4]
?assertEqual([1, 16#fffffff], lists:sort(Ids))
after ?TIMEOUT -> ct:fail("did not receive m2")
end,
receive {publish,
#{client_pid := C2,
topic := <<"t/3">>,
payload := <<"m3">>,
properties := #{'Subscription-Identifier' := 16#fffffff}}} -> ok
after ?TIMEOUT -> ct:fail("did not receive m3")
end,
receive {publish,
#{client_pid := C2,
topic := <<"t/2/xyz">>,
payload := <<"m4">>,
properties := Props}} ->
?assertNot(maps:is_key('Subscription-Identifier', Props))
after ?TIMEOUT -> ct:fail("did not receive m4")
end,
assert_nothing_received(),
ok = emqtt:disconnect(C1),
ok = emqtt:disconnect(C2).
subscription_identifier_amqp091(Config) ->
C1 = connect(<<"sub 1">>, Config),
C2 = connect(<<"sub 2">>, Config),
{ok, _, [1]} = emqtt:subscribe(C1, #{'Subscription-Identifier' => 1}, [{<<"a/+">>, [{qos, 1}]}]),
{ok, _, [1]} = emqtt:subscribe(C2, #{'Subscription-Identifier' => 16#fffffff}, [{<<"a/b">>, [{qos, 1}]}]),
Ch = rabbit_ct_client_helpers:open_channel(Config),
%% Test routing to a single queue.
amqp_channel:call(Ch, #'basic.publish'{exchange = <<"amq.topic">>,
routing_key = <<"a.a">>},
#amqp_msg{payload = <<"m1">>}),
receive {publish,
#{client_pid := C1,
topic := <<"a/a">>,
payload := <<"m1">>,
properties := #{'Subscription-Identifier' := 1}}} -> ok
after ?TIMEOUT -> ct:fail("did not receive message m1")
end,
%% Test routing to multiple queues.
amqp_channel:call(Ch, #'basic.publish'{exchange = <<"amq.topic">>,
routing_key = <<"a.b">>},
#amqp_msg{payload = <<"m2">>}),
receive {publish,
#{client_pid := C1,
topic := <<"a/b">>,
payload := <<"m2">>,
properties := #{'Subscription-Identifier' := 1}}} -> ok
after ?TIMEOUT -> ct:fail("did not receive message m2")
end,
receive {publish,
#{client_pid := C2,
topic := <<"a/b">>,
payload := <<"m2">>,
properties := #{'Subscription-Identifier' := 16#fffffff}}} -> ok
after ?TIMEOUT -> ct:fail("did not receive message m2")
end,
ok = emqtt:disconnect(C1),
ok = emqtt:disconnect(C2),
ok = rabbit_ct_client_helpers:close_channels_and_connection(Config, 0).
subscription_identifier_at_most_once_dead_letter(Config) ->
C = connect(?FUNCTION_NAME, Config),
{ok, _, [1]} = emqtt:subscribe(C, #{'Subscription-Identifier' => 1}, [{<<"dead letter/#">>, [{qos, 1}]}]),
Ch = rabbit_ct_client_helpers:open_channel(Config),
QArgs = [{<<"x-dead-letter-exchange">>, longstr, <<"amq.topic">>},
{<<"x-dead-letter-routing-key">>, longstr, <<"dead letter.a">>}],
#'queue.declare_ok'{} = amqp_channel:call(Ch, #'queue.declare'{queue = <<"source queue">>,
durable = true,
exclusive = true,
arguments = QArgs}),
amqp_channel:call(Ch, #'basic.publish'{routing_key = <<"source queue">>},
#amqp_msg{payload = <<"msg">>,
props = #'P_basic'{expiration = <<"0">>}}),
receive {publish,
#{client_pid := C,
topic := <<"dead letter/a">>,
payload := <<"msg">>,
properties := #{'Subscription-Identifier' := 1}}} -> ok
after ?TIMEOUT -> ct:fail("did not receive msg")
end,
ok = emqtt:disconnect(C),
ok = rabbit_ct_client_helpers:close_channels_and_connection(Config, 0).
at_most_once_dead_letter_detect_cycle(Config) ->
NumExpiredBefore = dead_letter_metric(messages_dead_lettered_expired_total, Config, at_most_once),
SubClientId = Payload = PolicyName = atom_to_binary(?FUNCTION_NAME),
ok = rabbit_ct_broker_helpers:set_policy(
Config, 0, PolicyName, <<"mqtt-subscription-", SubClientId/binary, "qos1">>, <<"queues">>,
%% Create dead letter cycle: qos1 queue -> topic exchange -> qos1 queue
[{<<"dead-letter-exchange">>, <<"amq.topic">>},
{<<"message-ttl">>, 1}]),
Sub1 = connect(SubClientId, Config, non_clean_sess_opts()),
{ok, _, [1]} = emqtt:subscribe(Sub1, #{'Subscription-Identifier' => 10}, [{<<"+/b">>, [{qos, 1}]}]),
ok = emqtt:disconnect(Sub1),
Pub = connect(<<"publisher">>, Config),
{ok, _} = emqtt:publish(Pub, <<"a/b">>, Payload, qos1),
ok = emqtt:disconnect(Pub),
%% Given our subscribing client is disconnected, the message should be dead lettered after 1 ms.
%% However, due to the dead letter cycle, we expect the message to be dropped.
timer:sleep(20),
Sub2 = connect(SubClientId, Config, [{clean_start, false}]),
assert_nothing_received(),
%% Double check that the message was indeed (exactly once) dead lettered.
NumExpired = dead_letter_metric(messages_dead_lettered_expired_total,
Config, at_most_once) - NumExpiredBefore,
?assertEqual(1, NumExpired),
ok = emqtt:disconnect(Sub2),
ok = rabbit_ct_broker_helpers:clear_policy(Config, 0, PolicyName).
%% Tests that the session state in the server includes subscription options
%% and subscription identifiers and that this session state is persisted.
subscription_options_persisted(Config) ->
ClientId = ?FUNCTION_NAME,
C1 = connect(ClientId, Config, non_clean_sess_opts()),
{ok, _, [0, 1]} = emqtt:subscribe(C1, #{'Subscription-Identifier' => 99},
[{<<"t1">>, [{nl, true}, {rap, false}, {qos, 0}]},
{<<"t2">>, [{nl, false}, {rap, true}, {qos, 1}]}]),
unlink(C1),
ok = rabbit_ct_broker_helpers:restart_node(Config, 0),
C2 = connect(ClientId, Config, [{clean_start, false}]),
ok = emqtt:publish(C2, <<"t1">>, <<"m1">>),
ok = emqtt:publish(C2, <<"t2">>, <<"m2">>, [{retain, true}]),
receive {publish,
#{client_pid := C2,
payload := <<"m2">>,
retain := true,
qos := 0,
properties := #{'Subscription-Identifier' := 99}}} -> ok
after ?TIMEOUT -> ct:fail("did not receive m2")
end,
assert_nothing_received(),
{ok, _} = emqtt:publish(C2, <<"t2">>, <<>>, [{retain, true}, {qos, 1}]),
ok = emqtt:disconnect(C2).
%% "If a Server receives a SUBSCRIBE packet containing a Topic Filter that is identical to a Non‑shared
%% Subscription’s Topic Filter for the current Session, then it MUST replace that existing Subscription
%% with a new Subscription [MQTT-3.8.4-3]. The Topic Filter in the new Subscription will be identical
%% to that in the previous Subscription, although its Subscription Options could be different."
%%
%% "The Subscription Identifiers are part of the Session State in the Server and are returned to the
%% Client receiving a matching PUBLISH packet. They are removed from the Server’s Session State when the
%% Server receives an UNSUBSCRIBE packet, when the Server receives a SUBSCRIBE packet from the Client for
%% the same Topic Filter but with a different Subscription Identifier or with no Subscription Identifier,
%% or when the Server sends Session Present 0 in a CONNACK packet" [v5 3.8.4]
subscription_options_modify(Config) ->
Topic = ClientId = atom_to_binary(?FUNCTION_NAME),
C = connect(ClientId, Config),
{ok, _, [0]} = emqtt:subscribe(C, #{'Subscription-Identifier' => 1}, Topic, [{nl, true}]),
{ok, _} = emqtt:publish(C, Topic, <<"m1">>, qos1),
assert_nothing_received(),
%% modify No Local
{ok, _, [0]} = emqtt:subscribe(C, #{'Subscription-Identifier' => 1}, Topic, [{nl, false}]),
{ok, _} = emqtt:publish(C, Topic, <<"m2">>, qos1),
receive {publish, #{payload := <<"m2">>,
qos := 0 }} -> ok
after ?TIMEOUT -> ct:fail("did not receive m2")
end,
%% modify QoS
{ok, _, [1]} = emqtt:subscribe(C, #{'Subscription-Identifier' => 1}, Topic, qos1),
{ok, _} = emqtt:publish(C, Topic, <<"m3">>, qos1),
receive {publish, #{payload := <<"m3">>,
qos := 1,
properties := #{'Subscription-Identifier' := 1}}} -> ok
after ?TIMEOUT -> ct:fail("did not receive m3")
end,
%% modify Subscription Identifier
{ok, _, [1]} = emqtt:subscribe(C, #{'Subscription-Identifier' => 2}, Topic, qos1),
{ok, _} = emqtt:publish(C, Topic, <<"m4">>, qos1),
receive {publish, #{payload := <<"m4">>,
properties := #{'Subscription-Identifier' := 2}}} -> ok
after ?TIMEOUT -> ct:fail("did not receive m4")
end,
%% remove Subscription Identifier
{ok, _, [1]} = emqtt:subscribe(C, Topic, qos1),
{ok, _} = emqtt:publish(C, Topic, <<"m5">>, [{retain, true}, {qos, 1}]),
receive {publish, #{payload := <<"m5">>,
retain := false,
properties := Props}} when map_size(Props) =:= 0 -> ok
after ?TIMEOUT -> ct:fail("did not receive m5")
end,
%% modify Retain As Published
{ok, _, [1]} = emqtt:subscribe(C, Topic, [{rap, true}, {qos, 1}]),
receive {publish, #{payload := <<"m5">>,
retain := true}} -> ok
after ?TIMEOUT -> ct:fail("did not receive retained m5")
end,
{ok, _} = emqtt:publish(C, Topic, <<"m6">>, [{retain, true}, {qos, 1}]),
receive {publish, #{payload := <<"m6">>,
retain := true}} -> ok
after ?TIMEOUT -> ct:fail("did not receive m6")
end,
assert_nothing_received(),
{ok, _} = emqtt:publish(C, Topic, <<>>, [{retain, true}, {qos, 1}]),
ok = emqtt:disconnect(C).
%% "If a Server receives a SUBSCRIBE packet containing a Topic Filter that is identical to a
%% Non‑shared Subscription’s Topic Filter for the current Session, then it MUST replace that
%% existing Subscription with a new Subscription [MQTT-3.8.4-3]. The Topic Filter in the new
%% Subscription will be identical to that in the previous Subscription, although its
%% Subscription Options could be different. [...] Applicaton Messages MUST NOT be lost due
%% to replacing the Subscription [MQTT-3.8.4-4]." [v5 3.8.4]
%%
%% This test ensures that messages are not lost when replacing a QoS 1 subscription.
subscription_options_modify_qos1(Config) ->
subscription_options_modify_qos(1, Config).
%% This test ensures that messages are received at most once
%% when replacing a QoS 0 subscription.
subscription_options_modify_qos0(Config) ->
subscription_options_modify_qos(0, Config).
subscription_options_modify_qos(Qos, Config) ->
Topic = atom_to_binary(?FUNCTION_NAME),
Pub = connect(<<"publisher">>, Config),
Sub = connect(<<"subscriber">>, Config),
{ok, _, [Qos]} = emqtt:subscribe(Sub, Topic, Qos),
Sender = spawn_link(?MODULE, send, [self(), Pub, Topic, 0]),
receive {publish, #{payload := <<"1">>,
properties := Props}} ->
?assertEqual(0, maps:size(Props))
after ?TIMEOUT -> ct:fail("did not receive 1")
end,
%% Replace subscription while another client is sending messages.
{ok, _, [Qos]} = emqtt:subscribe(Sub, #{'Subscription-Identifier' => 1}, Topic, Qos),
Sender ! stop,
NumSent = receive {N, Sender} -> N
after ?TIMEOUT -> ct:fail("could not stop publisher")
end,
ct:pal("Publisher sent ~b messages", [NumSent]),
LastExpectedPayload = integer_to_binary(NumSent),
receive {publish, #{payload := LastExpectedPayload,
qos := Qos,
client_pid := Sub,
properties := #{'Subscription-Identifier' := 1}}} -> ok
after ?TIMEOUT -> ct:fail("did not receive ~s", [LastExpectedPayload])
end,
case Qos of