|
| 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-2023 VMware, Inc. or its affiliates. All rights reserved. |
| 6 | +%% |
| 7 | + |
| 8 | +-module(unit_rabbit_db_topic_exchange_SUITE). |
| 9 | + |
| 10 | +-include_lib("rabbit_common/include/rabbit.hrl"). |
| 11 | +-include_lib("eunit/include/eunit.hrl"). |
| 12 | +-include_lib("common_test/include/ct.hrl"). |
| 13 | + |
| 14 | +-compile(export_all). |
| 15 | + |
| 16 | +-define(VHOST, <<"/">>). |
| 17 | + |
| 18 | +all() -> |
| 19 | + [ |
| 20 | + {group, all_tests} |
| 21 | + ]. |
| 22 | + |
| 23 | +groups() -> |
| 24 | + [ |
| 25 | + {all_tests, [], all_tests()} |
| 26 | + ]. |
| 27 | + |
| 28 | +all_tests() -> |
| 29 | + [ |
| 30 | + set, |
| 31 | + delete, |
| 32 | + delete_all_for_exchange, |
| 33 | + match |
| 34 | + ]. |
| 35 | + |
| 36 | +%% ------------------------------------------------------------------- |
| 37 | +%% Test suite setup/teardown. |
| 38 | +%% ------------------------------------------------------------------- |
| 39 | + |
| 40 | +init_per_suite(Config) -> |
| 41 | + rabbit_ct_helpers:log_environment(), |
| 42 | + rabbit_ct_helpers:run_setup_steps(Config). |
| 43 | + |
| 44 | +end_per_suite(Config) -> |
| 45 | + rabbit_ct_helpers:run_teardown_steps(Config). |
| 46 | + |
| 47 | +init_per_group(Group, Config) -> |
| 48 | + Config1 = rabbit_ct_helpers:set_config(Config, [ |
| 49 | + {rmq_nodename_suffix, Group}, |
| 50 | + {rmq_nodes_count, 1} |
| 51 | + ]), |
| 52 | + rabbit_ct_helpers:run_steps(Config1, |
| 53 | + rabbit_ct_broker_helpers:setup_steps() ++ |
| 54 | + rabbit_ct_client_helpers:setup_steps()). |
| 55 | + |
| 56 | +end_per_group(_Group, Config) -> |
| 57 | + rabbit_ct_helpers:run_steps(Config, |
| 58 | + rabbit_ct_client_helpers:teardown_steps() ++ |
| 59 | + rabbit_ct_broker_helpers:teardown_steps()). |
| 60 | + |
| 61 | +init_per_testcase(Testcase, Config) -> |
| 62 | + rabbit_ct_helpers:testcase_started(Config, Testcase). |
| 63 | + |
| 64 | +end_per_testcase(Testcase, Config) -> |
| 65 | + rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_db_topic_exchange, clear, []), |
| 66 | + rabbit_ct_helpers:testcase_finished(Config, Testcase). |
| 67 | + |
| 68 | +%% --------------------------------------------------------------------------- |
| 69 | +%% Test Cases |
| 70 | +%% --------------------------------------------------------------------------- |
| 71 | + |
| 72 | +set(Config) -> |
| 73 | + passed = rabbit_ct_broker_helpers:rpc(Config, 0, ?MODULE, set1, [Config]). |
| 74 | + |
| 75 | +set1(_Config) -> |
| 76 | + Src = rabbit_misc:r(?VHOST, exchange, <<"test-exchange">>), |
| 77 | + Dst = rabbit_misc:r(?VHOST, queue, <<"test-queue">>), |
| 78 | + RoutingKey = <<"a.b.c">>, |
| 79 | + Binding = #binding{source = Src, key = RoutingKey, destination = Dst, args = #{}}, |
| 80 | + ?assertEqual([], rabbit_db_topic_exchange:match(Src, RoutingKey)), |
| 81 | + ?assertEqual(ok, rabbit_db_topic_exchange:set(Binding)), |
| 82 | + ?assertEqual(ok, rabbit_db_topic_exchange:set(Binding)), |
| 83 | + ?assertEqual([Dst], rabbit_db_topic_exchange:match(Src, RoutingKey)), |
| 84 | + passed. |
| 85 | + |
| 86 | +delete(Config) -> |
| 87 | + passed = rabbit_ct_broker_helpers:rpc(Config, 0, ?MODULE, delete1, [Config]). |
| 88 | + |
| 89 | +delete1(_Config) -> |
| 90 | + Src = rabbit_misc:r(?VHOST, exchange, <<"test-exchange">>), |
| 91 | + Dst1 = rabbit_misc:r(?VHOST, queue, <<"test-queue1">>), |
| 92 | + Dst2 = rabbit_misc:r(?VHOST, queue, <<"test-queue2">>), |
| 93 | + Dst3= rabbit_misc:r(?VHOST, queue, <<"test-queue3">>), |
| 94 | + Dsts = lists:sort([Dst1, Dst2, Dst3]), |
| 95 | + RoutingKey = <<"a.b.c">>, |
| 96 | + Binding1 = #binding{source = Src, key = RoutingKey, destination = Dst1, args = #{}}, |
| 97 | + Binding2 = #binding{source = Src, key = RoutingKey, destination = Dst2, args = #{}}, |
| 98 | + Binding3 = #binding{source = Src, key = RoutingKey, destination = Dst3, args = #{}}, |
| 99 | + ?assertEqual(ok, rabbit_db_topic_exchange:delete([Binding1])), |
| 100 | + ?assertEqual(ok, rabbit_db_topic_exchange:set(Binding1)), |
| 101 | + ?assertEqual(ok, rabbit_db_topic_exchange:set(Binding2)), |
| 102 | + ?assertEqual(ok, rabbit_db_topic_exchange:set(Binding3)), |
| 103 | + ?assertEqual(Dsts, lists:sort(rabbit_db_topic_exchange:match(Src, RoutingKey))), |
| 104 | + ?assertEqual(ok, rabbit_db_topic_exchange:delete([Binding1, Binding2])), |
| 105 | + ?assertEqual([Dst3], rabbit_db_topic_exchange:match(Src, RoutingKey)), |
| 106 | + passed. |
| 107 | + |
| 108 | +delete_all_for_exchange(Config) -> |
| 109 | + passed = rabbit_ct_broker_helpers:rpc(Config, 0, ?MODULE, delete_all_for_exchange1, [Config]). |
| 110 | + |
| 111 | +delete_all_for_exchange1(_Config) -> |
| 112 | + Src1 = rabbit_misc:r(?VHOST, exchange, <<"test-exchange1">>), |
| 113 | + Src2 = rabbit_misc:r(?VHOST, exchange, <<"test-exchange2">>), |
| 114 | + Dst1 = rabbit_misc:r(?VHOST, queue, <<"test-queue1">>), |
| 115 | + Dst2 = rabbit_misc:r(?VHOST, queue, <<"test-queue2">>), |
| 116 | + Dsts = lists:sort([Dst1, Dst2]), |
| 117 | + RoutingKey = <<"a.b.c">>, |
| 118 | + ?assertEqual(ok, rabbit_db_topic_exchange:delete_all_for_exchange(Src1)), |
| 119 | + set(Src1, RoutingKey, Dst1), |
| 120 | + set(Src1, RoutingKey, Dst2), |
| 121 | + set(Src2, RoutingKey, Dst1), |
| 122 | + ?assertEqual(Dsts, lists:sort(rabbit_db_topic_exchange:match(Src1, RoutingKey))), |
| 123 | + ?assertEqual([Dst1], rabbit_db_topic_exchange:match(Src2, RoutingKey)), |
| 124 | + ?assertEqual(ok, rabbit_db_topic_exchange:delete_all_for_exchange(Src1)), |
| 125 | + ?assertEqual([], rabbit_db_topic_exchange:match(Src1, RoutingKey)), |
| 126 | + ?assertEqual([Dst1], rabbit_db_topic_exchange:match(Src2, RoutingKey)), |
| 127 | + passed. |
| 128 | + |
| 129 | +match(Config) -> |
| 130 | + passed = rabbit_ct_broker_helpers:rpc(Config, 0, ?MODULE, match1, [Config]). |
| 131 | + |
| 132 | +match1(_Config) -> |
| 133 | + Src = rabbit_misc:r(?VHOST, exchange, <<"test-exchange">>), |
| 134 | + Dst1 = rabbit_misc:r(?VHOST, queue, <<"test-queue1">>), |
| 135 | + Dst2 = rabbit_misc:r(?VHOST, queue, <<"test-queue2">>), |
| 136 | + Dst3 = rabbit_misc:r(?VHOST, queue, <<"test-queue3">>), |
| 137 | + Dst4 = rabbit_misc:r(?VHOST, queue, <<"test-queue4">>), |
| 138 | + Dst5 = rabbit_misc:r(?VHOST, queue, <<"test-queue5">>), |
| 139 | + Dst6 = rabbit_misc:r(?VHOST, queue, <<"test-queue6">>), |
| 140 | + set(Src, <<"a.b.c">>, Dst1), |
| 141 | + set(Src, <<"a.*.c">>, Dst2), |
| 142 | + set(Src, <<"*.#">>, Dst3), |
| 143 | + set(Src, <<"#">>, Dst4), |
| 144 | + set(Src, <<"#.#">>, Dst5), |
| 145 | + set(Src, <<"a.*">>, Dst6), |
| 146 | + Dsts1 = lists:sort([Dst1, Dst2, Dst3, Dst4, Dst5]), |
| 147 | + ?assertEqual(Dsts1, lists:usort(rabbit_db_topic_exchange:match(Src, <<"a.b.c">>))), |
| 148 | + Dsts2 = lists:sort([Dst3, Dst4, Dst5, Dst6]), |
| 149 | + ?assertEqual(Dsts2, lists:usort(rabbit_db_topic_exchange:match(Src, <<"a.b">>))), |
| 150 | + Dsts3 = lists:sort([Dst4, Dst5]), |
| 151 | + ?assertEqual(Dsts3, lists:usort(rabbit_db_topic_exchange:match(Src, <<"">>))), |
| 152 | + Dsts4 = lists:sort([Dst3, Dst4, Dst5]), |
| 153 | + ?assertEqual(Dsts4, lists:usort(rabbit_db_topic_exchange:match(Src, <<"zen.rabbit">>))), |
| 154 | + passed. |
| 155 | + |
| 156 | +set(Src, RoutingKey, Dst) -> |
| 157 | + Binding = #binding{source = Src, key = RoutingKey, destination = Dst, args = #{}}, |
| 158 | + ok = rabbit_db_topic_exchange:set(Binding). |
0 commit comments