Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include "aos/events/event_loop_param_test.h" |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 2 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 3 | #include <chrono> |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 4 | #include <unordered_map> |
| 5 | #include <unordered_set> |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 6 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 7 | #include "glog/logging.h" |
| 8 | #include "gmock/gmock.h" |
| 9 | #include "gtest/gtest.h" |
| 10 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 11 | #include "aos/events/test_message_generated.h" |
James Kuszmaul | b1c1105 | 2023-11-06 13:20:53 -0800 | [diff] [blame] | 12 | #include "aos/events/test_message_static.h" |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 13 | #include "aos/flatbuffer_merge.h" |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 14 | #include "aos/logging/log_message_generated.h" |
| 15 | #include "aos/logging/logging.h" |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 16 | #include "aos/realtime.h" |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 17 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 18 | namespace aos::testing { |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 19 | namespace { |
| 20 | namespace chrono = ::std::chrono; |
| 21 | } // namespace |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 22 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 23 | ::std::unique_ptr<EventLoop> AbstractEventLoopTest::Make( |
| 24 | std::string_view name) { |
| 25 | std::string name_copy(name); |
| 26 | if (name == "") { |
| 27 | name_copy = "loop"; |
| 28 | name_copy += std::to_string(event_loop_count_); |
| 29 | } |
| 30 | ++event_loop_count_; |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 31 | auto result = factory_->Make(name_copy); |
| 32 | if (do_timing_reports() == DoTimingReports::kNo) { |
| 33 | result->SkipTimingReport(); |
| 34 | } |
| 35 | return result; |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void AbstractEventLoopTest::VerifyBuffers( |
| 39 | int number_buffers, |
| 40 | std::vector<std::reference_wrapper<const Fetcher<TestMessage>>> fetchers, |
| 41 | std::vector<std::reference_wrapper<const Sender<TestMessage>>> senders) { |
| 42 | // The buffers which are in a sender. |
| 43 | std::unordered_set<int> in_sender; |
| 44 | for (const Sender<TestMessage> &sender : senders) { |
| 45 | const int this_buffer = sender.buffer_index(); |
| 46 | CHECK_GE(this_buffer, 0); |
| 47 | CHECK_LT(this_buffer, number_buffers); |
| 48 | CHECK(in_sender.insert(this_buffer).second) << ": " << this_buffer; |
| 49 | } |
| 50 | |
| 51 | if (read_method() != ReadMethod::PIN) { |
| 52 | // If we're not using PIN, we can't really verify anything about what |
| 53 | // buffers the fetchers have. |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | // Mapping from TestMessage::value to buffer index. |
| 58 | std::unordered_map<int, int> fetcher_values; |
| 59 | for (const Fetcher<TestMessage> &fetcher : fetchers) { |
| 60 | if (!fetcher.get()) { |
| 61 | continue; |
| 62 | } |
| 63 | const int this_buffer = fetcher.context().buffer_index; |
| 64 | CHECK_GE(this_buffer, 0); |
| 65 | CHECK_LT(this_buffer, number_buffers); |
| 66 | CHECK(in_sender.count(this_buffer) == 0) << ": " << this_buffer; |
| 67 | const auto insert_result = fetcher_values.insert( |
| 68 | std::make_pair(fetcher.get()->value(), this_buffer)); |
| 69 | if (!insert_result.second) { |
| 70 | CHECK_EQ(this_buffer, insert_result.first->second); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 75 | // Tests that watcher can receive messages from a sender. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 76 | // Also tests that OnRun() works. |
| 77 | TEST_P(AbstractEventLoopTest, Basic) { |
| 78 | auto loop1 = Make(); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 79 | auto loop2 = MakePrimary(); |
| 80 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 81 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 82 | |
| 83 | bool happened = false; |
| 84 | |
| 85 | loop2->OnRun([&]() { |
| 86 | happened = true; |
| 87 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 88 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 89 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 90 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 91 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 92 | }); |
| 93 | |
| 94 | loop2->MakeWatcher("/test", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 95 | EXPECT_EQ(message.value(), 200); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 96 | this->Exit(); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 97 | }); |
| 98 | |
| 99 | EXPECT_FALSE(happened); |
| 100 | Run(); |
| 101 | EXPECT_TRUE(happened); |
| 102 | } |
| 103 | |
James Kuszmaul | b1c1105 | 2023-11-06 13:20:53 -0800 | [diff] [blame] | 104 | // Tests that watcher can receive messages from a static sender. |
| 105 | // This confirms that the "static" flatbuffer API works with the EventLoop |
| 106 | // senders. |
| 107 | TEST_P(AbstractEventLoopTest, BasicStatic) { |
| 108 | auto loop1 = Make(); |
| 109 | auto loop2 = MakePrimary(); |
| 110 | |
| 111 | aos::Sender<TestMessageStatic> sender = |
| 112 | loop1->MakeSender<TestMessageStatic>("/test"); |
| 113 | |
| 114 | bool happened = false; |
| 115 | |
| 116 | loop2->OnRun([&]() { |
| 117 | happened = true; |
| 118 | |
| 119 | aos::Sender<TestMessageStatic>::StaticBuilder msg = |
| 120 | sender.MakeStaticBuilder(); |
James Kuszmaul | dde6563 | 2023-12-07 16:12:26 -0800 | [diff] [blame] | 121 | msg->set_value(200); |
James Kuszmaul | b1c1105 | 2023-11-06 13:20:53 -0800 | [diff] [blame] | 122 | CHECK(msg.builder()->Verify()); |
| 123 | msg.CheckOk(msg.Send()); |
| 124 | }); |
| 125 | |
| 126 | loop2->MakeWatcher("/test", [&](const TestMessage &message) { |
| 127 | EXPECT_EQ(message.value(), 200); |
| 128 | this->Exit(); |
| 129 | }); |
| 130 | |
| 131 | EXPECT_FALSE(happened); |
| 132 | Run(); |
| 133 | EXPECT_TRUE(happened); |
| 134 | } |
| 135 | |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 136 | // Tests that watcher can receive messages from a sender, sent via SendDetached. |
| 137 | TEST_P(AbstractEventLoopTest, BasicSendDetached) { |
| 138 | auto loop1 = Make(); |
| 139 | auto loop2 = MakePrimary(); |
| 140 | |
| 141 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
| 142 | |
| 143 | FlatbufferDetachedBuffer<TestMessage> detached = |
| 144 | flatbuffers::DetachedBuffer(); |
| 145 | { |
| 146 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 147 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 148 | builder.add_value(100); |
| 149 | detached = msg.Detach(builder.Finish()); |
| 150 | } |
| 151 | detached = flatbuffers::DetachedBuffer(); |
| 152 | { |
| 153 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 154 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 155 | builder.add_value(200); |
| 156 | detached = msg.Detach(builder.Finish()); |
| 157 | } |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 158 | sender.CheckOk(sender.SendDetached(std::move(detached))); |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 159 | |
| 160 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 161 | ASSERT_TRUE(fetcher.Fetch()); |
| 162 | EXPECT_EQ(fetcher->value(), 200); |
| 163 | } |
| 164 | |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 165 | // Verifies that a no-arg watcher will not have a data pointer. |
| 166 | TEST_P(AbstractEventLoopTest, NoArgNoData) { |
| 167 | auto loop1 = Make(); |
| 168 | auto loop2 = MakePrimary(); |
| 169 | |
| 170 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
| 171 | |
| 172 | bool happened = false; |
| 173 | |
| 174 | loop2->OnRun([&]() { |
| 175 | happened = true; |
| 176 | |
| 177 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 178 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 179 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 180 | }); |
| 181 | |
| 182 | loop2->MakeNoArgWatcher<TestMessage>("/test", [&]() { |
| 183 | EXPECT_GT(loop2->context().size, 0u); |
| 184 | EXPECT_EQ(nullptr, loop2->context().data); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 185 | EXPECT_EQ(-1, loop2->context().buffer_index); |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 186 | this->Exit(); |
| 187 | }); |
| 188 | |
| 189 | EXPECT_FALSE(happened); |
| 190 | Run(); |
| 191 | EXPECT_TRUE(happened); |
| 192 | } |
| 193 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 194 | // Tests that no-arg watcher can receive messages from a sender. |
| 195 | // Also tests that OnRun() works. |
| 196 | TEST_P(AbstractEventLoopTest, BasicNoArg) { |
| 197 | auto loop1 = Make(); |
| 198 | auto loop2 = MakePrimary(); |
| 199 | |
| 200 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
| 201 | |
| 202 | bool happened = false; |
| 203 | |
| 204 | loop2->OnRun([&]() { |
| 205 | happened = true; |
| 206 | |
| 207 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 208 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 209 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 210 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 211 | }); |
| 212 | |
| 213 | aos::Fetcher<TestMessage> fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 214 | loop2->MakeNoArgWatcher<TestMessage>("/test", [&]() { |
| 215 | ASSERT_TRUE(fetcher.Fetch()); |
| 216 | EXPECT_EQ(fetcher->value(), 200); |
| 217 | this->Exit(); |
| 218 | }); |
| 219 | |
| 220 | EXPECT_FALSE(happened); |
| 221 | Run(); |
| 222 | EXPECT_TRUE(happened); |
| 223 | } |
| 224 | |
| 225 | // Tests that a watcher can be created with an std::function. |
| 226 | TEST_P(AbstractEventLoopTest, BasicFunction) { |
| 227 | auto loop1 = Make(); |
| 228 | auto loop2 = MakePrimary(); |
| 229 | |
| 230 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
| 231 | |
| 232 | bool happened = false; |
| 233 | |
| 234 | loop2->OnRun([&]() { |
| 235 | happened = true; |
| 236 | |
| 237 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 238 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 239 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 240 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 241 | }); |
| 242 | |
| 243 | loop2->MakeWatcher("/test", std::function<void(const TestMessage &)>( |
| 244 | [&](const TestMessage &message) { |
| 245 | EXPECT_EQ(message.value(), 200); |
| 246 | this->Exit(); |
| 247 | })); |
| 248 | |
| 249 | EXPECT_FALSE(happened); |
| 250 | Run(); |
| 251 | EXPECT_TRUE(happened); |
| 252 | } |
| 253 | |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 254 | // Tests that watcher can receive messages from two senders. |
| 255 | // Also tests that OnRun() works. |
| 256 | TEST_P(AbstractEventLoopTest, BasicTwoSenders) { |
| 257 | auto loop1 = Make(); |
| 258 | auto loop2 = MakePrimary(); |
| 259 | |
| 260 | aos::Sender<TestMessage> sender1 = loop1->MakeSender<TestMessage>("/test"); |
| 261 | aos::Sender<TestMessage> sender2 = loop1->MakeSender<TestMessage>("/test"); |
| 262 | |
| 263 | bool happened = false; |
| 264 | |
| 265 | loop2->OnRun([&]() { |
| 266 | happened = true; |
| 267 | |
| 268 | { |
| 269 | aos::Sender<TestMessage>::Builder msg = sender1.MakeBuilder(); |
| 270 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 271 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 272 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 273 | } |
| 274 | { |
| 275 | aos::Sender<TestMessage>::Builder msg = sender2.MakeBuilder(); |
| 276 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 277 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 278 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 279 | } |
| 280 | }); |
| 281 | |
| 282 | int messages_received = 0; |
| 283 | loop2->MakeWatcher("/test", [&](const TestMessage &message) { |
| 284 | EXPECT_EQ(message.value(), 200); |
| 285 | this->Exit(); |
| 286 | ++messages_received; |
| 287 | }); |
| 288 | |
| 289 | EXPECT_FALSE(happened); |
| 290 | Run(); |
| 291 | EXPECT_TRUE(happened); |
| 292 | EXPECT_EQ(messages_received, 2); |
| 293 | } |
| 294 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 295 | // Tests that a fetcher can fetch from a sender. |
| 296 | // Also tests that OnRun() works. |
| 297 | TEST_P(AbstractEventLoopTest, FetchWithoutRun) { |
| 298 | auto loop1 = Make(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 299 | auto loop2 = Make(); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 300 | auto loop3 = MakePrimary(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 301 | |
| 302 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 303 | |
| 304 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 305 | |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 306 | EXPECT_FALSE(fetcher.Fetch()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 307 | EXPECT_EQ(fetcher.get(), nullptr); |
| 308 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 309 | EXPECT_EQ(fetcher.context().monotonic_event_time, monotonic_clock::min_time); |
| 310 | EXPECT_EQ(fetcher.context().monotonic_remote_time, monotonic_clock::min_time); |
| 311 | EXPECT_EQ(fetcher.context().realtime_event_time, realtime_clock::min_time); |
| 312 | EXPECT_EQ(fetcher.context().realtime_remote_time, realtime_clock::min_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 313 | EXPECT_EQ(fetcher.context().source_boot_uuid, UUID::Zero()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 314 | EXPECT_EQ(fetcher.context().queue_index, 0xffffffffu); |
| 315 | EXPECT_EQ(fetcher.context().size, 0u); |
| 316 | EXPECT_EQ(fetcher.context().data, nullptr); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 317 | EXPECT_EQ(fetcher.context().buffer_index, -1); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 318 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 319 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 320 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 321 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 322 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 323 | |
| 324 | EXPECT_TRUE(fetcher.Fetch()); |
| 325 | ASSERT_FALSE(fetcher.get() == nullptr); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 326 | EXPECT_EQ(fetcher.get()->value(), 200); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 327 | |
| 328 | const chrono::milliseconds kEpsilon(100); |
| 329 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 330 | const aos::monotonic_clock::time_point monotonic_now = loop2->monotonic_now(); |
| 331 | const aos::realtime_clock::time_point realtime_now = loop2->realtime_now(); |
| 332 | EXPECT_EQ(fetcher.context().monotonic_event_time, |
| 333 | fetcher.context().monotonic_remote_time); |
| 334 | EXPECT_EQ(fetcher.context().realtime_event_time, |
| 335 | fetcher.context().realtime_remote_time); |
| 336 | |
| 337 | EXPECT_GE(fetcher.context().monotonic_event_time, monotonic_now - kEpsilon); |
| 338 | EXPECT_LE(fetcher.context().monotonic_event_time, monotonic_now + kEpsilon); |
| 339 | EXPECT_GE(fetcher.context().realtime_event_time, realtime_now - kEpsilon); |
| 340 | EXPECT_LE(fetcher.context().realtime_event_time, realtime_now + kEpsilon); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 341 | EXPECT_EQ(fetcher.context().source_boot_uuid, loop2->boot_uuid()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 342 | EXPECT_EQ(fetcher.context().queue_index, 0x0u); |
| 343 | EXPECT_EQ(fetcher.context().size, 20u); |
| 344 | EXPECT_NE(fetcher.context().data, nullptr); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 345 | if (read_method() == ReadMethod::PIN) { |
| 346 | EXPECT_GE(fetcher.context().buffer_index, 0); |
| 347 | EXPECT_LT(fetcher.context().buffer_index, |
| 348 | loop2->NumberBuffers(fetcher.channel())); |
| 349 | } else { |
| 350 | EXPECT_EQ(fetcher.context().buffer_index, -1); |
| 351 | } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 354 | std::function<bool(const Context &)> MakeShouldFetch( |
| 355 | bool should_fetch, size_t *called_count = nullptr) { |
| 356 | return [should_fetch, called_count](const Context &) { |
| 357 | if (called_count != nullptr) { |
| 358 | (*called_count)++; |
| 359 | } |
| 360 | return should_fetch; |
| 361 | }; |
| 362 | } |
| 363 | |
| 364 | // Tests that a fetcher using FetchIf can fetch from a sender. |
| 365 | TEST_P(AbstractEventLoopTest, FetchIfWithoutRun) { |
| 366 | auto loop1 = Make(); |
| 367 | auto loop2 = Make(); |
| 368 | auto loop3 = MakePrimary(); |
| 369 | |
| 370 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 371 | |
| 372 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 373 | |
| 374 | for (const bool should_fetch : {true, false}) { |
| 375 | EXPECT_FALSE(fetcher.FetchIf(MakeShouldFetch(should_fetch))); |
| 376 | EXPECT_EQ(fetcher.get(), nullptr); |
| 377 | |
| 378 | EXPECT_EQ(fetcher.context().monotonic_event_time, |
| 379 | monotonic_clock::min_time); |
| 380 | EXPECT_EQ(fetcher.context().monotonic_remote_time, |
| 381 | monotonic_clock::min_time); |
| 382 | EXPECT_EQ(fetcher.context().realtime_event_time, realtime_clock::min_time); |
| 383 | EXPECT_EQ(fetcher.context().realtime_remote_time, realtime_clock::min_time); |
| 384 | EXPECT_EQ(fetcher.context().source_boot_uuid, UUID::Zero()); |
| 385 | EXPECT_EQ(fetcher.context().queue_index, 0xffffffffu); |
| 386 | EXPECT_EQ(fetcher.context().size, 0u); |
| 387 | EXPECT_EQ(fetcher.context().data, nullptr); |
| 388 | EXPECT_EQ(fetcher.context().buffer_index, -1); |
| 389 | } |
| 390 | |
| 391 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 392 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 393 | builder.add_value(200); |
| 394 | msg.CheckOk(msg.Send(builder.Finish())); |
| 395 | |
| 396 | // Make sure failing to fetch won't affect anything. |
| 397 | EXPECT_FALSE(fetcher.FetchIf(MakeShouldFetch(false))); |
| 398 | EXPECT_EQ(fetcher.get(), nullptr); |
| 399 | |
| 400 | EXPECT_EQ(fetcher.context().monotonic_event_time, monotonic_clock::min_time); |
| 401 | EXPECT_EQ(fetcher.context().monotonic_remote_time, monotonic_clock::min_time); |
| 402 | EXPECT_EQ(fetcher.context().realtime_event_time, realtime_clock::min_time); |
| 403 | EXPECT_EQ(fetcher.context().realtime_remote_time, realtime_clock::min_time); |
| 404 | EXPECT_EQ(fetcher.context().source_boot_uuid, UUID::Zero()); |
| 405 | EXPECT_EQ(fetcher.context().queue_index, 0xffffffffu); |
| 406 | EXPECT_EQ(fetcher.context().size, 0u); |
| 407 | EXPECT_EQ(fetcher.context().data, nullptr); |
| 408 | EXPECT_EQ(fetcher.context().buffer_index, -1); |
| 409 | |
| 410 | // And now confirm we succeed and everything gets set right. |
| 411 | EXPECT_TRUE(fetcher.FetchIf(MakeShouldFetch(true))); |
| 412 | ASSERT_FALSE(fetcher.get() == nullptr); |
| 413 | EXPECT_EQ(fetcher.get()->value(), 200); |
| 414 | |
| 415 | const chrono::milliseconds kEpsilon(100); |
| 416 | |
| 417 | const aos::monotonic_clock::time_point monotonic_now = loop2->monotonic_now(); |
| 418 | const aos::realtime_clock::time_point realtime_now = loop2->realtime_now(); |
| 419 | EXPECT_EQ(fetcher.context().monotonic_event_time, |
| 420 | fetcher.context().monotonic_remote_time); |
| 421 | EXPECT_EQ(fetcher.context().realtime_event_time, |
| 422 | fetcher.context().realtime_remote_time); |
| 423 | |
| 424 | EXPECT_GE(fetcher.context().monotonic_event_time, monotonic_now - kEpsilon); |
| 425 | EXPECT_LE(fetcher.context().monotonic_event_time, monotonic_now + kEpsilon); |
| 426 | EXPECT_GE(fetcher.context().realtime_event_time, realtime_now - kEpsilon); |
| 427 | EXPECT_LE(fetcher.context().realtime_event_time, realtime_now + kEpsilon); |
| 428 | EXPECT_EQ(fetcher.context().source_boot_uuid, loop2->boot_uuid()); |
| 429 | EXPECT_EQ(fetcher.context().queue_index, 0x0u); |
| 430 | EXPECT_EQ(fetcher.context().size, 20u); |
| 431 | EXPECT_NE(fetcher.context().data, nullptr); |
| 432 | if (read_method() == ReadMethod::PIN) { |
| 433 | EXPECT_GE(fetcher.context().buffer_index, 0); |
| 434 | EXPECT_LT(fetcher.context().buffer_index, |
| 435 | loop2->NumberBuffers(fetcher.channel())); |
| 436 | } else { |
| 437 | EXPECT_EQ(fetcher.context().buffer_index, -1); |
| 438 | } |
| 439 | } |
| 440 | |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 441 | // Tests that watcher will receive all messages sent if they are sent after |
| 442 | // initialization and before running. |
| 443 | TEST_P(AbstractEventLoopTest, DoubleSendAtStartup) { |
| 444 | auto loop1 = Make(); |
| 445 | auto loop2 = MakePrimary(); |
| 446 | |
| 447 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 448 | |
| 449 | ::std::vector<int> values; |
| 450 | |
| 451 | loop2->MakeWatcher("/test", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 452 | values.push_back(message.value()); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 453 | if (values.size() == 2) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 454 | this->Exit(); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 455 | } |
| 456 | }); |
| 457 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 458 | // Before Run, should be ignored. |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 459 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 460 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 461 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 462 | builder.add_value(199); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 463 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 464 | } |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 465 | |
| 466 | loop2->OnRun([&]() { |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 467 | for (int i = 200; i < 202; ++i) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 468 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 469 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 470 | builder.add_value(i); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 471 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 472 | } |
| 473 | }); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 474 | |
| 475 | Run(); |
| 476 | |
| 477 | EXPECT_THAT(values, ::testing::ElementsAreArray({200, 201})); |
| 478 | } |
| 479 | |
| 480 | // Tests that watcher will not receive messages sent before the watcher is |
| 481 | // created. |
| 482 | TEST_P(AbstractEventLoopTest, DoubleSendAfterStartup) { |
| 483 | auto loop1 = Make(); |
| 484 | auto loop2 = MakePrimary(); |
| 485 | |
| 486 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 487 | |
| 488 | ::std::vector<int> values; |
| 489 | |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 490 | for (int i = 200; i < 202; ++i) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 491 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 492 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 493 | builder.add_value(i); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 494 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | loop2->MakeWatcher("/test", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 498 | values.push_back(message.value()); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 499 | }); |
| 500 | |
| 501 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 502 | auto test_timer = loop2->AddTimer([this]() { this->Exit(); }); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 503 | loop2->OnRun([&test_timer, &loop2]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 504 | test_timer->Schedule(loop2->monotonic_now(), |
| 505 | ::std::chrono::milliseconds(100)); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 506 | }); |
| 507 | |
| 508 | Run(); |
| 509 | EXPECT_EQ(0, values.size()); |
| 510 | } |
| 511 | |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 512 | // Tests that FetchNext gets all the messages sent after it is constructed. |
| 513 | TEST_P(AbstractEventLoopTest, FetchNext) { |
| 514 | auto loop1 = Make(); |
| 515 | auto loop2 = MakePrimary(); |
| 516 | |
| 517 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 518 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 519 | |
| 520 | ::std::vector<int> values; |
| 521 | |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 522 | for (int i = 200; i < 202; ++i) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 523 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 524 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 525 | builder.add_value(i); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 526 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 530 | auto test_timer = loop2->AddTimer([&fetcher, &values, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 531 | while (fetcher.FetchNext()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 532 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 533 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 534 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 535 | }); |
| 536 | |
| 537 | loop2->OnRun([&test_timer, &loop2]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 538 | test_timer->Schedule(loop2->monotonic_now(), |
| 539 | ::std::chrono::milliseconds(100)); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 540 | }); |
| 541 | |
| 542 | Run(); |
| 543 | EXPECT_THAT(values, ::testing::ElementsAreArray({200, 201})); |
| 544 | } |
| 545 | |
| 546 | // Tests that FetchNext gets no messages sent before it is constructed. |
| 547 | TEST_P(AbstractEventLoopTest, FetchNextAfterSend) { |
| 548 | auto loop1 = Make(); |
| 549 | auto loop2 = MakePrimary(); |
| 550 | |
| 551 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 552 | |
| 553 | ::std::vector<int> values; |
| 554 | |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 555 | for (int i = 200; i < 202; ++i) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 556 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 557 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 558 | builder.add_value(i); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 559 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 563 | |
| 564 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 565 | auto test_timer = loop2->AddTimer([&fetcher, &values, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 566 | while (fetcher.FetchNext()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 567 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 568 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 569 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 570 | }); |
| 571 | |
| 572 | loop2->OnRun([&test_timer, &loop2]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 573 | test_timer->Schedule(loop2->monotonic_now(), |
| 574 | ::std::chrono::milliseconds(100)); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 575 | }); |
| 576 | |
| 577 | Run(); |
| 578 | EXPECT_THAT(0, values.size()); |
| 579 | } |
| 580 | |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 581 | // Tests that FetchNextIf gets no messages sent before it is constructed. |
| 582 | TEST_P(AbstractEventLoopTest, FetchNextIfAfterSend) { |
| 583 | auto loop1 = Make(); |
| 584 | auto loop2 = MakePrimary(); |
| 585 | |
| 586 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 587 | |
| 588 | ::std::vector<int> values; |
| 589 | |
| 590 | for (int i = 200; i < 202; ++i) { |
| 591 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 592 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 593 | builder.add_value(i); |
| 594 | msg.CheckOk(msg.Send(builder.Finish())); |
| 595 | } |
| 596 | |
| 597 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 598 | |
| 599 | // Add a timer to actually quit. |
| 600 | auto test_timer = loop2->AddTimer([&fetcher, &values, this]() { |
| 601 | while (fetcher.FetchNextIf(MakeShouldFetch(true))) { |
| 602 | values.push_back(fetcher.get()->value()); |
| 603 | } |
| 604 | this->Exit(); |
| 605 | }); |
| 606 | |
| 607 | loop2->OnRun([&test_timer, &loop2]() { |
| 608 | test_timer->Schedule(loop2->monotonic_now(), |
| 609 | ::std::chrono::milliseconds(100)); |
| 610 | }); |
| 611 | |
| 612 | Run(); |
| 613 | EXPECT_EQ(0, values.size()); |
| 614 | } |
| 615 | |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 616 | // Tests that Fetch returns the last message created before the loop was |
| 617 | // started. |
| 618 | TEST_P(AbstractEventLoopTest, FetchDataFromBeforeCreation) { |
| 619 | auto loop1 = Make(); |
| 620 | auto loop2 = MakePrimary(); |
| 621 | |
| 622 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 623 | |
| 624 | ::std::vector<int> values; |
| 625 | |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 626 | for (int i = 200; i < 202; ++i) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 627 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 628 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 629 | builder.add_value(i); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 630 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 634 | |
| 635 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 636 | auto test_timer = loop2->AddTimer([&fetcher, &values, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 637 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 638 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 639 | } |
| 640 | // Do it again to make sure we don't double fetch. |
| 641 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 642 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 643 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 644 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 645 | }); |
| 646 | |
| 647 | loop2->OnRun([&test_timer, &loop2]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 648 | test_timer->Schedule(loop2->monotonic_now(), |
| 649 | ::std::chrono::milliseconds(100)); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 650 | }); |
| 651 | |
| 652 | Run(); |
| 653 | EXPECT_THAT(values, ::testing::ElementsAreArray({201})); |
| 654 | } |
| 655 | |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 656 | // Tests that FetchIf returns the last message created before the loop was |
| 657 | // started. |
| 658 | TEST_P(AbstractEventLoopTest, FetchIfDataFromBeforeCreation) { |
| 659 | auto loop1 = Make(); |
| 660 | auto loop2 = MakePrimary(); |
| 661 | |
| 662 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 663 | |
| 664 | ::std::vector<int> values; |
| 665 | |
| 666 | for (int i = 200; i < 202; ++i) { |
| 667 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 668 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 669 | builder.add_value(i); |
| 670 | msg.CheckOk(msg.Send(builder.Finish())); |
| 671 | } |
| 672 | |
| 673 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 674 | |
| 675 | // Add a timer to actually quit. |
| 676 | auto test_timer = loop2->AddTimer([&fetcher, &values, this]() { |
| 677 | if (fetcher.FetchIf(MakeShouldFetch(true))) { |
| 678 | values.push_back(fetcher.get()->value()); |
| 679 | } |
| 680 | |
| 681 | if (fetcher.FetchIf(MakeShouldFetch(false))) { |
| 682 | values.push_back(fetcher.get()->value()); |
| 683 | } |
| 684 | // Do it again to make sure we don't double fetch. |
| 685 | if (fetcher.FetchIf(MakeShouldFetch(true))) { |
| 686 | values.push_back(fetcher.get()->value()); |
| 687 | } |
| 688 | this->Exit(); |
| 689 | }); |
| 690 | |
| 691 | loop2->OnRun([&test_timer, &loop2]() { |
| 692 | test_timer->Schedule(loop2->monotonic_now(), |
| 693 | ::std::chrono::milliseconds(100)); |
| 694 | }); |
| 695 | |
| 696 | Run(); |
| 697 | EXPECT_THAT(values, ::testing::ElementsAreArray({201})); |
| 698 | } |
| 699 | |
Naman Gupta | 4d13b0a | 2022-10-19 16:41:24 -0700 | [diff] [blame] | 700 | // Tests that timer handler is enabled after setup (even if it is in the past) |
| 701 | // and is disabled after running |
| 702 | TEST_P(AbstractEventLoopTest, CheckTimerDisabled) { |
| 703 | auto loop = MakePrimary("primary"); |
| 704 | |
| 705 | auto timer = loop->AddTimer([this]() { |
| 706 | LOG(INFO) << "timer called"; |
| 707 | Exit(); |
| 708 | }); |
| 709 | |
| 710 | loop->OnRun([&loop, timer]() { |
| 711 | EXPECT_TRUE(timer->IsDisabled()); |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 712 | timer->Schedule(loop->monotonic_now() + chrono::milliseconds(100)); |
Naman Gupta | 4d13b0a | 2022-10-19 16:41:24 -0700 | [diff] [blame] | 713 | EXPECT_FALSE(timer->IsDisabled()); |
| 714 | }); |
| 715 | |
| 716 | Run(); |
| 717 | EXPECT_TRUE(timer->IsDisabled()); |
| 718 | } |
| 719 | |
| 720 | // Tests that timer handler is enabled after setup (even if it is in the past) |
| 721 | // and is disabled after running |
| 722 | TEST_P(AbstractEventLoopTest, CheckTimerRunInPastDisabled) { |
| 723 | auto loop = MakePrimary("primary"); |
| 724 | |
| 725 | auto timer2 = loop->AddTimer([this]() { |
| 726 | LOG(INFO) << "timer called"; |
| 727 | Exit(); |
| 728 | }); |
| 729 | |
| 730 | auto timer = loop->AddTimer([&loop, timer2]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 731 | timer2->Schedule(loop->monotonic_now() - chrono::nanoseconds(1)); |
Naman Gupta | 4d13b0a | 2022-10-19 16:41:24 -0700 | [diff] [blame] | 732 | }); |
| 733 | |
| 734 | loop->OnRun([&loop, timer]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 735 | timer->Schedule(loop->monotonic_now() + chrono::seconds(1)); |
Naman Gupta | 4d13b0a | 2022-10-19 16:41:24 -0700 | [diff] [blame] | 736 | EXPECT_FALSE(timer->IsDisabled()); |
| 737 | }); |
| 738 | |
| 739 | Run(); |
| 740 | EXPECT_TRUE(timer2->IsDisabled()); |
| 741 | } |
| 742 | |
| 743 | // Tests that timer handler is not disabled even after calling Exit on the event |
| 744 | // loop within the timer |
| 745 | TEST_P(AbstractEventLoopTest, CheckTimerRepeatOnCountDisabled) { |
| 746 | auto loop = MakePrimary("primary"); |
| 747 | int counter = 0; |
| 748 | |
| 749 | auto timer = loop->AddTimer([&counter, this]() { |
| 750 | LOG(INFO) << "timer called"; |
| 751 | counter++; |
| 752 | if (counter >= 5) { |
| 753 | Exit(); |
| 754 | } |
| 755 | }); |
| 756 | |
| 757 | loop->OnRun([&loop, timer]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 758 | timer->Schedule(loop->monotonic_now() + chrono::seconds(1), |
| 759 | chrono::seconds(1)); |
Naman Gupta | 4d13b0a | 2022-10-19 16:41:24 -0700 | [diff] [blame] | 760 | EXPECT_FALSE(timer->IsDisabled()); |
| 761 | }); |
| 762 | Run(); |
| 763 | |
| 764 | // Sanity check |
| 765 | EXPECT_EQ(counter, 5); |
| 766 | |
| 767 | // if you run the loop again, the timer will start running again |
| 768 | EXPECT_FALSE(timer->IsDisabled()); |
| 769 | |
| 770 | counter = 0; |
| 771 | Run(); |
| 772 | timer->Disable(); |
| 773 | |
| 774 | EXPECT_TRUE(timer->IsDisabled()); |
| 775 | } |
| 776 | |
| 777 | // Tests that timer handler is not disabled even after calling Exit on the event |
| 778 | // loop using an external timer |
| 779 | TEST_P(AbstractEventLoopTest, CheckTimerRepeatTillEndTimerDisabled) { |
| 780 | auto loop = MakePrimary("primary"); |
| 781 | |
| 782 | auto timer = loop->AddTimer([]() { LOG(INFO) << "timer called"; }); |
| 783 | |
| 784 | loop->OnRun([&loop, timer]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 785 | timer->Schedule(loop->monotonic_now() + chrono::seconds(1), |
| 786 | chrono::seconds(1)); |
Naman Gupta | 4d13b0a | 2022-10-19 16:41:24 -0700 | [diff] [blame] | 787 | EXPECT_FALSE(timer->IsDisabled()); |
| 788 | }); |
| 789 | |
| 790 | EndEventLoop(loop.get(), chrono::seconds(5)); |
| 791 | Run(); |
| 792 | EXPECT_FALSE(timer->IsDisabled()); |
| 793 | |
| 794 | timer->Disable(); |
| 795 | EXPECT_TRUE(timer->IsDisabled()); |
| 796 | } |
| 797 | |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 798 | // Tests that Fetch and FetchNext interleave as expected. |
| 799 | TEST_P(AbstractEventLoopTest, FetchAndFetchNextTogether) { |
| 800 | auto loop1 = Make(); |
| 801 | auto loop2 = MakePrimary(); |
| 802 | |
| 803 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 804 | |
| 805 | ::std::vector<int> values; |
| 806 | |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 807 | for (int i = 200; i < 202; ++i) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 808 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 809 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 810 | builder.add_value(i); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 811 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 815 | |
| 816 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 817 | auto test_timer = loop2->AddTimer([&fetcher, &values, &sender, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 818 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 819 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 822 | for (int i = 202; i < 205; ++i) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 823 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 824 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 825 | builder.add_value(i); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 826 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | if (fetcher.FetchNext()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 830 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 834 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 837 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 838 | }); |
| 839 | |
| 840 | loop2->OnRun([&test_timer, &loop2]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 841 | test_timer->Schedule(loop2->monotonic_now(), |
| 842 | ::std::chrono::milliseconds(100)); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 843 | }); |
| 844 | |
| 845 | Run(); |
| 846 | EXPECT_THAT(values, ::testing::ElementsAreArray({201, 202, 204})); |
| 847 | } |
| 848 | |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 849 | // Tests that Fetch{If,} and FetchNext{If,} interleave as expected. |
| 850 | TEST_P(AbstractEventLoopTest, FetchAndFetchNextIfTogether) { |
| 851 | auto loop1 = Make(); |
| 852 | auto loop2 = MakePrimary(); |
| 853 | |
| 854 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 855 | |
| 856 | ::std::vector<int> values; |
| 857 | |
| 858 | for (int i = 200; i < 202; ++i) { |
| 859 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 860 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 861 | builder.add_value(i); |
| 862 | msg.CheckOk(msg.Send(builder.Finish())); |
| 863 | } |
| 864 | |
| 865 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 866 | |
| 867 | // Add a timer to actually quit. |
| 868 | auto test_timer = loop2->AddTimer([&fetcher, &values, &sender, this]() { |
| 869 | if (fetcher.Fetch()) { |
| 870 | values.push_back(fetcher.get()->value()); |
| 871 | } |
| 872 | |
| 873 | for (int i = 202; i < 205; ++i) { |
| 874 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 875 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 876 | builder.add_value(i); |
| 877 | msg.CheckOk(msg.Send(builder.Finish())); |
| 878 | } |
| 879 | |
| 880 | EXPECT_FALSE(fetcher.FetchNextIf(MakeShouldFetch(false))); |
| 881 | |
| 882 | if (fetcher.FetchNext()) { |
| 883 | values.push_back(fetcher.get()->value()); |
| 884 | } |
| 885 | |
| 886 | EXPECT_FALSE(fetcher.FetchNextIf(MakeShouldFetch(false))); |
| 887 | EXPECT_FALSE(fetcher.FetchIf(MakeShouldFetch(false))); |
| 888 | |
| 889 | if (fetcher.FetchIf(MakeShouldFetch(true))) { |
| 890 | values.push_back(fetcher.get()->value()); |
| 891 | } |
| 892 | |
| 893 | this->Exit(); |
| 894 | }); |
| 895 | |
| 896 | loop2->OnRun([&test_timer, &loop2]() { |
| 897 | test_timer->Schedule(loop2->monotonic_now(), |
| 898 | ::std::chrono::milliseconds(100)); |
| 899 | }); |
| 900 | |
| 901 | Run(); |
| 902 | EXPECT_THAT(values, ::testing::ElementsAreArray({201, 202, 204})); |
| 903 | } |
| 904 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 905 | // Tests that FetchNext behaves correctly when we get two messages in the queue |
| 906 | // but don't consume the first until after the second has been sent. |
| 907 | TEST_P(AbstractEventLoopTest, FetchNextTest) { |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 908 | auto send_loop = Make(); |
| 909 | auto fetch_loop = Make(); |
| 910 | auto sender = send_loop->MakeSender<TestMessage>("/test"); |
| 911 | Fetcher<TestMessage> fetcher = fetch_loop->MakeFetcher<TestMessage>("/test"); |
| 912 | |
| 913 | { |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 914 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 915 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 916 | builder.add_value(100); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 917 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 921 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 922 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 923 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 924 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | ASSERT_TRUE(fetcher.FetchNext()); |
| 928 | ASSERT_NE(nullptr, fetcher.get()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 929 | EXPECT_EQ(100, fetcher.get()->value()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 930 | |
| 931 | ASSERT_TRUE(fetcher.FetchNext()); |
| 932 | ASSERT_NE(nullptr, fetcher.get()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 933 | EXPECT_EQ(200, fetcher.get()->value()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 934 | |
| 935 | // When we run off the end of the queue, expect to still have the old message: |
| 936 | ASSERT_FALSE(fetcher.FetchNext()); |
| 937 | ASSERT_NE(nullptr, fetcher.get()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 938 | EXPECT_EQ(200, fetcher.get()->value()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 941 | // Tests that FetchNext behaves correctly when we get two messages in the queue |
| 942 | // but don't consume the first until after the second has been sent. |
| 943 | TEST_P(AbstractEventLoopTest, FetchNextIfTest) { |
| 944 | auto send_loop = Make(); |
| 945 | auto fetch_loop = Make(); |
| 946 | auto sender = send_loop->MakeSender<TestMessage>("/test"); |
| 947 | Fetcher<TestMessage> fetcher = fetch_loop->MakeFetcher<TestMessage>("/test"); |
| 948 | |
| 949 | { |
| 950 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 951 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 952 | builder.add_value(100); |
| 953 | msg.CheckOk(msg.Send(builder.Finish())); |
| 954 | } |
| 955 | |
| 956 | { |
| 957 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 958 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 959 | builder.add_value(200); |
| 960 | msg.CheckOk(msg.Send(builder.Finish())); |
| 961 | } |
| 962 | |
| 963 | size_t called_count = 0; |
| 964 | ASSERT_TRUE(fetcher.FetchNextIf(MakeShouldFetch(true, &called_count))); |
| 965 | ASSERT_NE(nullptr, fetcher.get()); |
| 966 | EXPECT_EQ(100, fetcher.get()->value()); |
| 967 | EXPECT_EQ(called_count, 1u); |
| 968 | |
| 969 | ASSERT_FALSE(fetcher.FetchNextIf(MakeShouldFetch(false, &called_count))); |
| 970 | EXPECT_EQ(called_count, 2u); |
| 971 | |
| 972 | ASSERT_TRUE(fetcher.FetchNextIf(MakeShouldFetch(true, &called_count))); |
| 973 | ASSERT_NE(nullptr, fetcher.get()); |
| 974 | EXPECT_EQ(200, fetcher.get()->value()); |
| 975 | EXPECT_EQ(called_count, 3u); |
| 976 | |
| 977 | // When we run off the end of the queue, expect to still have the old message: |
| 978 | ASSERT_FALSE(fetcher.FetchNextIf(MakeShouldFetch(false, &called_count))); |
| 979 | EXPECT_EQ(called_count, 3u); |
| 980 | ASSERT_NE(nullptr, fetcher.get()); |
| 981 | EXPECT_EQ(200, fetcher.get()->value()); |
| 982 | } |
| 983 | |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 984 | // Verify that a fetcher still holds its data, even after falling behind. |
| 985 | TEST_P(AbstractEventLoopTest, FetcherBehindData) { |
| 986 | auto send_loop = Make(); |
| 987 | auto fetch_loop = Make(); |
| 988 | auto sender = send_loop->MakeSender<TestMessage>("/test"); |
| 989 | Fetcher<TestMessage> fetcher = fetch_loop->MakeFetcher<TestMessage>("/test"); |
| 990 | { |
| 991 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 992 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 993 | builder.add_value(1); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 994 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 995 | } |
| 996 | ASSERT_TRUE(fetcher.Fetch()); |
| 997 | EXPECT_EQ(1, fetcher.get()->value()); |
| 998 | for (int i = 0; i < 300; ++i) { |
| 999 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 1000 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 1001 | builder.add_value(i + 2); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1002 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 1003 | } |
| 1004 | EXPECT_EQ(1, fetcher.get()->value()); |
| 1005 | } |
| 1006 | |
| 1007 | // Try a bunch of orderings of operations with fetchers and senders. Verify that |
| 1008 | // all the fetchers have the correct data at each step. |
| 1009 | TEST_P(AbstractEventLoopTest, FetcherPermutations) { |
| 1010 | for (int max_save = 0; max_save < 5; ++max_save) { |
| 1011 | SCOPED_TRACE("max_save=" + std::to_string(max_save)); |
| 1012 | |
| 1013 | auto send_loop = Make(); |
| 1014 | auto fetch_loop = Make(); |
| 1015 | auto sender = send_loop->MakeSender<TestMessage>("/test"); |
| 1016 | const auto send_message = [&sender](int i) { |
| 1017 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 1018 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 1019 | builder.add_value(i); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1020 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 1021 | }; |
| 1022 | std::vector<Fetcher<TestMessage>> fetchers; |
| 1023 | for (int i = 0; i < 10; ++i) { |
| 1024 | fetchers.emplace_back(fetch_loop->MakeFetcher<TestMessage>("/test")); |
| 1025 | } |
| 1026 | send_message(1); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 1027 | const auto verify_buffers = [&]() { |
| 1028 | std::vector<std::reference_wrapper<const Fetcher<TestMessage>>> |
| 1029 | fetchers_copy; |
| 1030 | for (const auto &fetcher : fetchers) { |
| 1031 | fetchers_copy.emplace_back(fetcher); |
| 1032 | } |
| 1033 | std::vector<std::reference_wrapper<const Sender<TestMessage>>> |
| 1034 | senders_copy; |
| 1035 | senders_copy.emplace_back(sender); |
| 1036 | VerifyBuffers(send_loop->NumberBuffers(sender.channel()), fetchers_copy, |
| 1037 | senders_copy); |
| 1038 | }; |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 1039 | for (auto &fetcher : fetchers) { |
| 1040 | ASSERT_TRUE(fetcher.Fetch()); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 1041 | verify_buffers(); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 1042 | EXPECT_EQ(1, fetcher.get()->value()); |
| 1043 | } |
| 1044 | |
| 1045 | for (int save = 1; save <= max_save; ++save) { |
| 1046 | SCOPED_TRACE("save=" + std::to_string(save)); |
| 1047 | send_message(100 + save); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 1048 | verify_buffers(); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 1049 | for (size_t i = 0; i < fetchers.size() - save; ++i) { |
| 1050 | SCOPED_TRACE("fetcher=" + std::to_string(i)); |
| 1051 | ASSERT_TRUE(fetchers[i].Fetch()); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 1052 | verify_buffers(); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 1053 | EXPECT_EQ(100 + save, fetchers[i].get()->value()); |
| 1054 | } |
| 1055 | for (size_t i = fetchers.size() - save; i < fetchers.size() - 1; ++i) { |
| 1056 | SCOPED_TRACE("fetcher=" + std::to_string(i)); |
| 1057 | EXPECT_EQ(100 + (fetchers.size() - 1 - i), fetchers[i].get()->value()); |
| 1058 | } |
| 1059 | EXPECT_EQ(1, fetchers.back().get()->value()); |
| 1060 | } |
| 1061 | |
| 1062 | for (int i = 0; i < 300; ++i) { |
| 1063 | send_message(200 + i); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 1064 | verify_buffers(); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | for (size_t i = 0; i < fetchers.size() - max_save; ++i) { |
| 1068 | SCOPED_TRACE("fetcher=" + std::to_string(i)); |
| 1069 | if (max_save > 0) { |
| 1070 | EXPECT_EQ(100 + max_save, fetchers[i].get()->value()); |
| 1071 | } else { |
| 1072 | EXPECT_EQ(1, fetchers[i].get()->value()); |
| 1073 | } |
| 1074 | } |
| 1075 | for (size_t i = fetchers.size() - max_save; i < fetchers.size() - 1; ++i) { |
| 1076 | SCOPED_TRACE("fetcher=" + std::to_string(i)); |
| 1077 | EXPECT_EQ(100 + (fetchers.size() - 1 - i), fetchers[i].get()->value()); |
| 1078 | } |
| 1079 | EXPECT_EQ(1, fetchers.back().get()->value()); |
| 1080 | } |
| 1081 | } |
| 1082 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 1083 | // Verify that making a fetcher and watcher for "/test" succeeds. |
| 1084 | TEST_P(AbstractEventLoopTest, FetcherAndWatcher) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 1085 | auto loop = Make(); |
| 1086 | auto fetcher = loop->MakeFetcher<TestMessage>("/test"); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 1087 | loop->MakeWatcher("/test", [&](const TestMessage &) {}); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 1088 | } |
| 1089 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 1090 | // Verify that making 2 fetchers for "/test" succeeds. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 1091 | TEST_P(AbstractEventLoopTest, TwoFetcher) { |
| 1092 | auto loop = Make(); |
| 1093 | auto fetcher = loop->MakeFetcher<TestMessage>("/test"); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 1094 | auto fetcher2 = loop->MakeFetcher<TestMessage>("/test"); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 1095 | } |
| 1096 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1097 | // Verify that registering a watcher for an invalid channel name dies. |
| 1098 | TEST_P(AbstractEventLoopDeathTest, InvalidChannelName) { |
| 1099 | auto loop = Make(); |
| 1100 | EXPECT_DEATH( |
| 1101 | { loop->MakeWatcher("/test/invalid", [&](const TestMessage &) {}); }, |
| 1102 | "/test/invalid"); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1103 | EXPECT_DEATH( |
| 1104 | { loop->MakeNoArgWatcher<TestMessage>("/test/invalid", [&]() {}); }, |
| 1105 | "/test/invalid"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
James Kuszmaul | 8866e64 | 2022-06-10 16:00:36 -0700 | [diff] [blame] | 1108 | // Verify that setting up a timer before monotonic_clock::epoch() fails. |
James Kuszmaul | 86e86c3 | 2022-07-21 17:39:47 -0700 | [diff] [blame] | 1109 | TEST_P(AbstractEventLoopDeathTest, NegativeTimeTimer) { |
James Kuszmaul | 8866e64 | 2022-06-10 16:00:36 -0700 | [diff] [blame] | 1110 | auto loop = Make(); |
| 1111 | TimerHandler *time = loop->AddTimer([]() {}); |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 1112 | EXPECT_DEATH( |
| 1113 | time->Schedule(monotonic_clock::epoch() - std::chrono::seconds(1)), |
| 1114 | "-1.000"); |
James Kuszmaul | 8866e64 | 2022-06-10 16:00:36 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 1117 | // Verify that registering a watcher twice for "/test" fails. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 1118 | TEST_P(AbstractEventLoopDeathTest, TwoWatcher) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 1119 | auto loop = Make(); |
| 1120 | loop->MakeWatcher("/test", [&](const TestMessage &) {}); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 1121 | EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}), |
| 1122 | "/test"); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1123 | EXPECT_DEATH(loop->MakeNoArgWatcher<TestMessage>("/test", [&]() {}), "/test"); |
| 1124 | } |
| 1125 | |
| 1126 | // Verify that registering a no-arg watcher twice for "/test" fails. |
| 1127 | TEST_P(AbstractEventLoopDeathTest, TwoNoArgWatcher) { |
| 1128 | auto loop = Make(); |
| 1129 | loop->MakeNoArgWatcher<TestMessage>("/test", [&]() {}); |
| 1130 | EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}), |
| 1131 | "/test"); |
| 1132 | EXPECT_DEATH(loop->MakeNoArgWatcher<TestMessage>("/test", [&]() {}), "/test"); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 1133 | } |
| 1134 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 1135 | // Verify that SetRuntimeRealtimePriority fails while running. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 1136 | TEST_P(AbstractEventLoopDeathTest, SetRuntimeRealtimePriority) { |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 1137 | auto loop = MakePrimary(); |
Austin Schuh | 65493d6 | 2022-08-17 15:10:37 -0700 | [diff] [blame] | 1138 | EXPECT_EQ(0, loop->runtime_realtime_priority()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 1139 | // Confirm that runtime priority calls work when not realtime. |
| 1140 | loop->SetRuntimeRealtimePriority(5); |
Austin Schuh | 65493d6 | 2022-08-17 15:10:37 -0700 | [diff] [blame] | 1141 | EXPECT_EQ(5, loop->runtime_realtime_priority()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 1142 | |
| 1143 | loop->OnRun([&]() { loop->SetRuntimeRealtimePriority(5); }); |
| 1144 | |
| 1145 | EXPECT_DEATH(Run(), "realtime"); |
| 1146 | } |
| 1147 | |
Austin Schuh | 65493d6 | 2022-08-17 15:10:37 -0700 | [diff] [blame] | 1148 | namespace { |
| 1149 | |
| 1150 | bool CpuSetEqual(const cpu_set_t &a, const cpu_set_t &b) { |
| 1151 | return CPU_EQUAL(&a, &b); |
| 1152 | } |
| 1153 | |
| 1154 | } // namespace |
| 1155 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 1156 | // Verify that SetRuntimeAffinity fails while running. |
| 1157 | TEST_P(AbstractEventLoopDeathTest, SetRuntimeAffinity) { |
Austin Schuh | de97329 | 2021-10-12 18:09:49 -0700 | [diff] [blame] | 1158 | const cpu_set_t available = GetCurrentThreadAffinity(); |
| 1159 | int first_cpu = -1; |
| 1160 | for (int i = 0; i < CPU_SETSIZE; ++i) { |
| 1161 | if (CPU_ISSET(i, &available)) { |
| 1162 | first_cpu = i; |
| 1163 | break; |
| 1164 | continue; |
| 1165 | } |
| 1166 | } |
| 1167 | CHECK_NE(first_cpu, -1) << ": Default affinity has no CPUs?"; |
| 1168 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 1169 | auto loop = MakePrimary(); |
Austin Schuh | 65493d6 | 2022-08-17 15:10:37 -0700 | [diff] [blame] | 1170 | EXPECT_TRUE( |
| 1171 | CpuSetEqual(EventLoop::DefaultAffinity(), loop->runtime_affinity())); |
| 1172 | const cpu_set_t new_affinity = MakeCpusetFromCpus({first_cpu}); |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 1173 | // Confirm that runtime priority calls work when not running. |
Austin Schuh | 65493d6 | 2022-08-17 15:10:37 -0700 | [diff] [blame] | 1174 | loop->SetRuntimeAffinity(new_affinity); |
| 1175 | EXPECT_TRUE(CpuSetEqual(new_affinity, loop->runtime_affinity())); |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 1176 | |
Austin Schuh | de97329 | 2021-10-12 18:09:49 -0700 | [diff] [blame] | 1177 | loop->OnRun( |
| 1178 | [&]() { loop->SetRuntimeAffinity(MakeCpusetFromCpus({first_cpu})); }); |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 1179 | |
| 1180 | EXPECT_DEATH(Run(), "Cannot set affinity while running"); |
| 1181 | } |
| 1182 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 1183 | // Verify that registering a watcher and a sender for "/test" fails. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 1184 | TEST_P(AbstractEventLoopDeathTest, WatcherAndSender) { |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 1185 | auto loop = Make(); |
| 1186 | auto sender = loop->MakeSender<TestMessage>("/test"); |
| 1187 | EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}), |
| 1188 | "/test"); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 1189 | } |
| 1190 | |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 1191 | // Verify that creating too many senders fails. |
| 1192 | TEST_P(AbstractEventLoopDeathTest, TooManySenders) { |
| 1193 | auto loop = Make(); |
| 1194 | std::vector<aos::Sender<TestMessage>> senders; |
| 1195 | for (int i = 0; i < 10; ++i) { |
| 1196 | senders.emplace_back(loop->MakeSender<TestMessage>("/test")); |
| 1197 | } |
| 1198 | EXPECT_DEATH({ loop->MakeSender<TestMessage>("/test"); }, |
| 1199 | "Failed to create sender on \\{ \"name\": \"/test\", \"type\": " |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 1200 | "\"aos.TestMessage\"[^}]*\\ }, too many senders."); |
| 1201 | } |
| 1202 | |
| 1203 | // Verify that creating too many fetchers fails. |
| 1204 | TEST_P(AbstractEventLoopDeathTest, TooManyFetchers) { |
| 1205 | if (read_method() != ReadMethod::PIN) { |
| 1206 | // Other read methods don't limit the number of readers, so just skip this. |
| 1207 | return; |
| 1208 | } |
| 1209 | |
| 1210 | auto loop = Make(); |
| 1211 | std::vector<aos::Fetcher<TestMessage>> fetchers; |
| 1212 | for (int i = 0; i < 10; ++i) { |
| 1213 | fetchers.emplace_back(loop->MakeFetcher<TestMessage>("/test")); |
| 1214 | } |
| 1215 | EXPECT_DEATH({ loop->MakeFetcher<TestMessage>("/test"); }, |
| 1216 | "Failed to create reader on \\{ \"name\": \"/test\", \"type\": " |
| 1217 | "\"aos.TestMessage\"[^}]*\\ }, too many readers."); |
| 1218 | } |
| 1219 | |
| 1220 | // Verify that creating too many fetchers, split between two event loops, fails. |
| 1221 | TEST_P(AbstractEventLoopDeathTest, TooManyFetchersTwoLoops) { |
| 1222 | if (read_method() != ReadMethod::PIN) { |
| 1223 | // Other read methods don't limit the number of readers, so just skip this. |
| 1224 | return; |
| 1225 | } |
| 1226 | |
| 1227 | auto loop = Make(); |
| 1228 | auto loop2 = Make(); |
| 1229 | std::vector<aos::Fetcher<TestMessage>> fetchers; |
| 1230 | for (int i = 0; i < 5; ++i) { |
| 1231 | fetchers.emplace_back(loop->MakeFetcher<TestMessage>("/test")); |
| 1232 | fetchers.emplace_back(loop2->MakeFetcher<TestMessage>("/test")); |
| 1233 | } |
| 1234 | EXPECT_DEATH({ loop->MakeFetcher<TestMessage>("/test"); }, |
| 1235 | "Failed to create reader on \\{ \"name\": \"/test\", \"type\": " |
| 1236 | "\"aos.TestMessage\"[^}]*\\ }, too many readers."); |
| 1237 | } |
| 1238 | |
| 1239 | // Verify that creating too many watchers fails. |
| 1240 | TEST_P(AbstractEventLoopDeathTest, TooManyWatchers) { |
| 1241 | if (read_method() != ReadMethod::PIN) { |
| 1242 | // Other read methods don't limit the number of readers, so just skip this. |
| 1243 | return; |
| 1244 | } |
| 1245 | |
| 1246 | std::vector<std::unique_ptr<EventLoop>> loops; |
| 1247 | for (int i = 0; i < 10; ++i) { |
| 1248 | loops.emplace_back(Make()); |
| 1249 | loops.back()->MakeWatcher("/test", [](const TestMessage &) {}); |
| 1250 | } |
| 1251 | EXPECT_DEATH({ Make()->MakeWatcher("/test", [](const TestMessage &) {}); }, |
| 1252 | "Failed to create reader on \\{ \"name\": \"/test\", \"type\": " |
| 1253 | "\"aos.TestMessage\"[^}]*\\ }, too many readers."); |
| 1254 | } |
| 1255 | |
| 1256 | // Verify that creating too many watchers and fetchers combined fails. |
| 1257 | TEST_P(AbstractEventLoopDeathTest, TooManyWatchersAndFetchers) { |
| 1258 | if (read_method() != ReadMethod::PIN) { |
| 1259 | // Other read methods don't limit the number of readers, so just skip this. |
| 1260 | return; |
| 1261 | } |
| 1262 | |
| 1263 | auto loop = Make(); |
| 1264 | std::vector<aos::Fetcher<TestMessage>> fetchers; |
| 1265 | std::vector<std::unique_ptr<EventLoop>> loops; |
| 1266 | for (int i = 0; i < 5; ++i) { |
| 1267 | fetchers.emplace_back(loop->MakeFetcher<TestMessage>("/test")); |
| 1268 | loops.emplace_back(Make()); |
| 1269 | loops.back()->MakeWatcher("/test", [](const TestMessage &) {}); |
| 1270 | } |
| 1271 | EXPECT_DEATH({ loop->MakeFetcher<TestMessage>("/test"); }, |
| 1272 | "Failed to create reader on \\{ \"name\": \"/test\", \"type\": " |
| 1273 | "\"aos.TestMessage\"[^}]*\\ }, too many readers."); |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 1276 | // Verify that we can't create a sender inside OnRun. |
| 1277 | TEST_P(AbstractEventLoopDeathTest, SenderInOnRun) { |
| 1278 | auto loop1 = MakePrimary(); |
| 1279 | |
| 1280 | loop1->OnRun( |
| 1281 | [&]() { auto sender = loop1->MakeSender<TestMessage>("/test2"); }); |
| 1282 | |
| 1283 | EXPECT_DEATH(Run(), "running"); |
| 1284 | } |
| 1285 | |
| 1286 | // Verify that we can't create a watcher inside OnRun. |
| 1287 | TEST_P(AbstractEventLoopDeathTest, WatcherInOnRun) { |
| 1288 | auto loop1 = MakePrimary(); |
| 1289 | |
| 1290 | loop1->OnRun( |
| 1291 | [&]() { loop1->MakeWatcher("/test", [&](const TestMessage &) {}); }); |
| 1292 | |
| 1293 | EXPECT_DEATH(Run(), "running"); |
| 1294 | } |
| 1295 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1296 | // Verify that we can't create a no-arg watcher inside OnRun. |
| 1297 | TEST_P(AbstractEventLoopDeathTest, NoArgWatcherInOnRun) { |
| 1298 | auto loop1 = MakePrimary(); |
| 1299 | |
| 1300 | loop1->OnRun( |
| 1301 | [&]() { loop1->MakeNoArgWatcher<TestMessage>("/test", [&]() {}); }); |
| 1302 | |
| 1303 | EXPECT_DEATH(Run(), "running"); |
| 1304 | } |
| 1305 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 1306 | // Verify that Quit() works when there are multiple watchers. |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 1307 | TEST_P(AbstractEventLoopTest, MultipleWatcherQuit) { |
| 1308 | auto loop1 = Make(); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 1309 | auto loop2 = MakePrimary(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 1310 | |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 1311 | loop2->MakeWatcher("/test1", [&](const TestMessage &) {}); |
| 1312 | loop2->MakeWatcher("/test2", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1313 | EXPECT_EQ(message.value(), 200); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 1314 | this->Exit(); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 1315 | }); |
| 1316 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 1317 | auto sender = loop1->MakeSender<TestMessage>("/test2"); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 1318 | |
| 1319 | loop2->OnRun([&]() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1320 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 1321 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 1322 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1323 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 1324 | }); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 1325 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 1326 | Run(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 1327 | } |
| 1328 | |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 1329 | // Verify that AOS_LOG has the right name. |
| 1330 | TEST_P(AbstractEventLoopTest, AOSLog) { |
| 1331 | auto loop2 = MakePrimary("loop1"); |
| 1332 | auto loop1 = Make("loop0"); |
| 1333 | |
| 1334 | auto fetcher = loop1->MakeFetcher<aos::logging::LogMessageFbs>("/aos"); |
| 1335 | |
| 1336 | EXPECT_FALSE(fetcher.Fetch()); |
| 1337 | |
| 1338 | loop2->OnRun([&]() { |
| 1339 | AOS_LOG(INFO, "Testing123"); |
| 1340 | this->Exit(); |
| 1341 | }); |
| 1342 | |
| 1343 | Run(); |
| 1344 | EXPECT_TRUE(fetcher.Fetch()); |
| 1345 | EXPECT_EQ(fetcher->name()->string_view(), "loop1"); |
| 1346 | } |
| 1347 | |
| 1348 | // Verify that AOS_LOG has the right name in a watcher. |
| 1349 | TEST_P(AbstractEventLoopTest, AOSLogWatcher) { |
| 1350 | auto loop2 = MakePrimary("loop1"); |
| 1351 | auto loop1 = Make("loop0"); |
| 1352 | |
| 1353 | auto fetcher = loop1->MakeFetcher<aos::logging::LogMessageFbs>("/aos"); |
| 1354 | |
| 1355 | EXPECT_FALSE(fetcher.Fetch()); |
| 1356 | |
| 1357 | auto sender = loop1->MakeSender<TestMessage>("/test2"); |
| 1358 | |
| 1359 | loop2->MakeWatcher("/test2", [&](const TestMessage & /*message*/) { |
| 1360 | AOS_LOG(INFO, "Testing123"); |
| 1361 | this->Exit(); |
| 1362 | }); |
| 1363 | |
| 1364 | loop2->OnRun([&]() { |
| 1365 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 1366 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 1367 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1368 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 1369 | }); |
| 1370 | |
| 1371 | Run(); |
| 1372 | EXPECT_TRUE(fetcher.Fetch()); |
| 1373 | EXPECT_EQ(fetcher->name()->string_view(), "loop1"); |
| 1374 | } |
| 1375 | |
| 1376 | // Verify that AOS_LOG has the right name in a timer. |
| 1377 | TEST_P(AbstractEventLoopTest, AOSLogTimer) { |
| 1378 | auto loop2 = MakePrimary("loop1"); |
| 1379 | auto loop1 = Make("loop0"); |
| 1380 | |
| 1381 | auto fetcher = loop1->MakeFetcher<aos::logging::LogMessageFbs>("/aos"); |
| 1382 | |
| 1383 | EXPECT_FALSE(fetcher.Fetch()); |
| 1384 | |
| 1385 | auto test_timer = loop2->AddTimer([&]() { |
| 1386 | AOS_LOG(INFO, "Testing123"); |
| 1387 | this->Exit(); |
| 1388 | }); |
| 1389 | |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 1390 | loop2->OnRun([&]() { test_timer->Schedule(loop2->monotonic_now()); }); |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 1391 | |
| 1392 | Run(); |
| 1393 | EXPECT_TRUE(fetcher.Fetch()); |
| 1394 | EXPECT_EQ(fetcher->name()->string_view(), "loop1"); |
| 1395 | } |
| 1396 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1397 | // Verify that timer intervals and duration function properly. |
| 1398 | TEST_P(AbstractEventLoopTest, TimerIntervalAndDuration) { |
Stephan Pleines | 3dce7ea | 2021-06-22 13:19:26 -0700 | [diff] [blame] | 1399 | // Force a slower rate so we are guaranteed to have reports for our timer. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1400 | FLAGS_timing_report_ms = 2000; |
| 1401 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1402 | const int kCount = 5; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1403 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1404 | auto loop = MakePrimary(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1405 | auto loop2 = Make(); |
| 1406 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1407 | ::std::vector<::aos::monotonic_clock::time_point> times; |
| 1408 | ::std::vector<::aos::monotonic_clock::time_point> expected_times; |
| 1409 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1410 | Fetcher<timing::Report> report_fetcher = |
| 1411 | loop2->MakeFetcher<timing::Report>("/aos"); |
| 1412 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 1413 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1414 | auto test_timer = loop->AddTimer([this, ×, &expected_times, &loop]() { |
| 1415 | times.push_back(loop->monotonic_now()); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1416 | EXPECT_EQ(loop->context().monotonic_remote_time, monotonic_clock::min_time); |
| 1417 | EXPECT_EQ(loop->context().realtime_event_time, realtime_clock::min_time); |
| 1418 | EXPECT_EQ(loop->context().realtime_remote_time, realtime_clock::min_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1419 | EXPECT_EQ(loop->context().source_boot_uuid, loop->boot_uuid()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1420 | EXPECT_EQ(loop->context().queue_index, 0xffffffffu); |
| 1421 | EXPECT_EQ(loop->context().size, 0u); |
| 1422 | EXPECT_EQ(loop->context().data, nullptr); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 1423 | EXPECT_EQ(loop->context().buffer_index, -1); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1424 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1425 | expected_times.push_back(loop->context().monotonic_event_time); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1426 | if (times.size() == kCount) { |
| 1427 | this->Exit(); |
| 1428 | } |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1429 | }); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1430 | test_timer->set_name("Test loop"); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1431 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1432 | const monotonic_clock::time_point start_time = loop->monotonic_now(); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1433 | // TODO(austin): This should be an error... Should be done in OnRun only. |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 1434 | test_timer->Schedule(start_time + chrono::seconds(1), chrono::seconds(1)); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1435 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 1436 | Run(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1437 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1438 | // Confirm that we got both the right number of samples, and it's odd. |
Stephan Pleines | 3dce7ea | 2021-06-22 13:19:26 -0700 | [diff] [blame] | 1439 | ASSERT_EQ(times.size(), static_cast<size_t>(kCount)); |
| 1440 | ASSERT_EQ(times.size(), expected_times.size()); |
| 1441 | ASSERT_EQ((times.size() % 2), 1); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1442 | |
| 1443 | // Grab the middle sample. |
| 1444 | ::aos::monotonic_clock::time_point average_time = times[times.size() / 2]; |
| 1445 | |
| 1446 | // Add up all the delays of all the times. |
| 1447 | ::aos::monotonic_clock::duration sum = chrono::seconds(0); |
| 1448 | for (const ::aos::monotonic_clock::time_point time : times) { |
| 1449 | sum += time - average_time; |
| 1450 | } |
| 1451 | |
| 1452 | // Average and add to the middle to find the average time. |
| 1453 | sum /= times.size(); |
| 1454 | average_time += sum; |
| 1455 | |
| 1456 | // Compute the offset from the average and the expected average. It |
| 1457 | // should be pretty close to 0. |
| 1458 | const ::aos::monotonic_clock::duration remainder = |
| 1459 | average_time - start_time - chrono::seconds(times.size() / 2 + 1); |
| 1460 | |
| 1461 | const chrono::milliseconds kEpsilon(100); |
| 1462 | EXPECT_LT(remainder, +kEpsilon); |
| 1463 | EXPECT_GT(remainder, -kEpsilon); |
| 1464 | |
| 1465 | // Make sure that the average duration is close to 1 second. |
| 1466 | EXPECT_NEAR(chrono::duration_cast<chrono::duration<double>>(times.back() - |
| 1467 | times.front()) |
| 1468 | .count() / |
| 1469 | static_cast<double>(times.size() - 1), |
| 1470 | 1.0, 0.1); |
| 1471 | |
| 1472 | // Confirm that the ideal wakeup times increment correctly. |
| 1473 | for (size_t i = 1; i < expected_times.size(); ++i) { |
| 1474 | EXPECT_EQ(expected_times[i], expected_times[i - 1] + chrono::seconds(1)); |
| 1475 | } |
| 1476 | |
| 1477 | for (size_t i = 0; i < expected_times.size(); ++i) { |
| 1478 | EXPECT_EQ((expected_times[i] - start_time) % chrono::seconds(1), |
| 1479 | chrono::seconds(0)); |
| 1480 | } |
| 1481 | |
| 1482 | EXPECT_LT(expected_times[expected_times.size() / 2], average_time + kEpsilon); |
| 1483 | EXPECT_GT(expected_times[expected_times.size() / 2], average_time - kEpsilon); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1484 | |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1485 | if (do_timing_reports() == DoTimingReports::kYes) { |
| 1486 | // And, since we are here, check that the timing report makes sense. |
| 1487 | // Start by looking for our event loop's timing. |
| 1488 | FlatbufferDetachedBuffer<timing::Report> report = |
| 1489 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 1490 | while (report_fetcher.FetchNext()) { |
| 1491 | if (report_fetcher->name()->string_view() == "primary") { |
| 1492 | report = CopyFlatBuffer(report_fetcher.get()); |
| 1493 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1494 | } |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1495 | |
| 1496 | // Confirm that we have the right number of reports, and the contents are |
| 1497 | // sane. |
| 1498 | VLOG(1) << FlatbufferToJson(report, {.multi_line = true}); |
| 1499 | |
| 1500 | EXPECT_EQ(report.message().name()->string_view(), "primary"); |
| 1501 | |
| 1502 | ASSERT_NE(report.message().senders(), nullptr); |
| 1503 | EXPECT_EQ(report.message().senders()->size(), 2); |
| 1504 | |
| 1505 | ASSERT_NE(report.message().timers(), nullptr); |
| 1506 | EXPECT_EQ(report.message().timers()->size(), 2); |
| 1507 | |
| 1508 | EXPECT_EQ(report.message().timers()->Get(0)->name()->string_view(), |
| 1509 | "Test loop"); |
| 1510 | EXPECT_GE(report.message().timers()->Get(0)->count(), 1); |
| 1511 | |
| 1512 | EXPECT_EQ(report.message().timers()->Get(1)->name()->string_view(), |
| 1513 | "timing_reports"); |
| 1514 | EXPECT_EQ(report.message().timers()->Get(1)->count(), 1); |
| 1515 | |
| 1516 | // Make sure there is a single phased loop report with our report in it. |
| 1517 | ASSERT_EQ(report.message().phased_loops(), nullptr); |
| 1518 | } else { |
| 1519 | ASSERT_FALSE(report_fetcher.Fetch()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1520 | } |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1521 | } |
| 1522 | |
James Kuszmaul | 762e869 | 2023-07-31 14:57:53 -0700 | [diff] [blame] | 1523 | // Test that setting a default version string results in it getting populated |
| 1524 | // correctly. |
| 1525 | TEST_P(AbstractEventLoopTest, DefaultVersionStringInTimingReport) { |
| 1526 | gflags::FlagSaver flag_saver; |
| 1527 | FLAGS_timing_report_ms = 1000; |
| 1528 | |
| 1529 | EventLoop::SetDefaultVersionString("default_version_string"); |
| 1530 | |
| 1531 | auto loop = MakePrimary(); |
| 1532 | |
| 1533 | Fetcher<timing::Report> report_fetcher = |
| 1534 | loop->MakeFetcher<timing::Report>("/aos"); |
| 1535 | |
| 1536 | TimerHandler *exit_timer = loop->AddTimer([this]() { Exit(); }); |
| 1537 | loop->OnRun([exit_timer, &loop, &report_fetcher]() { |
| 1538 | report_fetcher.Fetch(); |
| 1539 | exit_timer->Schedule(loop->monotonic_now() + std::chrono::seconds(2)); |
| 1540 | }); |
| 1541 | |
| 1542 | Run(); |
| 1543 | |
| 1544 | bool found_primary_report = false; |
| 1545 | while (report_fetcher.FetchNext()) { |
| 1546 | if (report_fetcher->name()->string_view() == "primary") { |
| 1547 | found_primary_report = true; |
| 1548 | EXPECT_EQ("default_version_string", |
| 1549 | report_fetcher->version()->string_view()); |
| 1550 | } else { |
| 1551 | FAIL() << report_fetcher->name()->string_view(); |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | if (do_timing_reports() == DoTimingReports::kYes) { |
| 1556 | EXPECT_TRUE(found_primary_report); |
| 1557 | } else { |
| 1558 | EXPECT_FALSE(found_primary_report); |
| 1559 | } |
| 1560 | } |
| 1561 | |
| 1562 | // Test that overriding the default version string results in it getting |
| 1563 | // populated correctly. |
| 1564 | TEST_P(AbstractEventLoopTest, OverrideDersionStringInTimingReport) { |
| 1565 | gflags::FlagSaver flag_saver; |
| 1566 | FLAGS_timing_report_ms = 1000; |
| 1567 | |
| 1568 | EventLoop::SetDefaultVersionString("default_version_string"); |
| 1569 | |
| 1570 | auto loop = MakePrimary(); |
| 1571 | loop->SetVersionString("override_version"); |
| 1572 | |
| 1573 | Fetcher<timing::Report> report_fetcher = |
| 1574 | loop->MakeFetcher<timing::Report>("/aos"); |
| 1575 | |
| 1576 | TimerHandler *exit_timer = loop->AddTimer([this]() { Exit(); }); |
| 1577 | loop->OnRun([exit_timer, &loop, &report_fetcher]() { |
| 1578 | report_fetcher.Fetch(); |
| 1579 | exit_timer->Schedule(loop->monotonic_now() + std::chrono::seconds(2)); |
| 1580 | }); |
| 1581 | |
| 1582 | Run(); |
| 1583 | |
| 1584 | bool found_primary_report = false; |
| 1585 | while (report_fetcher.FetchNext()) { |
| 1586 | if (report_fetcher->name()->string_view() == "primary") { |
| 1587 | found_primary_report = true; |
| 1588 | EXPECT_EQ("override_version", report_fetcher->version()->string_view()); |
| 1589 | } else { |
| 1590 | FAIL() << report_fetcher->name()->string_view(); |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | if (do_timing_reports() == DoTimingReports::kYes) { |
| 1595 | EXPECT_TRUE(found_primary_report); |
| 1596 | } else { |
| 1597 | EXPECT_FALSE(found_primary_report); |
| 1598 | } |
| 1599 | } |
| 1600 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1601 | // Verify that we can change a timer's parameters during execution. |
| 1602 | TEST_P(AbstractEventLoopTest, TimerChangeParameters) { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 1603 | auto loop = MakePrimary(); |
Austin Schuh | d892f10 | 2021-10-12 18:01:46 -0700 | [diff] [blame] | 1604 | loop->SetRuntimeRealtimePriority(1); |
Austin Schuh | 7f20f51 | 2021-01-31 17:56:16 -0800 | [diff] [blame] | 1605 | std::vector<monotonic_clock::time_point> iteration_list; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1606 | |
| 1607 | auto test_timer = loop->AddTimer([&iteration_list, &loop]() { |
Austin Schuh | 9b1d628 | 2022-06-10 17:03:21 -0700 | [diff] [blame] | 1608 | ScopedNotRealtime nrt; |
Austin Schuh | 7f20f51 | 2021-01-31 17:56:16 -0800 | [diff] [blame] | 1609 | iteration_list.push_back(loop->context().monotonic_event_time); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1610 | }); |
| 1611 | |
Austin Schuh | 7f20f51 | 2021-01-31 17:56:16 -0800 | [diff] [blame] | 1612 | monotonic_clock::time_point s; |
| 1613 | auto modifier_timer = loop->AddTimer([&test_timer, &s]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 1614 | test_timer->Schedule(s + chrono::milliseconds(1750), |
| 1615 | chrono::milliseconds(600)); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1616 | }); |
| 1617 | |
Austin Schuh | 7f20f51 | 2021-01-31 17:56:16 -0800 | [diff] [blame] | 1618 | s = loop->monotonic_now(); |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 1619 | test_timer->Schedule(s, chrono::milliseconds(500)); |
| 1620 | modifier_timer->Schedule(s + chrono::milliseconds(1250)); |
Austin Schuh | d892f10 | 2021-10-12 18:01:46 -0700 | [diff] [blame] | 1621 | EndEventLoop(loop.get(), chrono::milliseconds(3950)); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 1622 | Run(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1623 | |
Austin Schuh | d892f10 | 2021-10-12 18:01:46 -0700 | [diff] [blame] | 1624 | EXPECT_THAT( |
| 1625 | iteration_list, |
| 1626 | ::testing::ElementsAre( |
| 1627 | s, s + chrono::milliseconds(500), s + chrono::milliseconds(1000), |
| 1628 | s + chrono::milliseconds(1750), s + chrono::milliseconds(2350), |
| 1629 | s + chrono::milliseconds(2950), s + chrono::milliseconds(3550))); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | // Verify that we can disable a timer during execution. |
| 1633 | TEST_P(AbstractEventLoopTest, TimerDisable) { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 1634 | auto loop = MakePrimary(); |
Austin Schuh | d892f10 | 2021-10-12 18:01:46 -0700 | [diff] [blame] | 1635 | loop->SetRuntimeRealtimePriority(1); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1636 | ::std::vector<::aos::monotonic_clock::time_point> iteration_list; |
| 1637 | |
| 1638 | auto test_timer = loop->AddTimer([&iteration_list, &loop]() { |
Austin Schuh | 9b1d628 | 2022-06-10 17:03:21 -0700 | [diff] [blame] | 1639 | ScopedNotRealtime nrt; |
Austin Schuh | d892f10 | 2021-10-12 18:01:46 -0700 | [diff] [blame] | 1640 | iteration_list.push_back(loop->context().monotonic_event_time); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1641 | }); |
| 1642 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1643 | auto ender_timer = loop->AddTimer([&test_timer]() { test_timer->Disable(); }); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1644 | |
Austin Schuh | d892f10 | 2021-10-12 18:01:46 -0700 | [diff] [blame] | 1645 | monotonic_clock::time_point s = loop->monotonic_now(); |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 1646 | test_timer->Schedule(s, ::std::chrono::milliseconds(500)); |
| 1647 | ender_timer->Schedule(s + ::std::chrono::milliseconds(1250)); |
Austin Schuh | 73d9950 | 2021-12-08 12:05:39 -0800 | [diff] [blame] | 1648 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(2000)); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 1649 | Run(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1650 | |
Austin Schuh | d892f10 | 2021-10-12 18:01:46 -0700 | [diff] [blame] | 1651 | EXPECT_THAT(iteration_list, |
Austin Schuh | 73d9950 | 2021-12-08 12:05:39 -0800 | [diff] [blame] | 1652 | ::testing::ElementsAre(s, s + chrono::milliseconds(500), |
| 1653 | s + chrono::milliseconds(1000))); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1654 | } |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1655 | |
Brian Silverman | af9a4d8 | 2020-10-06 15:10:58 -0700 | [diff] [blame] | 1656 | // Verify that a timer can disable itself. |
| 1657 | // |
| 1658 | // TODO(Brian): Do something similar with phased loops, both with a quick |
| 1659 | // handler and a handler that would miss a cycle except it got deferred. Current |
| 1660 | // behavior doing that is a mess. |
| 1661 | TEST_P(AbstractEventLoopTest, TimerDisableSelf) { |
| 1662 | auto loop = MakePrimary(); |
| 1663 | |
| 1664 | int count = 0; |
| 1665 | aos::TimerHandler *test_timer; |
| 1666 | test_timer = loop->AddTimer([&count, &test_timer]() { |
| 1667 | ++count; |
| 1668 | test_timer->Disable(); |
| 1669 | }); |
| 1670 | |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 1671 | test_timer->Schedule(loop->monotonic_now(), ::std::chrono::milliseconds(20)); |
Brian Silverman | af9a4d8 | 2020-10-06 15:10:58 -0700 | [diff] [blame] | 1672 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(80)); |
| 1673 | Run(); |
| 1674 | |
| 1675 | EXPECT_EQ(count, 1); |
| 1676 | } |
| 1677 | |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 1678 | // Verify that we can disable a timer during execution of another timer |
| 1679 | // scheduled for the same time, with one ordering of creation for the timers. |
| 1680 | // |
| 1681 | // Also schedule some more events to reshuffle the heap in EventLoop used for |
| 1682 | // tracking events to change up the order. This used to segfault |
| 1683 | // SimulatedEventLoop. |
| 1684 | TEST_P(AbstractEventLoopTest, TimerDisableOther) { |
| 1685 | for (bool creation_order : {true, false}) { |
| 1686 | for (bool setup_order : {true, false}) { |
| 1687 | for (int shuffle_events = 0; shuffle_events < 5; ++shuffle_events) { |
| 1688 | auto loop = MakePrimary(); |
| 1689 | aos::TimerHandler *test_timer, *ender_timer; |
| 1690 | if (creation_order) { |
| 1691 | test_timer = loop->AddTimer([]() {}); |
| 1692 | ender_timer = |
| 1693 | loop->AddTimer([&test_timer]() { test_timer->Disable(); }); |
| 1694 | } else { |
| 1695 | ender_timer = |
| 1696 | loop->AddTimer([&test_timer]() { test_timer->Disable(); }); |
| 1697 | test_timer = loop->AddTimer([]() {}); |
| 1698 | } |
| 1699 | |
| 1700 | const auto start = loop->monotonic_now(); |
| 1701 | |
| 1702 | for (int i = 0; i < shuffle_events; ++i) { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 1703 | loop->AddTimer([]() {})->Schedule(start + |
| 1704 | std::chrono::milliseconds(10)); |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | if (setup_order) { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 1708 | test_timer->Schedule(start + ::std::chrono::milliseconds(20)); |
| 1709 | ender_timer->Schedule(start + ::std::chrono::milliseconds(20)); |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 1710 | } else { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 1711 | ender_timer->Schedule(start + ::std::chrono::milliseconds(20)); |
| 1712 | test_timer->Schedule(start + ::std::chrono::milliseconds(20)); |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 1713 | } |
| 1714 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(40)); |
| 1715 | Run(); |
| 1716 | } |
| 1717 | } |
| 1718 | } |
| 1719 | } |
| 1720 | |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 1721 | // Verifies that the event loop implementations detect when Channel is not a |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 1722 | // pointer into configuration(), or a name doesn't map to a channel in |
| 1723 | // configuration(). |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 1724 | TEST_P(AbstractEventLoopDeathTest, InvalidChannel) { |
| 1725 | auto loop = MakePrimary(); |
| 1726 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1727 | const Channel *channel = configuration::GetChannel( |
| 1728 | loop->configuration(), "/test", "aos.TestMessage", "", nullptr); |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 1729 | |
| 1730 | FlatbufferDetachedBuffer<Channel> channel_copy = CopyFlatBuffer(channel); |
| 1731 | |
| 1732 | EXPECT_DEATH( |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 1733 | loop->MakeRawSender(&channel_copy.message()), |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 1734 | "Channel pointer not found in configuration\\(\\)->channels\\(\\)"); |
| 1735 | |
| 1736 | EXPECT_DEATH( |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 1737 | loop->MakeSender<TestMessage>("/testbad"), |
| 1738 | "Channel \\{ \"name\": \"/testbad\", \"type\": \"aos.TestMessage\" \\}" |
| 1739 | " not found in config"); |
| 1740 | |
| 1741 | EXPECT_FALSE(loop->TryMakeSender<TestMessage>("/testbad")); |
| 1742 | |
| 1743 | EXPECT_DEATH( |
| 1744 | loop->MakeRawFetcher(&channel_copy.message()), |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 1745 | "Channel pointer not found in configuration\\(\\)->channels\\(\\)"); |
| 1746 | |
| 1747 | EXPECT_DEATH( |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 1748 | loop->MakeFetcher<TestMessage>("/testbad"), |
| 1749 | "Channel \\{ \"name\": \"/testbad\", \"type\": \"aos.TestMessage\" \\}" |
| 1750 | " not found in config"); |
| 1751 | |
| 1752 | EXPECT_FALSE(loop->TryMakeFetcher<TestMessage>("/testbad").valid()); |
| 1753 | |
| 1754 | EXPECT_DEATH( |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 1755 | { |
| 1756 | loop->MakeRawWatcher(&channel_copy.message(), |
| 1757 | [](const Context, const void *) {}); |
| 1758 | }, |
| 1759 | "Channel pointer not found in configuration\\(\\)->channels\\(\\)"); |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 1760 | |
| 1761 | EXPECT_DEATH( |
| 1762 | { loop->MakeWatcher("/testbad", [](const TestMessage &) {}); }, |
| 1763 | "Channel \\{ \"name\": \"/testbad\", \"type\": \"aos.TestMessage\" \\}" |
| 1764 | " not found in config"); |
| 1765 | } |
| 1766 | |
| 1767 | // Verifies that the event loop handles a channel which is not readable or |
| 1768 | // writable on the current node nicely. |
| 1769 | TEST_P(AbstractEventLoopDeathTest, InaccessibleChannel) { |
| 1770 | EnableNodes("me"); |
| 1771 | auto loop = MakePrimary("me"); |
| 1772 | auto loop2 = Make("them"); |
| 1773 | |
| 1774 | const Channel *channel = configuration::GetChannel( |
| 1775 | loop->configuration(), "/test_noforward", "aos.TestMessage", "", nullptr); |
| 1776 | |
| 1777 | FlatbufferDetachedBuffer<Channel> channel_copy = CopyFlatBuffer(channel); |
| 1778 | |
| 1779 | EXPECT_DEATH( |
| 1780 | loop2->MakeSender<TestMessage>("/test_forward"), |
| 1781 | "Channel" |
| 1782 | " \\{ \"name\": \"/test_forward\", \"type\": \"aos.TestMessage\" \\}" |
| 1783 | " is not able to be sent on this node"); |
| 1784 | |
| 1785 | EXPECT_FALSE(loop2->TryMakeSender<TestMessage>("/test_forward")); |
| 1786 | |
| 1787 | EXPECT_DEATH( |
| 1788 | loop2->MakeRawFetcher(channel), |
| 1789 | "Channel" |
| 1790 | " \\{ \"name\": \"/test_noforward\", \"type\": \"aos.TestMessage\" \\}" |
| 1791 | " is not able to be fetched on this node"); |
| 1792 | |
| 1793 | EXPECT_DEATH( |
| 1794 | loop2->MakeFetcher<TestMessage>("/test_noforward"), |
| 1795 | "Channel" |
| 1796 | " \\{ \"name\": \"/test_noforward\", \"type\": \"aos.TestMessage\" \\}" |
| 1797 | " is not able to be fetched on this node"); |
| 1798 | |
| 1799 | EXPECT_FALSE(loop2->TryMakeFetcher<TestMessage>("/test_noforward").valid()); |
| 1800 | |
| 1801 | EXPECT_DEATH( |
| 1802 | { loop2->MakeRawWatcher(channel, [](const Context, const void *) {}); }, |
| 1803 | "\\{ \"name\": \"/test_noforward\", \"type\": \"aos.TestMessage\", " |
| 1804 | "\"source_node\": \"them\" \\}" |
| 1805 | " is not able to be watched on this node"); |
| 1806 | |
| 1807 | EXPECT_DEATH( |
| 1808 | { loop2->MakeWatcher("/test_noforward", [](const TestMessage &) {}); }, |
| 1809 | "\\{ \"name\": \"/test_noforward\", \"type\": \"aos.TestMessage\", " |
| 1810 | "\"source_node\": \"them\" \\}" |
| 1811 | " is not able to be watched on this node"); |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 1812 | } |
| 1813 | |
Austin Schuh | d54780b | 2020-10-03 16:26:02 -0700 | [diff] [blame] | 1814 | // Verifies that the event loop implementations detect when Channel has an |
| 1815 | // invalid alignment. |
| 1816 | TEST_P(AbstractEventLoopDeathTest, InvalidChannelAlignment) { |
| 1817 | const char *const kError = "multiple of alignment"; |
| 1818 | InvalidChannelAlignment(); |
| 1819 | |
| 1820 | auto loop = MakePrimary(); |
| 1821 | |
| 1822 | const Channel *channel = configuration::GetChannel( |
| 1823 | loop->configuration(), "/test", "aos.TestMessage", "", nullptr); |
| 1824 | |
| 1825 | EXPECT_DEATH({ loop->MakeRawSender(channel); }, kError); |
| 1826 | EXPECT_DEATH({ loop->MakeSender<TestMessage>("/test"); }, kError); |
| 1827 | |
| 1828 | EXPECT_DEATH({ loop->MakeRawFetcher(channel); }, kError); |
| 1829 | EXPECT_DEATH({ loop->MakeFetcher<TestMessage>("/test"); }, kError); |
| 1830 | |
| 1831 | EXPECT_DEATH( |
| 1832 | { loop->MakeRawWatcher(channel, [](const Context &, const void *) {}); }, |
| 1833 | kError); |
| 1834 | EXPECT_DEATH({ loop->MakeRawNoArgWatcher(channel, [](const Context &) {}); }, |
| 1835 | kError); |
| 1836 | |
| 1837 | EXPECT_DEATH({ loop->MakeNoArgWatcher<TestMessage>("/test", []() {}); }, |
| 1838 | kError); |
| 1839 | EXPECT_DEATH({ loop->MakeWatcher("/test", [](const TestMessage &) {}); }, |
| 1840 | kError); |
| 1841 | } |
| 1842 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1843 | // Verify that the send time on a message is roughly right when using a watcher. |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1844 | TEST_P(AbstractEventLoopTest, MessageSendTime) { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 1845 | auto loop1 = MakePrimary(); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1846 | auto loop2 = Make(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1847 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1848 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 1849 | |
| 1850 | auto test_timer = loop1->AddTimer([&sender]() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1851 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 1852 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 1853 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1854 | msg.CheckOk(msg.Send(builder.Finish())); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1855 | }); |
| 1856 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1857 | bool triggered = false; |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1858 | loop1->MakeWatcher("/test", [&](const TestMessage &msg) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1859 | // Confirm that the data pointer makes sense from a watcher, and all the |
| 1860 | // timestamps look right. |
| 1861 | EXPECT_GT(&msg, loop1->context().data); |
| 1862 | EXPECT_EQ(loop1->context().monotonic_remote_time, |
| 1863 | loop1->context().monotonic_event_time); |
| 1864 | EXPECT_EQ(loop1->context().realtime_remote_time, |
| 1865 | loop1->context().realtime_event_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1866 | EXPECT_EQ(loop1->context().source_boot_uuid, loop1->boot_uuid()); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1867 | |
| 1868 | const aos::monotonic_clock::time_point monotonic_now = |
| 1869 | loop1->monotonic_now(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1870 | const aos::realtime_clock::time_point realtime_now = loop1->realtime_now(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1871 | |
| 1872 | EXPECT_LE(loop1->context().monotonic_event_time, monotonic_now); |
| 1873 | EXPECT_LE(loop1->context().realtime_event_time, realtime_now); |
| 1874 | EXPECT_GE(loop1->context().monotonic_event_time + chrono::milliseconds(500), |
| 1875 | monotonic_now); |
| 1876 | EXPECT_GE(loop1->context().realtime_event_time + chrono::milliseconds(500), |
| 1877 | realtime_now); |
| 1878 | |
Brian Silverman | eaa41d6 | 2020-07-08 19:47:35 -0700 | [diff] [blame] | 1879 | EXPECT_LT(&msg, reinterpret_cast<const void *>( |
| 1880 | reinterpret_cast<const char *>(loop1->context().data) + |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1881 | loop1->context().size)); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 1882 | if (read_method() == ReadMethod::PIN) { |
| 1883 | EXPECT_GE(loop1->context().buffer_index, 0); |
| 1884 | EXPECT_LT(loop1->context().buffer_index, |
| 1885 | loop1->NumberBuffers( |
| 1886 | configuration::GetChannel(loop1->configuration(), "/test", |
| 1887 | "aos.TestMessage", "", nullptr))); |
| 1888 | } else { |
| 1889 | EXPECT_EQ(-1, loop1->context().buffer_index); |
| 1890 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1891 | triggered = true; |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1892 | }); |
| 1893 | |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 1894 | test_timer->Schedule(loop1->monotonic_now() + ::std::chrono::seconds(1)); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1895 | |
| 1896 | EndEventLoop(loop1.get(), ::std::chrono::seconds(2)); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 1897 | Run(); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1898 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1899 | EXPECT_TRUE(triggered); |
| 1900 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1901 | ASSERT_TRUE(fetcher.Fetch()); |
| 1902 | |
| 1903 | monotonic_clock::duration monotonic_time_offset = |
| 1904 | fetcher.context().monotonic_event_time - |
| 1905 | (loop1->monotonic_now() - ::std::chrono::seconds(1)); |
| 1906 | realtime_clock::duration realtime_time_offset = |
| 1907 | fetcher.context().realtime_event_time - |
| 1908 | (loop1->realtime_now() - ::std::chrono::seconds(1)); |
| 1909 | |
| 1910 | EXPECT_EQ(fetcher.context().realtime_event_time, |
| 1911 | fetcher.context().realtime_remote_time); |
| 1912 | EXPECT_EQ(fetcher.context().monotonic_event_time, |
| 1913 | fetcher.context().monotonic_remote_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1914 | EXPECT_EQ(fetcher.context().source_boot_uuid, loop1->boot_uuid()); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1915 | |
| 1916 | EXPECT_TRUE(monotonic_time_offset > ::std::chrono::milliseconds(-500)) |
| 1917 | << ": Got " |
| 1918 | << fetcher.context().monotonic_event_time.time_since_epoch().count() |
| 1919 | << " expected " << loop1->monotonic_now().time_since_epoch().count(); |
| 1920 | // Confirm that the data pointer makes sense. |
| 1921 | EXPECT_GT(fetcher.get(), fetcher.context().data); |
| 1922 | EXPECT_LT(fetcher.get(), |
Brian Silverman | eaa41d6 | 2020-07-08 19:47:35 -0700 | [diff] [blame] | 1923 | reinterpret_cast<const void *>( |
| 1924 | reinterpret_cast<const char *>(fetcher.context().data) + |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1925 | fetcher.context().size)); |
| 1926 | EXPECT_TRUE(monotonic_time_offset < ::std::chrono::milliseconds(500)) |
| 1927 | << ": Got " |
| 1928 | << fetcher.context().monotonic_event_time.time_since_epoch().count() |
| 1929 | << " expected " << loop1->monotonic_now().time_since_epoch().count(); |
| 1930 | |
| 1931 | EXPECT_TRUE(realtime_time_offset > ::std::chrono::milliseconds(-500)) |
| 1932 | << ": Got " |
| 1933 | << fetcher.context().realtime_event_time.time_since_epoch().count() |
| 1934 | << " expected " << loop1->realtime_now().time_since_epoch().count(); |
| 1935 | EXPECT_TRUE(realtime_time_offset < ::std::chrono::milliseconds(500)) |
| 1936 | << ": Got " |
| 1937 | << fetcher.context().realtime_event_time.time_since_epoch().count() |
| 1938 | << " expected " << loop1->realtime_now().time_since_epoch().count(); |
| 1939 | } |
| 1940 | |
| 1941 | // Verify that the send time on a message is roughly right when using a no-arg |
| 1942 | // watcher. To get a message, we need to use a fetcher to actually access the |
| 1943 | // message. This is also the main use case for no-arg fetchers. |
| 1944 | TEST_P(AbstractEventLoopTest, MessageSendTimeNoArg) { |
| 1945 | auto loop1 = MakePrimary(); |
| 1946 | auto loop2 = Make(); |
| 1947 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 1948 | auto fetcher = loop1->MakeFetcher<TestMessage>("/test"); |
| 1949 | |
| 1950 | auto test_timer = loop1->AddTimer([&sender]() { |
| 1951 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 1952 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 1953 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1954 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1955 | }); |
| 1956 | |
| 1957 | bool triggered = false; |
| 1958 | loop1->MakeNoArgWatcher<TestMessage>("/test", [&]() { |
| 1959 | // Confirm that we can indeed use a fetcher on this channel from this |
| 1960 | // context, and it results in a sane data pointer and timestamps. |
| 1961 | ASSERT_TRUE(fetcher.Fetch()); |
| 1962 | |
| 1963 | EXPECT_EQ(loop1->context().monotonic_remote_time, |
| 1964 | loop1->context().monotonic_event_time); |
| 1965 | EXPECT_EQ(loop1->context().realtime_remote_time, |
| 1966 | loop1->context().realtime_event_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1967 | EXPECT_EQ(loop1->context().source_boot_uuid, loop1->boot_uuid()); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1968 | |
| 1969 | const aos::monotonic_clock::time_point monotonic_now = |
| 1970 | loop1->monotonic_now(); |
| 1971 | const aos::realtime_clock::time_point realtime_now = loop1->realtime_now(); |
| 1972 | |
| 1973 | EXPECT_LE(loop1->context().monotonic_event_time, monotonic_now); |
| 1974 | EXPECT_LE(loop1->context().realtime_event_time, realtime_now); |
| 1975 | EXPECT_GE(loop1->context().monotonic_event_time + chrono::milliseconds(500), |
| 1976 | monotonic_now); |
| 1977 | EXPECT_GE(loop1->context().realtime_event_time + chrono::milliseconds(500), |
| 1978 | realtime_now); |
| 1979 | |
| 1980 | triggered = true; |
| 1981 | }); |
| 1982 | |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 1983 | test_timer->Schedule(loop1->monotonic_now() + ::std::chrono::seconds(1)); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1984 | |
| 1985 | EndEventLoop(loop1.get(), ::std::chrono::seconds(2)); |
| 1986 | Run(); |
| 1987 | |
| 1988 | ASSERT_TRUE(triggered); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1989 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1990 | monotonic_clock::duration monotonic_time_offset = |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1991 | fetcher.context().monotonic_event_time - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1992 | (loop1->monotonic_now() - ::std::chrono::seconds(1)); |
| 1993 | realtime_clock::duration realtime_time_offset = |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1994 | fetcher.context().realtime_event_time - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1995 | (loop1->realtime_now() - ::std::chrono::seconds(1)); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1996 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1997 | EXPECT_EQ(fetcher.context().realtime_event_time, |
| 1998 | fetcher.context().realtime_remote_time); |
| 1999 | EXPECT_EQ(fetcher.context().monotonic_event_time, |
| 2000 | fetcher.context().monotonic_remote_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 2001 | EXPECT_EQ(fetcher.context().source_boot_uuid, loop1->boot_uuid()); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2002 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 2003 | EXPECT_TRUE(monotonic_time_offset > ::std::chrono::milliseconds(-500)) |
| 2004 | << ": Got " |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2005 | << fetcher.context().monotonic_event_time.time_since_epoch().count() |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 2006 | << " expected " << loop1->monotonic_now().time_since_epoch().count(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 2007 | // Confirm that the data pointer makes sense. |
| 2008 | EXPECT_GT(fetcher.get(), fetcher.context().data); |
| 2009 | EXPECT_LT(fetcher.get(), |
Brian Silverman | eaa41d6 | 2020-07-08 19:47:35 -0700 | [diff] [blame] | 2010 | reinterpret_cast<const void *>( |
| 2011 | reinterpret_cast<const char *>(fetcher.context().data) + |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 2012 | fetcher.context().size)); |
| 2013 | EXPECT_TRUE(monotonic_time_offset < ::std::chrono::milliseconds(500)) |
| 2014 | << ": Got " |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2015 | << fetcher.context().monotonic_event_time.time_since_epoch().count() |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 2016 | << " expected " << loop1->monotonic_now().time_since_epoch().count(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 2017 | |
| 2018 | EXPECT_TRUE(realtime_time_offset > ::std::chrono::milliseconds(-500)) |
| 2019 | << ": Got " |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2020 | << fetcher.context().realtime_event_time.time_since_epoch().count() |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 2021 | << " expected " << loop1->realtime_now().time_since_epoch().count(); |
| 2022 | EXPECT_TRUE(realtime_time_offset < ::std::chrono::milliseconds(500)) |
| 2023 | << ": Got " |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2024 | << fetcher.context().realtime_event_time.time_since_epoch().count() |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 2025 | << " expected " << loop1->realtime_now().time_since_epoch().count(); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 2026 | } |
| 2027 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 2028 | // Tests that a couple phased loops run in a row result in the correct offset |
| 2029 | // and period. |
| 2030 | TEST_P(AbstractEventLoopTest, PhasedLoopTest) { |
Stephan Pleines | 3dce7ea | 2021-06-22 13:19:26 -0700 | [diff] [blame] | 2031 | // Force a slower rate so we are guaranteed to have reports for our phased |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2032 | // loop. |
| 2033 | FLAGS_timing_report_ms = 2000; |
| 2034 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 2035 | const chrono::milliseconds kOffset = chrono::milliseconds(400); |
| 2036 | const int kCount = 5; |
| 2037 | |
| 2038 | auto loop1 = MakePrimary(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2039 | auto loop2 = Make(); |
| 2040 | |
| 2041 | Fetcher<timing::Report> report_fetcher = |
| 2042 | loop2->MakeFetcher<timing::Report>("/aos"); |
| 2043 | EXPECT_FALSE(report_fetcher.Fetch()); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 2044 | |
| 2045 | // Collect up a couple of samples. |
| 2046 | ::std::vector<::aos::monotonic_clock::time_point> times; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 2047 | ::std::vector<::aos::monotonic_clock::time_point> expected_times; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 2048 | |
| 2049 | // Run kCount iterations. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2050 | loop1 |
| 2051 | ->AddPhasedLoop( |
| 2052 | [×, &expected_times, &loop1, this](int count) { |
| 2053 | EXPECT_EQ(count, 1); |
| 2054 | times.push_back(loop1->monotonic_now()); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2055 | expected_times.push_back(loop1->context().monotonic_event_time); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2056 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2057 | EXPECT_EQ(loop1->context().monotonic_remote_time, |
| 2058 | monotonic_clock::min_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 2059 | EXPECT_EQ(loop1->context().source_boot_uuid, loop1->boot_uuid()); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2060 | EXPECT_EQ(loop1->context().realtime_event_time, |
| 2061 | realtime_clock::min_time); |
| 2062 | EXPECT_EQ(loop1->context().realtime_remote_time, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2063 | realtime_clock::min_time); |
| 2064 | EXPECT_EQ(loop1->context().queue_index, 0xffffffffu); |
| 2065 | EXPECT_EQ(loop1->context().size, 0u); |
| 2066 | EXPECT_EQ(loop1->context().data, nullptr); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 2067 | EXPECT_EQ(loop1->context().buffer_index, -1); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2068 | |
| 2069 | if (times.size() == kCount) { |
| 2070 | LOG(INFO) << "Exiting"; |
| 2071 | this->Exit(); |
| 2072 | } |
| 2073 | }, |
| 2074 | chrono::seconds(1), kOffset) |
| 2075 | ->set_name("Test loop"); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 2076 | |
| 2077 | // Add a delay to make sure that delay during startup doesn't result in a |
| 2078 | // "missed cycle". |
| 2079 | SleepFor(chrono::seconds(2)); |
| 2080 | |
| 2081 | Run(); |
| 2082 | |
| 2083 | // Confirm that we got both the right number of samples, and it's odd. |
Stephan Pleines | 3dce7ea | 2021-06-22 13:19:26 -0700 | [diff] [blame] | 2084 | ASSERT_EQ(times.size(), static_cast<size_t>(kCount)); |
| 2085 | ASSERT_EQ(times.size(), expected_times.size()); |
| 2086 | ASSERT_EQ((times.size() % 2), 1); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 2087 | |
| 2088 | // Grab the middle sample. |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 2089 | ::aos::monotonic_clock::time_point average_time = times[times.size() / 2]; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 2090 | |
| 2091 | // Add up all the delays of all the times. |
| 2092 | ::aos::monotonic_clock::duration sum = chrono::seconds(0); |
| 2093 | for (const ::aos::monotonic_clock::time_point time : times) { |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 2094 | sum += time - average_time; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | // Average and add to the middle to find the average time. |
| 2098 | sum /= times.size(); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 2099 | average_time += sum; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 2100 | |
| 2101 | // Compute the offset from the start of the second of the average time. This |
| 2102 | // should be pretty close to the offset. |
| 2103 | const ::aos::monotonic_clock::duration remainder = |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 2104 | average_time.time_since_epoch() - |
| 2105 | chrono::duration_cast<chrono::seconds>(average_time.time_since_epoch()); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 2106 | |
| 2107 | const chrono::milliseconds kEpsilon(100); |
| 2108 | EXPECT_LT(remainder, kOffset + kEpsilon); |
| 2109 | EXPECT_GT(remainder, kOffset - kEpsilon); |
| 2110 | |
| 2111 | // Make sure that the average duration is close to 1 second. |
| 2112 | EXPECT_NEAR(chrono::duration_cast<chrono::duration<double>>(times.back() - |
| 2113 | times.front()) |
| 2114 | .count() / |
| 2115 | static_cast<double>(times.size() - 1), |
| 2116 | 1.0, 0.1); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 2117 | |
| 2118 | // Confirm that the ideal wakeup times increment correctly. |
| 2119 | for (size_t i = 1; i < expected_times.size(); ++i) { |
| 2120 | EXPECT_EQ(expected_times[i], expected_times[i - 1] + chrono::seconds(1)); |
| 2121 | } |
| 2122 | |
| 2123 | for (size_t i = 0; i < expected_times.size(); ++i) { |
| 2124 | EXPECT_EQ(expected_times[i].time_since_epoch() % chrono::seconds(1), |
| 2125 | kOffset); |
| 2126 | } |
| 2127 | |
| 2128 | EXPECT_LT(expected_times[expected_times.size() / 2], average_time + kEpsilon); |
| 2129 | EXPECT_GT(expected_times[expected_times.size() / 2], average_time - kEpsilon); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2130 | |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 2131 | if (do_timing_reports() == DoTimingReports::kYes) { |
| 2132 | // And, since we are here, check that the timing report makes sense. |
| 2133 | // Start by looking for our event loop's timing. |
| 2134 | FlatbufferDetachedBuffer<timing::Report> report = |
| 2135 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 2136 | while (report_fetcher.FetchNext()) { |
| 2137 | if (report_fetcher->name()->string_view() == "primary") { |
| 2138 | report = CopyFlatBuffer(report_fetcher.get()); |
| 2139 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2140 | } |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 2141 | |
| 2142 | VLOG(1) << FlatbufferToJson(report, {.multi_line = true}); |
| 2143 | |
| 2144 | EXPECT_EQ(report.message().name()->string_view(), "primary"); |
| 2145 | |
| 2146 | ASSERT_NE(report.message().senders(), nullptr); |
| 2147 | EXPECT_EQ(report.message().senders()->size(), 2); |
| 2148 | |
| 2149 | ASSERT_NE(report.message().timers(), nullptr); |
| 2150 | EXPECT_EQ(report.message().timers()->size(), 1); |
| 2151 | |
| 2152 | // Make sure there is a single phased loop report with our report in it. |
| 2153 | ASSERT_NE(report.message().phased_loops(), nullptr); |
| 2154 | ASSERT_EQ(report.message().phased_loops()->size(), 1); |
| 2155 | EXPECT_EQ(report.message().phased_loops()->Get(0)->name()->string_view(), |
| 2156 | "Test loop"); |
| 2157 | EXPECT_GE(report.message().phased_loops()->Get(0)->count(), 1); |
| 2158 | } else { |
| 2159 | ASSERT_FALSE(report_fetcher.Fetch()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2160 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2161 | } |
| 2162 | |
Milind Upadhyay | 42589bb | 2021-05-19 20:05:16 -0700 | [diff] [blame] | 2163 | // Tests that a phased loop responds correctly to a changing offset. |
| 2164 | TEST_P(AbstractEventLoopTest, PhasedLoopChangingOffsetTest) { |
| 2165 | // Force a slower rate so we are guaranteed to have reports for our phased |
| 2166 | // loop. |
| 2167 | FLAGS_timing_report_ms = 2000; |
| 2168 | |
| 2169 | const chrono::milliseconds kOffset = chrono::milliseconds(400); |
| 2170 | const chrono::milliseconds kInterval = chrono::milliseconds(1000); |
| 2171 | const int kCount = 5; |
| 2172 | |
| 2173 | auto loop1 = MakePrimary(); |
| 2174 | |
| 2175 | // Collect up a couple of samples. |
| 2176 | ::std::vector<::aos::monotonic_clock::time_point> times; |
| 2177 | ::std::vector<::aos::monotonic_clock::time_point> expected_times; |
| 2178 | |
| 2179 | PhasedLoopHandler *phased_loop; |
| 2180 | |
| 2181 | // Run kCount iterations. |
| 2182 | phased_loop = loop1->AddPhasedLoop( |
| 2183 | [&phased_loop, ×, &expected_times, &loop1, this, kOffset, |
| 2184 | kInterval](int count) { |
| 2185 | EXPECT_EQ(count, 1); |
| 2186 | times.push_back(loop1->monotonic_now()); |
| 2187 | |
| 2188 | expected_times.push_back(loop1->context().monotonic_event_time); |
| 2189 | |
| 2190 | phased_loop->set_interval_and_offset( |
| 2191 | kInterval, kOffset - chrono::milliseconds(times.size())); |
| 2192 | LOG(INFO) << "new offset: " |
| 2193 | << (kOffset - chrono::milliseconds(times.size())).count(); |
| 2194 | |
| 2195 | if (times.size() == kCount) { |
| 2196 | LOG(INFO) << "Exiting"; |
| 2197 | this->Exit(); |
| 2198 | } |
| 2199 | }, |
| 2200 | kInterval, kOffset); |
| 2201 | phased_loop->set_name("Test loop"); |
| 2202 | |
| 2203 | // Add a delay to make sure that delay during startup doesn't result in a |
| 2204 | // "missed cycle". |
| 2205 | SleepFor(chrono::seconds(2)); |
| 2206 | |
| 2207 | Run(); |
| 2208 | // Confirm that we got both the right number of samples, and it's odd. |
| 2209 | EXPECT_EQ(times.size(), static_cast<size_t>(kCount)); |
| 2210 | EXPECT_EQ(times.size(), expected_times.size()); |
| 2211 | EXPECT_EQ((times.size() % 2), 1); |
| 2212 | |
| 2213 | // Grab the middle sample. |
| 2214 | ::aos::monotonic_clock::time_point average_time = times[times.size() / 2]; |
| 2215 | |
| 2216 | // Add up all the delays of all the times. |
| 2217 | ::aos::monotonic_clock::duration sum = chrono::seconds(0); |
| 2218 | for (const ::aos::monotonic_clock::time_point time : times) { |
| 2219 | sum += time - average_time; |
| 2220 | } |
| 2221 | |
| 2222 | // Average and add to the middle to find the average time. |
| 2223 | sum /= times.size(); |
| 2224 | average_time += sum; |
| 2225 | |
| 2226 | // Compute the offset from the start of the second of the average time. This |
| 2227 | // should be pretty close to the offset. |
| 2228 | const ::aos::monotonic_clock::duration remainder = |
| 2229 | average_time.time_since_epoch() - |
| 2230 | chrono::duration_cast<chrono::seconds>(average_time.time_since_epoch()); |
| 2231 | |
| 2232 | const chrono::milliseconds kEpsilon(100); |
| 2233 | EXPECT_LT(remainder, kOffset + kEpsilon); |
| 2234 | EXPECT_GT(remainder, kOffset - kEpsilon); |
| 2235 | |
| 2236 | // Make sure that the average duration is close to 1 second. |
| 2237 | EXPECT_NEAR(chrono::duration_cast<chrono::duration<double>>(times.back() - |
| 2238 | times.front()) |
| 2239 | .count() / |
| 2240 | static_cast<double>(times.size() - 1), |
| 2241 | 1.0, 0.1); |
| 2242 | |
| 2243 | // Confirm that the ideal wakeup times increment correctly. |
| 2244 | for (size_t i = 1; i < expected_times.size(); ++i) { |
| 2245 | LOG(INFO) << i - 1 << ": " << expected_times[i - 1] << ", " << i << ": " |
| 2246 | << expected_times[i]; |
| 2247 | EXPECT_EQ(expected_times[i], expected_times[i - 1] + chrono::seconds(1) - |
| 2248 | chrono::milliseconds(1)); |
| 2249 | } |
| 2250 | |
| 2251 | for (size_t i = 0; i < expected_times.size(); ++i) { |
| 2252 | EXPECT_EQ(expected_times[i].time_since_epoch() % chrono::seconds(1), |
| 2253 | kOffset - chrono::milliseconds(i)); |
| 2254 | } |
| 2255 | |
| 2256 | EXPECT_LT(expected_times[expected_times.size() / 2], average_time + kEpsilon); |
| 2257 | EXPECT_GT(expected_times[expected_times.size() / 2], average_time - kEpsilon); |
| 2258 | } |
| 2259 | |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame] | 2260 | // Tests that a phased loop responds correctly to a changing offset; sweep |
| 2261 | // across a variety of potential offset changes, to ensure that we are |
| 2262 | // exercising a variety of potential cases. |
| 2263 | TEST_P(AbstractEventLoopTest, PhasedLoopChangingOffsetSweep) { |
| 2264 | const chrono::milliseconds kInterval = chrono::milliseconds(1000); |
| 2265 | const int kCount = 5; |
| 2266 | |
| 2267 | auto loop1 = MakePrimary(); |
| 2268 | |
| 2269 | std::vector<aos::monotonic_clock::duration> offset_options; |
| 2270 | for (int ii = 0; ii < kCount; ++ii) { |
| 2271 | offset_options.push_back(ii * kInterval / kCount); |
| 2272 | } |
| 2273 | std::vector<aos::monotonic_clock::duration> offset_sweep; |
| 2274 | // Run over all the pair-wise combinations of offsets. |
| 2275 | for (int ii = 0; ii < kCount; ++ii) { |
| 2276 | for (int jj = 0; jj < kCount; ++jj) { |
| 2277 | offset_sweep.push_back(offset_options.at(ii)); |
| 2278 | offset_sweep.push_back(offset_options.at(jj)); |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | std::vector<::aos::monotonic_clock::time_point> expected_times; |
| 2283 | |
| 2284 | PhasedLoopHandler *phased_loop; |
| 2285 | |
| 2286 | // Run kCount iterations. |
| 2287 | size_t counter = 0; |
| 2288 | phased_loop = loop1->AddPhasedLoop( |
| 2289 | [&phased_loop, &expected_times, &loop1, this, kInterval, &counter, |
| 2290 | offset_sweep](int count) { |
| 2291 | EXPECT_EQ(count, 1); |
| 2292 | expected_times.push_back(loop1->context().monotonic_event_time); |
| 2293 | |
| 2294 | counter++; |
| 2295 | |
| 2296 | if (counter == offset_sweep.size()) { |
| 2297 | LOG(INFO) << "Exiting"; |
| 2298 | this->Exit(); |
| 2299 | return; |
| 2300 | } |
| 2301 | |
| 2302 | phased_loop->set_interval_and_offset(kInterval, |
| 2303 | offset_sweep.at(counter)); |
| 2304 | }, |
| 2305 | kInterval, offset_sweep.at(0)); |
| 2306 | |
| 2307 | Run(); |
| 2308 | ASSERT_EQ(expected_times.size(), offset_sweep.size()); |
| 2309 | for (size_t ii = 1; ii < expected_times.size(); ++ii) { |
| 2310 | EXPECT_LE(expected_times.at(ii) - expected_times.at(ii - 1), kInterval); |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | // Tests that a phased loop responds correctly to being rescheduled with now |
| 2315 | // equal to a time in the past. |
| 2316 | TEST_P(AbstractEventLoopTest, PhasedLoopRescheduleInPast) { |
| 2317 | const chrono::milliseconds kOffset = chrono::milliseconds(400); |
| 2318 | const chrono::milliseconds kInterval = chrono::milliseconds(1000); |
| 2319 | |
| 2320 | auto loop1 = MakePrimary(); |
| 2321 | |
| 2322 | std::vector<::aos::monotonic_clock::time_point> expected_times; |
| 2323 | |
| 2324 | PhasedLoopHandler *phased_loop; |
| 2325 | |
| 2326 | int expected_count = 1; |
| 2327 | |
| 2328 | // Set up a timer that will get run immediately after the phased loop and |
| 2329 | // which will attempt to reschedule the phased loop to just before now. This |
| 2330 | // should succeed, but will result in 0 cycles elapsing. |
| 2331 | TimerHandler *manager_timer = |
| 2332 | loop1->AddTimer([&phased_loop, &loop1, &expected_count, this]() { |
| 2333 | if (expected_count == 0) { |
| 2334 | LOG(INFO) << "Exiting"; |
| 2335 | this->Exit(); |
| 2336 | return; |
| 2337 | } |
| 2338 | phased_loop->Reschedule(loop1->context().monotonic_event_time - |
| 2339 | std::chrono::nanoseconds(1)); |
| 2340 | expected_count = 0; |
| 2341 | }); |
| 2342 | |
| 2343 | phased_loop = loop1->AddPhasedLoop( |
| 2344 | [&expected_count, &expected_times, &loop1, manager_timer](int count) { |
| 2345 | EXPECT_EQ(count, expected_count); |
| 2346 | expected_times.push_back(loop1->context().monotonic_event_time); |
| 2347 | |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 2348 | manager_timer->Schedule(loop1->context().monotonic_event_time); |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame] | 2349 | }, |
| 2350 | kInterval, kOffset); |
| 2351 | phased_loop->set_name("Test loop"); |
| 2352 | manager_timer->set_name("Manager timer"); |
| 2353 | |
| 2354 | Run(); |
| 2355 | |
| 2356 | ASSERT_EQ(2u, expected_times.size()); |
| 2357 | ASSERT_EQ(expected_times[0], expected_times[1]); |
| 2358 | } |
| 2359 | |
| 2360 | // Tests that a phased loop responds correctly to being rescheduled at the time |
| 2361 | // when it should be triggering (it should kick the trigger to the next cycle). |
| 2362 | TEST_P(AbstractEventLoopTest, PhasedLoopRescheduleNow) { |
| 2363 | const chrono::milliseconds kOffset = chrono::milliseconds(400); |
| 2364 | const chrono::milliseconds kInterval = chrono::milliseconds(1000); |
| 2365 | |
| 2366 | auto loop1 = MakePrimary(); |
| 2367 | |
| 2368 | std::vector<::aos::monotonic_clock::time_point> expected_times; |
| 2369 | |
| 2370 | PhasedLoopHandler *phased_loop; |
| 2371 | |
| 2372 | bool should_exit = false; |
| 2373 | // Set up a timer that will get run immediately after the phased loop and |
| 2374 | // which will attempt to reschedule the phased loop to now. This should |
| 2375 | // succeed, but will result in no change to the expected behavior (since this |
| 2376 | // is the same thing that is actually done internally). |
| 2377 | TimerHandler *manager_timer = |
| 2378 | loop1->AddTimer([&phased_loop, &loop1, &should_exit, this]() { |
| 2379 | if (should_exit) { |
| 2380 | LOG(INFO) << "Exiting"; |
| 2381 | this->Exit(); |
| 2382 | return; |
| 2383 | } |
| 2384 | phased_loop->Reschedule(loop1->context().monotonic_event_time); |
| 2385 | should_exit = true; |
| 2386 | }); |
| 2387 | |
| 2388 | phased_loop = loop1->AddPhasedLoop( |
| 2389 | [&expected_times, &loop1, manager_timer](int count) { |
| 2390 | EXPECT_EQ(count, 1); |
| 2391 | expected_times.push_back(loop1->context().monotonic_event_time); |
| 2392 | |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 2393 | manager_timer->Schedule(loop1->context().monotonic_event_time); |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame] | 2394 | }, |
| 2395 | kInterval, kOffset); |
| 2396 | phased_loop->set_name("Test loop"); |
| 2397 | manager_timer->set_name("Manager timer"); |
| 2398 | |
| 2399 | Run(); |
| 2400 | |
| 2401 | ASSERT_EQ(2u, expected_times.size()); |
| 2402 | ASSERT_EQ(expected_times[0] + kInterval, expected_times[1]); |
| 2403 | } |
| 2404 | |
| 2405 | // Tests that a phased loop responds correctly to being rescheduled at a time in |
| 2406 | // the distant future. |
| 2407 | TEST_P(AbstractEventLoopTest, PhasedLoopRescheduleFuture) { |
| 2408 | const chrono::milliseconds kOffset = chrono::milliseconds(400); |
| 2409 | const chrono::milliseconds kInterval = chrono::milliseconds(1000); |
| 2410 | |
| 2411 | auto loop1 = MakePrimary(); |
| 2412 | |
| 2413 | std::vector<::aos::monotonic_clock::time_point> expected_times; |
| 2414 | |
| 2415 | PhasedLoopHandler *phased_loop; |
| 2416 | |
| 2417 | bool should_exit = false; |
| 2418 | int expected_count = 1; |
| 2419 | TimerHandler *manager_timer = loop1->AddTimer( |
| 2420 | [&expected_count, &phased_loop, &loop1, &should_exit, this, kInterval]() { |
| 2421 | if (should_exit) { |
| 2422 | LOG(INFO) << "Exiting"; |
| 2423 | this->Exit(); |
| 2424 | return; |
| 2425 | } |
| 2426 | expected_count = 10; |
| 2427 | // Knock off 1 ns, since the scheduler rounds up when it is |
| 2428 | // scheduled to exactly a loop time. |
| 2429 | phased_loop->Reschedule(loop1->context().monotonic_event_time + |
| 2430 | kInterval * expected_count - |
| 2431 | std::chrono::nanoseconds(1)); |
| 2432 | should_exit = true; |
| 2433 | }); |
| 2434 | |
| 2435 | phased_loop = loop1->AddPhasedLoop( |
| 2436 | [&expected_times, &loop1, manager_timer, &expected_count](int count) { |
| 2437 | EXPECT_EQ(count, expected_count); |
| 2438 | expected_times.push_back(loop1->context().monotonic_event_time); |
| 2439 | |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 2440 | manager_timer->Schedule(loop1->context().monotonic_event_time); |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame] | 2441 | }, |
| 2442 | kInterval, kOffset); |
| 2443 | phased_loop->set_name("Test loop"); |
| 2444 | manager_timer->set_name("Manager timer"); |
| 2445 | |
| 2446 | Run(); |
| 2447 | |
| 2448 | ASSERT_EQ(2u, expected_times.size()); |
| 2449 | ASSERT_EQ(expected_times[0] + expected_count * kInterval, expected_times[1]); |
| 2450 | } |
| 2451 | |
| 2452 | // Tests that a phased loop responds correctly to having its phase offset |
| 2453 | // incremented and then being scheduled after a set time, exercising a pattern |
| 2454 | // where a phased loop's offset is changed while trying to maintain the trigger |
| 2455 | // at a consistent period. |
| 2456 | TEST_P(AbstractEventLoopTest, PhasedLoopRescheduleWithLaterOffset) { |
| 2457 | const chrono::milliseconds kOffset = chrono::milliseconds(400); |
| 2458 | const chrono::milliseconds kInterval = chrono::milliseconds(1000); |
| 2459 | |
| 2460 | auto loop1 = MakePrimary(); |
| 2461 | |
| 2462 | std::vector<::aos::monotonic_clock::time_point> expected_times; |
| 2463 | |
| 2464 | PhasedLoopHandler *phased_loop; |
| 2465 | |
| 2466 | bool should_exit = false; |
| 2467 | TimerHandler *manager_timer = loop1->AddTimer( |
| 2468 | [&phased_loop, &loop1, &should_exit, this, kInterval, kOffset]() { |
| 2469 | if (should_exit) { |
| 2470 | LOG(INFO) << "Exiting"; |
| 2471 | this->Exit(); |
| 2472 | return; |
| 2473 | } |
| 2474 | // Schedule the next callback to be strictly later than the current time |
| 2475 | // + interval / 2, to ensure a consistent frequency. |
| 2476 | monotonic_clock::time_point half_time = |
| 2477 | loop1->context().monotonic_event_time + kInterval / 2; |
| 2478 | phased_loop->set_interval_and_offset( |
| 2479 | kInterval, kOffset + std::chrono::nanoseconds(1), half_time); |
| 2480 | phased_loop->Reschedule(half_time); |
| 2481 | should_exit = true; |
| 2482 | }); |
| 2483 | |
| 2484 | phased_loop = loop1->AddPhasedLoop( |
| 2485 | [&expected_times, &loop1, manager_timer](int count) { |
| 2486 | EXPECT_EQ(1, count); |
| 2487 | expected_times.push_back(loop1->context().monotonic_event_time); |
| 2488 | |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 2489 | manager_timer->Schedule(loop1->context().monotonic_event_time); |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame] | 2490 | }, |
| 2491 | kInterval, kOffset); |
| 2492 | phased_loop->set_name("Test loop"); |
| 2493 | manager_timer->set_name("Manager timer"); |
| 2494 | |
| 2495 | Run(); |
| 2496 | |
| 2497 | ASSERT_EQ(2u, expected_times.size()); |
| 2498 | ASSERT_EQ(expected_times[0] + kInterval + std::chrono::nanoseconds(1), |
| 2499 | expected_times[1]); |
| 2500 | } |
| 2501 | |
| 2502 | // Tests that a phased loop responds correctly to having its phase offset |
| 2503 | // decremented and then being scheduled after a set time, exercising a pattern |
| 2504 | // where a phased loop's offset is changed while trying to maintain the trigger |
| 2505 | // at a consistent period. |
| 2506 | TEST_P(AbstractEventLoopTest, PhasedLoopRescheduleWithEarlierOffset) { |
| 2507 | const chrono::milliseconds kOffset = chrono::milliseconds(400); |
| 2508 | const chrono::milliseconds kInterval = chrono::milliseconds(1000); |
| 2509 | |
| 2510 | auto loop1 = MakePrimary(); |
| 2511 | |
| 2512 | std::vector<::aos::monotonic_clock::time_point> expected_times; |
| 2513 | |
| 2514 | PhasedLoopHandler *phased_loop; |
| 2515 | |
| 2516 | bool should_exit = false; |
| 2517 | TimerHandler *manager_timer = loop1->AddTimer( |
| 2518 | [&phased_loop, &loop1, &should_exit, this, kInterval, kOffset]() { |
| 2519 | if (should_exit) { |
| 2520 | LOG(INFO) << "Exiting"; |
| 2521 | this->Exit(); |
| 2522 | return; |
| 2523 | } |
| 2524 | // Schedule the next callback to be strictly later than the current time |
| 2525 | // + interval / 2, to ensure a consistent frequency. |
| 2526 | const aos::monotonic_clock::time_point half_time = |
| 2527 | loop1->context().monotonic_event_time + kInterval / 2; |
| 2528 | phased_loop->set_interval_and_offset( |
| 2529 | kInterval, kOffset - std::chrono::nanoseconds(1), half_time); |
| 2530 | phased_loop->Reschedule(half_time); |
| 2531 | should_exit = true; |
| 2532 | }); |
| 2533 | |
| 2534 | phased_loop = loop1->AddPhasedLoop( |
| 2535 | [&expected_times, &loop1, manager_timer](int count) { |
| 2536 | EXPECT_EQ(1, count); |
| 2537 | expected_times.push_back(loop1->context().monotonic_event_time); |
| 2538 | |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 2539 | manager_timer->Schedule(loop1->context().monotonic_event_time); |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame] | 2540 | }, |
| 2541 | kInterval, kOffset); |
| 2542 | phased_loop->set_name("Test loop"); |
| 2543 | manager_timer->set_name("Manager timer"); |
| 2544 | |
| 2545 | Run(); |
| 2546 | |
| 2547 | ASSERT_EQ(2u, expected_times.size()); |
| 2548 | ASSERT_EQ(expected_times[0] + kInterval - std::chrono::nanoseconds(1), |
| 2549 | expected_times[1]); |
| 2550 | } |
| 2551 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2552 | // Tests that senders count correctly in the timing report. |
| 2553 | TEST_P(AbstractEventLoopTest, SenderTimingReport) { |
| 2554 | FLAGS_timing_report_ms = 1000; |
| 2555 | auto loop1 = MakePrimary(); |
| 2556 | |
| 2557 | auto loop2 = Make("watcher_loop"); |
| 2558 | loop2->MakeWatcher("/test", [](const TestMessage &) {}); |
| 2559 | |
| 2560 | auto loop3 = Make(); |
| 2561 | |
| 2562 | Fetcher<timing::Report> report_fetcher = |
| 2563 | loop3->MakeFetcher<timing::Report>("/aos"); |
| 2564 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 2565 | |
| 2566 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 2567 | |
James Kuszmaul | 7851433 | 2022-04-06 15:08:34 -0700 | [diff] [blame] | 2568 | // Sanity check channel frequencies to ensure that we've designed the test |
| 2569 | // correctly. |
| 2570 | ASSERT_EQ(800, sender.channel()->frequency()); |
Austin Schuh | fff9c3a | 2023-06-16 18:48:23 -0700 | [diff] [blame] | 2571 | ASSERT_EQ(2000000000, configuration::ChannelStorageDuration( |
| 2572 | loop1->configuration(), sender.channel()) |
| 2573 | .count()); |
James Kuszmaul | 7851433 | 2022-04-06 15:08:34 -0700 | [diff] [blame] | 2574 | constexpr int kMaxAllowedMessages = 800 * 2; |
| 2575 | constexpr int kSendMessages = kMaxAllowedMessages * 2; |
| 2576 | constexpr int kDroppedMessages = kSendMessages - kMaxAllowedMessages; |
| 2577 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2578 | // Add a timer to actually quit. |
| 2579 | auto test_timer = loop1->AddTimer([&sender]() { |
James Kuszmaul | 7851433 | 2022-04-06 15:08:34 -0700 | [diff] [blame] | 2580 | for (int i = 0; i < kSendMessages; ++i) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2581 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 2582 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 2583 | builder.add_value(200 + i); |
James Kuszmaul | 7851433 | 2022-04-06 15:08:34 -0700 | [diff] [blame] | 2584 | if (i < kMaxAllowedMessages) { |
| 2585 | msg.CheckOk(msg.Send(builder.Finish())); |
| 2586 | } else { |
| 2587 | EXPECT_EQ(RawSender::Error::kMessagesSentTooFast, |
| 2588 | msg.Send(builder.Finish())); |
| 2589 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2590 | } |
| 2591 | }); |
| 2592 | |
| 2593 | // Quit after 1 timing report, mid way through the next cycle. |
| 2594 | EndEventLoop(loop1.get(), chrono::milliseconds(2500)); |
| 2595 | |
| 2596 | loop1->OnRun([&test_timer, &loop1]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 2597 | test_timer->Schedule(loop1->monotonic_now() + chrono::milliseconds(1500)); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2598 | }); |
| 2599 | |
| 2600 | Run(); |
| 2601 | |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 2602 | if (do_timing_reports() == DoTimingReports::kYes) { |
| 2603 | // And, since we are here, check that the timing report makes sense. |
| 2604 | // Start by looking for our event loop's timing. |
| 2605 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 2606 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 2607 | while (report_fetcher.FetchNext()) { |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 2608 | VLOG(1) << "Report " << FlatbufferToJson(report_fetcher.get()); |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 2609 | if (report_fetcher->name()->string_view() == "primary") { |
| 2610 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 2611 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2612 | } |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 2613 | |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 2614 | VLOG(1) << FlatbufferToJson(primary_report, {.multi_line = true}); |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 2615 | |
| 2616 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 2617 | |
| 2618 | ASSERT_NE(primary_report.message().senders(), nullptr); |
| 2619 | EXPECT_EQ(primary_report.message().senders()->size(), 3); |
| 2620 | |
| 2621 | // Confirm that the sender looks sane. |
| 2622 | EXPECT_EQ( |
| 2623 | loop1->configuration() |
| 2624 | ->channels() |
| 2625 | ->Get(primary_report.message().senders()->Get(0)->channel_index()) |
| 2626 | ->name() |
| 2627 | ->string_view(), |
| 2628 | "/test"); |
James Kuszmaul | 7851433 | 2022-04-06 15:08:34 -0700 | [diff] [blame] | 2629 | EXPECT_EQ(primary_report.message().senders()->Get(0)->count(), |
| 2630 | kMaxAllowedMessages); |
| 2631 | ASSERT_TRUE(primary_report.message().senders()->Get(0)->has_error_counts()); |
| 2632 | ASSERT_EQ( |
| 2633 | primary_report.message().senders()->Get(0)->error_counts()->size(), 2u); |
| 2634 | EXPECT_EQ( |
| 2635 | primary_report.message() |
| 2636 | .senders() |
| 2637 | ->Get(0) |
| 2638 | ->error_counts() |
| 2639 | ->Get(static_cast<size_t>(timing::SendError::MESSAGE_SENT_TOO_FAST)) |
| 2640 | ->count(), |
| 2641 | kDroppedMessages) |
| 2642 | << aos::FlatbufferToJson(primary_report); |
| 2643 | EXPECT_EQ(primary_report.message() |
| 2644 | .senders() |
| 2645 | ->Get(0) |
| 2646 | ->error_counts() |
| 2647 | ->Get(static_cast<size_t>(timing::SendError::INVALID_REDZONE)) |
| 2648 | ->count(), |
| 2649 | 0); |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 2650 | |
| 2651 | // Confirm that the timing primary_report sender looks sane. |
| 2652 | EXPECT_EQ( |
| 2653 | loop1->configuration() |
| 2654 | ->channels() |
| 2655 | ->Get(primary_report.message().senders()->Get(1)->channel_index()) |
| 2656 | ->name() |
| 2657 | ->string_view(), |
| 2658 | "/aos"); |
| 2659 | EXPECT_EQ(primary_report.message().senders()->Get(1)->count(), 1); |
| 2660 | |
| 2661 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 2662 | EXPECT_EQ(primary_report.message().timers()->size(), 3); |
| 2663 | |
| 2664 | // Make sure there are no phased loops or watchers. |
| 2665 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 2666 | ASSERT_EQ(primary_report.message().watchers(), nullptr); |
| 2667 | } else { |
| 2668 | ASSERT_FALSE(report_fetcher.Fetch()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2669 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2670 | } |
| 2671 | |
James Kuszmaul | 93abac1 | 2022-04-14 15:05:10 -0700 | [diff] [blame] | 2672 | // Tests that the RawSender::Send(void*, size_t) overload tracks things properly |
| 2673 | // in its timing report. |
| 2674 | TEST_P(AbstractEventLoopTest, CopySenderTimingReport) { |
| 2675 | gflags::FlagSaver flag_saver; |
| 2676 | FLAGS_timing_report_ms = 1000; |
| 2677 | auto loop1 = Make(); |
| 2678 | auto loop2 = MakePrimary(); |
| 2679 | |
| 2680 | const FlatbufferDetachedBuffer<TestMessage> kMessage = |
| 2681 | JsonToFlatbuffer<TestMessage>("{}"); |
| 2682 | |
| 2683 | std::unique_ptr<aos::RawSender> sender = |
| 2684 | loop2->MakeRawSender(configuration::GetChannel( |
| 2685 | loop2->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
| 2686 | |
| 2687 | Fetcher<timing::Report> report_fetcher = |
| 2688 | loop1->MakeFetcher<timing::Report>("/aos"); |
| 2689 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 2690 | |
| 2691 | loop2->OnRun([&]() { |
| 2692 | for (int ii = 0; ii < TestChannelQueueSize(loop2.get()); ++ii) { |
| 2693 | EXPECT_EQ(sender->Send(kMessage.span().data(), kMessage.span().size()), |
| 2694 | RawSender::Error::kOk); |
| 2695 | } |
| 2696 | EXPECT_EQ(sender->Send(kMessage.span().data(), kMessage.span().size()), |
| 2697 | RawSender::Error::kMessagesSentTooFast); |
| 2698 | }); |
| 2699 | // Quit after 1 timing report, mid way through the next cycle. |
| 2700 | EndEventLoop(loop2.get(), chrono::milliseconds(1500)); |
| 2701 | |
| 2702 | Run(); |
| 2703 | |
| 2704 | if (do_timing_reports() == DoTimingReports::kYes) { |
| 2705 | // Check that the sent too fast actually got recorded by the timing report. |
| 2706 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 2707 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 2708 | while (report_fetcher.FetchNext()) { |
| 2709 | if (report_fetcher->name()->string_view() == "primary") { |
| 2710 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 2711 | } |
| 2712 | } |
| 2713 | |
| 2714 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 2715 | |
| 2716 | ASSERT_NE(primary_report.message().senders(), nullptr); |
| 2717 | EXPECT_EQ(primary_report.message().senders()->size(), 3); |
| 2718 | EXPECT_EQ( |
| 2719 | primary_report.message() |
| 2720 | .senders() |
| 2721 | ->Get(0) |
| 2722 | ->error_counts() |
| 2723 | ->Get(static_cast<size_t>(timing::SendError::MESSAGE_SENT_TOO_FAST)) |
| 2724 | ->count(), |
| 2725 | 1); |
| 2726 | } |
| 2727 | } |
| 2728 | |
Austin Schuh | e0ab4de | 2023-05-03 08:05:08 -0700 | [diff] [blame] | 2729 | // Tests that the RawSender::Send(SharedSpan) overload works. |
| 2730 | TEST_P(AbstractEventLoopTest, SharedSenderTimingReport) { |
| 2731 | gflags::FlagSaver flag_saver; |
| 2732 | FLAGS_timing_report_ms = 1000; |
| 2733 | auto loop1 = Make(); |
| 2734 | auto loop2 = MakePrimary(); |
| 2735 | |
| 2736 | const FlatbufferDetachedBuffer<TestMessage> kMessage = |
| 2737 | JsonToFlatbuffer<TestMessage>("{}"); |
| 2738 | |
| 2739 | std::unique_ptr<aos::RawSender> sender = |
| 2740 | loop2->MakeRawSender(configuration::GetChannel( |
| 2741 | loop2->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
| 2742 | |
| 2743 | Fetcher<timing::Report> report_fetcher = |
| 2744 | loop1->MakeFetcher<timing::Report>("/aos"); |
| 2745 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 2746 | |
| 2747 | loop2->OnRun([&]() { |
| 2748 | for (int ii = 0; ii < TestChannelQueueSize(loop2.get()); ++ii) { |
| 2749 | auto shared_span = MakeSharedSpan(kMessage.span().size()); |
| 2750 | memcpy(shared_span.second.data(), kMessage.span().data(), |
| 2751 | kMessage.span().size()); |
| 2752 | EXPECT_EQ(sender->Send(std::move(shared_span.first)), |
| 2753 | RawSender::Error::kOk); |
| 2754 | } |
| 2755 | auto shared_span = MakeSharedSpan(kMessage.span().size()); |
| 2756 | memcpy(shared_span.second.data(), kMessage.span().data(), |
| 2757 | kMessage.span().size()); |
| 2758 | EXPECT_EQ(sender->Send(std::move(shared_span.first)), |
| 2759 | RawSender::Error::kMessagesSentTooFast); |
| 2760 | }); |
| 2761 | // Quit after 1 timing report, mid way through the next cycle. |
| 2762 | EndEventLoop(loop2.get(), chrono::milliseconds(1500)); |
| 2763 | |
| 2764 | Run(); |
| 2765 | |
| 2766 | if (do_timing_reports() == DoTimingReports::kYes) { |
| 2767 | // Check that the sent too fast actually got recorded by the timing report. |
| 2768 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 2769 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 2770 | while (report_fetcher.FetchNext()) { |
| 2771 | if (report_fetcher->name()->string_view() == "primary") { |
| 2772 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 2773 | } |
| 2774 | } |
| 2775 | |
| 2776 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 2777 | |
| 2778 | ASSERT_NE(primary_report.message().senders(), nullptr); |
| 2779 | EXPECT_EQ(primary_report.message().senders()->size(), 3); |
| 2780 | EXPECT_EQ( |
| 2781 | primary_report.message() |
| 2782 | .senders() |
| 2783 | ->Get(0) |
| 2784 | ->error_counts() |
| 2785 | ->Get(static_cast<size_t>(timing::SendError::MESSAGE_SENT_TOO_FAST)) |
| 2786 | ->count(), |
| 2787 | 1); |
| 2788 | } |
| 2789 | } |
| 2790 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2791 | // Tests that senders count correctly in the timing report. |
| 2792 | TEST_P(AbstractEventLoopTest, WatcherTimingReport) { |
| 2793 | FLAGS_timing_report_ms = 1000; |
| 2794 | auto loop1 = MakePrimary(); |
| 2795 | loop1->MakeWatcher("/test", [](const TestMessage &) {}); |
| 2796 | |
| 2797 | auto loop2 = Make("sender_loop"); |
| 2798 | |
| 2799 | auto loop3 = Make(); |
| 2800 | |
| 2801 | Fetcher<timing::Report> report_fetcher = |
| 2802 | loop3->MakeFetcher<timing::Report>("/aos"); |
| 2803 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 2804 | |
| 2805 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 2806 | |
| 2807 | // Add a timer to actually quit. |
| 2808 | auto test_timer = loop1->AddTimer([&sender]() { |
| 2809 | for (int i = 0; i < 10; ++i) { |
| 2810 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 2811 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 2812 | builder.add_value(200 + i); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2813 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2814 | } |
| 2815 | }); |
| 2816 | |
| 2817 | // Quit after 1 timing report, mid way through the next cycle. |
| 2818 | EndEventLoop(loop1.get(), chrono::milliseconds(2500)); |
| 2819 | |
| 2820 | loop1->OnRun([&test_timer, &loop1]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 2821 | test_timer->Schedule(loop1->monotonic_now() + chrono::milliseconds(1500)); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2822 | }); |
| 2823 | |
| 2824 | Run(); |
| 2825 | |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 2826 | if (do_timing_reports() == DoTimingReports::kYes) { |
| 2827 | // And, since we are here, check that the timing report makes sense. |
| 2828 | // Start by looking for our event loop's timing. |
| 2829 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 2830 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 2831 | while (report_fetcher.FetchNext()) { |
| 2832 | LOG(INFO) << "Report " << FlatbufferToJson(report_fetcher.get()); |
| 2833 | if (report_fetcher->name()->string_view() == "primary") { |
| 2834 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 2835 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2836 | } |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 2837 | |
| 2838 | // Check the watcher report. |
| 2839 | VLOG(1) << FlatbufferToJson(primary_report, {.multi_line = true}); |
| 2840 | |
| 2841 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 2842 | |
| 2843 | // Just the timing report timer. |
| 2844 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 2845 | EXPECT_EQ(primary_report.message().timers()->size(), 3); |
| 2846 | |
| 2847 | // No phased loops |
| 2848 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 2849 | |
| 2850 | ASSERT_NE(primary_report.message().watchers(), nullptr); |
| 2851 | ASSERT_EQ(primary_report.message().watchers()->size(), 1); |
| 2852 | EXPECT_EQ(primary_report.message().watchers()->Get(0)->count(), 10); |
| 2853 | } else { |
| 2854 | ASSERT_FALSE(report_fetcher.Fetch()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2855 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | // Tests that fetchers count correctly in the timing report. |
| 2859 | TEST_P(AbstractEventLoopTest, FetcherTimingReport) { |
| 2860 | FLAGS_timing_report_ms = 1000; |
| 2861 | auto loop1 = MakePrimary(); |
| 2862 | auto loop2 = Make("sender_loop"); |
| 2863 | |
| 2864 | auto loop3 = Make(); |
| 2865 | |
| 2866 | Fetcher<timing::Report> report_fetcher = |
| 2867 | loop3->MakeFetcher<timing::Report>("/aos"); |
| 2868 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 2869 | |
| 2870 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 2871 | auto fetcher1 = loop1->MakeFetcher<TestMessage>("/test"); |
| 2872 | auto fetcher2 = loop1->MakeFetcher<TestMessage>("/test"); |
| 2873 | fetcher1.Fetch(); |
| 2874 | fetcher2.Fetch(); |
| 2875 | |
| 2876 | // Add a timer to actually quit. |
| 2877 | auto test_timer = loop1->AddTimer([&sender]() { |
| 2878 | for (int i = 0; i < 10; ++i) { |
| 2879 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 2880 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 2881 | builder.add_value(200 + i); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2882 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2883 | } |
| 2884 | }); |
| 2885 | |
| 2886 | auto test_timer2 = loop1->AddTimer([&fetcher1, &fetcher2]() { |
| 2887 | fetcher1.Fetch(); |
| 2888 | while (fetcher2.FetchNext()) { |
| 2889 | } |
| 2890 | }); |
| 2891 | |
| 2892 | // Quit after 1 timing report, mid way through the next cycle. |
| 2893 | EndEventLoop(loop1.get(), chrono::milliseconds(2500)); |
| 2894 | |
| 2895 | loop1->OnRun([test_timer, test_timer2, &loop1]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 2896 | test_timer->Schedule(loop1->monotonic_now() + chrono::milliseconds(1400)); |
| 2897 | test_timer2->Schedule(loop1->monotonic_now() + chrono::milliseconds(1600)); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2898 | }); |
| 2899 | |
| 2900 | Run(); |
| 2901 | |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 2902 | if (do_timing_reports() == DoTimingReports::kYes) { |
| 2903 | // And, since we are here, check that the timing report makes sense. |
| 2904 | // Start by looking for our event loop's timing. |
| 2905 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 2906 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 2907 | while (report_fetcher.FetchNext()) { |
| 2908 | if (report_fetcher->name()->string_view() == "primary") { |
| 2909 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 2910 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2911 | } |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 2912 | |
| 2913 | VLOG(1) << FlatbufferToJson(primary_report, {.multi_line = true}); |
| 2914 | |
| 2915 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 2916 | |
| 2917 | ASSERT_NE(primary_report.message().senders(), nullptr); |
| 2918 | EXPECT_EQ(primary_report.message().senders()->size(), 2); |
| 2919 | |
| 2920 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 2921 | EXPECT_EQ(primary_report.message().timers()->size(), 4); |
| 2922 | |
| 2923 | // Make sure there are no phased loops or watchers. |
| 2924 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 2925 | ASSERT_EQ(primary_report.message().watchers(), nullptr); |
| 2926 | |
| 2927 | // Now look at the fetchrs. |
| 2928 | ASSERT_NE(primary_report.message().fetchers(), nullptr); |
| 2929 | ASSERT_EQ(primary_report.message().fetchers()->size(), 2); |
| 2930 | |
| 2931 | EXPECT_EQ(primary_report.message().fetchers()->Get(0)->count(), 1); |
| 2932 | EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->average(), |
| 2933 | 0.1); |
| 2934 | EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->min(), |
| 2935 | 0.1); |
| 2936 | EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->max(), |
| 2937 | 0.1); |
| 2938 | EXPECT_EQ(primary_report.message() |
| 2939 | .fetchers() |
| 2940 | ->Get(0) |
| 2941 | ->latency() |
| 2942 | ->standard_deviation(), |
| 2943 | 0.0); |
| 2944 | |
| 2945 | EXPECT_EQ(primary_report.message().fetchers()->Get(1)->count(), 10); |
| 2946 | } else { |
| 2947 | ASSERT_FALSE(report_fetcher.Fetch()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2948 | } |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 2949 | } |
| 2950 | |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2951 | // Tests that a raw watcher and raw fetcher can receive messages from a raw |
| 2952 | // sender without messing up offsets. |
| 2953 | TEST_P(AbstractEventLoopTest, RawBasic) { |
| 2954 | auto loop1 = Make(); |
| 2955 | auto loop2 = MakePrimary(); |
| 2956 | auto loop3 = Make(); |
| 2957 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2958 | const FlatbufferDetachedBuffer<TestMessage> kMessage = |
| 2959 | JsonToFlatbuffer<TestMessage>("{}"); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2960 | |
| 2961 | std::unique_ptr<aos::RawSender> sender = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2962 | loop1->MakeRawSender(configuration::GetChannel( |
| 2963 | loop1->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2964 | |
| 2965 | std::unique_ptr<aos::RawFetcher> fetcher = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2966 | loop3->MakeRawFetcher(configuration::GetChannel( |
| 2967 | loop3->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2968 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2969 | loop2->OnRun([&]() { |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2970 | EXPECT_EQ(sender->Send(kMessage.span().data(), kMessage.span().size()), |
| 2971 | RawSender::Error::kOk); |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2972 | }); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2973 | |
| 2974 | bool happened = false; |
| 2975 | loop2->MakeRawWatcher( |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2976 | configuration::GetChannel(loop2->configuration(), "/test", |
| 2977 | "aos.TestMessage", "", nullptr), |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2978 | [this, &kMessage, &fetcher, &happened](const Context &context, |
| 2979 | const void *message) { |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2980 | happened = true; |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2981 | EXPECT_EQ( |
| 2982 | kMessage.span(), |
| 2983 | absl::Span<const uint8_t>( |
| 2984 | reinterpret_cast<const uint8_t *>(message), context.size)); |
| 2985 | EXPECT_EQ(message, context.data); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2986 | |
| 2987 | ASSERT_TRUE(fetcher->Fetch()); |
| 2988 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2989 | EXPECT_EQ(kMessage.span(), |
| 2990 | absl::Span<const uint8_t>(reinterpret_cast<const uint8_t *>( |
| 2991 | fetcher->context().data), |
| 2992 | fetcher->context().size)); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2993 | |
| 2994 | this->Exit(); |
| 2995 | }); |
| 2996 | |
| 2997 | EXPECT_FALSE(happened); |
| 2998 | Run(); |
| 2999 | EXPECT_TRUE(happened); |
| 3000 | } |
| 3001 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 3002 | // Tests that a raw watcher and raw fetcher can receive messages from a raw |
Brian Silverman | bf88992 | 2021-11-10 12:41:57 -0800 | [diff] [blame] | 3003 | // sender without messing up offsets, using the RawSpan overload. |
| 3004 | TEST_P(AbstractEventLoopTest, RawBasicSharedSpan) { |
| 3005 | auto loop1 = Make(); |
| 3006 | auto loop2 = MakePrimary(); |
| 3007 | auto loop3 = Make(); |
| 3008 | |
| 3009 | const FlatbufferDetachedBuffer<TestMessage> kMessage = |
| 3010 | JsonToFlatbuffer<TestMessage>("{}"); |
| 3011 | |
| 3012 | std::unique_ptr<aos::RawSender> sender = |
| 3013 | loop1->MakeRawSender(configuration::GetChannel( |
| 3014 | loop1->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
| 3015 | |
| 3016 | std::unique_ptr<aos::RawFetcher> fetcher = |
| 3017 | loop3->MakeRawFetcher(configuration::GetChannel( |
| 3018 | loop3->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
| 3019 | |
| 3020 | loop2->OnRun([&]() { |
Austin Schuh | e0ab4de | 2023-05-03 08:05:08 -0700 | [diff] [blame] | 3021 | auto shared_span = MakeSharedSpan(kMessage.span().size()); |
| 3022 | memcpy(shared_span.second.data(), kMessage.span().data(), |
| 3023 | kMessage.span().size()); |
| 3024 | sender->CheckOk(sender->Send(std::move(shared_span.first))); |
Brian Silverman | bf88992 | 2021-11-10 12:41:57 -0800 | [diff] [blame] | 3025 | }); |
| 3026 | |
| 3027 | bool happened = false; |
| 3028 | loop2->MakeRawWatcher( |
| 3029 | configuration::GetChannel(loop2->configuration(), "/test", |
| 3030 | "aos.TestMessage", "", nullptr), |
| 3031 | [this, &kMessage, &fetcher, &happened](const Context &context, |
| 3032 | const void *message) { |
| 3033 | happened = true; |
| 3034 | EXPECT_EQ( |
| 3035 | kMessage.span(), |
| 3036 | absl::Span<const uint8_t>( |
| 3037 | reinterpret_cast<const uint8_t *>(message), context.size)); |
| 3038 | EXPECT_EQ(message, context.data); |
| 3039 | |
| 3040 | ASSERT_TRUE(fetcher->Fetch()); |
| 3041 | |
| 3042 | EXPECT_EQ(kMessage.span(), |
| 3043 | absl::Span<const uint8_t>(reinterpret_cast<const uint8_t *>( |
| 3044 | fetcher->context().data), |
| 3045 | fetcher->context().size)); |
| 3046 | |
| 3047 | this->Exit(); |
| 3048 | }); |
| 3049 | |
| 3050 | EXPECT_FALSE(happened); |
| 3051 | Run(); |
| 3052 | EXPECT_TRUE(happened); |
| 3053 | } |
| 3054 | |
| 3055 | // Tests that a raw watcher and raw fetcher can receive messages from a raw |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 3056 | // sender with remote times filled out. |
| 3057 | TEST_P(AbstractEventLoopTest, RawRemoteTimes) { |
| 3058 | auto loop1 = Make(); |
| 3059 | auto loop2 = MakePrimary(); |
| 3060 | auto loop3 = Make(); |
| 3061 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 3062 | const FlatbufferDetachedBuffer<TestMessage> kMessage = |
| 3063 | JsonToFlatbuffer<TestMessage>("{}"); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 3064 | |
| 3065 | const aos::monotonic_clock::time_point monotonic_remote_time = |
| 3066 | aos::monotonic_clock::time_point(chrono::seconds(1501)); |
| 3067 | const aos::realtime_clock::time_point realtime_remote_time = |
| 3068 | aos::realtime_clock::time_point(chrono::seconds(3132)); |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 3069 | const uint32_t remote_queue_index = 0x254971; |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 3070 | const UUID source_boot_uuid = UUID::Random(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 3071 | |
| 3072 | std::unique_ptr<aos::RawSender> sender = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 3073 | loop1->MakeRawSender(configuration::GetChannel( |
| 3074 | loop1->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 3075 | |
| 3076 | std::unique_ptr<aos::RawFetcher> fetcher = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 3077 | loop3->MakeRawFetcher(configuration::GetChannel( |
| 3078 | loop3->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 3079 | |
| 3080 | loop2->OnRun([&]() { |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 3081 | EXPECT_EQ(sender->Send(kMessage.span().data(), kMessage.span().size(), |
| 3082 | monotonic_remote_time, realtime_remote_time, |
| 3083 | remote_queue_index, source_boot_uuid), |
| 3084 | RawSender::Error::kOk); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 3085 | }); |
| 3086 | |
| 3087 | bool happened = false; |
| 3088 | loop2->MakeRawWatcher( |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 3089 | configuration::GetChannel(loop2->configuration(), "/test", |
| 3090 | "aos.TestMessage", "", nullptr), |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 3091 | [this, monotonic_remote_time, realtime_remote_time, source_boot_uuid, |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 3092 | remote_queue_index, &fetcher, |
| 3093 | &happened](const Context &context, const void * /*message*/) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 3094 | happened = true; |
| 3095 | EXPECT_EQ(monotonic_remote_time, context.monotonic_remote_time); |
| 3096 | EXPECT_EQ(realtime_remote_time, context.realtime_remote_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 3097 | EXPECT_EQ(source_boot_uuid, context.source_boot_uuid); |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 3098 | EXPECT_EQ(remote_queue_index, context.remote_queue_index); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 3099 | |
| 3100 | ASSERT_TRUE(fetcher->Fetch()); |
| 3101 | EXPECT_EQ(monotonic_remote_time, |
| 3102 | fetcher->context().monotonic_remote_time); |
| 3103 | EXPECT_EQ(realtime_remote_time, |
| 3104 | fetcher->context().realtime_remote_time); |
| 3105 | |
| 3106 | this->Exit(); |
| 3107 | }); |
| 3108 | |
| 3109 | EXPECT_FALSE(happened); |
| 3110 | Run(); |
| 3111 | EXPECT_TRUE(happened); |
| 3112 | } |
| 3113 | |
| 3114 | // Tests that a raw sender fills out sent data. |
| 3115 | TEST_P(AbstractEventLoopTest, RawSenderSentData) { |
| 3116 | auto loop1 = MakePrimary(); |
| 3117 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 3118 | const FlatbufferDetachedBuffer<TestMessage> kMessage = |
| 3119 | JsonToFlatbuffer<TestMessage>("{}"); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 3120 | |
| 3121 | std::unique_ptr<aos::RawSender> sender = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 3122 | loop1->MakeRawSender(configuration::GetChannel( |
| 3123 | loop1->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 3124 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 3125 | const aos::monotonic_clock::time_point monotonic_now = loop1->monotonic_now(); |
| 3126 | const aos::realtime_clock::time_point realtime_now = loop1->realtime_now(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 3127 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 3128 | EXPECT_EQ(sender->Send(kMessage.span().data(), kMessage.span().size()), |
| 3129 | RawSender::Error::kOk); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 3130 | |
| 3131 | EXPECT_GE(sender->monotonic_sent_time(), monotonic_now); |
| 3132 | EXPECT_LE(sender->monotonic_sent_time(), |
| 3133 | monotonic_now + chrono::milliseconds(100)); |
| 3134 | EXPECT_GE(sender->realtime_sent_time(), realtime_now); |
| 3135 | EXPECT_LE(sender->realtime_sent_time(), |
| 3136 | realtime_now + chrono::milliseconds(100)); |
| 3137 | EXPECT_EQ(sender->sent_queue_index(), 0u); |
| 3138 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 3139 | EXPECT_EQ(sender->Send(kMessage.span().data(), kMessage.span().size()), |
| 3140 | RawSender::Error::kOk); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 3141 | |
| 3142 | EXPECT_GE(sender->monotonic_sent_time(), monotonic_now); |
| 3143 | EXPECT_LE(sender->monotonic_sent_time(), |
| 3144 | monotonic_now + chrono::milliseconds(100)); |
| 3145 | EXPECT_GE(sender->realtime_sent_time(), realtime_now); |
| 3146 | EXPECT_LE(sender->realtime_sent_time(), |
| 3147 | realtime_now + chrono::milliseconds(100)); |
| 3148 | EXPECT_EQ(sender->sent_queue_index(), 1u); |
| 3149 | } |
| 3150 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 3151 | // Tests that not setting up nodes results in no node. |
| 3152 | TEST_P(AbstractEventLoopTest, NoNode) { |
| 3153 | auto loop1 = Make(); |
| 3154 | auto loop2 = MakePrimary(); |
| 3155 | |
| 3156 | EXPECT_EQ(loop1->node(), nullptr); |
| 3157 | EXPECT_EQ(loop2->node(), nullptr); |
| 3158 | } |
| 3159 | |
| 3160 | // Tests that setting up nodes results in node being set. |
| 3161 | TEST_P(AbstractEventLoopTest, Node) { |
| 3162 | EnableNodes("me"); |
| 3163 | |
| 3164 | auto loop1 = Make(); |
| 3165 | auto loop2 = MakePrimary(); |
| 3166 | |
| 3167 | EXPECT_NE(loop1->node(), nullptr); |
| 3168 | EXPECT_NE(loop2->node(), nullptr); |
| 3169 | } |
| 3170 | |
| 3171 | // Tests that watchers work with a node setup. |
| 3172 | TEST_P(AbstractEventLoopTest, NodeWatcher) { |
| 3173 | EnableNodes("me"); |
| 3174 | |
| 3175 | auto loop1 = Make(); |
| 3176 | auto loop2 = Make(); |
| 3177 | loop1->MakeWatcher("/test", [](const TestMessage &) {}); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 3178 | loop2->MakeRawWatcher( |
| 3179 | configuration::GetChannel(configuration(), "/test", "aos.TestMessage", "", |
| 3180 | nullptr), |
| 3181 | [](const Context &, const void *) {}); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 3182 | } |
| 3183 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 3184 | // Tests that no-arg watchers work with a node setup. |
| 3185 | TEST_P(AbstractEventLoopTest, NodeNoArgWatcher) { |
| 3186 | EnableNodes("me"); |
| 3187 | |
| 3188 | auto loop1 = Make(); |
| 3189 | auto loop2 = Make(); |
| 3190 | loop1->MakeWatcher("/test", [](const TestMessage &) {}); |
| 3191 | loop2->MakeRawNoArgWatcher( |
| 3192 | configuration::GetChannel(configuration(), "/test", "aos.TestMessage", "", |
| 3193 | nullptr), |
| 3194 | [](const Context &) {}); |
| 3195 | } |
| 3196 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 3197 | // Tests that fetcher work with a node setup. |
| 3198 | TEST_P(AbstractEventLoopTest, NodeFetcher) { |
| 3199 | EnableNodes("me"); |
| 3200 | auto loop1 = Make(); |
| 3201 | |
| 3202 | auto fetcher = loop1->MakeFetcher<TestMessage>("/test"); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 3203 | auto raw_fetcher = loop1->MakeRawFetcher(configuration::GetChannel( |
| 3204 | configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 3205 | } |
| 3206 | |
| 3207 | // Tests that sender work with a node setup. |
| 3208 | TEST_P(AbstractEventLoopTest, NodeSender) { |
| 3209 | EnableNodes("me"); |
| 3210 | auto loop1 = Make(); |
| 3211 | |
| 3212 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
| 3213 | } |
| 3214 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 3215 | // Tests that a non-realtime event loop timer is marked non-realtime. |
| 3216 | TEST_P(AbstractEventLoopTest, NonRealtimeEventLoopTimer) { |
| 3217 | auto loop1 = MakePrimary(); |
| 3218 | |
| 3219 | // Add a timer to actually quit. |
| 3220 | auto test_timer = loop1->AddTimer([this]() { |
| 3221 | CheckNotRealtime(); |
| 3222 | this->Exit(); |
| 3223 | }); |
| 3224 | |
| 3225 | loop1->OnRun([&test_timer, &loop1]() { |
| 3226 | CheckNotRealtime(); |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 3227 | test_timer->Schedule(loop1->monotonic_now(), |
| 3228 | ::std::chrono::milliseconds(100)); |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 3229 | }); |
| 3230 | |
| 3231 | Run(); |
| 3232 | } |
| 3233 | |
| 3234 | // Tests that a realtime event loop timer is marked realtime. |
Austin Schuh | 9b1d628 | 2022-06-10 17:03:21 -0700 | [diff] [blame] | 3235 | TEST_P(AbstractEventLoopTest, RealtimeSend) { |
| 3236 | auto loop1 = MakePrimary(); |
| 3237 | |
| 3238 | loop1->SetRuntimeRealtimePriority(1); |
| 3239 | |
| 3240 | auto sender = loop1->MakeSender<TestMessage>("/test2"); |
| 3241 | |
| 3242 | loop1->OnRun([&]() { |
| 3243 | CheckRealtime(); |
| 3244 | |
| 3245 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 3246 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 3247 | builder.add_value(200); |
| 3248 | msg.CheckOk(msg.Send(builder.Finish())); |
| 3249 | |
| 3250 | this->Exit(); |
| 3251 | }); |
| 3252 | |
| 3253 | Run(); |
| 3254 | } |
| 3255 | |
| 3256 | // Tests that a realtime event loop timer is marked realtime. |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 3257 | TEST_P(AbstractEventLoopTest, RealtimeEventLoopTimer) { |
| 3258 | auto loop1 = MakePrimary(); |
| 3259 | |
| 3260 | loop1->SetRuntimeRealtimePriority(1); |
| 3261 | |
| 3262 | // Add a timer to actually quit. |
| 3263 | auto test_timer = loop1->AddTimer([this]() { |
| 3264 | CheckRealtime(); |
| 3265 | this->Exit(); |
| 3266 | }); |
| 3267 | |
| 3268 | loop1->OnRun([&test_timer, &loop1]() { |
| 3269 | CheckRealtime(); |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 3270 | test_timer->Schedule(loop1->monotonic_now(), |
| 3271 | ::std::chrono::milliseconds(100)); |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 3272 | }); |
| 3273 | |
| 3274 | Run(); |
| 3275 | } |
| 3276 | |
| 3277 | // Tests that a non-realtime event loop phased loop is marked non-realtime. |
| 3278 | TEST_P(AbstractEventLoopTest, NonRealtimeEventLoopPhasedLoop) { |
| 3279 | auto loop1 = MakePrimary(); |
| 3280 | |
| 3281 | // Add a timer to actually quit. |
| 3282 | loop1->AddPhasedLoop( |
| 3283 | [this](int) { |
| 3284 | CheckNotRealtime(); |
| 3285 | this->Exit(); |
| 3286 | }, |
| 3287 | chrono::seconds(1), chrono::seconds(0)); |
| 3288 | |
| 3289 | Run(); |
| 3290 | } |
| 3291 | |
| 3292 | // Tests that a realtime event loop phased loop is marked realtime. |
| 3293 | TEST_P(AbstractEventLoopTest, RealtimeEventLoopPhasedLoop) { |
| 3294 | auto loop1 = MakePrimary(); |
| 3295 | |
| 3296 | loop1->SetRuntimeRealtimePriority(1); |
| 3297 | |
| 3298 | // Add a timer to actually quit. |
| 3299 | loop1->AddPhasedLoop( |
| 3300 | [this](int) { |
| 3301 | CheckRealtime(); |
| 3302 | this->Exit(); |
| 3303 | }, |
| 3304 | chrono::seconds(1), chrono::seconds(0)); |
| 3305 | |
| 3306 | Run(); |
| 3307 | } |
| 3308 | |
| 3309 | // Tests that a non-realtime event loop watcher is marked non-realtime. |
| 3310 | TEST_P(AbstractEventLoopTest, NonRealtimeEventLoopWatcher) { |
| 3311 | auto loop1 = MakePrimary(); |
| 3312 | auto loop2 = Make(); |
| 3313 | |
| 3314 | aos::Sender<TestMessage> sender = loop2->MakeSender<TestMessage>("/test"); |
| 3315 | |
| 3316 | loop1->OnRun([&]() { |
| 3317 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 3318 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 3319 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 3320 | }); |
| 3321 | |
| 3322 | loop1->MakeWatcher("/test", [&](const TestMessage &) { |
| 3323 | CheckNotRealtime(); |
| 3324 | this->Exit(); |
| 3325 | }); |
| 3326 | |
| 3327 | Run(); |
| 3328 | } |
| 3329 | |
| 3330 | // Tests that a realtime event loop watcher is marked realtime. |
| 3331 | TEST_P(AbstractEventLoopTest, RealtimeEventLoopWatcher) { |
| 3332 | auto loop1 = MakePrimary(); |
| 3333 | auto loop2 = Make(); |
| 3334 | |
| 3335 | loop1->SetRuntimeRealtimePriority(1); |
| 3336 | |
| 3337 | aos::Sender<TestMessage> sender = loop2->MakeSender<TestMessage>("/test"); |
| 3338 | |
| 3339 | loop1->OnRun([&]() { |
| 3340 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 3341 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 3342 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 3343 | }); |
| 3344 | |
| 3345 | loop1->MakeWatcher("/test", [&](const TestMessage &) { |
| 3346 | CheckRealtime(); |
| 3347 | this->Exit(); |
| 3348 | }); |
| 3349 | |
| 3350 | Run(); |
| 3351 | } |
| 3352 | |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 3353 | // Tests that event loop's context's monotonic time is set to a value on OnRun. |
| 3354 | TEST_P(AbstractEventLoopTest, SetContextOnRun) { |
| 3355 | auto loop = MakePrimary(); |
| 3356 | |
Austin Schuh | 0debde1 | 2022-08-17 16:25:17 -0700 | [diff] [blame] | 3357 | EXPECT_EQ(loop->context().monotonic_event_time, monotonic_clock::min_time); |
| 3358 | EXPECT_EQ(loop->context().monotonic_remote_time, monotonic_clock::min_time); |
| 3359 | EXPECT_EQ(loop->context().realtime_event_time, realtime_clock::min_time); |
| 3360 | EXPECT_EQ(loop->context().realtime_remote_time, realtime_clock::min_time); |
| 3361 | EXPECT_EQ(loop->context().source_boot_uuid, loop->boot_uuid()); |
| 3362 | EXPECT_EQ(loop->context().queue_index, 0xffffffffu); |
| 3363 | EXPECT_EQ(loop->context().remote_queue_index, 0xffffffffu); |
| 3364 | EXPECT_EQ(loop->context().size, 0u); |
| 3365 | EXPECT_EQ(loop->context().data, nullptr); |
| 3366 | EXPECT_EQ(loop->context().buffer_index, -1); |
| 3367 | |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 3368 | // We want to check that monotonic event time is before monotonic now |
| 3369 | // called inside of callback, but after time point obtained callback. |
| 3370 | aos::monotonic_clock::time_point monotonic_event_time_on_run; |
| 3371 | |
| 3372 | loop->OnRun([&]() { |
| 3373 | monotonic_event_time_on_run = loop->context().monotonic_event_time; |
| 3374 | EXPECT_LE(monotonic_event_time_on_run, loop->monotonic_now()); |
| 3375 | EXPECT_EQ(loop->context().monotonic_remote_time, monotonic_clock::min_time); |
| 3376 | EXPECT_EQ(loop->context().realtime_event_time, realtime_clock::min_time); |
| 3377 | EXPECT_EQ(loop->context().realtime_remote_time, realtime_clock::min_time); |
| 3378 | EXPECT_EQ(loop->context().source_boot_uuid, loop->boot_uuid()); |
| 3379 | EXPECT_EQ(loop->context().queue_index, 0xffffffffu); |
Austin Schuh | 0debde1 | 2022-08-17 16:25:17 -0700 | [diff] [blame] | 3380 | EXPECT_EQ(loop->context().remote_queue_index, 0xffffffffu); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 3381 | EXPECT_EQ(loop->context().size, 0u); |
| 3382 | EXPECT_EQ(loop->context().data, nullptr); |
| 3383 | EXPECT_EQ(loop->context().buffer_index, -1); |
| 3384 | }); |
| 3385 | |
| 3386 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(200)); |
| 3387 | |
| 3388 | const aos::monotonic_clock::time_point before_run_time = |
| 3389 | loop->monotonic_now(); |
| 3390 | Run(); |
| 3391 | EXPECT_GE(monotonic_event_time_on_run, before_run_time); |
Austin Schuh | 0debde1 | 2022-08-17 16:25:17 -0700 | [diff] [blame] | 3392 | |
| 3393 | EXPECT_EQ(loop->context().monotonic_event_time, monotonic_clock::min_time); |
| 3394 | EXPECT_EQ(loop->context().monotonic_remote_time, monotonic_clock::min_time); |
| 3395 | EXPECT_EQ(loop->context().realtime_event_time, realtime_clock::min_time); |
| 3396 | EXPECT_EQ(loop->context().realtime_remote_time, realtime_clock::min_time); |
| 3397 | EXPECT_EQ(loop->context().source_boot_uuid, loop->boot_uuid()); |
| 3398 | EXPECT_EQ(loop->context().queue_index, 0xffffffffu); |
| 3399 | EXPECT_EQ(loop->context().remote_queue_index, 0xffffffffu); |
| 3400 | EXPECT_EQ(loop->context().size, 0u); |
| 3401 | EXPECT_EQ(loop->context().data, nullptr); |
| 3402 | EXPECT_EQ(loop->context().buffer_index, -1); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 3403 | } |
| 3404 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 3405 | // Tests that watchers fail when created on the wrong node. |
| 3406 | TEST_P(AbstractEventLoopDeathTest, NodeWatcher) { |
| 3407 | EnableNodes("them"); |
| 3408 | |
| 3409 | auto loop1 = Make(); |
| 3410 | auto loop2 = Make(); |
| 3411 | EXPECT_DEATH({ loop1->MakeWatcher("/test", [](const TestMessage &) {}); }, |
| 3412 | "node"); |
| 3413 | EXPECT_DEATH( |
| 3414 | { |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 3415 | loop2->MakeRawWatcher( |
| 3416 | configuration::GetChannel(configuration(), "/test", |
| 3417 | "aos.TestMessage", "", nullptr), |
| 3418 | [](const Context &, const void *) {}); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 3419 | }, |
| 3420 | "node"); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 3421 | EXPECT_DEATH({ loop1->MakeNoArgWatcher<TestMessage>("/test", []() {}); }, |
| 3422 | "node"); |
| 3423 | EXPECT_DEATH( |
| 3424 | { |
| 3425 | loop2->MakeRawNoArgWatcher( |
| 3426 | configuration::GetChannel(configuration(), "/test", |
| 3427 | "aos.TestMessage", "", nullptr), |
| 3428 | [](const Context &) {}); |
| 3429 | }, |
| 3430 | "node"); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 3431 | } |
| 3432 | |
| 3433 | // Tests that fetchers fail when created on the wrong node. |
| 3434 | TEST_P(AbstractEventLoopDeathTest, NodeFetcher) { |
| 3435 | EnableNodes("them"); |
| 3436 | auto loop1 = Make(); |
| 3437 | |
| 3438 | EXPECT_DEATH({ auto fetcher = loop1->MakeFetcher<TestMessage>("/test"); }, |
| 3439 | "node"); |
| 3440 | EXPECT_DEATH( |
| 3441 | { |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 3442 | auto raw_fetcher = loop1->MakeRawFetcher(configuration::GetChannel( |
| 3443 | configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 3444 | }, |
| 3445 | "node"); |
| 3446 | } |
| 3447 | |
| 3448 | // Tests that senders fail when created on the wrong node. |
| 3449 | TEST_P(AbstractEventLoopDeathTest, NodeSender) { |
| 3450 | EnableNodes("them"); |
| 3451 | auto loop1 = Make(); |
| 3452 | |
| 3453 | EXPECT_DEATH( |
| 3454 | { |
| 3455 | aos::Sender<TestMessage> sender = |
| 3456 | loop1->MakeSender<TestMessage>("/test"); |
| 3457 | }, |
| 3458 | "node"); |
| 3459 | |
| 3460 | // Note: Creating raw senders is always supported. Right now, this lets us |
| 3461 | // use them to create message_gateway. |
| 3462 | } |
| 3463 | |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 3464 | // Tests creating multiple Builders from a single Sender at the same time. |
| 3465 | TEST_P(AbstractEventLoopDeathTest, MultipleBuilders) { |
| 3466 | auto loop1 = Make(); |
| 3467 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
| 3468 | |
| 3469 | { auto builder = sender.MakeBuilder(); } |
| 3470 | { |
| 3471 | auto builder = sender.MakeBuilder(); |
| 3472 | builder.MakeBuilder<TestMessage>().Finish(); |
| 3473 | } |
| 3474 | { |
| 3475 | // Creating this after the first one was destroyed should be fine. |
| 3476 | auto builder = sender.MakeBuilder(); |
| 3477 | builder.MakeBuilder<TestMessage>().Finish(); |
| 3478 | // But not a second one. |
| 3479 | EXPECT_DEATH(sender.MakeBuilder().MakeBuilder<TestMessage>().Finish(), |
James Kuszmaul | b1c1105 | 2023-11-06 13:20:53 -0800 | [diff] [blame] | 3480 | "May not have multiple active allocators"); |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 3481 | } |
| 3482 | |
| 3483 | FlatbufferDetachedBuffer<TestMessage> detached = |
| 3484 | flatbuffers::DetachedBuffer(); |
| 3485 | { |
| 3486 | auto builder = sender.MakeBuilder(); |
| 3487 | detached = builder.Detach(builder.MakeBuilder<TestMessage>().Finish()); |
| 3488 | } |
| 3489 | { |
| 3490 | // This is the second one, after the detached one, so it should fail. |
| 3491 | EXPECT_DEATH(sender.MakeBuilder().MakeBuilder<TestMessage>().Finish(), |
James Kuszmaul | b1c1105 | 2023-11-06 13:20:53 -0800 | [diff] [blame] | 3492 | "May not have multiple active allocators"); |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 3493 | } |
| 3494 | |
| 3495 | // Clear the detached one, and then we should be able to create another. |
| 3496 | detached = flatbuffers::DetachedBuffer(); |
| 3497 | { |
| 3498 | auto builder = sender.MakeBuilder(); |
| 3499 | builder.MakeBuilder<TestMessage>().Finish(); |
| 3500 | } |
| 3501 | |
| 3502 | // And then detach another one. |
| 3503 | { |
| 3504 | auto builder = sender.MakeBuilder(); |
| 3505 | detached = builder.Detach(builder.MakeBuilder<TestMessage>().Finish()); |
| 3506 | } |
| 3507 | } |
| 3508 | |
| 3509 | // Tests sending a buffer detached from a different builder. |
| 3510 | TEST_P(AbstractEventLoopDeathTest, WrongDetachedBuffer) { |
| 3511 | auto loop1 = Make(); |
| 3512 | aos::Sender<TestMessage> sender1 = loop1->MakeSender<TestMessage>("/test"); |
| 3513 | aos::Sender<TestMessage> sender2 = loop1->MakeSender<TestMessage>("/test"); |
| 3514 | |
| 3515 | auto builder = sender1.MakeBuilder(); |
| 3516 | FlatbufferDetachedBuffer<TestMessage> detached = |
| 3517 | builder.Detach(builder.MakeBuilder<TestMessage>().Finish()); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 3518 | EXPECT_DEATH(sender2.CheckOk(sender2.SendDetached(std::move(detached))), |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 3519 | "May only send the buffer detached from this Sender"); |
| 3520 | } |
| 3521 | |
James Kuszmaul | 762e869 | 2023-07-31 14:57:53 -0700 | [diff] [blame] | 3522 | // Tests that senders fail when created on the wrong node. |
| 3523 | TEST_P(AbstractEventLoopDeathTest, SetVersionWhileRunning) { |
| 3524 | auto loop1 = MakePrimary(); |
| 3525 | |
| 3526 | loop1->OnRun([&loop1, this]() { |
| 3527 | EXPECT_DEATH({ loop1->SetVersionString("abcdef"); }, |
| 3528 | "timing report while running"); |
| 3529 | Exit(); |
| 3530 | }); |
| 3531 | |
| 3532 | Run(); |
| 3533 | } |
| 3534 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 3535 | int TestChannelFrequency(EventLoop *event_loop) { |
| 3536 | return event_loop->GetChannel<TestMessage>("/test")->frequency(); |
| 3537 | } |
| 3538 | |
| 3539 | int TestChannelQueueSize(EventLoop *event_loop) { |
Austin Schuh | fff9c3a | 2023-06-16 18:48:23 -0700 | [diff] [blame] | 3540 | return configuration::QueueSize(event_loop->configuration(), |
| 3541 | event_loop->GetChannel<TestMessage>("/test")); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 3542 | } |
| 3543 | |
| 3544 | RawSender::Error SendTestMessage(aos::Sender<TestMessage> &sender) { |
| 3545 | aos::Sender<TestMessage>::Builder builder = sender.MakeBuilder(); |
| 3546 | TestMessage::Builder test_message_builder = |
| 3547 | builder.MakeBuilder<TestMessage>(); |
| 3548 | test_message_builder.add_value(0); |
| 3549 | return builder.Send(test_message_builder.Finish()); |
| 3550 | } |
| 3551 | |
| 3552 | // Test that sending messages too fast returns |
| 3553 | // RawSender::Error::kMessagesSentTooFast. |
| 3554 | TEST_P(AbstractEventLoopTest, SendingMessagesTooFast) { |
| 3555 | auto event_loop = MakePrimary(); |
| 3556 | |
| 3557 | auto sender = event_loop->MakeSender<TestMessage>("/test"); |
| 3558 | |
| 3559 | // Send one message in the beginning, then wait until the |
| 3560 | // channel_storage_duration is almost done and start sending messages rapidly, |
| 3561 | // having some come in the next chanel_storage_duration. The queue_size is |
| 3562 | // 1600, so the 1601st message will be the last valid one (the initial message |
| 3563 | // having being sent more than a channel_storage_duration ago), and trying to |
| 3564 | // send the 1602nd message should return |
| 3565 | // RawSender::Error::kMessagesSentTooFast. |
| 3566 | EXPECT_EQ(SendTestMessage(sender), RawSender::Error::kOk); |
| 3567 | int msgs_sent = 1; |
| 3568 | const int queue_size = TestChannelQueueSize(event_loop.get()); |
| 3569 | |
| 3570 | const auto timer = event_loop->AddTimer([&]() { |
| 3571 | const bool done = (msgs_sent == queue_size + 1); |
| 3572 | ASSERT_EQ( |
| 3573 | SendTestMessage(sender), |
| 3574 | done ? RawSender::Error::kMessagesSentTooFast : RawSender::Error::kOk); |
| 3575 | msgs_sent++; |
| 3576 | if (done) { |
| 3577 | Exit(); |
| 3578 | } |
| 3579 | }); |
| 3580 | |
| 3581 | const auto kRepeatOffset = std::chrono::milliseconds(1); |
Austin Schuh | fff9c3a | 2023-06-16 18:48:23 -0700 | [diff] [blame] | 3582 | const auto base_offset = configuration::ChannelStorageDuration( |
| 3583 | event_loop->configuration(), sender.channel()) - |
| 3584 | (kRepeatOffset * (queue_size / 2)); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 3585 | event_loop->OnRun([&event_loop, &timer, &base_offset, &kRepeatOffset]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 3586 | timer->Schedule(event_loop->monotonic_now() + base_offset, kRepeatOffset); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 3587 | }); |
| 3588 | |
| 3589 | Run(); |
| 3590 | } |
| 3591 | |
| 3592 | // Tests that we are able to send messages successfully after sending messages |
| 3593 | // too fast and waiting while continuously attempting to send messages. |
| 3594 | // Also tests that SendFailureCounter is working correctly in this |
| 3595 | // situation |
| 3596 | TEST_P(AbstractEventLoopTest, SendingAfterSendingTooFast) { |
| 3597 | auto event_loop = MakePrimary(); |
| 3598 | |
| 3599 | auto sender = event_loop->MakeSender<TestMessage>("/test"); |
| 3600 | |
Austin Schuh | 0e96d37 | 2023-05-08 10:10:21 -0700 | [diff] [blame] | 3601 | // We are sending bunches of messages at 100 Hz, so we will be sending too |
| 3602 | // fast after queue_size (800) ms. After this, keep sending messages, and |
| 3603 | // exactly a channel storage duration (2s) after we send the first message we |
| 3604 | // should be able to successfully send a message. |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 3605 | |
Austin Schuh | 0e96d37 | 2023-05-08 10:10:21 -0700 | [diff] [blame] | 3606 | const std::chrono::milliseconds kInterval = std::chrono::milliseconds(10); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 3607 | const monotonic_clock::duration channel_storage_duration = |
Austin Schuh | fff9c3a | 2023-06-16 18:48:23 -0700 | [diff] [blame] | 3608 | configuration::ChannelStorageDuration(event_loop->configuration(), |
| 3609 | sender.channel()); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 3610 | const int queue_size = TestChannelQueueSize(event_loop.get()); |
| 3611 | |
| 3612 | int msgs_sent = 0; |
| 3613 | SendFailureCounter counter; |
| 3614 | auto start = monotonic_clock::min_time; |
| 3615 | |
| 3616 | event_loop->AddPhasedLoop( |
Austin Schuh | 0e96d37 | 2023-05-08 10:10:21 -0700 | [diff] [blame] | 3617 | [&](int elapsed_cycles) { |
| 3618 | // The queue is setup for 800 messages/sec. We want to fill that up at |
| 3619 | // a rate of 2000 messages/sec so we make sure we fill it up. |
| 3620 | for (int i = 0; i < 2 * kInterval.count() * elapsed_cycles; ++i) { |
| 3621 | const auto actual_err = SendTestMessage(sender); |
| 3622 | const bool done_waiting = (start != monotonic_clock::min_time && |
| 3623 | sender.monotonic_sent_time() >= |
| 3624 | (start + channel_storage_duration)); |
| 3625 | const auto expected_err = |
| 3626 | (msgs_sent < queue_size || done_waiting |
| 3627 | ? RawSender::Error::kOk |
| 3628 | : RawSender::Error::kMessagesSentTooFast); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 3629 | |
Austin Schuh | 0e96d37 | 2023-05-08 10:10:21 -0700 | [diff] [blame] | 3630 | if (start == monotonic_clock::min_time) { |
| 3631 | start = sender.monotonic_sent_time(); |
| 3632 | } |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 3633 | |
Austin Schuh | 0e96d37 | 2023-05-08 10:10:21 -0700 | [diff] [blame] | 3634 | ASSERT_EQ(actual_err, expected_err); |
| 3635 | counter.Count(actual_err); |
| 3636 | msgs_sent++; |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 3637 | |
Austin Schuh | 0e96d37 | 2023-05-08 10:10:21 -0700 | [diff] [blame] | 3638 | EXPECT_EQ(counter.failures(), |
| 3639 | msgs_sent <= queue_size |
| 3640 | ? 0 |
| 3641 | : (msgs_sent - queue_size) - |
| 3642 | (actual_err == RawSender::Error::kOk ? 1 : 0)); |
| 3643 | EXPECT_EQ(counter.just_failed(), actual_err != RawSender::Error::kOk); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 3644 | |
Austin Schuh | 0e96d37 | 2023-05-08 10:10:21 -0700 | [diff] [blame] | 3645 | if (done_waiting) { |
| 3646 | Exit(); |
| 3647 | return; |
| 3648 | } |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 3649 | } |
| 3650 | }, |
| 3651 | kInterval); |
| 3652 | Run(); |
| 3653 | } |
| 3654 | |
| 3655 | // Tests that RawSender::Error::kMessagesSentTooFast is returned |
| 3656 | // when messages are sent too fast from senders in different loops |
| 3657 | TEST_P(AbstractEventLoopTest, SendingTooFastWithMultipleLoops) { |
| 3658 | auto loop1 = MakePrimary(); |
| 3659 | auto loop2 = Make(); |
| 3660 | |
| 3661 | auto sender1 = loop1->MakeSender<TestMessage>("/test"); |
| 3662 | auto sender2 = loop2->MakeSender<TestMessage>("/test"); |
| 3663 | |
| 3664 | // Send queue_size messages split between the senders. |
| 3665 | const int queue_size = TestChannelQueueSize(loop1.get()); |
| 3666 | for (int i = 0; i < queue_size / 2; i++) { |
| 3667 | ASSERT_EQ(SendTestMessage(sender1), RawSender::Error::kOk); |
| 3668 | ASSERT_EQ(SendTestMessage(sender2), RawSender::Error::kOk); |
| 3669 | } |
| 3670 | |
| 3671 | // Since queue_size messages have been sent, this should return an error |
| 3672 | EXPECT_EQ(SendTestMessage(sender2), RawSender::Error::kMessagesSentTooFast); |
| 3673 | } |
| 3674 | |
Austin Schuh | dda6db7 | 2023-06-21 17:02:34 -0700 | [diff] [blame] | 3675 | // Tests that a longer storage durations store more messages. |
| 3676 | TEST_P(AbstractEventLoopTest, SendingTooFastWithLongDuration) { |
| 3677 | auto loop1 = MakePrimary(); |
| 3678 | |
| 3679 | auto sender1 = loop1->MakeSender<TestMessage>("/test3"); |
| 3680 | |
| 3681 | // Send queue_size messages split between the senders. |
| 3682 | const int queue_size = |
| 3683 | configuration::QueueSize(loop1->configuration(), sender1.channel()); |
| 3684 | EXPECT_EQ(queue_size, 100 * 10); |
| 3685 | for (int i = 0; i < queue_size; i++) { |
| 3686 | ASSERT_EQ(SendTestMessage(sender1), RawSender::Error::kOk); |
| 3687 | } |
| 3688 | |
| 3689 | // Since queue_size messages have been sent, and little time has elapsed, |
| 3690 | // this should return an error. |
| 3691 | EXPECT_EQ(SendTestMessage(sender1), RawSender::Error::kMessagesSentTooFast); |
| 3692 | } |
| 3693 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 3694 | } // namespace aos::testing |