|
| 1 | +%% This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +%% License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +%% file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | +%% |
| 5 | +%% Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. |
| 6 | +%% |
| 7 | + |
| 8 | +-module(pending_count_SUITE). |
| 9 | + |
| 10 | +-compile(export_all). |
| 11 | + |
| 12 | +-include_lib("eunit/include/eunit.hrl"). |
| 13 | +-include_lib("rabbit/include/mc.hrl"). |
| 14 | +-include("../include/rabbit_shovel.hrl"). |
| 15 | + |
| 16 | +%%%=================================================================== |
| 17 | +%%% Common Test callbacks |
| 18 | +%%%=================================================================== |
| 19 | + |
| 20 | +all() -> |
| 21 | + [ |
| 22 | + {group, pending_count_tests} |
| 23 | + ]. |
| 24 | + |
| 25 | +groups() -> |
| 26 | + [ |
| 27 | + {pending_count_tests, [], [ |
| 28 | + amqp091_pending_count_empty_queue, |
| 29 | + amqp091_pending_count_with_messages, |
| 30 | + amqp091_pending_count_after_drain, |
| 31 | + amqp10_pending_count_empty_list, |
| 32 | + amqp10_pending_count_with_messages, |
| 33 | + amqp10_pending_count_after_clear, |
| 34 | + local_pending_count_empty_queue, |
| 35 | + local_pending_count_after_settle, |
| 36 | + behaviour_metrics_includes_pending, |
| 37 | + behaviour_pending_count_delegation |
| 38 | + ]} |
| 39 | + ]. |
| 40 | + |
| 41 | +init_per_suite(Config) -> |
| 42 | + Config. |
| 43 | + |
| 44 | +end_per_suite(_Config) -> |
| 45 | + ok. |
| 46 | + |
| 47 | +init_per_group(_Group, Config) -> |
| 48 | + Config. |
| 49 | + |
| 50 | +end_per_group(_Group, _Config) -> |
| 51 | + ok. |
| 52 | + |
| 53 | +init_per_testcase(_TestCase, Config) -> |
| 54 | + Config. |
| 55 | + |
| 56 | +end_per_testcase(_TestCase, _Config) -> |
| 57 | + meck:unload(), |
| 58 | + ok. |
| 59 | + |
| 60 | +%%%=================================================================== |
| 61 | +%%% Test cases |
| 62 | +%%%=================================================================== |
| 63 | + |
| 64 | +%% Test AMQP 0.9.1 pending_count functionality |
| 65 | +amqp091_pending_count_empty_queue(_Config) -> |
| 66 | + %% Test that pending_count returns 0 when no messages are pending |
| 67 | + State = #{dest => #{}}, |
| 68 | + ?assertEqual(0, rabbit_amqp091_shovel:pending_count(State)). |
| 69 | + |
| 70 | +amqp091_pending_count_with_messages(_Config) -> |
| 71 | + %% Test that pending_count returns correct count when messages are pending |
| 72 | + PendingQueue = queue:from_list([{1, msg1}, {2, msg2}, {3, msg3}]), |
| 73 | + State = #{dest => #{pending => PendingQueue}}, |
| 74 | + ?assertEqual(3, rabbit_amqp091_shovel:pending_count(State)). |
| 75 | + |
| 76 | +amqp091_pending_count_after_drain(_Config) -> |
| 77 | + %% Test that pending_count returns 0 after messages are drained |
| 78 | + EmptyQueue = queue:new(), |
| 79 | + State = #{dest => #{pending => EmptyQueue}}, |
| 80 | + ?assertEqual(0, rabbit_amqp091_shovel:pending_count(State)). |
| 81 | + |
| 82 | +%% Test AMQP 1.0 pending_count functionality |
| 83 | +amqp10_pending_count_empty_list(_Config) -> |
| 84 | + %% Test that pending_count returns 0 when no messages are pending |
| 85 | + State = #{dest => #{}}, |
| 86 | + ?assertEqual(0, rabbit_amqp10_shovel:pending_count(State)). |
| 87 | + |
| 88 | +amqp10_pending_count_with_messages(_Config) -> |
| 89 | + %% Test that pending_count returns correct count when messages are pending |
| 90 | + PendingList = [{1, msg1}, {2, msg2}], |
| 91 | + State = #{dest => #{pending => PendingList}}, |
| 92 | + ?assertEqual(2, rabbit_amqp10_shovel:pending_count(State)). |
| 93 | + |
| 94 | +amqp10_pending_count_after_clear(_Config) -> |
| 95 | + %% Test that pending_count returns 0 after pending list is cleared |
| 96 | + State = #{dest => #{pending => []}}, |
| 97 | + ?assertEqual(0, rabbit_amqp10_shovel:pending_count(State)). |
| 98 | + |
| 99 | +%% Test Local shovel pending_count functionality |
| 100 | +local_pending_count_empty_queue(_Config) -> |
| 101 | + %% Test that pending_count returns 0 when unacked message queue is empty |
| 102 | + EmptyQueue = lqueue:new(), |
| 103 | + State = #{source => #{current => #{unacked_message_q => EmptyQueue}}}, |
| 104 | + ?assertEqual(0, rabbit_local_shovel:pending_count(State)). |
| 105 | + |
| 106 | + |
| 107 | +local_pending_count_after_settle(_Config) -> |
| 108 | + %% Test that pending_count returns 0 when state doesn't contain unacked queue |
| 109 | + State = #{source => #{current => #{}}}, |
| 110 | + ?assertEqual(0, rabbit_local_shovel:pending_count(State)). |
| 111 | + |
| 112 | +%% Test behaviour module integration |
| 113 | +behaviour_metrics_includes_pending(_Config) -> |
| 114 | + %% Mock the destination module's pending_count and status functions |
| 115 | + meck:new(rabbit_amqp091_shovel, [passthrough]), |
| 116 | + meck:expect(rabbit_amqp091_shovel, pending_count, fun(_) -> 5 end), |
| 117 | + meck:expect(rabbit_amqp091_shovel, status, fun(_) -> running end), |
| 118 | + |
| 119 | + State = #{source => #{remaining => 10, remaining_unacked => 3}, |
| 120 | + dest => #{module => rabbit_amqp091_shovel, forwarded => 7}}, |
| 121 | + |
| 122 | + {_Status, Metrics} = rabbit_shovel_behaviour:status(State), |
| 123 | + |
| 124 | + ?assertMatch(#{remaining := 10, |
| 125 | + remaining_unacked := 3, |
| 126 | + pending := 5, |
| 127 | + forwarded := 7}, Metrics), |
| 128 | + |
| 129 | + ?assert(meck:validate(rabbit_amqp091_shovel)). |
| 130 | + |
| 131 | +behaviour_pending_count_delegation(_Config) -> |
| 132 | + %% Test that the behaviour module correctly delegates to the specific implementation |
| 133 | + meck:new(rabbit_amqp10_shovel, [passthrough]), |
| 134 | + meck:expect(rabbit_amqp10_shovel, pending_count, fun(_State) -> 3 end), |
| 135 | + meck:expect(rabbit_amqp10_shovel, status, fun(_State) -> running end), |
| 136 | + |
| 137 | + State = #{dest => #{module => rabbit_amqp10_shovel}}, |
| 138 | + |
| 139 | + %% This would be called indirectly through status/1 |
| 140 | + {_Status, Metrics} = rabbit_shovel_behaviour:status(#{source => #{}, |
| 141 | + dest => maps:get(dest, State)}), |
| 142 | + |
| 143 | + ?assertEqual(3, maps:get(pending, Metrics)), |
| 144 | + ?assert(meck:validate(rabbit_amqp10_shovel)). |
0 commit comments