Skip to content

Commit b1b995c

Browse files
Rework message size limit test
* Use smaller messages for tests * No need to publish a message above the hard limit, use a helper (these are unit tests) * Wording
1 parent 2cd21ee commit b1b995c

File tree

2 files changed

+54
-58
lines changed

2 files changed

+54
-58
lines changed

src/rabbit_channel.erl

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
-export([get_vhost/1, get_user/1]).
7373
%% For testing
7474
-export([build_topic_variable_map/3]).
75-
-export([list_queue_states/1]).
75+
-export([list_queue_states/1, get_max_message_size/0]).
7676

7777
%% Mgmt HTTP API refactor
7878
-export([handle_method/5]).
@@ -443,12 +443,7 @@ init([Channel, ReaderPid, WriterPid, ConnPid, ConnName, Protocol, User, VHost,
443443
_ ->
444444
Limiter0
445445
end,
446-
MaxMessageSize = case application:get_env(rabbit, max_message_size) of
447-
{ok, MS} when is_integer(MS) ->
448-
erlang:min(MS, ?MAX_MSG_SIZE);
449-
_ ->
450-
?MAX_MSG_SIZE
451-
end,
446+
MaxMessageSize = get_max_message_size(),
452447
State = #ch{state = starting,
453448
protocol = Protocol,
454449
channel = Channel,
@@ -802,6 +797,16 @@ code_change(_OldVsn, State, _Extra) ->
802797

803798
format_message_queue(Opt, MQ) -> rabbit_misc:format_message_queue(Opt, MQ).
804799

800+
-spec get_max_message_size() -> non_neg_integer().
801+
802+
get_max_message_size() ->
803+
case application:get_env(rabbit, max_message_size) of
804+
{ok, MS} when is_integer(MS) ->
805+
erlang:min(MS, ?MAX_MSG_SIZE);
806+
_ ->
807+
?MAX_MSG_SIZE
808+
end.
809+
805810
%%---------------------------------------------------------------------------
806811

807812
reply(Reply, NewState) -> {reply, Reply, next_state(NewState), hibernate}.
@@ -1000,9 +1005,9 @@ check_msg_size(Content, MaxMessageSize) ->
10001005
S when S > MaxMessageSize ->
10011006
ErrorMessage = case MaxMessageSize of
10021007
?MAX_MSG_SIZE ->
1003-
"message size ~B larger than max size ~B";
1008+
"message size ~B is larger than max size ~B";
10041009
_ ->
1005-
"message size ~B larger than configured max size ~B"
1010+
"message size ~B is larger than configured max size ~B"
10061011
end,
10071012
precondition_failed(ErrorMessage,
10081013
[Size, MaxMessageSize]);

test/unit_inbroker_parallel_SUITE.erl

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
-define(TIMEOUT_LIST_OPS_PASS, 5000).
2727
-define(TIMEOUT, 30000).
28+
-define(TIMEOUT_CHANNEL_EXCEPTION, 5000).
2829

2930
-define(CLEANUP_QUEUE_NAME, <<"cleanup-queue">>).
3031

@@ -58,13 +59,18 @@ groups() ->
5859
set_disk_free_limit_command,
5960
set_vm_memory_high_watermark_command,
6061
topic_matching,
61-
max_message_size,
6262
{queue_max_length, [], [
6363
{max_length_simple, [], MaxLengthTests},
64-
{max_length_mirrored, [], MaxLengthTests}]}
64+
{max_length_mirrored, [], MaxLengthTests}]},
65+
max_message_size
6566
]}
6667
].
6768

69+
suite() ->
70+
[
71+
{timetrap, {seconds, 30}}
72+
].
73+
6874
%% -------------------------------------------------------------------
6975
%% Testsuite setup/teardown.
7076
%% -------------------------------------------------------------------
@@ -1308,80 +1314,65 @@ assert_channel_alive(Ch) ->
13081314
amqp_channel:call(Ch, #'basic.publish'{routing_key = <<"nope">>},
13091315
#amqp_msg{payload = <<"HI">>}).
13101316

1311-
assert_channel_fail_max_size(Ch, Monitor, ExpectedException) ->
1317+
assert_channel_fail_max_size(Ch, Monitor) ->
13121318
receive
13131319
{'DOWN', Monitor, process, Ch,
13141320
{shutdown,
1315-
{server_initiated_close, 406, Exception}}} ->
1316-
?assertMatch(Exception, ExpectedException)
1317-
after 100000 ->
1321+
{server_initiated_close, 406, _Error}}} ->
1322+
ok
1323+
after ?TIMEOUT_CHANNEL_EXCEPTION ->
13181324
error({channel_exception_expected, max_message_size})
13191325
end.
13201326

13211327
max_message_size(Config) ->
1322-
Binary128M = gen_binary_mb(128),
1328+
Binary2M = gen_binary_mb(2),
1329+
Binary4M = gen_binary_mb(4),
1330+
Binary6M = gen_binary_mb(6),
1331+
Binary10M = gen_binary_mb(10),
1332+
1333+
Size2Mb = 1024 * 1024 * 2,
1334+
Size2Mb = byte_size(Binary2M),
13231335

1324-
%% Default message size is 128MB
1325-
Size128Mb = 1024 * 1024 * 128,
1326-
Size128Mb = byte_size(Binary128M),
1336+
rabbit_ct_broker_helpers:rpc(Config, 0,
1337+
application, set_env, [rabbit, max_message_size, 1024 * 1024 * 3]),
13271338

