|
| 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(rabbit_top_util_SUITE). |
| 9 | + |
| 10 | +-compile(export_all). |
| 11 | + |
| 12 | +-include_lib("common_test/include/ct.hrl"). |
| 13 | +-include_lib("eunit/include/eunit.hrl"). |
| 14 | + |
| 15 | +all() -> |
| 16 | + [ |
| 17 | + {group, unit_tests} |
| 18 | + ]. |
| 19 | + |
| 20 | +groups() -> |
| 21 | + [ |
| 22 | + {unit_tests, [parallel], |
| 23 | + [ |
| 24 | + sort_by_param_known_process_keys, |
| 25 | + sort_by_param_known_ets_table_keys, |
| 26 | + sort_by_param_missing_uses_default, |
| 27 | + sort_by_param_unknown_uses_default, |
| 28 | + sort_by_param_unknown_does_not_create_atoms |
| 29 | + ]} |
| 30 | + ]. |
| 31 | + |
| 32 | +init_per_suite(Config) -> |
| 33 | + Config. |
| 34 | + |
| 35 | +end_per_suite(_Config) -> |
| 36 | + ok. |
| 37 | + |
| 38 | +init_per_group(_, Config) -> |
| 39 | + Config. |
| 40 | + |
| 41 | +end_per_group(_, _Config) -> |
| 42 | + ok. |
| 43 | + |
| 44 | +init_per_testcase(_Testcase, Config) -> |
| 45 | + Config. |
| 46 | + |
| 47 | +end_per_testcase(_Testcase, _Config) -> |
| 48 | + ok. |
| 49 | + |
| 50 | +%% |
| 51 | +%% Tests |
| 52 | +%% |
| 53 | + |
| 54 | +sort_by_param_known_process_keys(_Config) -> |
| 55 | + KnownKeys = [ |
| 56 | + {<<"reduction_delta">>, reduction_delta}, |
| 57 | + {<<"memory">>, memory}, |
| 58 | + {<<"message_queue_len">>, message_queue_len}, |
| 59 | + {<<"reductions">>, reductions}, |
| 60 | + {<<"status">>, status}, |
| 61 | + {<<"buffer_len">>, buffer_len}, |
| 62 | + {<<"pid">>, pid} |
| 63 | + ], |
| 64 | + [?assertEqual(Atom, rabbit_top_util:sort_by_param(req(Bin), some_default)) |
| 65 | + || {Bin, Atom} <- KnownKeys]. |
| 66 | + |
| 67 | +sort_by_param_known_ets_table_keys(_Config) -> |
| 68 | + KnownKeys = [ |
| 69 | + {<<"size">>, size}, |
| 70 | + {<<"name">>, name}, |
| 71 | + {<<"owner">>, owner}, |
| 72 | + {<<"owner_name">>, owner_name}, |
| 73 | + {<<"type">>, type}, |
| 74 | + {<<"protection">>, protection}, |
| 75 | + {<<"node">>, node}, |
| 76 | + {<<"keypos">>, keypos}, |
| 77 | + {<<"compressed">>, compressed}, |
| 78 | + {<<"named_table">>, named_table}, |
| 79 | + {<<"write_concurrency">>, write_concurrency}, |
| 80 | + {<<"read_concurrency">>, read_concurrency}, |
| 81 | + {<<"decentralized_counters">>, decentralized_counters} |
| 82 | + ], |
| 83 | + [?assertEqual(Atom, rabbit_top_util:sort_by_param(req(Bin), some_default)) |
| 84 | + || {Bin, Atom} <- KnownKeys]. |
| 85 | + |
| 86 | +sort_by_param_missing_uses_default(_Config) -> |
| 87 | + ?assertEqual(my_default, rabbit_top_util:sort_by_param(#{qs => <<>>}, my_default)). |
| 88 | + |
| 89 | +sort_by_param_unknown_uses_default(_Config) -> |
| 90 | + ?assertEqual(my_default, rabbit_top_util:sort_by_param(req(<<"unknown_column">>), my_default)). |
| 91 | + |
| 92 | +sort_by_param_unknown_does_not_create_atoms(_Config) -> |
| 93 | + Key = <<"nonexistent_sort_key_for_rabbitmq_top_12345">>, |
| 94 | + %% Confirm the atom does not exist before the call. |
| 95 | + ?assertError(badarg, binary_to_existing_atom(Key, utf8)), |
| 96 | + %% An unknown sort key must not create a new atom. |
| 97 | + _ = rabbit_top_util:sort_by_param(req(Key), reduction_delta), |
| 98 | + %% The atom must still not exist after the call. |
| 99 | + ?assertError(badarg, binary_to_existing_atom(Key, utf8)). |
| 100 | + |
| 101 | +%% |
| 102 | +%% Helpers |
| 103 | +%% |
| 104 | + |
| 105 | +req(SortValue) -> |
| 106 | + #{qs => <<"sort=", SortValue/binary>>}. |
0 commit comments