|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "google/cloud/pubsublite/internal/default_routing_policy.h" |
| 16 | +#include <gmock/gmock.h> |
| 17 | +#include <unordered_map> |
| 18 | + |
| 19 | +namespace google { |
| 20 | +namespace cloud { |
| 21 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 22 | +namespace pubsublite_internal { |
| 23 | + |
| 24 | +using google::cloud::pubsublite_internal::DefaultRoutingPolicy; |
| 25 | + |
| 26 | +TEST(DefaultRoutingPolicyTest, RouteWithKey) { |
| 27 | + // same list of test values as in other client libraries |
| 28 | + DefaultRoutingPolicy rp; |
| 29 | + std::unordered_map<std::string, std::uint64_t> mods = { |
| 30 | + {"oaisdhfoiahsd", 18}, |
| 31 | + {"P(#*YNPOIUDF", 9}, |
| 32 | + {"LCIUNDFPOASIUN", 8}, |
| 33 | + {";odsfiupoius", 9}, |
| 34 | + {"OPISUDfpoiu", 2}, |
| 35 | + {"dokjwO:IDf", 21}, |
| 36 | + {"%^&*", 19}, |
| 37 | + {"XXXXXXXXX", 15}, |
| 38 | + {"dpcollins", 28}, |
| 39 | + {"#()&$IJHLOIURF", 2}, |
| 40 | + {"dfasiduyf", 6}, |
| 41 | + {"983u2poer", 3}, |
| 42 | + {"8888888", 6}, |
| 43 | + {"OPUIPOUYPOIOPUIOIPUOUIPJOP", 2}, |
| 44 | + {"x", 16}}; |
| 45 | + for (auto const& kv : mods) { |
| 46 | + EXPECT_EQ(rp.Route(kv.first, 29), kv.second); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +TEST(DefaultRoutingPolicyTest, RouteWithoutKey) { |
| 51 | + unsigned int num_partitions = 29; |
| 52 | + DefaultRoutingPolicy rp; |
| 53 | + uint64_t initial_partition = rp.Route(num_partitions); |
| 54 | + for (unsigned int i = 0; i < num_partitions; ++i) { |
| 55 | + uint64_t next_partition = rp.Route(num_partitions); |
| 56 | + EXPECT_EQ((initial_partition + 1) % num_partitions, |
| 57 | + next_partition % num_partitions); |
| 58 | + initial_partition = next_partition; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// expected values obtained from Python3 REPL |
| 63 | +TEST(TestGetMod, MaxValue) { |
| 64 | + std::array<std::uint8_t, 32> arr{255, 255, 255, 255, 255, 255, 255, 255, |
| 65 | + 255, 255, 255, 255, 255, 255, 255, 255, |
| 66 | + 255, 255, 255, 255, 255, 255, 255, 255, |
| 67 | + 255, 255, 255, 255, 255, 255, 255, 255}; |
| 68 | + EXPECT_EQ(GetMod(arr, 2), 1); |
| 69 | + EXPECT_EQ(GetMod(arr, 18), 15); |
| 70 | + EXPECT_EQ(GetMod(arr, 100), 35); |
| 71 | + EXPECT_EQ(GetMod(arr, 10023), 5397); |
| 72 | + EXPECT_EQ(GetMod(arr, UINT8_MAX), 0); |
| 73 | +} |
| 74 | + |
| 75 | +TEST(TestGetMod, OneLessThanMaxValue) { |
| 76 | + std::array<std::uint8_t, 32> arr{255, 255, 255, 255, 255, 255, 255, 255, |
| 77 | + 255, 255, 255, 255, 255, 255, 255, 255, |
| 78 | + 255, 255, 255, 255, 255, 255, 255, 255, |
| 79 | + 255, 255, 255, 255, 255, 255, 255, 254}; |
| 80 | + EXPECT_EQ(GetMod(arr, 2), 0); |
| 81 | + EXPECT_EQ(GetMod(arr, 18), 14); |
| 82 | + EXPECT_EQ(GetMod(arr, 100), 34); |
| 83 | + EXPECT_EQ(GetMod(arr, 10023), 5396); |
| 84 | + EXPECT_EQ(GetMod(arr, UINT8_MAX), 254); |
| 85 | +} |
| 86 | + |
| 87 | +TEST(TestGetMod, Zeros) { |
| 88 | + std::array<std::uint8_t, 32> arr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 89 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 90 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 91 | + EXPECT_EQ(GetMod(arr, 2), 0); |
| 92 | + EXPECT_EQ(GetMod(arr, 18), 0); |
| 93 | + EXPECT_EQ(GetMod(arr, 100), 0); |
| 94 | + EXPECT_EQ(GetMod(arr, 10023), 0); |
| 95 | + EXPECT_EQ(GetMod(arr, UINT8_MAX), 0); |
| 96 | +} |
| 97 | + |
| 98 | +TEST(TestGetMod, ArbitraryValue) { |
| 99 | + std::array<std::uint8_t, 32> arr{255, 255, 255, 255, 255, 255, 2, 255, |
| 100 | + 5, 79, 255, 255, 255, 255, 80, 255, |
| 101 | + 255, 255, 8, 255, 255, 4, 255, 255, |
| 102 | + 78, 255, 255, 100, 255, 255, 255, 254}; |
| 103 | + EXPECT_EQ(GetMod(arr, 10), 0); |
| 104 | + EXPECT_EQ(GetMod(arr, 109), 4); |
| 105 | + EXPECT_EQ(GetMod(arr, 10023), 3346); |
| 106 | + EXPECT_EQ(GetMod(arr, 109000), 60390); |
| 107 | + EXPECT_EQ(GetMod(arr, UINT8_MAX), 100); |
| 108 | +} |
| 109 | + |
| 110 | +TEST(TestGetMod, ArbitraryValue1) { |
| 111 | + std::array<std::uint8_t, 32> arr{0, 48, 0, 0, 60, 0, 0, 56, 0, 99, 0, |
| 112 | + 0, 0, 0, 0, 90, 231, 0, 89, 0, 27, 80, |
| 113 | + 0, 0, 0, 254, 0, 0, 0, 0, 23, 0}; |
| 114 | + EXPECT_EQ(GetMod(arr, 109001), 68945); |
| 115 | + EXPECT_EQ(GetMod(arr, 102301), 93535); |
| 116 | + EXPECT_EQ(GetMod(arr, 23), 13); |
| 117 | + EXPECT_EQ(GetMod(arr, UINT8_MAX), 37); |
| 118 | +} |
| 119 | + |
| 120 | +} // namespace pubsublite_internal |
| 121 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 122 | +} // namespace cloud |
| 123 | +} // namespace google |
0 commit comments