Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 1 | // Copyright 2021 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // 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, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
| 15 | #include "pw_multisink/multisink.h" |
| 16 | |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 17 | #include <array> |
| 18 | #include <cstdint> |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 19 | #include <cstring> |
Prashanth Swaminathan | 3eb97d4 | 2021-07-13 10:14:38 -0700 | [diff] [blame] | 20 | #include <optional> |
Ewout van Bekkum | 6937a09 | 2022-03-04 08:13:37 -0800 | [diff] [blame] | 21 | #include <span> |
Armando Montanez | fb7b478 | 2021-09-30 21:38:20 -0700 | [diff] [blame] | 22 | #include <string_view> |
Prashanth Swaminathan | 3eb97d4 | 2021-07-13 10:14:38 -0700 | [diff] [blame] | 23 | |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 24 | #include "gtest/gtest.h" |
Armando Montanez | fb7b478 | 2021-09-30 21:38:20 -0700 | [diff] [blame] | 25 | #include "pw_function/function.h" |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 26 | #include "pw_status/status.h" |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 27 | |
| 28 | namespace pw::multisink { |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 29 | using Drain = MultiSink::Drain; |
| 30 | using Listener = MultiSink::Listener; |
| 31 | |
| 32 | class CountingListener : public Listener { |
| 33 | public: |
| 34 | void OnNewEntryAvailable() override { notification_count_++; } |
| 35 | |
| 36 | size_t GetNotificationCount() { return notification_count_; } |
| 37 | |
| 38 | void ResetNotificationCount() { notification_count_ = 0; } |
| 39 | |
| 40 | private: |
| 41 | size_t notification_count_ = 0; |
| 42 | }; |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 43 | |
| 44 | class MultiSinkTest : public ::testing::Test { |
| 45 | protected: |
| 46 | static constexpr std::byte kMessage[] = { |
| 47 | (std::byte)0xDE, (std::byte)0xAD, (std::byte)0xBE, (std::byte)0xEF}; |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 48 | static constexpr std::byte kMessageOther[] = { |
| 49 | (std::byte)0x12, (std::byte)0x34, (std::byte)0x56, (std::byte)0x78}; |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 50 | static constexpr size_t kMaxDrains = 3; |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 51 | static constexpr size_t kMaxListeners = 3; |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 52 | static constexpr size_t kEntryBufferSize = 1024; |
| 53 | static constexpr size_t kBufferSize = 5 * kEntryBufferSize; |
| 54 | |
| 55 | MultiSinkTest() : multisink_(buffer_) {} |
| 56 | |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 57 | // Expects the peeked or popped message to equal the provided non-empty |
| 58 | // message, and the drop count to match. If `expected_message` is empty, the |
| 59 | // Pop call status expected is OUT_OF_RANGE. |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 60 | void ExpectMessageAndDropCounts(Result<ConstByteSpan>& result, |
| 61 | uint32_t result_drop_count, |
| 62 | uint32_t result_ingress_drop_count, |
| 63 | std::optional<ConstByteSpan> expected_message, |
| 64 | uint32_t expected_drop_count, |
| 65 | uint32_t expected_ingress_drop_count) { |
Prashanth Swaminathan | 3eb97d4 | 2021-07-13 10:14:38 -0700 | [diff] [blame] | 66 | if (!expected_message.has_value()) { |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 67 | EXPECT_EQ(Status::OutOfRange(), result.status()); |
| 68 | } else { |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 69 | ASSERT_EQ(result.status(), OkStatus()); |
Prashanth Swaminathan | 3eb97d4 | 2021-07-13 10:14:38 -0700 | [diff] [blame] | 70 | if (!expected_message.value().empty()) { |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 71 | ASSERT_FALSE(result.value().empty()); |
| 72 | ASSERT_EQ(result.value().size_bytes(), |
| 73 | expected_message.value().size_bytes()); |
Prashanth Swaminathan | 3eb97d4 | 2021-07-13 10:14:38 -0700 | [diff] [blame] | 74 | EXPECT_EQ(memcmp(result.value().data(), |
| 75 | expected_message.value().data(), |
| 76 | expected_message.value().size_bytes()), |
| 77 | 0); |
| 78 | } |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 79 | } |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 80 | EXPECT_EQ(result_drop_count, expected_drop_count); |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 81 | EXPECT_EQ(result_ingress_drop_count, expected_ingress_drop_count); |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void VerifyPopEntry(Drain& drain, |
| 85 | std::optional<ConstByteSpan> expected_message, |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 86 | uint32_t expected_drop_count, |
| 87 | uint32_t expected_ingress_drop_count) { |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 88 | uint32_t drop_count = 0; |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 89 | uint32_t ingress_drop_count = 0; |
| 90 | Result<ConstByteSpan> result = |
| 91 | drain.PopEntry(entry_buffer_, drop_count, ingress_drop_count); |
| 92 | ExpectMessageAndDropCounts(result, |
| 93 | drop_count, |
| 94 | ingress_drop_count, |
| 95 | expected_message, |
| 96 | expected_drop_count, |
| 97 | expected_ingress_drop_count); |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void VerifyPeekResult(const Result<Drain::PeekedEntry>& peek_result, |
| 101 | uint32_t result_drop_count, |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 102 | uint32_t result_ingress_drop_count, |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 103 | std::optional<ConstByteSpan> expected_message, |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 104 | uint32_t expected_drop_count, |
| 105 | uint32_t expected_ingress_drop_count) { |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 106 | if (peek_result.ok()) { |
| 107 | ASSERT_FALSE(peek_result.value().entry().empty()); |
| 108 | Result<ConstByteSpan> verify_result(peek_result.value().entry()); |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 109 | ExpectMessageAndDropCounts(verify_result, |
| 110 | result_drop_count, |
| 111 | result_ingress_drop_count, |
| 112 | expected_message, |
| 113 | expected_drop_count, |
| 114 | expected_ingress_drop_count); |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | if (expected_message.has_value()) { |
| 118 | // Fail since we expected OkStatus. |
| 119 | ASSERT_EQ(peek_result.status(), OkStatus()); |
| 120 | } |
| 121 | EXPECT_EQ(Status::OutOfRange(), peek_result.status()); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 124 | void ExpectNotificationCount(CountingListener& listener, |
| 125 | size_t expected_notification_count) { |
| 126 | EXPECT_EQ(listener.GetNotificationCount(), expected_notification_count); |
| 127 | listener.ResetNotificationCount(); |
| 128 | } |
| 129 | |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 130 | std::byte buffer_[kBufferSize]; |
| 131 | std::byte entry_buffer_[kEntryBufferSize]; |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 132 | CountingListener listeners_[kMaxListeners]; |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 133 | Drain drains_[kMaxDrains]; |
| 134 | MultiSink multisink_; |
| 135 | }; |
| 136 | |
| 137 | TEST_F(MultiSinkTest, SingleDrain) { |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 138 | multisink_.AttachDrain(drains_[0]); |
| 139 | multisink_.AttachListener(listeners_[0]); |
Max Koopman | 696d686 | 2021-09-29 10:48:29 -0700 | [diff] [blame] | 140 | ExpectNotificationCount(listeners_[0], 1u); |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 141 | multisink_.HandleEntry(kMessage); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 142 | |
| 143 | // Single entry push and pop. |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 144 | ExpectNotificationCount(listeners_[0], 1u); |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 145 | VerifyPopEntry(drains_[0], kMessage, 0u, 0u); |
Prashanth Swaminathan | 3eb97d4 | 2021-07-13 10:14:38 -0700 | [diff] [blame] | 146 | // Single empty entry push and pop. |
| 147 | multisink_.HandleEntry(ConstByteSpan()); |
| 148 | ExpectNotificationCount(listeners_[0], 1u); |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 149 | VerifyPopEntry(drains_[0], ConstByteSpan(), 0u, 0u); |
Prashanth Swaminathan | 3eb97d4 | 2021-07-13 10:14:38 -0700 | [diff] [blame] | 150 | |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 151 | // Multiple entries with intermittent drops. |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 152 | multisink_.HandleEntry(kMessage); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 153 | multisink_.HandleDropped(); |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 154 | multisink_.HandleEntry(kMessage); |
| 155 | ExpectNotificationCount(listeners_[0], 3u); |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 156 | VerifyPopEntry(drains_[0], kMessage, 0u, 0u); |
| 157 | VerifyPopEntry(drains_[0], kMessage, 0u, 1u); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 158 | |
| 159 | // Send drops only. |
| 160 | multisink_.HandleDropped(); |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 161 | ExpectNotificationCount(listeners_[0], 1u); |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 162 | VerifyPopEntry(drains_[0], std::nullopt, 0u, 1u); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 163 | |
| 164 | // Confirm out-of-range if no entries are expected. |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 165 | ExpectNotificationCount(listeners_[0], 0u); |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 166 | VerifyPopEntry(drains_[0], std::nullopt, 0u, 0u); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | TEST_F(MultiSinkTest, MultipleDrain) { |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 170 | multisink_.AttachDrain(drains_[0]); |
| 171 | multisink_.AttachDrain(drains_[1]); |
| 172 | multisink_.AttachListener(listeners_[0]); |
| 173 | multisink_.AttachListener(listeners_[1]); |
Max Koopman | 696d686 | 2021-09-29 10:48:29 -0700 | [diff] [blame] | 174 | ExpectNotificationCount(listeners_[0], 1u); |
| 175 | ExpectNotificationCount(listeners_[1], 1u); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 176 | |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 177 | multisink_.HandleEntry(kMessage); |
| 178 | multisink_.HandleEntry(kMessage); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 179 | multisink_.HandleDropped(); |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 180 | multisink_.HandleEntry(kMessage); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 181 | multisink_.HandleDropped(); |
| 182 | |
| 183 | // Drain one drain entirely. |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 184 | ExpectNotificationCount(listeners_[0], 5u); |
| 185 | ExpectNotificationCount(listeners_[1], 5u); |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 186 | VerifyPopEntry(drains_[0], kMessage, 0u, 0u); |
| 187 | VerifyPopEntry(drains_[0], kMessage, 0u, 0u); |
| 188 | VerifyPopEntry(drains_[0], kMessage, 0u, 1u); |
| 189 | VerifyPopEntry(drains_[0], std::nullopt, 0u, 1u); |
| 190 | VerifyPopEntry(drains_[0], std::nullopt, 0u, 0u); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 191 | |
| 192 | // Confirm the other drain can be drained separately. |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 193 | ExpectNotificationCount(listeners_[0], 0u); |
| 194 | ExpectNotificationCount(listeners_[1], 0u); |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 195 | VerifyPopEntry(drains_[1], kMessage, 0u, 0u); |
| 196 | VerifyPopEntry(drains_[1], kMessage, 0u, 0u); |
| 197 | VerifyPopEntry(drains_[1], kMessage, 0u, 1u); |
| 198 | VerifyPopEntry(drains_[1], std::nullopt, 0u, 1u); |
| 199 | VerifyPopEntry(drains_[1], std::nullopt, 0u, 0u); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 202 | TEST_F(MultiSinkTest, LateDrainRegistration) { |
Prashanth Swaminathan | 099f716 | 2021-07-15 13:42:20 -0700 | [diff] [blame] | 203 | // Drains attached after entries are pushed should still observe those entries |
| 204 | // if they have not been evicted from the ring buffer. |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 205 | multisink_.HandleEntry(kMessage); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 206 | |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 207 | multisink_.AttachDrain(drains_[0]); |
| 208 | multisink_.AttachListener(listeners_[0]); |
Max Koopman | 696d686 | 2021-09-29 10:48:29 -0700 | [diff] [blame] | 209 | ExpectNotificationCount(listeners_[0], 1u); |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 210 | VerifyPopEntry(drains_[0], kMessage, 0u, 0u); |
| 211 | VerifyPopEntry(drains_[0], std::nullopt, 0u, 0u); |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 212 | |
| 213 | multisink_.HandleEntry(kMessage); |
| 214 | ExpectNotificationCount(listeners_[0], 1u); |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 215 | VerifyPopEntry(drains_[0], kMessage, 0u, 0u); |
| 216 | VerifyPopEntry(drains_[0], std::nullopt, 0u, 0u); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | TEST_F(MultiSinkTest, DynamicDrainRegistration) { |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 220 | multisink_.AttachDrain(drains_[0]); |
| 221 | multisink_.AttachListener(listeners_[0]); |
Max Koopman | 696d686 | 2021-09-29 10:48:29 -0700 | [diff] [blame] | 222 | ExpectNotificationCount(listeners_[0], 1u); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 223 | |
| 224 | multisink_.HandleDropped(); |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 225 | multisink_.HandleEntry(kMessage); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 226 | multisink_.HandleDropped(); |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 227 | multisink_.HandleEntry(kMessage); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 228 | |
| 229 | // Drain out one message and detach it. |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 230 | ExpectNotificationCount(listeners_[0], 4u); |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 231 | VerifyPopEntry(drains_[0], kMessage, 0u, 1u); |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 232 | multisink_.DetachDrain(drains_[0]); |
| 233 | multisink_.DetachListener(listeners_[0]); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 234 | |
Prashanth Swaminathan | 099f716 | 2021-07-15 13:42:20 -0700 | [diff] [blame] | 235 | // Re-attaching the drain should reproduce the last observed message. Note |
| 236 | // that notifications are not expected, nor are drops observed before the |
| 237 | // first valid message in the buffer. |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 238 | multisink_.AttachDrain(drains_[0]); |
| 239 | multisink_.AttachListener(listeners_[0]); |
Max Koopman | 696d686 | 2021-09-29 10:48:29 -0700 | [diff] [blame] | 240 | ExpectNotificationCount(listeners_[0], 1u); |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 241 | VerifyPopEntry(drains_[0], kMessage, 0u, 1u); |
| 242 | VerifyPopEntry(drains_[0], kMessage, 0u, 1u); |
| 243 | VerifyPopEntry(drains_[0], std::nullopt, 0u, 0u); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 244 | |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 245 | multisink_.HandleEntry(kMessage); |
| 246 | ExpectNotificationCount(listeners_[0], 1u); |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 247 | VerifyPopEntry(drains_[0], kMessage, 0u, 0u); |
| 248 | VerifyPopEntry(drains_[0], std::nullopt, 0u, 0u); |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 251 | TEST_F(MultiSinkTest, TooSmallBuffer) { |
| 252 | multisink_.AttachDrain(drains_[0]); |
| 253 | |
| 254 | // Insert an entry and a drop, then try to read into an insufficient buffer. |
| 255 | uint32_t drop_count = 0; |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 256 | uint32_t ingress_drop_count = 0; |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 257 | multisink_.HandleDropped(); |
| 258 | multisink_.HandleEntry(kMessage); |
| 259 | |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 260 | // Attempting to acquire an entry with a small buffer should result in |
| 261 | // RESOURCE_EXHAUSTED and remove it. |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 262 | Result<ConstByteSpan> result = drains_[0].PopEntry( |
| 263 | std::span(entry_buffer_, 1), drop_count, ingress_drop_count); |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 264 | EXPECT_EQ(result.status(), Status::ResourceExhausted()); |
| 265 | |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 266 | VerifyPopEntry(drains_[0], std::nullopt, 1u, 1u); |
Prashanth Swaminathan | 3b1536d | 2021-05-18 14:21:30 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Prashanth Swaminathan | 16541e7 | 2021-06-21 09:22:25 -0700 | [diff] [blame] | 269 | TEST_F(MultiSinkTest, Iterator) { |
| 270 | multisink_.AttachDrain(drains_[0]); |
| 271 | |
| 272 | // Insert entries and consume them all. |
| 273 | multisink_.HandleEntry(kMessage); |
| 274 | multisink_.HandleEntry(kMessage); |
| 275 | multisink_.HandleEntry(kMessage); |
| 276 | |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 277 | VerifyPopEntry(drains_[0], kMessage, 0u, 0u); |
| 278 | VerifyPopEntry(drains_[0], kMessage, 0u, 0u); |
| 279 | VerifyPopEntry(drains_[0], kMessage, 0u, 0u); |
Prashanth Swaminathan | 16541e7 | 2021-06-21 09:22:25 -0700 | [diff] [blame] | 280 | |
| 281 | // Confirm that the iterator still observes the messages in the ring buffer. |
| 282 | size_t iterated_entries = 0; |
| 283 | for (ConstByteSpan entry : multisink_.UnsafeIteration()) { |
| 284 | EXPECT_EQ(memcmp(entry.data(), kMessage, sizeof(kMessage)), 0); |
| 285 | iterated_entries++; |
| 286 | } |
| 287 | EXPECT_EQ(iterated_entries, 3u); |
| 288 | } |
| 289 | |
| 290 | TEST_F(MultiSinkTest, IteratorNoDrains) { |
| 291 | // Insert entries with no drains attached. Even though there are no consumers, |
| 292 | // iterators should still walk from the oldest entry. |
| 293 | multisink_.HandleEntry(kMessage); |
| 294 | multisink_.HandleEntry(kMessage); |
| 295 | multisink_.HandleEntry(kMessage); |
| 296 | |
| 297 | // Confirm that the iterator still observes the messages in the ring buffer. |
| 298 | size_t iterated_entries = 0; |
| 299 | for (ConstByteSpan entry : multisink_.UnsafeIteration()) { |
| 300 | EXPECT_EQ(memcmp(entry.data(), kMessage, sizeof(kMessage)), 0); |
| 301 | iterated_entries++; |
| 302 | } |
| 303 | EXPECT_EQ(iterated_entries, 3u); |
| 304 | } |
| 305 | |
| 306 | TEST_F(MultiSinkTest, IteratorNoEntries) { |
| 307 | // Attach a drain, but don't add any entries. |
| 308 | multisink_.AttachDrain(drains_[0]); |
| 309 | // Confirm that the iterator has no entries. |
| 310 | MultiSink::UnsafeIterationWrapper unsafe_iterator = |
| 311 | multisink_.UnsafeIteration(); |
| 312 | EXPECT_EQ(unsafe_iterator.begin(), unsafe_iterator.end()); |
| 313 | } |
| 314 | |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 315 | TEST_F(MultiSinkTest, PeekEntryNoEntries) { |
| 316 | multisink_.AttachDrain(drains_[0]); |
| 317 | |
| 318 | // Peek empty multisink. |
| 319 | uint32_t drop_count = 0; |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 320 | uint32_t ingress_drop_count = 0; |
| 321 | auto peek_result = |
| 322 | drains_[0].PeekEntry(entry_buffer_, drop_count, ingress_drop_count); |
| 323 | VerifyPeekResult(peek_result, 0, drop_count, std::nullopt, 0, 0); |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | TEST_F(MultiSinkTest, PeekAndPop) { |
| 327 | multisink_.AttachDrain(drains_[0]); |
| 328 | multisink_.AttachDrain(drains_[1]); |
| 329 | |
| 330 | // Peek entry after multisink has some entries. |
| 331 | multisink_.HandleEntry(kMessage); |
| 332 | multisink_.HandleEntry(kMessageOther); |
| 333 | uint32_t drop_count = 0; |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 334 | uint32_t ingress_drop_count = 0; |
| 335 | auto first_peek_result = |
| 336 | drains_[0].PeekEntry(entry_buffer_, drop_count, ingress_drop_count); |
| 337 | VerifyPeekResult( |
| 338 | first_peek_result, drop_count, ingress_drop_count, kMessage, 0, 0); |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 339 | |
| 340 | // Multiple peeks must return the front message. |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 341 | auto peek_duplicate = |
| 342 | drains_[0].PeekEntry(entry_buffer_, drop_count, ingress_drop_count); |
| 343 | VerifyPeekResult( |
| 344 | peek_duplicate, drop_count, ingress_drop_count, kMessage, 0, 0); |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 345 | // A second drain must peek the front message. |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 346 | auto peek_other_drain = |
| 347 | drains_[1].PeekEntry(entry_buffer_, drop_count, ingress_drop_count); |
| 348 | VerifyPeekResult( |
| 349 | peek_other_drain, drop_count, ingress_drop_count, kMessage, 0, 0); |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 350 | |
| 351 | // After a drain pops a peeked entry, the next peek call must return the next |
| 352 | // message. |
| 353 | ASSERT_EQ(drains_[0].PopEntry(first_peek_result.value()), OkStatus()); |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 354 | auto second_peek_result = |
| 355 | drains_[0].PeekEntry(entry_buffer_, drop_count, ingress_drop_count); |
| 356 | VerifyPeekResult( |
| 357 | second_peek_result, drop_count, ingress_drop_count, kMessageOther, 0, 0); |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 358 | // Slower readers must be unchanged. |
| 359 | auto peek_other_drain_duplicate = |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 360 | drains_[1].PeekEntry(entry_buffer_, drop_count, ingress_drop_count); |
| 361 | VerifyPeekResult(peek_other_drain_duplicate, |
| 362 | drop_count, |
| 363 | ingress_drop_count, |
| 364 | kMessage, |
| 365 | 0, |
| 366 | 0); |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 367 | |
| 368 | // PopEntry prior to popping the previously peeked entry. |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 369 | VerifyPopEntry(drains_[0], kMessageOther, 0, 0); |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 370 | // Popping an entry already handled must not trigger errors. |
| 371 | ASSERT_EQ(drains_[0].PopEntry(second_peek_result.value()), OkStatus()); |
| 372 | // Popping with an old peek context must not trigger errors. |
| 373 | ASSERT_EQ(drains_[0].PopEntry(first_peek_result.value()), OkStatus()); |
| 374 | |
| 375 | // Multisink is empty, pops and peeks should trigger OUT_OF_RANGE. |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 376 | VerifyPopEntry(drains_[0], std::nullopt, 0, 0); |
| 377 | auto empty_peek_result = |
| 378 | drains_[0].PeekEntry(entry_buffer_, drop_count, ingress_drop_count); |
| 379 | VerifyPeekResult( |
| 380 | empty_peek_result, drop_count, ingress_drop_count, std::nullopt, 0, 0); |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 381 | |
| 382 | // // Slower readers must be unchanged. |
| 383 | auto peek_other_drain_unchanged = |
Carlos Chinchilla | d226342 | 2022-03-15 17:47:43 -0700 | [diff] [blame] | 384 | drains_[1].PeekEntry(entry_buffer_, drop_count, ingress_drop_count); |
| 385 | VerifyPeekResult(peek_other_drain_unchanged, |
| 386 | drop_count, |
| 387 | ingress_drop_count, |
| 388 | kMessage, |
| 389 | 0, |
| 390 | 0); |
| 391 | } |
| 392 | |
| 393 | TEST_F(MultiSinkTest, PeekReportsIngressDropCount) { |
| 394 | multisink_.AttachDrain(drains_[0]); |
| 395 | |
| 396 | // Peek entry after multisink has some entries. |
| 397 | multisink_.HandleEntry(kMessage); |
| 398 | const uint32_t ingress_drops = 10; |
| 399 | multisink_.HandleDropped(ingress_drops); |
| 400 | |
| 401 | uint32_t drop_count = 0; |
| 402 | uint32_t ingress_drop_count = 0; |
| 403 | auto peek_result1 = |
| 404 | drains_[0].PeekEntry(entry_buffer_, drop_count, ingress_drop_count); |
| 405 | // No drops reported until the drain finds a gap in the sequence IDs. |
| 406 | VerifyPeekResult( |
| 407 | peek_result1, drop_count, ingress_drop_count, kMessage, 0, 0); |
| 408 | |
| 409 | // Popping the peeked entry advances the drain, and a new peek will find the |
| 410 | // gap in sequence IDs. |
| 411 | ASSERT_EQ(drains_[0].PopEntry(peek_result1.value()), OkStatus()); |
| 412 | auto peek_result2 = |
| 413 | drains_[0].PeekEntry(entry_buffer_, drop_count, ingress_drop_count); |
| 414 | ASSERT_EQ(peek_result2.status(), Status::OutOfRange()); |
| 415 | EXPECT_EQ(drop_count, 0u); |
| 416 | EXPECT_EQ(ingress_drop_count, ingress_drops); |
| 417 | } |
| 418 | |
| 419 | TEST_F(MultiSinkTest, PeekReportsSlowDrainDropCount) { |
| 420 | multisink_.AttachDrain(drains_[0]); |
| 421 | |
| 422 | // Add entries until buffer is full and drain has to be advanced. |
| 423 | // The sequence ID takes 1 byte when less than 128. |
| 424 | const size_t max_multisink_messages = 128; |
| 425 | const size_t buffer_entry_size = kBufferSize / max_multisink_messages; |
| 426 | // Account for 1 byte of preamble (sequnce ID) and 1 byte of data size. |
| 427 | const size_t message_size = buffer_entry_size - 2; |
| 428 | std::array<std::byte, message_size> message; |
| 429 | std::memset(message.data(), 'a', message.size()); |
| 430 | for (size_t i = 0; i < max_multisink_messages; ++i) { |
| 431 | multisink_.HandleEntry(message); |
| 432 | } |
| 433 | |
| 434 | // At this point the buffer is full, but the sequence ID will take 1 more byte |
| 435 | // in the preamble, meaning that adding N new entries, drops N + 1 entries. |
| 436 | // Account for that offset. |
| 437 | const size_t expected_drops = 5; |
| 438 | for (size_t i = 1; i < expected_drops; ++i) { |
| 439 | multisink_.HandleEntry(message); |
| 440 | } |
| 441 | |
| 442 | uint32_t drop_count = 0; |
| 443 | uint32_t ingress_drop_count = 0; |
| 444 | auto peek_result = |
| 445 | drains_[0].PeekEntry(entry_buffer_, drop_count, ingress_drop_count); |
| 446 | VerifyPeekResult( |
| 447 | peek_result, drop_count, ingress_drop_count, message, expected_drops, 0); |
| 448 | } |
| 449 | |
| 450 | TEST_F(MultiSinkTest, IngressDropCountOverflow) { |
| 451 | multisink_.AttachDrain(drains_[0]); |
| 452 | |
| 453 | // Make drain's last handled drop larger than multisink drop count, which |
| 454 | // overflowed. |
| 455 | const uint32_t drop_count_close_to_overflow = |
| 456 | std::numeric_limits<uint32_t>::max() - 3; |
| 457 | multisink_.HandleDropped(drop_count_close_to_overflow); |
| 458 | multisink_.HandleEntry(kMessage); |
| 459 | |
| 460 | // Catch up drain's drop count. |
| 461 | uint32_t drop_count = 0; |
| 462 | uint32_t ingress_drop_count = 0; |
| 463 | auto peek_result1 = |
| 464 | drains_[0].PeekEntry(entry_buffer_, drop_count, ingress_drop_count); |
| 465 | VerifyPeekResult(peek_result1, |
| 466 | drop_count, |
| 467 | ingress_drop_count, |
| 468 | kMessage, |
| 469 | 0, |
| 470 | drop_count_close_to_overflow); |
| 471 | // Popping the peeked entry advances the drain, and a new peek will find the |
| 472 | // gap in sequence IDs. |
| 473 | ASSERT_EQ(drains_[0].PopEntry(peek_result1.value()), OkStatus()); |
| 474 | |
| 475 | // Overflow multisink's drop count. |
| 476 | const uint32_t expected_ingress_drop_count = 10; |
| 477 | multisink_.HandleDropped(expected_ingress_drop_count); |
| 478 | |
| 479 | auto peek_result2 = |
| 480 | drains_[0].PeekEntry(entry_buffer_, drop_count, ingress_drop_count); |
| 481 | ASSERT_EQ(peek_result2.status(), Status::OutOfRange()); |
| 482 | EXPECT_EQ(drop_count, 0u); |
| 483 | EXPECT_EQ(ingress_drop_count, expected_ingress_drop_count); |
| 484 | |
| 485 | multisink_.HandleEntry(kMessage); |
| 486 | auto peek_result3 = |
| 487 | drains_[0].PeekEntry(entry_buffer_, drop_count, ingress_drop_count); |
| 488 | VerifyPeekResult( |
| 489 | peek_result3, drop_count, ingress_drop_count, kMessage, 0, 0); |
| 490 | } |
| 491 | |
| 492 | TEST_F(MultiSinkTest, DetachedDrainReportsDropCount) { |
| 493 | multisink_.AttachDrain(drains_[0]); |
| 494 | |
| 495 | const uint32_t ingress_drops = 10; |
| 496 | multisink_.HandleDropped(ingress_drops); |
| 497 | multisink_.HandleEntry(kMessage); |
| 498 | VerifyPopEntry(drains_[0], kMessage, 0, ingress_drops); |
| 499 | |
| 500 | // Detaching and attaching drain should report the same drops. |
| 501 | multisink_.DetachDrain(drains_[0]); |
| 502 | multisink_.AttachDrain(drains_[0]); |
| 503 | VerifyPopEntry(drains_[0], kMessage, 0, ingress_drops); |
Carlos Chinchilla | 64021a8 | 2021-08-06 00:18:41 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Armando Montanez | fb7b478 | 2021-09-30 21:38:20 -0700 | [diff] [blame] | 506 | TEST(UnsafeIteration, NoLimit) { |
| 507 | constexpr std::array<std::string_view, 5> kExpectedEntries{ |
| 508 | "one", "two", "three", "four", "five"}; |
| 509 | std::array<std::byte, 32> buffer; |
| 510 | MultiSink multisink(buffer); |
| 511 | |
| 512 | for (std::string_view entry : kExpectedEntries) { |
| 513 | multisink.HandleEntry(std::as_bytes(std::span(entry))); |
| 514 | } |
| 515 | |
| 516 | size_t entry_count = 0; |
| 517 | struct { |
| 518 | size_t& entry_count; |
| 519 | std::span<const std::string_view> expected_results; |
| 520 | } ctx{entry_count, kExpectedEntries}; |
| 521 | auto cb = [&ctx](ConstByteSpan data) { |
| 522 | std::string_view expected_entry = ctx.expected_results[ctx.entry_count]; |
| 523 | EXPECT_EQ(data.size(), expected_entry.size()); |
| 524 | const int result = |
| 525 | memcmp(data.data(), expected_entry.data(), expected_entry.size()); |
| 526 | EXPECT_EQ(0, result); |
| 527 | ctx.entry_count++; |
| 528 | }; |
| 529 | |
| 530 | EXPECT_EQ(OkStatus(), multisink.UnsafeForEachEntry(cb)); |
| 531 | EXPECT_EQ(kExpectedEntries.size(), entry_count); |
| 532 | } |
| 533 | |
| 534 | TEST(UnsafeIteration, Subset) { |
| 535 | constexpr std::array<std::string_view, 5> kExpectedEntries{ |
| 536 | "one", "two", "three", "four", "five"}; |
| 537 | constexpr size_t kStartOffset = 3; |
| 538 | constexpr size_t kExpectedEntriesMaxEntries = |
| 539 | kExpectedEntries.size() - kStartOffset; |
| 540 | std::array<std::byte, 32> buffer; |
| 541 | MultiSink multisink(buffer); |
| 542 | |
| 543 | for (std::string_view entry : kExpectedEntries) { |
| 544 | multisink.HandleEntry(std::as_bytes(std::span(entry))); |
| 545 | } |
| 546 | |
| 547 | size_t entry_count = 0; |
| 548 | struct { |
| 549 | size_t& entry_count; |
| 550 | std::span<const std::string_view> expected_results; |
| 551 | } ctx{entry_count, kExpectedEntries}; |
| 552 | auto cb = [&ctx](ConstByteSpan data) { |
| 553 | std::string_view expected_entry = |
| 554 | ctx.expected_results[ctx.entry_count + kStartOffset]; |
| 555 | EXPECT_EQ(data.size(), expected_entry.size()); |
| 556 | const int result = |
| 557 | memcmp(data.data(), expected_entry.data(), expected_entry.size()); |
| 558 | EXPECT_EQ(0, result); |
| 559 | ctx.entry_count++; |
| 560 | }; |
| 561 | |
| 562 | EXPECT_EQ( |
| 563 | OkStatus(), |
| 564 | multisink.UnsafeForEachEntry(cb, kExpectedEntries.size() - kStartOffset)); |
| 565 | EXPECT_EQ(kExpectedEntriesMaxEntries, entry_count); |
| 566 | } |
| 567 | |
Prashanth Swaminathan | f36832a | 2021-01-27 16:20:32 -0800 | [diff] [blame] | 568 | } // namespace pw::multisink |