-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
dead_lettering_SUITE.erl
1708 lines (1537 loc) · 85 KB
/
dead_lettering_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-2024 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
%%
%% For the full spec see: https://www.rabbitmq.com/dlx.html
%%
-module(dead_lettering_SUITE).
-include_lib("common_test/include/ct.hrl").
-include_lib("amqp_client/include/amqp_client.hrl").
-include_lib("eunit/include/eunit.hrl").
-include_lib("rabbitmq_ct_helpers/include/rabbit_assert.hrl").
-compile([export_all, nowarn_export_all]).
-import(queue_utils, [wait_for_messages/2]).
all() ->
[
{group, dead_letter_tests}
].
groups() ->
DeadLetterTests = [dead_letter_nack,
dead_letter_multiple_nack,
dead_letter_nack_requeue,
dead_letter_nack_requeue_multiple,
dead_letter_reject,
dead_letter_reject_expire_expire,
dead_letter_reject_many,
dead_letter_reject_requeue,
dead_letter_max_length_drop_head,
dead_letter_reject_requeue_reject_norequeue,
dead_letter_missing_exchange,
dead_letter_routing_key,
dead_letter_headers_should_be_appended_for_each_event,
dead_letter_headers_should_not_be_appended_for_republish,
dead_letter_routing_key_header_CC,
dead_letter_routing_key_header_BCC,
dead_letter_routing_key_cycle_max_length,
dead_letter_routing_key_cycle_with_reject,
dead_letter_policy,
dead_letter_override_policy,
dead_letter_ignore_policy,
dead_letter_headers,
dead_letter_headers_reason_maxlen,
dead_letter_headers_cycle,
dead_letter_headers_BCC,
dead_letter_headers_CC,
dead_letter_headers_CC_with_routing_key,
dead_letter_headers_first_death,
dead_letter_headers_first_death_route,
dead_letter_ttl,
dead_letter_routing_key_cycle_ttl,
dead_letter_headers_reason_expired,
dead_letter_headers_reason_expired_per_message,
dead_letter_extra_bcc],
DisabledMetricTests = [metric_maxlen,
metric_rejected,
metric_expired_queue_msg_ttl,
metric_expired_per_msg_msg_ttl],
Opts = [shuffle],
[
{dead_letter_tests, Opts,
[
{classic_queue, Opts, [{at_most_once, Opts, [dead_letter_max_length_reject_publish_dlx | DeadLetterTests]},
{disabled, Opts, DisabledMetricTests}]},
{mirrored_queue, Opts, [{at_most_once, Opts, [dead_letter_max_length_reject_publish_dlx | DeadLetterTests]},
{disabled, Opts, DisabledMetricTests}]},
{quorum_queue, Opts, [{at_most_once, Opts, DeadLetterTests},
{disabled, Opts, DisabledMetricTests},
{at_least_once, Opts, DeadLetterTests --
[
%% dead-letter-strategy at-least-once is incompatible with overflow drop-head
dead_letter_max_length_drop_head,
dead_letter_routing_key_cycle_max_length,
dead_letter_headers_reason_maxlen,
%% tested separately in rabbit_fifo_dlx_integration_SUITE
dead_letter_missing_exchange
]}
]
}]}].
suite() ->
[
{timetrap, {minutes, 8}}
].
%% -------------------------------------------------------------------
%% Testsuite setup/teardown.
%% -------------------------------------------------------------------
init_per_suite(Config0) ->
rabbit_ct_helpers:log_environment(),
Config = rabbit_ct_helpers:merge_app_env(
Config0, {rabbit, [{dead_letter_worker_publisher_confirm_timeout, 2000}]}),
rabbit_ct_helpers:run_setup_steps(Config).
end_per_suite(Config) ->
rabbit_ct_helpers:run_teardown_steps(Config).
init_per_group(classic_queue, Config) ->
rabbit_ct_helpers:set_config(
Config,
[{queue_args, [{<<"x-queue-type">>, longstr, <<"classic">>}]},
{queue_durable, false}]);
init_per_group(mirrored_queue, Config) ->
case rabbit_ct_broker_helpers:configured_metadata_store(Config) of
mnesia ->
rabbit_ct_broker_helpers:set_ha_policy(Config, 0, <<"^max_length.*queue">>,
<<"all">>, [{<<"ha-sync-mode">>, <<"automatic">>}]),
Config1 = rabbit_ct_helpers:set_config(
Config, [{is_mirrored, true},
{queue_args, [{<<"x-queue-type">>, longstr, <<"classic">>}]},
{queue_durable, false}]),
rabbit_ct_helpers:run_steps(Config1, []);
_ ->
{skip, "Classic mirroring not supported by Khepri"}
end;
init_per_group(quorum_queue, Config) ->
rabbit_ct_helpers:set_config(
Config,
[{queue_args, [{<<"x-queue-type">>, longstr, <<"quorum">>}]},
{queue_durable, true}]);
init_per_group(at_most_once, Config) ->
case outer_group_name(Config) of
quorum_queue ->
QueueArgs0 = rabbit_ct_helpers:get_config(Config, queue_args),
QueueArgs = lists:keystore(<<"x-dead-letter-strategy">>,
1,
QueueArgs0,
{<<"x-dead-letter-strategy">>, longstr, <<"at-most-once">>}),
rabbit_ct_helpers:set_config(Config, {queue_args, QueueArgs});
_ ->
Config
end;
init_per_group(at_least_once, Config) ->
case outer_group_name(Config) of
quorum_queue ->
QueueArgs0 = rabbit_ct_helpers:get_config(Config, queue_args),
QueueArgs1 = lists:keystore(<<"x-dead-letter-strategy">>,
1,
QueueArgs0,
{<<"x-dead-letter-strategy">>, longstr, <<"at-least-once">>}),
QueueArgs = lists:keystore(<<"x-overflow">>,
1,
QueueArgs1,
{<<"x-overflow">>, longstr, <<"reject-publish">>}),
rabbit_ct_helpers:set_config(Config, {queue_args, QueueArgs});
_ ->
Config
end;
init_per_group(Group, Config) ->
case lists:member({group, Group}, all()) of
true ->
ClusterSize = 3,
Config1 = rabbit_ct_helpers:set_config(
Config, [
{rmq_nodename_suffix, Group},
{rmq_nodes_count, ClusterSize}
]),
rabbit_ct_helpers:run_steps(
Config1,
rabbit_ct_broker_helpers:setup_steps() ++
rabbit_ct_client_helpers:setup_steps());
false ->
rabbit_ct_helpers:run_steps(Config, [])
end.
end_per_group(Group, Config) ->
case lists:member({group, Group}, all()) of
true ->
rabbit_ct_helpers:run_steps(Config,
rabbit_ct_client_helpers:teardown_steps() ++
rabbit_ct_broker_helpers:teardown_steps());
false ->
Config
end.
init_per_testcase(Testcase, Config) ->
IsMixed = rabbit_ct_helpers:is_mixed_versions(),
case Testcase of
dead_letter_headers_should_not_be_appended_for_republish when IsMixed ->
{skip, "dead_letter_headers_should_not_be_appended_for_republish isn't mixed versions compatible"};
_ ->
Group = proplists:get_value(name, ?config(tc_group_properties, Config)),
Q = rabbit_data_coercion:to_binary(io_lib:format("~p_~p", [Group, Testcase])),
Q2 = rabbit_data_coercion:to_binary(io_lib:format("~p_~p_2", [Group, Testcase])),
Q3 = rabbit_data_coercion:to_binary(io_lib:format("~p_~p_3", [Group, Testcase])),
Policy = rabbit_data_coercion:to_binary(io_lib:format("~p_~p_policy", [Group, Testcase])),
DLXExchange = rabbit_data_coercion:to_binary(io_lib:format("~p_~p_dlx_exchange",
[Group, Testcase])),
Counters = get_global_counters(Config),
Config1 = rabbit_ct_helpers:set_config(Config, [{dlx_exchange, DLXExchange},
{queue_name, Q},
{queue_name_dlx, Q2},
{queue_name_dlx_2, Q3},
{policy, Policy},
{counters, Counters}]),
rabbit_ct_helpers:testcase_started(Config1, Testcase)
end.
end_per_testcase(Testcase, Config) ->
rabbit_ct_broker_helpers:rpc(Config, 0, ?MODULE, delete_queues, []),
{_, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
amqp_channel:call(Ch, #'exchange.delete'{exchange = ?config(dlx_exchange, Config)}),
_ = rabbit_ct_broker_helpers:clear_policy(Config, 0, ?config(policy, Config)),
rabbit_ct_helpers:testcase_finished(Config, Testcase).
delete_queues() ->
[rabbit_amqqueue:delete(Q, false, false, <<"tests">>) || Q <- rabbit_amqqueue:list()].
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Dead letter exchanges
%%
%% Messages are dead-lettered when:
%% 1) message is rejected with basic.reject or basic.nack with requeue=false
%% 2) message ttl expires
%% 3) queue length limit is exceeded (only drop-head implemented in quorum queues)
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 1) message is rejected with basic.nack, requeue=false and multiple=false
dead_letter_nack(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
declare_dead_letter_queues(Ch, Config, QName, DLXQName),
P1 = <<"msg1">>,
P2 = <<"msg2">>,
P3 = <<"msg3">>,
%% Publish 3 messages
publish(Ch, QName, [P1, P2, P3]),
wait_for_messages(Config, [[QName, <<"3">>, <<"3">>, <<"0">>]]),
%% Consume them
[DTag1, DTag2, DTag3] = consume(Ch, QName, [P1, P2, P3]),
%% Nack the last one with multiple = false
amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag3,
multiple = false,
requeue = false}),
wait_for_messages(Config, [[DLXQName, <<"1">>, <<"1">>, <<"0">>]]),
%% Queue is empty
consume_empty(Ch, QName),
%% Consume the last message from the dead letter queue
consume(Ch, DLXQName, [P3]),
consume_empty(Ch, DLXQName),
%% Nack the other two
amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag1,
multiple = false,
requeue = false}),
amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag2,
multiple = false,
requeue = false}),
%% Queue is empty
consume_empty(Ch, QName),
%% Consume the first two messages from the dead letter queue
consume(Ch, DLXQName, [P1, P2]),
consume_empty(Ch, DLXQName),
?assertEqual(3, counted(messages_dead_lettered_rejected_total, Config)).
%% 1) message is rejected with basic.nack, requeue=false and multiple=true
dead_letter_multiple_nack(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
declare_dead_letter_queues(Ch, Config, QName, DLXQName),
P1 = <<"msg1">>,
P2 = <<"msg2">>,
P3 = <<"msg3">>,
%% Publish 3 messages
publish(Ch, QName, [P1, P2, P3]),
wait_for_messages(Config, [[QName, <<"3">>, <<"3">>, <<"0">>]]),
%% Consume them
[_, _, DTag3] = consume(Ch, QName, [P1, P2, P3]),
%% Nack the last one with multiple = true
amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag3,
multiple = true,
requeue = false}),
wait_for_messages(Config, [[DLXQName, <<"3">>, <<"3">>, <<"0">>]]),
%% Consume the 3 messages from the dead letter queue
consume(Ch, DLXQName, [P1, P2, P3]),
consume_empty(Ch, DLXQName),
%% Queue is empty
consume_empty(Ch, QName),
?assertEqual(3, counted(messages_dead_lettered_rejected_total, Config)).
%% 1) message is rejected with basic.nack, requeue=true and multiple=false. Dead-lettering does not take place
dead_letter_nack_requeue(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
declare_dead_letter_queues(Ch, Config, QName, DLXQName),
P1 = <<"msg1">>,
P2 = <<"msg2">>,
P3 = <<"msg3">>,
%% Publish 3 messages
publish(Ch, QName, [P1, P2, P3]),
%% Consume them
wait_for_messages(Config, [[QName, <<"3">>, <<"3">>, <<"0">>]]),
[_, _, DTag3] = consume(Ch, QName, [P1, P2, P3]),
%% Queue is empty
consume_empty(Ch, QName),
%% Nack the last one with multiple = false
amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag3,
multiple = false,
requeue = true}),
%% Consume the last message from the queue
wait_for_messages(Config, [[QName, <<"3">>, <<"1">>, <<"2">>]]),
consume(Ch, QName, [P3]),
consume_empty(Ch, QName),
%% Dead letter queue is empty
consume_empty(Ch, DLXQName),
?assertEqual(0, counted(messages_dead_lettered_rejected_total, Config)).
%% 1) message is rejected with basic.nack, requeue=true and multiple=true. Dead-lettering does not take place
dead_letter_nack_requeue_multiple(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
declare_dead_letter_queues(Ch, Config, QName, DLXQName),
P1 = <<"msg1">>,
P2 = <<"msg2">>,
P3 = <<"msg3">>,
%% Publish 3 messages
publish(Ch, QName, [P1, P2, P3]),
%% Consume them
wait_for_messages(Config, [[QName, <<"3">>, <<"3">>, <<"0">>]]),
[_, _, DTag3] = consume(Ch, QName, [P1, P2, P3]),
%% Queue is empty
consume_empty(Ch, QName),
%% Nack the last one with multiple = true
amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag3,
multiple = true,
requeue = true}),
%% Consume the three messages from the queue
wait_for_messages(Config, [[QName, <<"3">>, <<"3">>, <<"0">>]]),
consume(Ch, QName, [P1, P2, P3]),
consume_empty(Ch, QName),
%% Dead letter queue is empty
consume_empty(Ch, DLXQName),
?assertEqual(0, counted(messages_dead_lettered_rejected_total, Config)).
%% 1) message is rejected with basic.reject, requeue=false
dead_letter_reject(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
declare_dead_letter_queues(Ch, Config, QName, DLXQName),
P1 = <<"msg1">>,
P2 = <<"msg2">>,
P3 = <<"msg3">>,
%% Publish 3 messages
publish(Ch, QName, [P1, P2, P3]),
%% Consume the first message
wait_for_messages(Config, [[QName, <<"3">>, <<"3">>, <<"0">>]]),
[DTag] = consume(Ch, QName, [P1]),
%% Reject it
amqp_channel:cast(Ch, #'basic.reject'{delivery_tag = DTag,
requeue = false}),
%% Consume it from the dead letter queue
wait_for_messages(Config, [[DLXQName, <<"1">>, <<"1">>, <<"0">>]]),
_ = consume(Ch, DLXQName, [P1]),
consume_empty(Ch, DLXQName),
%% Consume the last two from the queue
_ = consume(Ch, QName, [P2, P3]),
consume_empty(Ch, QName),
?assertEqual(1, counted(messages_dead_lettered_rejected_total, Config)).
dead_letter_reject_expire_expire(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
%% In 3.13.0, and 3.13.1 there is a bug in mc:is_death_cycle/2 where the queue names matter.
%% The following queue names triggered the bug because they affect the order returned by maps:keys/1.
Q1 = <<"b">>,
Q2 = <<"a2">>,
Q3 = <<"a3">>,
Args = ?config(queue_args, Config),
Durable = ?config(queue_durable, Config),
%% Test the followig topology message flow:
%% Q1 --rejected--> Q2 --expired--> Q3 --expired-->
%% Q1 --rejected--> Q2 --expired--> Q3 --expired-->
%% Q1
#'queue.declare_ok'{} = amqp_channel:call(
Ch,
#'queue.declare'{
queue = Q1,
arguments = Args ++ [{<<"x-dead-letter-exchange">>, longstr, <<>>},
{<<"x-dead-letter-routing-key">>, longstr, Q2}],
durable = Durable}),
#'queue.declare_ok'{} = amqp_channel:call(
Ch,
#'queue.declare'{
queue = Q2,
arguments = Args ++ [{<<"x-dead-letter-exchange">>, longstr, <<>>},
{<<"x-dead-letter-routing-key">>, longstr, Q3},
{<<"x-message-ttl">>, long, 5}],
durable = Durable}),
#'queue.declare_ok'{} = amqp_channel:call(
Ch,
#'queue.declare'{
queue = Q3,
arguments = Args ++ [{<<"x-dead-letter-exchange">>, longstr, <<>>},
{<<"x-dead-letter-routing-key">>, longstr, Q1},
{<<"x-message-ttl">>, long, 5}],
durable = Durable}),
%% Send a single message.
P = <<"msg">>,
publish(Ch, Q1, [P]),
wait_for_messages(Config, [[Q1, <<"1">>, <<"1">>, <<"0">>]]),
%% Reject the 1st time.
[DTag1] = consume(Ch, Q1, [P]),
amqp_channel:cast(Ch, #'basic.reject'{delivery_tag = DTag1,
requeue = false}),
%% Message should now flow from Q1 -> Q2 -> Q3 -> Q1
wait_for_messages(Config, [[Q1, <<"1">>, <<"1">>, <<"0">>]]),
%% Reject the 2nd time.
[DTag2] = consume(Ch, Q1, [P]),
amqp_channel:cast(Ch, #'basic.reject'{delivery_tag = DTag2,
requeue = false}),
%% Message should again flow from Q1 -> Q2 -> Q3 -> Q1
wait_for_messages(Config, [[Q1, <<"1">>, <<"1">>, <<"0">>]]).
%% 1) Many messages are rejected. They get dead-lettered in correct order.
dead_letter_reject_many(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
declare_dead_letter_queues(Ch, Config, QName, DLXQName),
%% Publish 100 messages
Payloads = lists:map(fun erlang:integer_to_binary/1, lists:seq(1, 100)),
publish(Ch, QName, Payloads),
wait_for_messages(Config, [[QName, <<"100">>, <<"100">>, <<"0">>]]),
%% Reject all messages using same consumer
amqp_channel:subscribe(Ch, #'basic.consume'{queue = QName}, self()),
CTag = receive #'basic.consume_ok'{consumer_tag = C} -> C end,
[begin
receive {#'basic.deliver'{consumer_tag = CTag, delivery_tag = DTag}, #amqp_msg{payload = P}} ->
amqp_channel:cast(Ch, #'basic.reject'{delivery_tag = DTag, requeue = false})
after 5000 ->
amqp_channel:call(Ch, #'basic.cancel'{consumer_tag = CTag}),
exit(timeout)
end
end || P <- Payloads],
amqp_channel:call(Ch, #'basic.cancel'{consumer_tag = CTag}),
%% Consume all messages from dead letter queue in correct order (i.e. from payload <<1>> to <<100>>)
wait_for_messages(Config, [[DLXQName, <<"100">>, <<"100">>, <<"0">>]]),
_ = consume(Ch, DLXQName, Payloads),
consume_empty(Ch, DLXQName),
?assertEqual(100, counted(messages_dead_lettered_rejected_total, Config)).
%% 1) Message is rejected with basic.reject, requeue=true. Dead-lettering does not take place.
dead_letter_reject_requeue(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
declare_dead_letter_queues(Ch, Config, QName, DLXQName),
%% Setting a delivery-limit will cause a quorum queue to requeue at the head of the queue
%% (same behaviour as in classic queues).
ok = rabbit_ct_broker_helpers:set_policy(Config, 0, ?config(policy, Config), QName,
<<"queues">>, [{<<"delivery-limit">>, 50}]),
P1 = <<"msg1">>,
P2 = <<"msg2">>,
P3 = <<"msg3">>,
%% Publish 3 messages
publish(Ch, QName, [P1, P2, P3]),
%% Consume the first one
wait_for_messages(Config, [[QName, <<"3">>, <<"3">>, <<"0">>]]),
[DTag] = consume(Ch, QName, [P1]),
%% Reject the first one
amqp_channel:cast(Ch, #'basic.reject'{delivery_tag = DTag,
requeue = true}),
%% Consume the three messages from the queue
wait_for_messages(Config, [[QName, <<"3">>, <<"3">>, <<"0">>]]),
_ = consume(Ch, QName, [P1, P2, P3]),
consume_empty(Ch, QName),
%% Dead letter is empty
consume_empty(Ch, DLXQName),
?assertEqual(0, counted(messages_dead_lettered_rejected_total, Config)).
%% 2) Message ttl expires
dead_letter_ttl(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
declare_dead_letter_queues(Ch, Config, QName, DLXQName, [{<<"x-message-ttl">>, long, 1}]),
%% Publish message
P1 = <<"msg1">>,
publish(Ch, QName, [P1]),
wait_for_messages(Config, [[DLXQName, <<"1">>, <<"1">>, <<"0">>]]),
consume_empty(Ch, QName),
[_] = consume(Ch, DLXQName, [P1]),
?assertEqual(1, counted(messages_dead_lettered_expired_total, Config)).
%% 3) The queue length limit is exceeded, message dropped is dead lettered.
%% Default strategy: drop-head
dead_letter_max_length_drop_head(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
declare_dead_letter_queues(Ch, Config, QName, DLXQName, [{<<"x-max-length">>, long, 1}]),
P1 = <<"msg1">>,
P2 = <<"msg2">>,
P3 = <<"msg3">>,
%% Publish 3 messages
publish(Ch, QName, [P1, P2, P3]),
%% Consume the last one from the queue (max-length = 1)
wait_for_messages(Config, [[QName, <<"1">>, <<"1">>, <<"0">>]]),
_ = consume(Ch, QName, [P3]),
consume_empty(Ch, QName),
%% Consume the dropped ones from the dead letter queue
wait_for_messages(Config, [[DLXQName, <<"2">>, <<"2">>, <<"0">>]]),
_ = consume(Ch, DLXQName, [P1, P2]),
consume_empty(Ch, DLXQName),
?assertEqual(2, counted(messages_dead_lettered_maxlen_total, Config)).
%% https://github.com/rabbitmq/rabbitmq-server/issues/4940
dead_letter_reject_requeue_reject_norequeue(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
declare_dead_letter_queues(Ch, Config, QName, DLXQName),
P = <<"msg">>,
publish(Ch, QName, [P]),
%% Reject with requeue
wait_for_messages(Config, [[QName, <<"1">>, <<"1">>, <<"0">>]]),
[DTag0] = consume(Ch, QName, [P]),
wait_for_messages(Config, [[QName, <<"1">>, <<"0">>, <<"1">>]]),
amqp_channel:cast(Ch, #'basic.reject'{delivery_tag = DTag0,
requeue = true}),
%% If QName is a quorum queue, Ra log contains #requeue{} command
%% instead of #enqueue{} command because no delivery-limit is set.
wait_for_messages(Config, [[QName, <<"1">>, <<"1">>, <<"0">>]]),
%% Reject without requeue
[DTag1] = consume(Ch, QName, [P]),
wait_for_messages(Config, [[QName, <<"1">>, <<"0">>, <<"1">>]]),
amqp_channel:cast(Ch, #'basic.reject'{delivery_tag = DTag1,
requeue = false}),
wait_for_messages(Config, [[DLXQName, <<"1">>, <<"1">>, <<"0">>]]),
_ = consume(Ch, DLXQName, [P]),
consume_empty(Ch, DLXQName),
consume_empty(Ch, QName),
?assertEqual(1, counted(messages_dead_lettered_rejected_total, Config)).
%% Another strategy: reject-publish-dlx
dead_letter_max_length_reject_publish_dlx(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
declare_dead_letter_queues(Ch, Config, QName, DLXQName,
[{<<"x-max-length">>, long, 1},
{<<"x-overflow">>, longstr, <<"reject-publish-dlx">>}]),
P1 = <<"msg1">>,
P2 = <<"msg2">>,
P3 = <<"msg3">>,
%% Publish 3 messages
publish(Ch, QName, [P1, P2, P3]),
%% Consume the first one from the queue (max-length = 1)
wait_for_messages(Config, [[QName, <<"1">>, <<"1">>, <<"0">>]]),
_ = consume(Ch, QName, [P1]),
consume_empty(Ch, QName),
%% Consume the dropped ones from the dead letter queue
wait_for_messages(Config, [[DLXQName, <<"2">>, <<"2">>, <<"0">>]]),
_ = consume(Ch, DLXQName, [P2, P3]),
consume_empty(Ch, DLXQName),
?assertEqual(2, counted(messages_dead_lettered_maxlen_total, Config)).
%% Dead letter exchange does not have to be declared when the queue is declared, but it should
%% exist by the time messages need to be dead-lettered; if it is missing then, the messages will
%% be silently dropped.
dead_letter_missing_exchange(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
Args = ?config(queue_args, Config),
Durable = ?config(queue_durable, Config),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
DLXExchange = <<"dlx-exchange-2">>,
#'exchange.delete_ok'{} = amqp_channel:call(Ch, #'exchange.delete'{exchange = DLXExchange}),
DeadLetterArgs = [{<<"x-max-length">>, long, 1},
{<<"x-dead-letter-exchange">>, longstr, DLXExchange},
{<<"x-dead-letter-routing-key">>, longstr, DLXQName}],
#'queue.declare_ok'{} = amqp_channel:call(Ch, #'queue.declare'{queue = QName, arguments = DeadLetterArgs ++ Args, durable = Durable}),
#'queue.declare_ok'{} = amqp_channel:call(Ch, #'queue.declare'{queue = DLXQName, durable = Durable}),
P1 = <<"msg1">>,
P2 = <<"msg2">>,
%% Publish one message
publish(Ch, QName, [P1]),
%% Consume it
wait_for_messages(Config, [[QName, <<"1">>, <<"1">>, <<"0">>]]),
[DTag] = consume(Ch, QName, [P1]),
%% Reject it
amqp_channel:cast(Ch, #'basic.reject'{delivery_tag = DTag,
requeue = false}),
wait_for_messages(Config, [[QName, <<"0">>, <<"0">>, <<"0">>]]),
%% Message is not in the dead letter queue (exchange does not exist)
consume_empty(Ch, DLXQName),
%% Declare the dead-letter exchange
#'exchange.declare_ok'{} = amqp_channel:call(Ch, #'exchange.declare'{exchange = DLXExchange}),
#'queue.bind_ok'{} = amqp_channel:call(Ch, #'queue.bind'{queue = DLXQName,
exchange = DLXExchange,
routing_key = DLXQName}),
%% Publish another message
publish(Ch, QName, [P2]),
%% Consume it
wait_for_messages(Config, [[QName, <<"1">>, <<"1">>, <<"0">>]]),
[DTag2] = consume(Ch, QName, [P2]),
%% Reject it
amqp_channel:cast(Ch, #'basic.reject'{delivery_tag = DTag2,
requeue = false}),
%% Consume the rejected message from the dead letter queue
wait_for_messages(Config, [[DLXQName, <<"1">>, <<"1">>, <<"0">>]]),
{#'basic.get_ok'{}, #amqp_msg{payload = P2}} =
amqp_channel:call(Ch, #'basic.get'{queue = DLXQName}),
consume_empty(Ch, DLXQName),
?assertEqual(1, counted(messages_dead_lettered_rejected_total, Config)).
%%
%% ROUTING
%%
%% Dead-lettered messages are routed to their dead letter exchange either:
%% with the routing key specified for the queue they were on; or,
%% if this was not set, (3) with the same routing keys they were originally published with.
%% (4) This includes routing keys added by the CC and BCC headers.
%%
%% 3) All previous tests used a specific key, test the original ones now.
dead_letter_routing_key(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
Args = ?config(queue_args, Config),
Durable = ?config(queue_durable, Config),
DLXExchange = ?config(dlx_exchange, Config),
%% Do not use a specific key
DeadLetterArgs = [{<<"x-dead-letter-exchange">>, longstr, DLXExchange}],
#'exchange.declare_ok'{} = amqp_channel:call(Ch, #'exchange.declare'{exchange = DLXExchange}),
#'queue.declare_ok'{} = amqp_channel:call(Ch, #'queue.declare'{queue = QName, arguments = DeadLetterArgs ++ Args, durable = Durable}),
#'queue.declare_ok'{} = amqp_channel:call(Ch, #'queue.declare'{queue = DLXQName, durable = Durable}),
P1 = <<"msg1">>,
P2 = <<"msg2">>,
%% Publish, consume and nack the first message
publish(Ch, QName, [P1]),
wait_for_messages(Config, [[QName, <<"1">>, <<"1">>, <<"0">>]]),
[DTag1] = consume(Ch, QName, [P1]),
amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag1,
multiple = false,
requeue = false}),
case group_name(Config) of
at_most_once ->
%% Both queues are empty as the message could not been routed in the dlx exchange
wait_for_messages(Config, [[QName, <<"0">>, <<"0">>, <<"0">>]]);
at_least_once ->
wait_for_messages(Config, [[QName, <<"1">>, <<"0">>, <<"0">>]])
end,
consume_empty(Ch, QName),
consume_empty(Ch, DLXQName),
%% Bind the dlx queue with the original queue routing key
#'queue.bind_ok'{} = amqp_channel:call(Ch, #'queue.bind'{queue = DLXQName,
exchange = DLXExchange,
routing_key = QName}),
%% Publish, consume and nack the second message
publish(Ch, QName, [P2]),
wait_for_messages(Config, [[QName, <<"1">>, <<"1">>, <<"0">>]]),
[DTag2] = consume(Ch, QName, [P2]),
amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag2,
multiple = false,
requeue = false}),
%% Message can now be routed using the recently binded key
case group_name(Config) of
at_most_once ->
wait_for_messages(Config, [[DLXQName, <<"1">>, <<"1">>, <<"0">>]]),
consume(Ch, DLXQName, [P2]);
at_least_once ->
wait_for_messages(Config, [[DLXQName, <<"2">>, <<"2">>, <<"0">>]]),
consume(Ch, DLXQName, [P1, P2]),
?assertEqual(2, counted(messages_dead_lettered_confirmed_total, Config))
end,
consume_empty(Ch, QName),
?assertEqual(2, counted(messages_dead_lettered_rejected_total, Config)).
%% 4a) If a specific routing key was not set for the queue, use routing keys added by the
%% CC and BCC headers
dead_letter_routing_key_header_CC(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
Args = ?config(queue_args, Config),
Durable = ?config(queue_durable, Config),
DLXExchange = ?config(dlx_exchange, Config),
%% Do not use a specific key
DeadLetterArgs = [{<<"x-dead-letter-exchange">>, longstr, DLXExchange}],
#'exchange.declare_ok'{} = amqp_channel:call(Ch, #'exchange.declare'{exchange = DLXExchange}),
#'queue.declare_ok'{} = amqp_channel:call(Ch, #'queue.declare'{queue = QName, arguments = DeadLetterArgs ++ Args, durable = Durable}),
#'queue.declare_ok'{} = amqp_channel:call(Ch, #'queue.declare'{queue = DLXQName, durable = Durable}),
#'queue.bind_ok'{} = amqp_channel:call(Ch, #'queue.bind'{queue = DLXQName,
exchange = DLXExchange,
routing_key = DLXQName}),
P1 = <<"msg1">>,
P2 = <<"msg2">>,
CCHeader = {<<"CC">>, array, [{longstr, DLXQName}]},
%% Publish, consume and nack two messages, one with CC header
publish(Ch, QName, [P1]),
publish(Ch, QName, [P2], [CCHeader]),
wait_for_messages(Config, [[QName, <<"2">>, <<"2">>, <<"0">>]]),
[_, DTag2] = consume(Ch, QName, [P1, P2]),
%% P2 is also published to the DLX queue because of the binding to the default exchange
[_] = consume(Ch, DLXQName, [P2]),
amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag2,
multiple = true,
requeue = false}),
%% The second message should have been routed using the CC header
wait_for_messages(Config, [[DLXQName, <<"2">>, <<"1">>, <<"1">>]]),
consume_empty(Ch, QName),
consume(Ch, DLXQName, [P2]),
consume_empty(Ch, DLXQName).
%% 4b) If a specific routing key was not set for the queue, use routing keys added by the
%% CC and BCC headers
dead_letter_routing_key_header_BCC(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
Args = ?config(queue_args, Config),
Durable = ?config(queue_durable, Config),
DLXExchange = ?config(dlx_exchange, Config),
%% Do not use a specific key
DeadLetterArgs = [{<<"x-dead-letter-exchange">>, longstr, DLXExchange}],
#'exchange.declare_ok'{} = amqp_channel:call(Ch, #'exchange.declare'{exchange = DLXExchange}),
#'queue.declare_ok'{} = amqp_channel:call(Ch, #'queue.declare'{queue = QName, arguments = DeadLetterArgs ++ Args, durable = Durable}),
#'queue.declare_ok'{} = amqp_channel:call(Ch, #'queue.declare'{queue = DLXQName, durable = Durable}),
#'queue.bind_ok'{} = amqp_channel:call(Ch, #'queue.bind'{queue = DLXQName,
exchange = DLXExchange,
routing_key = DLXQName}),
P1 = <<"msg1">>,
P2 = <<"msg2">>,
BCCHeader = {<<"BCC">>, array, [{longstr, DLXQName}]},
%% Publish, consume and nack two messages, one with BCC header
publish(Ch, QName, [P1]),
publish(Ch, QName, [P2], [BCCHeader]),
wait_for_messages(Config, [[QName, <<"2">>, <<"2">>, <<"0">>]]),
[_, DTag2] = consume(Ch, QName, [P1, P2]),
%% P2 is also published to the DLX queue because of the binding to the default exchange
[_] = consume(Ch, DLXQName, [P2]),
amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag2,
multiple = true,
requeue = false}),
%% The second message should have been routed using the BCC header
wait_for_messages(Config, [[DLXQName, <<"2">>, <<"1">>, <<"1">>]]),
consume_empty(Ch, QName),
consume(Ch, DLXQName, [P2]),
consume_empty(Ch, DLXQName).
%% It is possible to form a cycle of message dead-lettering. For instance,
%% this can happen when a queue dead-letters messages to the default exchange without
%% specifying a dead-letter routing key (5). Messages in such cycles (i.e. messages that
%% reach the same queue twice) will be dropped if there was no rejections in the entire cycle.
%% i.e. x-message-ttl (7), x-max-length (6)
%%
%% 6) Message is dead lettered due to queue length limit, and then dropped by the broker as it is
%% republished to the same queue.
dead_letter_routing_key_cycle_max_length(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
Args = ?config(queue_args, Config),
Durable = ?config(queue_durable, Config),
QName = ?config(queue_name, Config),
DeadLetterArgs = [{<<"x-max-length">>, long, 1},
{<<"x-dead-letter-exchange">>, longstr, <<>>}],
#'queue.declare_ok'{} = amqp_channel:call(Ch, #'queue.declare'{queue = QName, arguments = DeadLetterArgs ++ Args, durable = Durable}),
P1 = <<"msg1">>,
P2 = <<"msg2">>,
%% Publish messages, consume and acknowledge the second one (x-max-length = 1)
publish(Ch, QName, [P1, P2]),
wait_for_messages(Config, [[QName, <<"1">>, <<"1">>, <<"0">>]]),
[DTag] = consume(Ch, QName, [P2]),
consume_empty(Ch, QName),
amqp_channel:cast(Ch, #'basic.ack'{delivery_tag = DTag}),
%% Queue is empty, P1 has not been republished in a loop
wait_for_messages(Config, [[QName, <<"0">>, <<"0">>, <<"0">>]]),
consume_empty(Ch, QName),
?assertEqual(1, counted(messages_dead_lettered_maxlen_total, Config)).
%% 7) Message is dead lettered due to message ttl.
dead_letter_routing_key_cycle_ttl(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
Args = ?config(queue_args, Config),
Durable = ?config(queue_durable, Config),
QName = ?config(queue_name, Config),
DeadLetterArgs = [{<<"x-message-ttl">>, long, 1},
{<<"x-dead-letter-exchange">>, longstr, <<>>}],
#'queue.declare_ok'{} = amqp_channel:call(Ch, #'queue.declare'{queue = QName, arguments = DeadLetterArgs ++ Args, durable = Durable}),
P1 = <<"msg1">>,
P2 = <<"msg2">>,
%% Publish messages
publish(Ch, QName, [P1, P2]),
wait_for_messages(Config, [[QName, <<"0">>, <<"0">>, <<"0">>]]),
consume_empty(Ch, QName),
?assertEqual(2, counted(messages_dead_lettered_expired_total, Config)).
%% 5) Messages continue to be republished as there are manual rejections
dead_letter_routing_key_cycle_with_reject(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
Args = ?config(queue_args, Config),
Durable = ?config(queue_durable, Config),
QName = ?config(queue_name, Config),
DeadLetterArgs = [{<<"x-dead-letter-exchange">>, longstr, <<>>}],
#'queue.declare_ok'{} = amqp_channel:call(Ch, #'queue.declare'{queue = QName, arguments = DeadLetterArgs ++ Args, durable = Durable}),
P = <<"msg1">>,
%% Publish message
publish(Ch, QName, [P]),
wait_for_messages(Config, [[QName, <<"1">>, <<"1">>, <<"0">>]]),
[DTag] = consume(Ch, QName, [P]),
amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag,
multiple = false,
requeue = false}),
wait_for_messages(Config, [[QName, <<"1">>, <<"1">>, <<"0">>]]),
[DTag1] = consume(Ch, QName, [P]),
amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag1,
multiple = false,
requeue = false}),
%% Message its being republished
wait_for_messages(Config, [[QName, <<"1">>, <<"1">>, <<"0">>]]),
[_] = consume(Ch, QName, [P]),
?assertEqual(2, counted(messages_dead_lettered_rejected_total, Config)).
%%
%% For any given queue, a DLX can be defined by clients using the queue's arguments,
%% or in the server using policies (8). In the case where both policy and arguments specify a DLX,
%% the one specified in arguments overrules the one specified in policy (9).
%%
%% 8) Use server policies
dead_letter_policy(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
Args0 = ?config(queue_args, Config),
%% declaring a quorum queue with x-dead-letter-strategy without defining a DLX will fail
Args = proplists:delete(<<"x-dead-letter-strategy">>, Args0),
Durable = ?config(queue_durable, Config),
DLXExchange = ?config(dlx_exchange, Config),
%% Do not use arguments
#'exchange.declare_ok'{} = amqp_channel:call(Ch, #'exchange.declare'{exchange = DLXExchange}),
#'queue.declare_ok'{} = amqp_channel:call(Ch, #'queue.declare'{queue = QName, arguments = Args,
durable = Durable}),
#'queue.declare_ok'{} = amqp_channel:call(Ch, #'queue.declare'{queue = DLXQName,
durable = Durable}),
#'queue.bind_ok'{} = amqp_channel:call(Ch, #'queue.bind'{queue = DLXQName,
exchange = DLXExchange,
routing_key = DLXQName}),
P1 = <<"msg1">>,
P2 = <<"msg2">>,
%% Publish 2 messages
publish(Ch, QName, [P1, P2]),
%% Consume them
wait_for_messages(Config, [[QName, <<"2">>, <<"2">>, <<"0">>]]),
[DTag1, DTag2] = consume(Ch, QName, [P1, P2]),
%% Nack the first one with multiple = false
amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag1,
multiple = false,
requeue = false}),
%% Only one message unack left in the queue
wait_for_messages(Config, [[QName, <<"1">>, <<"0">>, <<"1">>]]),
consume_empty(Ch, QName),
consume_empty(Ch, DLXQName),
%% Set a policy
ok = rabbit_ct_broker_helpers:set_policy(Config, 0, ?config(policy, Config), QName,
<<"queues">>,
[{<<"dead-letter-exchange">>, DLXExchange},
{<<"dead-letter-routing-key">>, DLXQName}]),
?awaitMatch([_ | _],
begin
{ok, Q0} = rabbit_ct_broker_helpers:rpc(
Config, 0,
rabbit_amqqueue, lookup,
[rabbit_misc:r(<<"/">>, queue, QName)], infinity),
amqqueue:get_policy(Q0)
end,
30000),
%% Nack the second message
amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag2,
multiple = false,
requeue = false}),
%% Queue is empty
wait_for_messages(Config, [[QName, <<"0">>, <<"0">>, <<"0">>]]),
consume_empty(Ch, QName),
%% Consume the message from the dead letter queue
consume(Ch, DLXQName, [P2]),
consume_empty(Ch, DLXQName).
%% 9) Argument overrides server policy
dead_letter_override_policy(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
%% Set a policy, it creates a cycle but message will be republished with the nack.
%% Good enough for this test.
ok = rabbit_ct_broker_helpers:set_policy(Config, 0, ?config(policy, Config), QName,
<<"queues">>,
[{<<"dead-letter-exchange">>, <<>>},
{<<"dead-letter-routing-key">>, QName}]),
%% Declare arguments override the policy and set routing queue
declare_dead_letter_queues(Ch, Config, QName, DLXQName),
P1 = <<"msg1">>,
publish(Ch, QName, [P1]),
wait_for_messages(Config, [[QName, <<"1">>, <<"1">>, <<"0">>]]),
[DTag1] = consume(Ch, QName, [P1]),
amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag1,
multiple = false,
requeue = false}),
%% Queue is empty
wait_for_messages(Config, [[DLXQName, <<"1">>, <<"1">>, <<"0">>]]),
consume_empty(Ch, QName),
[_] = consume(Ch, DLXQName, [P1]).
%% 9) Policy is set after have declared a queue with dead letter arguments. Policy will be
%% overridden/ignored.
dead_letter_ignore_policy(Config) ->
{_Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
QName = ?config(queue_name, Config),
DLXQName = ?config(queue_name_dlx, Config),
declare_dead_letter_queues(Ch, Config, QName, DLXQName),
%% Set a policy
ok = rabbit_ct_broker_helpers:set_policy(Config, 0, ?config(policy, Config), QName,
<<"queues">>,
[{<<"dead-letter-exchange">>, <<>>},
{<<"dead-letter-routing-key">>, QName}]),
P1 = <<"msg1">>,
publish(Ch, QName, [P1]),
wait_for_messages(Config, [[QName, <<"1">>, <<"1">>, <<"0">>]]),
[DTag1] = consume(Ch, QName, [P1]),
amqp_channel:cast(Ch, #'basic.nack'{delivery_tag = DTag1,
multiple = false,
requeue = false}),
%% Message is in the dead letter queue, original queue is empty
wait_for_messages(Config, [[DLXQName, <<"1">>, <<"1">>, <<"0">>]]),