Skip to content

Commit f4034f3

Browse files
committed
fixup: add msg_size_metrics unit test
1 parent 48da5d2 commit f4034f3

File tree

4 files changed

+147
-1
lines changed

4 files changed

+147
-1
lines changed

deps/rabbit/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,11 @@ rabbitmq_integration_suite(
996996
size = "medium",
997997
)
998998

999+
rabbitmq_suite(
1000+
name = "unit_msg_size_metrics_SUITE",
1001+
size = "small",
1002+
)
1003+
9991004
rabbitmq_suite(
10001005
name = "unit_operator_policy_SUITE",
10011006
size = "small",

deps/rabbit/app.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,15 @@ def test_suite_beam_files(name = "test_suite_beam_files"):
17151715
erlc_opts = "//:test_erlc_opts",
17161716
deps = ["//deps/amqp_client:erlang_app", "//deps/rabbitmq_ct_helpers:erlang_app"],
17171717
)
1718+
erlang_bytecode(
1719+
name = "unit_msg_size_metrics_SUITE_beam_files",
1720+
testonly = True,
1721+
srcs = ["test/unit_msg_size_metrics_SUITE.erl"],
1722+
outs = ["test/unit_msg_size_metrics_SUITE.beam"],
1723+
app_name = "rabbit",
1724+
erlc_opts = "//:test_erlc_opts",
1725+
deps = [],
1726+
)
17181727
erlang_bytecode(
17191728
name = "unit_operator_policy_SUITE_beam_files",
17201729
testonly = True,

deps/rabbit/src/rabbit_msg_size_metrics.erl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
%% Useful for testing
1919
-export([overview/1,
20-
changed_buckets/2]).
20+
changed_buckets/2,
21+
cleanup/1]).
2122

2223
-define(MSG_SIZE_BUCKETS,
2324
[{1, 64},
@@ -44,6 +45,10 @@ init([{protocol, Protocol}]) ->
4445
Counters = counters:new(Size, [write_concurrency]),
4546
put_counters(Protocol, Counters).
4647

48+
-spec cleanup(labels()) -> any().
49+
cleanup([{protocol, Protocol}]) ->
50+
delete_counters(Protocol).
51+
4752
-spec overview() -> #{labels() => #{atom() => hist_values()}}.
4853
overview() ->
4954
LabelsList = fetch_labels(),
@@ -121,3 +126,6 @@ fetch_counters(Protocol) ->
121126

122127
fetch_labels() ->
123128
[[{protocol, Protocol}] || {{?MODULE, Protocol}, _} <- persistent_term:get()].
129+
130+
delete_counters(Protocol) ->
131+
persistent_term:erase({?MODULE, Protocol}).
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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-2024 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
6+
%%
7+
8+
-module(unit_msg_size_metrics_SUITE).
9+
10+
-include_lib("stdlib/include/assert.hrl").
11+
12+
-compile(export_all).
13+
14+
all() ->
15+
[
16+
{group, tests}
17+
].
18+
19+
groups() ->
20+
[
21+
{tests, [],
22+
[
23+
smoketest
24+
]}
25+
].
26+
27+
%% -------------------------------------------------------------------
28+
%% Testsuite setup/teardown.
29+
%% -------------------------------------------------------------------
30+
31+
init_per_suite(Config) ->
32+
_ = rabbit_msg_size_metrics:init([{protocol, proto1}]),
33+
Config.
34+
35+
end_per_suite(Config) ->
36+
_ = rabbit_msg_size_metrics:cleanup([{protocol, proto1}]),
37+
Config.
38+
39+
init_per_group(_, Config) ->
40+
Config.
41+
42+
end_per_group(_, Config) ->
43+
Config.
44+
45+
init_per_testcase(_Testcase, Config) ->
46+
Config.
47+
48+
end_per_testcase(_Testcase, Config) ->
49+
Config.
50+
51+
%% -------------------------------------------------------------------
52+
%% Testcases.
53+
%% -------------------------------------------------------------------
54+
55+
smoketest(_Config) ->
56+
57+
OverviewBefore = rabbit_msg_size_metrics:overview([{protocol, proto1}]),
58+
59+
_ = rabbit_msg_size_metrics:update(proto1, 100),
60+
_ = rabbit_msg_size_metrics:update(proto1, 60_000),
61+
62+
OverviewAfter = rabbit_msg_size_metrics:overview([{protocol, proto1}]),
63+
64+
?assertEqual(
65+
#{256 => 1,
66+
65536 => 1,
67+
sum => 60_100},
68+
rabbit_msg_size_metrics:changed_buckets(OverviewAfter, OverviewBefore)),
69+
70+
ExpectedHistValues =
71+
#{64 => 0,
72+
256 => 1,
73+
1024 => 0,
74+
4096 => 0,
75+
16384 => 0,
76+
65536 => 1,
77+
262144 => 0,
78+
1048576 => 0,
79+
4194304 => 0,
80+
16777216 => 0,
81+
67108864 => 0,
82+
268435456 => 0,
83+
infinity => 0,
84+
sum => 60_100},
85+
86+
?assertEqual(ExpectedHistValues, OverviewAfter),
87+
?assertEqual(
88+
#{[{protocol, proto1}] => #{message_size_bytes => ExpectedHistValues}},
89+
rabbit_msg_size_metrics:overview()),
90+
91+
?assertEqual(
92+
#{message_size_bytes =>
93+
#{type => histogram,
94+
values =>
95+
[{[{protocol,proto1}],
96+
[{64,0},
97+
{256,1},
98+
{1024,1},
99+
{4096,1},
100+
{16384,1},
101+
{65536,2},
102+
{262144,2},
103+
{1048576,2},
104+
{4194304,2},
105+
{16777216,2},
106+
{67108864,2},
107+
{268435456,2},
108+
{infinity,2}],
109+
2,
110+
60_100}],
111+
help =>
112+
"Size of messages received from publishers"}},
113+
rabbit_msg_size_metrics:prometheus_format()),
114+
115+
%% Value larger than largest limit
116+
_ = rabbit_msg_size_metrics:update(proto1, 1_000_000_000_000),
117+
OverviewInf = rabbit_msg_size_metrics:overview([{protocol, proto1}]),
118+
119+
?assertEqual(
120+
#{infinity => 1,
121+
sum => 1_000_000_000_000},
122+
rabbit_msg_size_metrics:changed_buckets(OverviewInf, OverviewAfter)),
123+
124+
ok.

0 commit comments

Comments
 (0)