|
| 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-2026 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. |
| 6 | +%% |
| 7 | + |
| 8 | +-module(amqp10_connection_max_SUITE). |
| 9 | + |
| 10 | +-compile([export_all, nowarn_export_all]). |
| 11 | + |
| 12 | +-include_lib("common_test/include/ct.hrl"). |
| 13 | +-include_lib("eunit/include/eunit.hrl"). |
| 14 | +-include_lib("amqp_client/include/amqp_client.hrl"). |
| 15 | +-include_lib("amqp10_common/include/amqp10_framing.hrl"). |
| 16 | + |
| 17 | +-define(TIMEOUT, 30_000). |
| 18 | + |
| 19 | +all() -> |
| 20 | + [{group, single_node}]. |
| 21 | + |
| 22 | +groups() -> |
| 23 | + [{single_node, [], |
| 24 | + [ |
| 25 | + node_connection_limit_amqp10, |
| 26 | + node_limit_infinity_allows_amqp10, |
| 27 | + node_limit_room_after_close, |
| 28 | + node_connection_limit_shared_with_amqp091, |
| 29 | + amqp10_connection_is_tracked |
| 30 | + ]}]. |
| 31 | + |
| 32 | +suite() -> |
| 33 | + [{timetrap, {minutes, 3}}]. |
| 34 | + |
| 35 | +init_per_suite(Config) -> |
| 36 | + {ok, _} = application:ensure_all_started(amqp10_client), |
| 37 | + rabbit_ct_helpers:log_environment(), |
| 38 | + rabbit_ct_helpers:run_setup_steps(Config). |
| 39 | + |
| 40 | +end_per_suite(Config) -> |
| 41 | + rabbit_ct_helpers:run_teardown_steps(Config). |
| 42 | + |
| 43 | +init_per_group(Group, Config0) -> |
| 44 | + Config1 = rabbit_ct_helpers:set_config(Config0, |
| 45 | + [{rmq_nodename_suffix, Group}, |
| 46 | + {rmq_nodes_count, 1}]), |
| 47 | + rabbit_ct_helpers:run_steps(Config1, |
| 48 | + rabbit_ct_broker_helpers:setup_steps() ++ |
| 49 | + rabbit_ct_client_helpers:setup_steps()). |
| 50 | + |
| 51 | +end_per_group(_Group, Config) -> |
| 52 | + rabbit_ct_helpers:run_steps(Config, |
| 53 | + rabbit_ct_client_helpers:teardown_steps() ++ |
| 54 | + rabbit_ct_broker_helpers:teardown_steps()). |
| 55 | + |
| 56 | +init_per_testcase(Testcase, Config) -> |
| 57 | + set_node_limit(Config, infinity), |
| 58 | + rabbit_ct_helpers:testcase_started(Config, Testcase). |
| 59 | + |
| 60 | +end_per_testcase(Testcase, Config) -> |
| 61 | + set_node_limit(Config, infinity), |
| 62 | + rabbit_ct_helpers:testcase_finished(Config, Testcase). |
| 63 | + |
| 64 | +%% ------------------------------------------------------------------- |
| 65 | +%% Test cases |
| 66 | +%% ------------------------------------------------------------------- |
| 67 | + |
| 68 | +node_connection_limit_amqp10(Config) -> |
| 69 | + set_node_limit(Config, 0), |
| 70 | + {refused, _} = open_amqp10(Config), |
| 71 | + |
| 72 | + set_node_limit(Config, 5), |
| 73 | + Conns = [open_amqp10_ok(Config) || _ <- lists:seq(1, 5)], |
| 74 | + {refused, _} = open_amqp10(Config), |
| 75 | + [amqp_utils:close_connection_sync(C) || C <- Conns], |
| 76 | + ok. |
| 77 | + |
| 78 | +node_limit_infinity_allows_amqp10(Config) -> |
| 79 | + set_node_limit(Config, infinity), |
| 80 | + Conns = [open_amqp10_ok(Config) || _ <- lists:seq(1, 10)], |
| 81 | + [amqp_utils:close_connection_sync(C) || C <- Conns], |
| 82 | + ok. |
| 83 | + |
| 84 | +node_limit_room_after_close(Config) -> |
| 85 | + set_node_limit(Config, 2), |
| 86 | + C1 = open_amqp10_ok(Config), |
| 87 | + C2 = open_amqp10_ok(Config), |
| 88 | + {refused, _} = open_amqp10(Config), |
| 89 | + amqp_utils:close_connection_sync(C1), |
| 90 | + {ok, C3} = retry_open_amqp10(Config, 30), |
| 91 | + [amqp_utils:close_connection_sync(C) || C <- [C2, C3]], |
| 92 | + ok. |
| 93 | + |
| 94 | +%% Historically only AMQP 0-9-1 honored `connection_max`, |
| 95 | +%% so this example uses both protocols. |
| 96 | +node_connection_limit_shared_with_amqp091(Config) -> |
| 97 | + set_node_limit(Config, 2), |
| 98 | + C091 = rabbit_ct_client_helpers:open_unmanaged_connection(Config, 0), |
| 99 | + true = is_pid(C091), |
| 100 | + C10 = open_amqp10_ok(Config), |
| 101 | + {refused, _} = open_amqp10(Config), |
| 102 | + {error, not_allowed} = rabbit_ct_client_helpers:open_unmanaged_connection(Config, 0), |
| 103 | + amqp_utils:close_connection_sync(C10), |
| 104 | + rabbit_ct_client_helpers:close_connection(C091), |
| 105 | + ok. |
| 106 | + |
| 107 | +amqp10_connection_is_tracked(Config) -> |
| 108 | + ok = event_recorder:start(Config), |
| 109 | + try |
| 110 | + C = open_amqp10_ok(Config), |
| 111 | + ServerPid = wait_for_created_pid(Config), |
| 112 | + Local = rabbit_ct_broker_helpers:rpc( |
| 113 | + Config, 0, rabbit_networking, local_connections, []), |
| 114 | + ?assert(lists:member(ServerPid, Local)), |
| 115 | + amqp_utils:close_connection_sync(C), |
| 116 | + wait_for_event_types(Config, [connection_closed]) |
| 117 | + after |
| 118 | + ok = event_recorder:stop(Config) |
| 119 | + end, |
| 120 | + ok. |
| 121 | + |
| 122 | +%% ------------------------------------------------------------------- |
| 123 | +%% Helpers |
| 124 | +%% ------------------------------------------------------------------- |
| 125 | + |
| 126 | +set_node_limit(Config, Limit) -> |
| 127 | + ok = rabbit_ct_broker_helpers:rpc(Config, 0, |
| 128 | + application, set_env, |
| 129 | + [rabbit, connection_max, Limit]). |
| 130 | + |
| 131 | +open_amqp10_ok(Config) -> |
| 132 | + {ok, Conn} = open_amqp10(Config), |
| 133 | + Conn. |
| 134 | + |
| 135 | +open_amqp10(Config) -> |
| 136 | + {ok, Conn} = amqp10_client:open_connection(amqp_utils:connection_config(Config)), |
| 137 | + receive |
| 138 | + {amqp10_event, {connection, Conn, opened}} -> |
| 139 | + {ok, Conn}; |
| 140 | + {amqp10_event, {connection, Conn, {closed, Reason}}} -> |
| 141 | + {refused, Reason} |
| 142 | + after ?TIMEOUT -> |
| 143 | + ct:fail({open_amqp10_timeout, Conn}) |
| 144 | + end. |
| 145 | + |
| 146 | +%% Covers the brief window between client-side close and Ranch |
| 147 | +%% actually dropping the active TCP connection. |
| 148 | +retry_open_amqp10(_Config, 0) -> |
| 149 | + ct:fail(retry_open_amqp10_exhausted); |
| 150 | +retry_open_amqp10(Config, Attempts) -> |
| 151 | + case open_amqp10(Config) of |
| 152 | + {ok, _} = Result -> Result; |
| 153 | + {refused, _} -> |
| 154 | + timer:sleep(100), |
| 155 | + retry_open_amqp10(Config, Attempts - 1) |
| 156 | + end. |
| 157 | + |
| 158 | +wait_for_created_pid(Config) -> |
| 159 | + wait_for_created_pid(Config, [], 30). |
| 160 | + |
| 161 | +wait_for_created_pid(_Config, _Acc, 0) -> |
| 162 | + ct:fail(missing_connection_created_event); |
| 163 | +wait_for_created_pid(Config, Acc, N) -> |
| 164 | + Acc1 = Acc ++ event_recorder:get_events(Config), |
| 165 | + case [proplists:get_value(pid, Props) |
| 166 | + || #event{type = connection_created, props = Props} <- Acc1] of |
| 167 | + [Pid | _] when is_pid(Pid) -> Pid; |
| 168 | + [] -> wait_for_created_pid(Config, Acc1, N - 1) |
| 169 | + end. |
| 170 | + |
| 171 | +wait_for_event_types(Config, Wanted) -> |
| 172 | + wait_for_event_types(Config, Wanted, 30). |
| 173 | + |
| 174 | +wait_for_event_types(_Config, [], _N) -> |
| 175 | + ok; |
| 176 | +wait_for_event_types(_Config, Wanted, 0) -> |
| 177 | + ct:fail({missing_event_types, Wanted}); |
| 178 | +wait_for_event_types(Config, Wanted, N) -> |
| 179 | + Seen = [T || #event{type = T} <- event_recorder:get_events(Config)], |
| 180 | + Remaining = [T || T <- Wanted, not lists:member(T, Seen)], |
| 181 | + case Remaining of |
| 182 | + [] -> ok; |
| 183 | + _ -> wait_for_event_types(Config, Remaining, N - 1) |
| 184 | + end. |
0 commit comments