1328-
Size128Mb = rabbit_ct_broker_helpers:rpc(Config, 0,
1329-
application, get_env, [rabbit, max_message_size, undefined]),
13301339
{_, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
13311340

13321341
%% Binary is whithin the max size limit
1333-
amqp_channel:call(Ch, #'basic.publish'{routing_key = <<"nope">>}, #amqp_msg{payload = Binary128M}),
1342+
amqp_channel:call(Ch, #'basic.publish'{routing_key = <<"none">>}, #amqp_msg{payload = Binary2M}),
13341343
%% The channel process is alive
13351344
assert_channel_alive(Ch),
13361345

13371346
Monitor = monitor(process, Ch),
1338-
%% This publish should cause a channel exception
1339-
BinaryBiggerThan128M = <<"_", Binary128M/binary>>,
1340-
amqp_channel:call(Ch, #'basic.publish'{routing_key = <<"nope">>}, #amqp_msg{payload = BinaryBiggerThan128M}),
1341-
ct:pal("Assert channel error 128"),
1342-
ExpectedException = <<"PRECONDITION_FAILED - message size ",
1343-
(integer_to_binary(byte_size(BinaryBiggerThan128M)))/binary,
1344-
" larger than configured max size ",
1345-
(integer_to_binary(Size128Mb))/binary>>,
1346-
assert_channel_fail_max_size(Ch, Monitor, ExpectedException),
1347-
1348-
%% Set a bigger message size
1347+
amqp_channel:call(Ch, #'basic.publish'{routing_key = <<"none">>}, #amqp_msg{payload = Binary4M}),
1348+
assert_channel_fail_max_size(Ch, Monitor),
1349+
1350+
%% increase the limit
13491351
rabbit_ct_broker_helpers:rpc(Config, 0,
1350-
application, set_env, [rabbit, max_message_size, 1024 * 1024 * 256]),
1352+
application, set_env, [rabbit, max_message_size, 1024 * 1024 * 8]),
13511353

13521354
{_, Ch1} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
13531355

1354-
amqp_channel:call(Ch1, #'basic.publish'{routing_key = <<"nope">>}, #amqp_msg{payload = Binary128M}),
1356+
amqp_channel:call(Ch1, #'basic.publish'{routing_key = <<"nope">>}, #amqp_msg{payload = Binary2M}),
13551357
assert_channel_alive(Ch1),
13561358

1357-
amqp_channel:call(Ch1, #'basic.publish'{routing_key = <<"nope">>}, #amqp_msg{payload = BinaryBiggerThan128M}),
1359+
amqp_channel:call(Ch1, #'basic.publish'{routing_key = <<"nope">>}, #amqp_msg{payload = Binary4M}),
13581360
assert_channel_alive(Ch1),
13591361

1360-
%% Set message size above 512MB.
1361-
%% The actual limit will be 512MB
1362-
rabbit_ct_broker_helpers:rpc(Config, 0,
1363-
application, set_env, [rabbit, max_message_size, 1024 * 1024 * 515]),
1364-
1365-
%% Need a new channel for changes to take effect
1366-
rabbit_ct_client_helpers:close_channel(Ch1),
1367-
Ch2 = rabbit_ct_client_helpers:open_channel(Config),
1368-
1369-
Binary512M = << Binary128M/binary, Binary128M/binary,
1370-
Binary128M/binary, Binary128M/binary>>,
1362+
amqp_channel:call(Ch1, #'basic.publish'{routing_key = <<"nope">>}, #amqp_msg{payload = Binary6M}),
1363+
assert_channel_alive(Ch1),
13711364

1372-
BinaryBiggerThan512M = <<"_", Binary512M/binary>>,
1365+
Monitor1 = monitor(process, Ch1),
1366+
amqp_channel:call(Ch1, #'basic.publish'{routing_key = <<"none">>}, #amqp_msg{payload = Binary10M}),
1367+
assert_channel_fail_max_size(Ch1, Monitor1),
13731368

1374-
amqp_channel:call(Ch2, #'basic.publish'{routing_key = <<"nope">>}, #amqp_msg{payload = Binary512M}),
1375-
assert_channel_alive(Ch2),
1369+
%% increase beyond the hard limit
1370+
rabbit_ct_broker_helpers:rpc(Config, 0,
1371+
application, set_env, [rabbit, max_message_size, 1024 * 1024 * 600]),
1372+
Val = rabbit_ct_broker_helpers:rpc(Config, 0,
1373+
rabbit_channel, get_max_message_size, []),
13761374

1377-
Monitor2 = monitor(process, Ch2),
1378-
amqp_channel:call(Ch2, #'basic.publish'{routing_key = <<"nope">>}, #amqp_msg{payload = BinaryBiggerThan512M}),
1379-
ct:pal("Assert channel error 512"),
1380-
ExpectedException1 = <<"PRECONDITION_FAILED - message size ",
1381-
(integer_to_binary(byte_size(BinaryBiggerThan512M)))/binary,
1382-
" larger than max size ",
1383-
(integer_to_binary(byte_size(Binary512M)))/binary>>,
1384-
assert_channel_fail_max_size(Ch2, Monitor2, ExpectedException1).
1375+
?assertEqual(?MAX_MSG_SIZE, Val).
13851376

13861377
%% ---------------------------------------------------------------------------
13871378
%% rabbitmqctl helpers.

0 commit comments

Comments
 (0)