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 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 7 | #include "aos/events/test_message_generated.h" |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 8 | #include "aos/flatbuffer_merge.h" |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 9 | #include "aos/logging/log_message_generated.h" |
| 10 | #include "aos/logging/logging.h" |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 11 | #include "aos/realtime.h" |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 12 | #include "glog/logging.h" |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 13 | #include "gmock/gmock.h" |
| 14 | #include "gtest/gtest.h" |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 15 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 16 | namespace aos { |
| 17 | namespace testing { |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 18 | namespace { |
| 19 | namespace chrono = ::std::chrono; |
| 20 | } // namespace |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 21 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 22 | ::std::unique_ptr<EventLoop> AbstractEventLoopTest::Make( |
| 23 | std::string_view name) { |
| 24 | std::string name_copy(name); |
| 25 | if (name == "") { |
| 26 | name_copy = "loop"; |
| 27 | name_copy += std::to_string(event_loop_count_); |
| 28 | } |
| 29 | ++event_loop_count_; |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 30 | auto result = factory_->Make(name_copy); |
| 31 | if (do_timing_reports() == DoTimingReports::kNo) { |
| 32 | result->SkipTimingReport(); |
| 33 | } |
| 34 | return result; |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void AbstractEventLoopTest::VerifyBuffers( |
| 38 | int number_buffers, |
| 39 | std::vector<std::reference_wrapper<const Fetcher<TestMessage>>> fetchers, |
| 40 | std::vector<std::reference_wrapper<const Sender<TestMessage>>> senders) { |
| 41 | // The buffers which are in a sender. |
| 42 | std::unordered_set<int> in_sender; |
| 43 | for (const Sender<TestMessage> &sender : senders) { |
| 44 | const int this_buffer = sender.buffer_index(); |
| 45 | CHECK_GE(this_buffer, 0); |
| 46 | CHECK_LT(this_buffer, number_buffers); |
| 47 | CHECK(in_sender.insert(this_buffer).second) << ": " << this_buffer; |
| 48 | } |
| 49 | |
| 50 | if (read_method() != ReadMethod::PIN) { |
| 51 | // If we're not using PIN, we can't really verify anything about what |
| 52 | // buffers the fetchers have. |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | // Mapping from TestMessage::value to buffer index. |
| 57 | std::unordered_map<int, int> fetcher_values; |
| 58 | for (const Fetcher<TestMessage> &fetcher : fetchers) { |
| 59 | if (!fetcher.get()) { |
| 60 | continue; |
| 61 | } |
| 62 | const int this_buffer = fetcher.context().buffer_index; |
| 63 | CHECK_GE(this_buffer, 0); |
| 64 | CHECK_LT(this_buffer, number_buffers); |
| 65 | CHECK(in_sender.count(this_buffer) == 0) << ": " << this_buffer; |
| 66 | const auto insert_result = fetcher_values.insert( |
| 67 | std::make_pair(fetcher.get()->value(), this_buffer)); |
| 68 | if (!insert_result.second) { |
| 69 | CHECK_EQ(this_buffer, insert_result.first->second); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 74 | // Tests that watcher can receive messages from a sender. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 75 | // Also tests that OnRun() works. |
| 76 | TEST_P(AbstractEventLoopTest, Basic) { |
| 77 | auto loop1 = Make(); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 78 | auto loop2 = MakePrimary(); |
| 79 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 80 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 81 | |
| 82 | bool happened = false; |
| 83 | |
| 84 | loop2->OnRun([&]() { |
| 85 | happened = true; |
| 86 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 87 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 88 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 89 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 90 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 91 | }); |
| 92 | |
| 93 | loop2->MakeWatcher("/test", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 94 | EXPECT_EQ(message.value(), 200); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 95 | this->Exit(); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 96 | }); |
| 97 | |
| 98 | EXPECT_FALSE(happened); |
| 99 | Run(); |
| 100 | EXPECT_TRUE(happened); |
| 101 | } |
| 102 | |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 103 | // Tests that watcher can receive messages from a sender, sent via SendDetached. |
| 104 | TEST_P(AbstractEventLoopTest, BasicSendDetached) { |
| 105 | auto loop1 = Make(); |
| 106 | auto loop2 = MakePrimary(); |
| 107 | |
| 108 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
| 109 | |
| 110 | FlatbufferDetachedBuffer<TestMessage> detached = |
| 111 | flatbuffers::DetachedBuffer(); |
| 112 | { |
| 113 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 114 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 115 | builder.add_value(100); |
| 116 | detached = msg.Detach(builder.Finish()); |
| 117 | } |
| 118 | detached = flatbuffers::DetachedBuffer(); |
| 119 | { |
| 120 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 121 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 122 | builder.add_value(200); |
| 123 | detached = msg.Detach(builder.Finish()); |
| 124 | } |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 125 | sender.CheckOk(sender.SendDetached(std::move(detached))); |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 126 | |
| 127 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 128 | ASSERT_TRUE(fetcher.Fetch()); |
| 129 | EXPECT_EQ(fetcher->value(), 200); |
| 130 | } |
| 131 | |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 132 | // Verifies that a no-arg watcher will not have a data pointer. |
| 133 | TEST_P(AbstractEventLoopTest, NoArgNoData) { |
| 134 | auto loop1 = Make(); |
| 135 | auto loop2 = MakePrimary(); |
| 136 | |
| 137 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
| 138 | |
| 139 | bool happened = false; |
| 140 | |
| 141 | loop2->OnRun([&]() { |
| 142 | happened = true; |
| 143 | |
| 144 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 145 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 146 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 147 | }); |
| 148 | |
| 149 | loop2->MakeNoArgWatcher<TestMessage>("/test", [&]() { |
| 150 | EXPECT_GT(loop2->context().size, 0u); |
| 151 | EXPECT_EQ(nullptr, loop2->context().data); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 152 | EXPECT_EQ(-1, loop2->context().buffer_index); |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 153 | this->Exit(); |
| 154 | }); |
| 155 | |
| 156 | EXPECT_FALSE(happened); |
| 157 | Run(); |
| 158 | EXPECT_TRUE(happened); |
| 159 | } |
| 160 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 161 | // Tests that no-arg watcher can receive messages from a sender. |
| 162 | // Also tests that OnRun() works. |
| 163 | TEST_P(AbstractEventLoopTest, BasicNoArg) { |
| 164 | auto loop1 = Make(); |
| 165 | auto loop2 = MakePrimary(); |
| 166 | |
| 167 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
| 168 | |
| 169 | bool happened = false; |
| 170 | |
| 171 | loop2->OnRun([&]() { |
| 172 | happened = true; |
| 173 | |
| 174 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 175 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 176 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 177 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 178 | }); |
| 179 | |
| 180 | aos::Fetcher<TestMessage> fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 181 | loop2->MakeNoArgWatcher<TestMessage>("/test", [&]() { |
| 182 | ASSERT_TRUE(fetcher.Fetch()); |
| 183 | EXPECT_EQ(fetcher->value(), 200); |
| 184 | this->Exit(); |
| 185 | }); |
| 186 | |
| 187 | EXPECT_FALSE(happened); |
| 188 | Run(); |
| 189 | EXPECT_TRUE(happened); |
| 190 | } |
| 191 | |
| 192 | // Tests that a watcher can be created with an std::function. |
| 193 | TEST_P(AbstractEventLoopTest, BasicFunction) { |
| 194 | auto loop1 = Make(); |
| 195 | auto loop2 = MakePrimary(); |
| 196 | |
| 197 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
| 198 | |
| 199 | bool happened = false; |
| 200 | |
| 201 | loop2->OnRun([&]() { |
| 202 | happened = true; |
| 203 | |
| 204 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 205 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 206 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 207 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 208 | }); |
| 209 | |
| 210 | loop2->MakeWatcher("/test", std::function<void(const TestMessage &)>( |
| 211 | [&](const TestMessage &message) { |
| 212 | EXPECT_EQ(message.value(), 200); |
| 213 | this->Exit(); |
| 214 | })); |
| 215 | |
| 216 | EXPECT_FALSE(happened); |
| 217 | Run(); |
| 218 | EXPECT_TRUE(happened); |
| 219 | } |
| 220 | |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 221 | // Tests that watcher can receive messages from two senders. |
| 222 | // Also tests that OnRun() works. |
| 223 | TEST_P(AbstractEventLoopTest, BasicTwoSenders) { |
| 224 | auto loop1 = Make(); |
| 225 | auto loop2 = MakePrimary(); |
| 226 | |
| 227 | aos::Sender<TestMessage> sender1 = loop1->MakeSender<TestMessage>("/test"); |
| 228 | aos::Sender<TestMessage> sender2 = loop1->MakeSender<TestMessage>("/test"); |
| 229 | |
| 230 | bool happened = false; |
| 231 | |
| 232 | loop2->OnRun([&]() { |
| 233 | happened = true; |
| 234 | |
| 235 | { |
| 236 | aos::Sender<TestMessage>::Builder msg = sender1.MakeBuilder(); |
| 237 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 238 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 239 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 240 | } |
| 241 | { |
| 242 | aos::Sender<TestMessage>::Builder msg = sender2.MakeBuilder(); |
| 243 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 244 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 245 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 246 | } |
| 247 | }); |
| 248 | |
| 249 | int messages_received = 0; |
| 250 | loop2->MakeWatcher("/test", [&](const TestMessage &message) { |
| 251 | EXPECT_EQ(message.value(), 200); |
| 252 | this->Exit(); |
| 253 | ++messages_received; |
| 254 | }); |
| 255 | |
| 256 | EXPECT_FALSE(happened); |
| 257 | Run(); |
| 258 | EXPECT_TRUE(happened); |
| 259 | EXPECT_EQ(messages_received, 2); |
| 260 | } |
| 261 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 262 | // Tests that a fetcher can fetch from a sender. |
| 263 | // Also tests that OnRun() works. |
| 264 | TEST_P(AbstractEventLoopTest, FetchWithoutRun) { |
| 265 | auto loop1 = Make(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 266 | auto loop2 = Make(); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 267 | auto loop3 = MakePrimary(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 268 | |
| 269 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 270 | |
| 271 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 272 | |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 273 | EXPECT_FALSE(fetcher.Fetch()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 274 | EXPECT_EQ(fetcher.get(), nullptr); |
| 275 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 276 | EXPECT_EQ(fetcher.context().monotonic_event_time, monotonic_clock::min_time); |
| 277 | EXPECT_EQ(fetcher.context().monotonic_remote_time, monotonic_clock::min_time); |
| 278 | EXPECT_EQ(fetcher.context().realtime_event_time, realtime_clock::min_time); |
| 279 | EXPECT_EQ(fetcher.context().realtime_remote_time, realtime_clock::min_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 280 | EXPECT_EQ(fetcher.context().source_boot_uuid, UUID::Zero()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 281 | EXPECT_EQ(fetcher.context().queue_index, 0xffffffffu); |
| 282 | EXPECT_EQ(fetcher.context().size, 0u); |
| 283 | EXPECT_EQ(fetcher.context().data, nullptr); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 284 | EXPECT_EQ(fetcher.context().buffer_index, -1); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 285 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 286 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 287 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 288 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 289 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 290 | |
| 291 | EXPECT_TRUE(fetcher.Fetch()); |
| 292 | ASSERT_FALSE(fetcher.get() == nullptr); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 293 | EXPECT_EQ(fetcher.get()->value(), 200); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 294 | |
| 295 | const chrono::milliseconds kEpsilon(100); |
| 296 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 297 | const aos::monotonic_clock::time_point monotonic_now = loop2->monotonic_now(); |
| 298 | const aos::realtime_clock::time_point realtime_now = loop2->realtime_now(); |
| 299 | EXPECT_EQ(fetcher.context().monotonic_event_time, |
| 300 | fetcher.context().monotonic_remote_time); |
| 301 | EXPECT_EQ(fetcher.context().realtime_event_time, |
| 302 | fetcher.context().realtime_remote_time); |
| 303 | |
| 304 | EXPECT_GE(fetcher.context().monotonic_event_time, monotonic_now - kEpsilon); |
| 305 | EXPECT_LE(fetcher.context().monotonic_event_time, monotonic_now + kEpsilon); |
| 306 | EXPECT_GE(fetcher.context().realtime_event_time, realtime_now - kEpsilon); |
| 307 | EXPECT_LE(fetcher.context().realtime_event_time, realtime_now + kEpsilon); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 308 | EXPECT_EQ(fetcher.context().source_boot_uuid, loop2->boot_uuid()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 309 | EXPECT_EQ(fetcher.context().queue_index, 0x0u); |
| 310 | EXPECT_EQ(fetcher.context().size, 20u); |
| 311 | EXPECT_NE(fetcher.context().data, nullptr); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 312 | if (read_method() == ReadMethod::PIN) { |
| 313 | EXPECT_GE(fetcher.context().buffer_index, 0); |
| 314 | EXPECT_LT(fetcher.context().buffer_index, |
| 315 | loop2->NumberBuffers(fetcher.channel())); |
| 316 | } else { |
| 317 | EXPECT_EQ(fetcher.context().buffer_index, -1); |
| 318 | } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 321 | // Tests that watcher will receive all messages sent if they are sent after |
| 322 | // initialization and before running. |
| 323 | TEST_P(AbstractEventLoopTest, DoubleSendAtStartup) { |
| 324 | auto loop1 = Make(); |
| 325 | auto loop2 = MakePrimary(); |
| 326 | |
| 327 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 328 | |
| 329 | ::std::vector<int> values; |
| 330 | |
| 331 | loop2->MakeWatcher("/test", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 332 | values.push_back(message.value()); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 333 | if (values.size() == 2) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 334 | this->Exit(); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 335 | } |
| 336 | }); |
| 337 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 338 | // Before Run, should be ignored. |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 339 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 340 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 341 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 342 | builder.add_value(199); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 343 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 344 | } |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 345 | |
| 346 | loop2->OnRun([&]() { |
| 347 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 348 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 349 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 350 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 351 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 352 | } |
| 353 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 354 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 355 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 356 | builder.add_value(201); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 357 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 358 | } |
| 359 | }); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 360 | |
| 361 | Run(); |
| 362 | |
| 363 | EXPECT_THAT(values, ::testing::ElementsAreArray({200, 201})); |
| 364 | } |
| 365 | |
| 366 | // Tests that watcher will not receive messages sent before the watcher is |
| 367 | // created. |
| 368 | TEST_P(AbstractEventLoopTest, DoubleSendAfterStartup) { |
| 369 | auto loop1 = Make(); |
| 370 | auto loop2 = MakePrimary(); |
| 371 | |
| 372 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 373 | |
| 374 | ::std::vector<int> values; |
| 375 | |
| 376 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 377 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 378 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 379 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 380 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 381 | } |
| 382 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 383 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 384 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 385 | builder.add_value(201); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 386 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | loop2->MakeWatcher("/test", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 390 | values.push_back(message.value()); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 391 | }); |
| 392 | |
| 393 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 394 | auto test_timer = loop2->AddTimer([this]() { this->Exit(); }); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 395 | loop2->OnRun([&test_timer, &loop2]() { |
| 396 | test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 397 | }); |
| 398 | |
| 399 | Run(); |
| 400 | EXPECT_EQ(0, values.size()); |
| 401 | } |
| 402 | |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 403 | // Tests that FetchNext gets all the messages sent after it is constructed. |
| 404 | TEST_P(AbstractEventLoopTest, FetchNext) { |
| 405 | auto loop1 = Make(); |
| 406 | auto loop2 = MakePrimary(); |
| 407 | |
| 408 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 409 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 410 | |
| 411 | ::std::vector<int> values; |
| 412 | |
| 413 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 414 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 415 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 416 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 417 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 418 | } |
| 419 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 420 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 421 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 422 | builder.add_value(201); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 423 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 427 | auto test_timer = loop2->AddTimer([&fetcher, &values, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 428 | while (fetcher.FetchNext()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 429 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 430 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 431 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 432 | }); |
| 433 | |
| 434 | loop2->OnRun([&test_timer, &loop2]() { |
| 435 | test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 436 | }); |
| 437 | |
| 438 | Run(); |
| 439 | EXPECT_THAT(values, ::testing::ElementsAreArray({200, 201})); |
| 440 | } |
| 441 | |
| 442 | // Tests that FetchNext gets no messages sent before it is constructed. |
| 443 | TEST_P(AbstractEventLoopTest, FetchNextAfterSend) { |
| 444 | auto loop1 = Make(); |
| 445 | auto loop2 = MakePrimary(); |
| 446 | |
| 447 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 448 | |
| 449 | ::std::vector<int> values; |
| 450 | |
| 451 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 452 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 453 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 454 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 455 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 456 | } |
| 457 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 458 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 459 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 460 | builder.add_value(201); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 461 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 465 | |
| 466 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 467 | auto test_timer = loop2->AddTimer([&fetcher, &values, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 468 | while (fetcher.FetchNext()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 469 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 470 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 471 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 472 | }); |
| 473 | |
| 474 | loop2->OnRun([&test_timer, &loop2]() { |
| 475 | test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 476 | }); |
| 477 | |
| 478 | Run(); |
| 479 | EXPECT_THAT(0, values.size()); |
| 480 | } |
| 481 | |
| 482 | // Tests that Fetch returns the last message created before the loop was |
| 483 | // started. |
| 484 | TEST_P(AbstractEventLoopTest, FetchDataFromBeforeCreation) { |
| 485 | auto loop1 = Make(); |
| 486 | auto loop2 = MakePrimary(); |
| 487 | |
| 488 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 489 | |
| 490 | ::std::vector<int> values; |
| 491 | |
| 492 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 493 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 494 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 495 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 496 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 497 | } |
| 498 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 499 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 500 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 501 | builder.add_value(201); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 502 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 506 | |
| 507 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 508 | auto test_timer = loop2->AddTimer([&fetcher, &values, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 509 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 510 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 511 | } |
| 512 | // Do it again to make sure we don't double fetch. |
| 513 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 514 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 515 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 516 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 517 | }); |
| 518 | |
| 519 | loop2->OnRun([&test_timer, &loop2]() { |
| 520 | test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 521 | }); |
| 522 | |
| 523 | Run(); |
| 524 | EXPECT_THAT(values, ::testing::ElementsAreArray({201})); |
| 525 | } |
| 526 | |
| 527 | // Tests that Fetch and FetchNext interleave as expected. |
| 528 | TEST_P(AbstractEventLoopTest, FetchAndFetchNextTogether) { |
| 529 | auto loop1 = Make(); |
| 530 | auto loop2 = MakePrimary(); |
| 531 | |
| 532 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 533 | |
| 534 | ::std::vector<int> values; |
| 535 | |
| 536 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 537 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 538 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 539 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 540 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 541 | } |
| 542 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 543 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 544 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 545 | builder.add_value(201); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 546 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 550 | |
| 551 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 552 | auto test_timer = loop2->AddTimer([&fetcher, &values, &sender, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 553 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 554 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 558 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 559 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 560 | builder.add_value(202); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 561 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 562 | } |
| 563 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 564 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 565 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 566 | builder.add_value(203); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 567 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 568 | } |
| 569 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 570 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 571 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 572 | builder.add_value(204); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 573 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | if (fetcher.FetchNext()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 577 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 581 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 584 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 585 | }); |
| 586 | |
| 587 | loop2->OnRun([&test_timer, &loop2]() { |
| 588 | test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 589 | }); |
| 590 | |
| 591 | Run(); |
| 592 | EXPECT_THAT(values, ::testing::ElementsAreArray({201, 202, 204})); |
| 593 | } |
| 594 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 595 | // Tests that FetchNext behaves correctly when we get two messages in the queue |
| 596 | // but don't consume the first until after the second has been sent. |
| 597 | TEST_P(AbstractEventLoopTest, FetchNextTest) { |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 598 | auto send_loop = Make(); |
| 599 | auto fetch_loop = Make(); |
| 600 | auto sender = send_loop->MakeSender<TestMessage>("/test"); |
| 601 | Fetcher<TestMessage> fetcher = fetch_loop->MakeFetcher<TestMessage>("/test"); |
| 602 | |
| 603 | { |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 604 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 605 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 606 | builder.add_value(100); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 607 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 611 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 612 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 613 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 614 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | ASSERT_TRUE(fetcher.FetchNext()); |
| 618 | ASSERT_NE(nullptr, fetcher.get()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 619 | EXPECT_EQ(100, fetcher.get()->value()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 620 | |
| 621 | ASSERT_TRUE(fetcher.FetchNext()); |
| 622 | ASSERT_NE(nullptr, fetcher.get()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 623 | EXPECT_EQ(200, fetcher.get()->value()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 624 | |
| 625 | // When we run off the end of the queue, expect to still have the old message: |
| 626 | ASSERT_FALSE(fetcher.FetchNext()); |
| 627 | ASSERT_NE(nullptr, fetcher.get()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 628 | EXPECT_EQ(200, fetcher.get()->value()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 631 | // Verify that a fetcher still holds its data, even after falling behind. |
| 632 | TEST_P(AbstractEventLoopTest, FetcherBehindData) { |
| 633 | auto send_loop = Make(); |
| 634 | auto fetch_loop = Make(); |
| 635 | auto sender = send_loop->MakeSender<TestMessage>("/test"); |
| 636 | Fetcher<TestMessage> fetcher = fetch_loop->MakeFetcher<TestMessage>("/test"); |
| 637 | { |
| 638 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 639 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 640 | builder.add_value(1); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 641 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 642 | } |
| 643 | ASSERT_TRUE(fetcher.Fetch()); |
| 644 | EXPECT_EQ(1, fetcher.get()->value()); |
| 645 | for (int i = 0; i < 300; ++i) { |
| 646 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 647 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 648 | builder.add_value(i + 2); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 649 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 650 | } |
| 651 | EXPECT_EQ(1, fetcher.get()->value()); |
| 652 | } |
| 653 | |
| 654 | // Try a bunch of orderings of operations with fetchers and senders. Verify that |
| 655 | // all the fetchers have the correct data at each step. |
| 656 | TEST_P(AbstractEventLoopTest, FetcherPermutations) { |
| 657 | for (int max_save = 0; max_save < 5; ++max_save) { |
| 658 | SCOPED_TRACE("max_save=" + std::to_string(max_save)); |
| 659 | |
| 660 | auto send_loop = Make(); |
| 661 | auto fetch_loop = Make(); |
| 662 | auto sender = send_loop->MakeSender<TestMessage>("/test"); |
| 663 | const auto send_message = [&sender](int i) { |
| 664 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 665 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 666 | builder.add_value(i); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 667 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 668 | }; |
| 669 | std::vector<Fetcher<TestMessage>> fetchers; |
| 670 | for (int i = 0; i < 10; ++i) { |
| 671 | fetchers.emplace_back(fetch_loop->MakeFetcher<TestMessage>("/test")); |
| 672 | } |
| 673 | send_message(1); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 674 | const auto verify_buffers = [&]() { |
| 675 | std::vector<std::reference_wrapper<const Fetcher<TestMessage>>> |
| 676 | fetchers_copy; |
| 677 | for (const auto &fetcher : fetchers) { |
| 678 | fetchers_copy.emplace_back(fetcher); |
| 679 | } |
| 680 | std::vector<std::reference_wrapper<const Sender<TestMessage>>> |
| 681 | senders_copy; |
| 682 | senders_copy.emplace_back(sender); |
| 683 | VerifyBuffers(send_loop->NumberBuffers(sender.channel()), fetchers_copy, |
| 684 | senders_copy); |
| 685 | }; |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 686 | for (auto &fetcher : fetchers) { |
| 687 | ASSERT_TRUE(fetcher.Fetch()); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 688 | verify_buffers(); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 689 | EXPECT_EQ(1, fetcher.get()->value()); |
| 690 | } |
| 691 | |
| 692 | for (int save = 1; save <= max_save; ++save) { |
| 693 | SCOPED_TRACE("save=" + std::to_string(save)); |
| 694 | send_message(100 + save); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 695 | verify_buffers(); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 696 | for (size_t i = 0; i < fetchers.size() - save; ++i) { |
| 697 | SCOPED_TRACE("fetcher=" + std::to_string(i)); |
| 698 | ASSERT_TRUE(fetchers[i].Fetch()); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 699 | verify_buffers(); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 700 | EXPECT_EQ(100 + save, fetchers[i].get()->value()); |
| 701 | } |
| 702 | for (size_t i = fetchers.size() - save; i < fetchers.size() - 1; ++i) { |
| 703 | SCOPED_TRACE("fetcher=" + std::to_string(i)); |
| 704 | EXPECT_EQ(100 + (fetchers.size() - 1 - i), fetchers[i].get()->value()); |
| 705 | } |
| 706 | EXPECT_EQ(1, fetchers.back().get()->value()); |
| 707 | } |
| 708 | |
| 709 | for (int i = 0; i < 300; ++i) { |
| 710 | send_message(200 + i); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 711 | verify_buffers(); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | for (size_t i = 0; i < fetchers.size() - max_save; ++i) { |
| 715 | SCOPED_TRACE("fetcher=" + std::to_string(i)); |
| 716 | if (max_save > 0) { |
| 717 | EXPECT_EQ(100 + max_save, fetchers[i].get()->value()); |
| 718 | } else { |
| 719 | EXPECT_EQ(1, fetchers[i].get()->value()); |
| 720 | } |
| 721 | } |
| 722 | for (size_t i = fetchers.size() - max_save; i < fetchers.size() - 1; ++i) { |
| 723 | SCOPED_TRACE("fetcher=" + std::to_string(i)); |
| 724 | EXPECT_EQ(100 + (fetchers.size() - 1 - i), fetchers[i].get()->value()); |
| 725 | } |
| 726 | EXPECT_EQ(1, fetchers.back().get()->value()); |
| 727 | } |
| 728 | } |
| 729 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 730 | // Verify that making a fetcher and watcher for "/test" succeeds. |
| 731 | TEST_P(AbstractEventLoopTest, FetcherAndWatcher) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 732 | auto loop = Make(); |
| 733 | auto fetcher = loop->MakeFetcher<TestMessage>("/test"); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 734 | loop->MakeWatcher("/test", [&](const TestMessage &) {}); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 735 | } |
| 736 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 737 | // Verify that making 2 fetchers for "/test" succeeds. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 738 | TEST_P(AbstractEventLoopTest, TwoFetcher) { |
| 739 | auto loop = Make(); |
| 740 | auto fetcher = loop->MakeFetcher<TestMessage>("/test"); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 741 | auto fetcher2 = loop->MakeFetcher<TestMessage>("/test"); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 742 | } |
| 743 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 744 | // Verify that registering a watcher for an invalid channel name dies. |
| 745 | TEST_P(AbstractEventLoopDeathTest, InvalidChannelName) { |
| 746 | auto loop = Make(); |
| 747 | EXPECT_DEATH( |
| 748 | { loop->MakeWatcher("/test/invalid", [&](const TestMessage &) {}); }, |
| 749 | "/test/invalid"); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 750 | EXPECT_DEATH( |
| 751 | { loop->MakeNoArgWatcher<TestMessage>("/test/invalid", [&]() {}); }, |
| 752 | "/test/invalid"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 755 | // Verify that registering a watcher twice for "/test" fails. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 756 | TEST_P(AbstractEventLoopDeathTest, TwoWatcher) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 757 | auto loop = Make(); |
| 758 | loop->MakeWatcher("/test", [&](const TestMessage &) {}); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 759 | EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}), |
| 760 | "/test"); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 761 | EXPECT_DEATH(loop->MakeNoArgWatcher<TestMessage>("/test", [&]() {}), "/test"); |
| 762 | } |
| 763 | |
| 764 | // Verify that registering a no-arg watcher twice for "/test" fails. |
| 765 | TEST_P(AbstractEventLoopDeathTest, TwoNoArgWatcher) { |
| 766 | auto loop = Make(); |
| 767 | loop->MakeNoArgWatcher<TestMessage>("/test", [&]() {}); |
| 768 | EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}), |
| 769 | "/test"); |
| 770 | EXPECT_DEATH(loop->MakeNoArgWatcher<TestMessage>("/test", [&]() {}), "/test"); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 771 | } |
| 772 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 773 | // Verify that SetRuntimeRealtimePriority fails while running. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 774 | TEST_P(AbstractEventLoopDeathTest, SetRuntimeRealtimePriority) { |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 775 | auto loop = MakePrimary(); |
| 776 | // Confirm that runtime priority calls work when not realtime. |
| 777 | loop->SetRuntimeRealtimePriority(5); |
| 778 | |
| 779 | loop->OnRun([&]() { loop->SetRuntimeRealtimePriority(5); }); |
| 780 | |
| 781 | EXPECT_DEATH(Run(), "realtime"); |
| 782 | } |
| 783 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 784 | // Verify that SetRuntimeAffinity fails while running. |
| 785 | TEST_P(AbstractEventLoopDeathTest, SetRuntimeAffinity) { |
Austin Schuh | de97329 | 2021-10-12 18:09:49 -0700 | [diff] [blame] | 786 | const cpu_set_t available = GetCurrentThreadAffinity(); |
| 787 | int first_cpu = -1; |
| 788 | for (int i = 0; i < CPU_SETSIZE; ++i) { |
| 789 | if (CPU_ISSET(i, &available)) { |
| 790 | first_cpu = i; |
| 791 | break; |
| 792 | continue; |
| 793 | } |
| 794 | } |
| 795 | CHECK_NE(first_cpu, -1) << ": Default affinity has no CPUs?"; |
| 796 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 797 | auto loop = MakePrimary(); |
| 798 | // Confirm that runtime priority calls work when not running. |
Austin Schuh | de97329 | 2021-10-12 18:09:49 -0700 | [diff] [blame] | 799 | loop->SetRuntimeAffinity(MakeCpusetFromCpus({first_cpu})); |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 800 | |
Austin Schuh | de97329 | 2021-10-12 18:09:49 -0700 | [diff] [blame] | 801 | loop->OnRun( |
| 802 | [&]() { loop->SetRuntimeAffinity(MakeCpusetFromCpus({first_cpu})); }); |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 803 | |
| 804 | EXPECT_DEATH(Run(), "Cannot set affinity while running"); |
| 805 | } |
| 806 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 807 | // Verify that registering a watcher and a sender for "/test" fails. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 808 | TEST_P(AbstractEventLoopDeathTest, WatcherAndSender) { |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 809 | auto loop = Make(); |
| 810 | auto sender = loop->MakeSender<TestMessage>("/test"); |
| 811 | EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}), |
| 812 | "/test"); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 813 | } |
| 814 | |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 815 | // Verify that creating too many senders fails. |
| 816 | TEST_P(AbstractEventLoopDeathTest, TooManySenders) { |
| 817 | auto loop = Make(); |
| 818 | std::vector<aos::Sender<TestMessage>> senders; |
| 819 | for (int i = 0; i < 10; ++i) { |
| 820 | senders.emplace_back(loop->MakeSender<TestMessage>("/test")); |
| 821 | } |
| 822 | EXPECT_DEATH({ loop->MakeSender<TestMessage>("/test"); }, |
| 823 | "Failed to create sender on \\{ \"name\": \"/test\", \"type\": " |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 824 | "\"aos.TestMessage\"[^}]*\\ }, too many senders."); |
| 825 | } |
| 826 | |
| 827 | // Verify that creating too many fetchers fails. |
| 828 | TEST_P(AbstractEventLoopDeathTest, TooManyFetchers) { |
| 829 | if (read_method() != ReadMethod::PIN) { |
| 830 | // Other read methods don't limit the number of readers, so just skip this. |
| 831 | return; |
| 832 | } |
| 833 | |
| 834 | auto loop = Make(); |
| 835 | std::vector<aos::Fetcher<TestMessage>> fetchers; |
| 836 | for (int i = 0; i < 10; ++i) { |
| 837 | fetchers.emplace_back(loop->MakeFetcher<TestMessage>("/test")); |
| 838 | } |
| 839 | EXPECT_DEATH({ loop->MakeFetcher<TestMessage>("/test"); }, |
| 840 | "Failed to create reader on \\{ \"name\": \"/test\", \"type\": " |
| 841 | "\"aos.TestMessage\"[^}]*\\ }, too many readers."); |
| 842 | } |
| 843 | |
| 844 | // Verify that creating too many fetchers, split between two event loops, fails. |
| 845 | TEST_P(AbstractEventLoopDeathTest, TooManyFetchersTwoLoops) { |
| 846 | if (read_method() != ReadMethod::PIN) { |
| 847 | // Other read methods don't limit the number of readers, so just skip this. |
| 848 | return; |
| 849 | } |
| 850 | |
| 851 | auto loop = Make(); |
| 852 | auto loop2 = Make(); |
| 853 | std::vector<aos::Fetcher<TestMessage>> fetchers; |
| 854 | for (int i = 0; i < 5; ++i) { |
| 855 | fetchers.emplace_back(loop->MakeFetcher<TestMessage>("/test")); |
| 856 | fetchers.emplace_back(loop2->MakeFetcher<TestMessage>("/test")); |
| 857 | } |
| 858 | EXPECT_DEATH({ loop->MakeFetcher<TestMessage>("/test"); }, |
| 859 | "Failed to create reader on \\{ \"name\": \"/test\", \"type\": " |
| 860 | "\"aos.TestMessage\"[^}]*\\ }, too many readers."); |
| 861 | } |
| 862 | |
| 863 | // Verify that creating too many watchers fails. |
| 864 | TEST_P(AbstractEventLoopDeathTest, TooManyWatchers) { |
| 865 | if (read_method() != ReadMethod::PIN) { |
| 866 | // Other read methods don't limit the number of readers, so just skip this. |
| 867 | return; |
| 868 | } |
| 869 | |
| 870 | std::vector<std::unique_ptr<EventLoop>> loops; |
| 871 | for (int i = 0; i < 10; ++i) { |
| 872 | loops.emplace_back(Make()); |
| 873 | loops.back()->MakeWatcher("/test", [](const TestMessage &) {}); |
| 874 | } |
| 875 | EXPECT_DEATH({ Make()->MakeWatcher("/test", [](const TestMessage &) {}); }, |
| 876 | "Failed to create reader on \\{ \"name\": \"/test\", \"type\": " |
| 877 | "\"aos.TestMessage\"[^}]*\\ }, too many readers."); |
| 878 | } |
| 879 | |
| 880 | // Verify that creating too many watchers and fetchers combined fails. |
| 881 | TEST_P(AbstractEventLoopDeathTest, TooManyWatchersAndFetchers) { |
| 882 | if (read_method() != ReadMethod::PIN) { |
| 883 | // Other read methods don't limit the number of readers, so just skip this. |
| 884 | return; |
| 885 | } |
| 886 | |
| 887 | auto loop = Make(); |
| 888 | std::vector<aos::Fetcher<TestMessage>> fetchers; |
| 889 | std::vector<std::unique_ptr<EventLoop>> loops; |
| 890 | for (int i = 0; i < 5; ++i) { |
| 891 | fetchers.emplace_back(loop->MakeFetcher<TestMessage>("/test")); |
| 892 | loops.emplace_back(Make()); |
| 893 | loops.back()->MakeWatcher("/test", [](const TestMessage &) {}); |
| 894 | } |
| 895 | EXPECT_DEATH({ loop->MakeFetcher<TestMessage>("/test"); }, |
| 896 | "Failed to create reader on \\{ \"name\": \"/test\", \"type\": " |
| 897 | "\"aos.TestMessage\"[^}]*\\ }, too many readers."); |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 900 | // Verify that we can't create a sender inside OnRun. |
| 901 | TEST_P(AbstractEventLoopDeathTest, SenderInOnRun) { |
| 902 | auto loop1 = MakePrimary(); |
| 903 | |
| 904 | loop1->OnRun( |
| 905 | [&]() { auto sender = loop1->MakeSender<TestMessage>("/test2"); }); |
| 906 | |
| 907 | EXPECT_DEATH(Run(), "running"); |
| 908 | } |
| 909 | |
| 910 | // Verify that we can't create a watcher inside OnRun. |
| 911 | TEST_P(AbstractEventLoopDeathTest, WatcherInOnRun) { |
| 912 | auto loop1 = MakePrimary(); |
| 913 | |
| 914 | loop1->OnRun( |
| 915 | [&]() { loop1->MakeWatcher("/test", [&](const TestMessage &) {}); }); |
| 916 | |
| 917 | EXPECT_DEATH(Run(), "running"); |
| 918 | } |
| 919 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 920 | // Verify that we can't create a no-arg watcher inside OnRun. |
| 921 | TEST_P(AbstractEventLoopDeathTest, NoArgWatcherInOnRun) { |
| 922 | auto loop1 = MakePrimary(); |
| 923 | |
| 924 | loop1->OnRun( |
| 925 | [&]() { loop1->MakeNoArgWatcher<TestMessage>("/test", [&]() {}); }); |
| 926 | |
| 927 | EXPECT_DEATH(Run(), "running"); |
| 928 | } |
| 929 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 930 | // Verify that Quit() works when there are multiple watchers. |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 931 | TEST_P(AbstractEventLoopTest, MultipleWatcherQuit) { |
| 932 | auto loop1 = Make(); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 933 | auto loop2 = MakePrimary(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 934 | |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 935 | loop2->MakeWatcher("/test1", [&](const TestMessage &) {}); |
| 936 | loop2->MakeWatcher("/test2", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 937 | EXPECT_EQ(message.value(), 200); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 938 | this->Exit(); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 939 | }); |
| 940 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 941 | auto sender = loop1->MakeSender<TestMessage>("/test2"); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 942 | |
| 943 | loop2->OnRun([&]() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 944 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 945 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 946 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 947 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 948 | }); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 949 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 950 | Run(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 951 | } |
| 952 | |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 953 | // Verify that AOS_LOG has the right name. |
| 954 | TEST_P(AbstractEventLoopTest, AOSLog) { |
| 955 | auto loop2 = MakePrimary("loop1"); |
| 956 | auto loop1 = Make("loop0"); |
| 957 | |
| 958 | auto fetcher = loop1->MakeFetcher<aos::logging::LogMessageFbs>("/aos"); |
| 959 | |
| 960 | EXPECT_FALSE(fetcher.Fetch()); |
| 961 | |
| 962 | loop2->OnRun([&]() { |
| 963 | AOS_LOG(INFO, "Testing123"); |
| 964 | this->Exit(); |
| 965 | }); |
| 966 | |
| 967 | Run(); |
| 968 | EXPECT_TRUE(fetcher.Fetch()); |
| 969 | EXPECT_EQ(fetcher->name()->string_view(), "loop1"); |
| 970 | } |
| 971 | |
| 972 | // Verify that AOS_LOG has the right name in a watcher. |
| 973 | TEST_P(AbstractEventLoopTest, AOSLogWatcher) { |
| 974 | auto loop2 = MakePrimary("loop1"); |
| 975 | auto loop1 = Make("loop0"); |
| 976 | |
| 977 | auto fetcher = loop1->MakeFetcher<aos::logging::LogMessageFbs>("/aos"); |
| 978 | |
| 979 | EXPECT_FALSE(fetcher.Fetch()); |
| 980 | |
| 981 | auto sender = loop1->MakeSender<TestMessage>("/test2"); |
| 982 | |
| 983 | loop2->MakeWatcher("/test2", [&](const TestMessage & /*message*/) { |
| 984 | AOS_LOG(INFO, "Testing123"); |
| 985 | this->Exit(); |
| 986 | }); |
| 987 | |
| 988 | loop2->OnRun([&]() { |
| 989 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 990 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 991 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 992 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 993 | }); |
| 994 | |
| 995 | Run(); |
| 996 | EXPECT_TRUE(fetcher.Fetch()); |
| 997 | EXPECT_EQ(fetcher->name()->string_view(), "loop1"); |
| 998 | } |
| 999 | |
| 1000 | // Verify that AOS_LOG has the right name in a timer. |
| 1001 | TEST_P(AbstractEventLoopTest, AOSLogTimer) { |
| 1002 | auto loop2 = MakePrimary("loop1"); |
| 1003 | auto loop1 = Make("loop0"); |
| 1004 | |
| 1005 | auto fetcher = loop1->MakeFetcher<aos::logging::LogMessageFbs>("/aos"); |
| 1006 | |
| 1007 | EXPECT_FALSE(fetcher.Fetch()); |
| 1008 | |
| 1009 | auto test_timer = loop2->AddTimer([&]() { |
| 1010 | AOS_LOG(INFO, "Testing123"); |
| 1011 | this->Exit(); |
| 1012 | }); |
| 1013 | |
| 1014 | loop2->OnRun([&]() { test_timer->Setup(loop2->monotonic_now()); }); |
| 1015 | |
| 1016 | Run(); |
| 1017 | EXPECT_TRUE(fetcher.Fetch()); |
| 1018 | EXPECT_EQ(fetcher->name()->string_view(), "loop1"); |
| 1019 | } |
| 1020 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1021 | // Verify that timer intervals and duration function properly. |
| 1022 | TEST_P(AbstractEventLoopTest, TimerIntervalAndDuration) { |
Stephan Pleines | 3dce7ea | 2021-06-22 13:19:26 -0700 | [diff] [blame] | 1023 | // 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] | 1024 | FLAGS_timing_report_ms = 2000; |
| 1025 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1026 | const int kCount = 5; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1027 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1028 | auto loop = MakePrimary(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1029 | auto loop2 = Make(); |
| 1030 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1031 | ::std::vector<::aos::monotonic_clock::time_point> times; |
| 1032 | ::std::vector<::aos::monotonic_clock::time_point> expected_times; |
| 1033 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1034 | Fetcher<timing::Report> report_fetcher = |
| 1035 | loop2->MakeFetcher<timing::Report>("/aos"); |
| 1036 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 1037 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1038 | auto test_timer = loop->AddTimer([this, ×, &expected_times, &loop]() { |
| 1039 | times.push_back(loop->monotonic_now()); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1040 | EXPECT_EQ(loop->context().monotonic_remote_time, monotonic_clock::min_time); |
| 1041 | EXPECT_EQ(loop->context().realtime_event_time, realtime_clock::min_time); |
| 1042 | EXPECT_EQ(loop->context().realtime_remote_time, realtime_clock::min_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1043 | EXPECT_EQ(loop->context().source_boot_uuid, loop->boot_uuid()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1044 | EXPECT_EQ(loop->context().queue_index, 0xffffffffu); |
| 1045 | EXPECT_EQ(loop->context().size, 0u); |
| 1046 | EXPECT_EQ(loop->context().data, nullptr); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 1047 | EXPECT_EQ(loop->context().buffer_index, -1); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1048 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1049 | expected_times.push_back(loop->context().monotonic_event_time); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1050 | if (times.size() == kCount) { |
| 1051 | this->Exit(); |
| 1052 | } |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1053 | }); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1054 | test_timer->set_name("Test loop"); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1055 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1056 | const monotonic_clock::time_point start_time = loop->monotonic_now(); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1057 | // TODO(austin): This should be an error... Should be done in OnRun only. |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1058 | test_timer->Setup(start_time + chrono::seconds(1), chrono::seconds(1)); |
| 1059 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 1060 | Run(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1061 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1062 | // 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] | 1063 | ASSERT_EQ(times.size(), static_cast<size_t>(kCount)); |
| 1064 | ASSERT_EQ(times.size(), expected_times.size()); |
| 1065 | ASSERT_EQ((times.size() % 2), 1); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1066 | |
| 1067 | // Grab the middle sample. |
| 1068 | ::aos::monotonic_clock::time_point average_time = times[times.size() / 2]; |
| 1069 | |
| 1070 | // Add up all the delays of all the times. |
| 1071 | ::aos::monotonic_clock::duration sum = chrono::seconds(0); |
| 1072 | for (const ::aos::monotonic_clock::time_point time : times) { |
| 1073 | sum += time - average_time; |
| 1074 | } |
| 1075 | |
| 1076 | // Average and add to the middle to find the average time. |
| 1077 | sum /= times.size(); |
| 1078 | average_time += sum; |
| 1079 | |
| 1080 | // Compute the offset from the average and the expected average. It |
| 1081 | // should be pretty close to 0. |
| 1082 | const ::aos::monotonic_clock::duration remainder = |
| 1083 | average_time - start_time - chrono::seconds(times.size() / 2 + 1); |
| 1084 | |
| 1085 | const chrono::milliseconds kEpsilon(100); |
| 1086 | EXPECT_LT(remainder, +kEpsilon); |
| 1087 | EXPECT_GT(remainder, -kEpsilon); |
| 1088 | |
| 1089 | // Make sure that the average duration is close to 1 second. |
| 1090 | EXPECT_NEAR(chrono::duration_cast<chrono::duration<double>>(times.back() - |
| 1091 | times.front()) |
| 1092 | .count() / |
| 1093 | static_cast<double>(times.size() - 1), |
| 1094 | 1.0, 0.1); |
| 1095 | |
| 1096 | // Confirm that the ideal wakeup times increment correctly. |
| 1097 | for (size_t i = 1; i < expected_times.size(); ++i) { |
| 1098 | EXPECT_EQ(expected_times[i], expected_times[i - 1] + chrono::seconds(1)); |
| 1099 | } |
| 1100 | |
| 1101 | for (size_t i = 0; i < expected_times.size(); ++i) { |
| 1102 | EXPECT_EQ((expected_times[i] - start_time) % chrono::seconds(1), |
| 1103 | chrono::seconds(0)); |
| 1104 | } |
| 1105 | |
| 1106 | EXPECT_LT(expected_times[expected_times.size() / 2], average_time + kEpsilon); |
| 1107 | EXPECT_GT(expected_times[expected_times.size() / 2], average_time - kEpsilon); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1108 | |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1109 | if (do_timing_reports() == DoTimingReports::kYes) { |
| 1110 | // And, since we are here, check that the timing report makes sense. |
| 1111 | // Start by looking for our event loop's timing. |
| 1112 | FlatbufferDetachedBuffer<timing::Report> report = |
| 1113 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 1114 | while (report_fetcher.FetchNext()) { |
| 1115 | if (report_fetcher->name()->string_view() == "primary") { |
| 1116 | report = CopyFlatBuffer(report_fetcher.get()); |
| 1117 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1118 | } |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1119 | |
| 1120 | // Confirm that we have the right number of reports, and the contents are |
| 1121 | // sane. |
| 1122 | VLOG(1) << FlatbufferToJson(report, {.multi_line = true}); |
| 1123 | |
| 1124 | EXPECT_EQ(report.message().name()->string_view(), "primary"); |
| 1125 | |
| 1126 | ASSERT_NE(report.message().senders(), nullptr); |
| 1127 | EXPECT_EQ(report.message().senders()->size(), 2); |
| 1128 | |
| 1129 | ASSERT_NE(report.message().timers(), nullptr); |
| 1130 | EXPECT_EQ(report.message().timers()->size(), 2); |
| 1131 | |
| 1132 | EXPECT_EQ(report.message().timers()->Get(0)->name()->string_view(), |
| 1133 | "Test loop"); |
| 1134 | EXPECT_GE(report.message().timers()->Get(0)->count(), 1); |
| 1135 | |
| 1136 | EXPECT_EQ(report.message().timers()->Get(1)->name()->string_view(), |
| 1137 | "timing_reports"); |
| 1138 | EXPECT_EQ(report.message().timers()->Get(1)->count(), 1); |
| 1139 | |
| 1140 | // Make sure there is a single phased loop report with our report in it. |
| 1141 | ASSERT_EQ(report.message().phased_loops(), nullptr); |
| 1142 | } else { |
| 1143 | ASSERT_FALSE(report_fetcher.Fetch()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1144 | } |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | // Verify that we can change a timer's parameters during execution. |
| 1148 | TEST_P(AbstractEventLoopTest, TimerChangeParameters) { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 1149 | auto loop = MakePrimary(); |
Austin Schuh | d892f10 | 2021-10-12 18:01:46 -0700 | [diff] [blame] | 1150 | loop->SetRuntimeRealtimePriority(1); |
Austin Schuh | 7f20f51 | 2021-01-31 17:56:16 -0800 | [diff] [blame] | 1151 | std::vector<monotonic_clock::time_point> iteration_list; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1152 | |
| 1153 | auto test_timer = loop->AddTimer([&iteration_list, &loop]() { |
Austin Schuh | 7f20f51 | 2021-01-31 17:56:16 -0800 | [diff] [blame] | 1154 | iteration_list.push_back(loop->context().monotonic_event_time); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1155 | }); |
| 1156 | |
Austin Schuh | 7f20f51 | 2021-01-31 17:56:16 -0800 | [diff] [blame] | 1157 | monotonic_clock::time_point s; |
| 1158 | auto modifier_timer = loop->AddTimer([&test_timer, &s]() { |
Austin Schuh | d892f10 | 2021-10-12 18:01:46 -0700 | [diff] [blame] | 1159 | test_timer->Setup(s + chrono::milliseconds(1750), |
| 1160 | chrono::milliseconds(600)); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1161 | }); |
| 1162 | |
Austin Schuh | 7f20f51 | 2021-01-31 17:56:16 -0800 | [diff] [blame] | 1163 | s = loop->monotonic_now(); |
Austin Schuh | d892f10 | 2021-10-12 18:01:46 -0700 | [diff] [blame] | 1164 | test_timer->Setup(s, chrono::milliseconds(500)); |
| 1165 | modifier_timer->Setup(s + chrono::milliseconds(1250)); |
| 1166 | EndEventLoop(loop.get(), chrono::milliseconds(3950)); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 1167 | Run(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1168 | |
Austin Schuh | d892f10 | 2021-10-12 18:01:46 -0700 | [diff] [blame] | 1169 | EXPECT_THAT( |
| 1170 | iteration_list, |
| 1171 | ::testing::ElementsAre( |
| 1172 | s, s + chrono::milliseconds(500), s + chrono::milliseconds(1000), |
| 1173 | s + chrono::milliseconds(1750), s + chrono::milliseconds(2350), |
| 1174 | s + chrono::milliseconds(2950), s + chrono::milliseconds(3550))); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | // Verify that we can disable a timer during execution. |
| 1178 | TEST_P(AbstractEventLoopTest, TimerDisable) { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 1179 | auto loop = MakePrimary(); |
Austin Schuh | d892f10 | 2021-10-12 18:01:46 -0700 | [diff] [blame] | 1180 | loop->SetRuntimeRealtimePriority(1); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1181 | ::std::vector<::aos::monotonic_clock::time_point> iteration_list; |
| 1182 | |
| 1183 | auto test_timer = loop->AddTimer([&iteration_list, &loop]() { |
Austin Schuh | d892f10 | 2021-10-12 18:01:46 -0700 | [diff] [blame] | 1184 | iteration_list.push_back(loop->context().monotonic_event_time); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1185 | }); |
| 1186 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1187 | auto ender_timer = loop->AddTimer([&test_timer]() { test_timer->Disable(); }); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1188 | |
Austin Schuh | d892f10 | 2021-10-12 18:01:46 -0700 | [diff] [blame] | 1189 | monotonic_clock::time_point s = loop->monotonic_now(); |
Austin Schuh | 73d9950 | 2021-12-08 12:05:39 -0800 | [diff] [blame] | 1190 | test_timer->Setup(s, ::std::chrono::milliseconds(500)); |
| 1191 | ender_timer->Setup(s + ::std::chrono::milliseconds(1250)); |
| 1192 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(2000)); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 1193 | Run(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1194 | |
Austin Schuh | d892f10 | 2021-10-12 18:01:46 -0700 | [diff] [blame] | 1195 | EXPECT_THAT(iteration_list, |
Austin Schuh | 73d9950 | 2021-12-08 12:05:39 -0800 | [diff] [blame] | 1196 | ::testing::ElementsAre(s, s + chrono::milliseconds(500), |
| 1197 | s + chrono::milliseconds(1000))); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 1198 | } |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1199 | |
Brian Silverman | af9a4d8 | 2020-10-06 15:10:58 -0700 | [diff] [blame] | 1200 | // Verify that a timer can disable itself. |
| 1201 | // |
| 1202 | // TODO(Brian): Do something similar with phased loops, both with a quick |
| 1203 | // handler and a handler that would miss a cycle except it got deferred. Current |
| 1204 | // behavior doing that is a mess. |
| 1205 | TEST_P(AbstractEventLoopTest, TimerDisableSelf) { |
| 1206 | auto loop = MakePrimary(); |
| 1207 | |
| 1208 | int count = 0; |
| 1209 | aos::TimerHandler *test_timer; |
| 1210 | test_timer = loop->AddTimer([&count, &test_timer]() { |
| 1211 | ++count; |
| 1212 | test_timer->Disable(); |
| 1213 | }); |
| 1214 | |
| 1215 | test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(20)); |
| 1216 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(80)); |
| 1217 | Run(); |
| 1218 | |
| 1219 | EXPECT_EQ(count, 1); |
| 1220 | } |
| 1221 | |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 1222 | // Verify that we can disable a timer during execution of another timer |
| 1223 | // scheduled for the same time, with one ordering of creation for the timers. |
| 1224 | // |
| 1225 | // Also schedule some more events to reshuffle the heap in EventLoop used for |
| 1226 | // tracking events to change up the order. This used to segfault |
| 1227 | // SimulatedEventLoop. |
| 1228 | TEST_P(AbstractEventLoopTest, TimerDisableOther) { |
| 1229 | for (bool creation_order : {true, false}) { |
| 1230 | for (bool setup_order : {true, false}) { |
| 1231 | for (int shuffle_events = 0; shuffle_events < 5; ++shuffle_events) { |
| 1232 | auto loop = MakePrimary(); |
| 1233 | aos::TimerHandler *test_timer, *ender_timer; |
| 1234 | if (creation_order) { |
| 1235 | test_timer = loop->AddTimer([]() {}); |
| 1236 | ender_timer = |
| 1237 | loop->AddTimer([&test_timer]() { test_timer->Disable(); }); |
| 1238 | } else { |
| 1239 | ender_timer = |
| 1240 | loop->AddTimer([&test_timer]() { test_timer->Disable(); }); |
| 1241 | test_timer = loop->AddTimer([]() {}); |
| 1242 | } |
| 1243 | |
| 1244 | const auto start = loop->monotonic_now(); |
| 1245 | |
| 1246 | for (int i = 0; i < shuffle_events; ++i) { |
| 1247 | loop->AddTimer([]() {})->Setup(start + std::chrono::milliseconds(10)); |
| 1248 | } |
| 1249 | |
| 1250 | if (setup_order) { |
| 1251 | test_timer->Setup(start + ::std::chrono::milliseconds(20)); |
| 1252 | ender_timer->Setup(start + ::std::chrono::milliseconds(20)); |
| 1253 | } else { |
| 1254 | ender_timer->Setup(start + ::std::chrono::milliseconds(20)); |
| 1255 | test_timer->Setup(start + ::std::chrono::milliseconds(20)); |
| 1256 | } |
| 1257 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(40)); |
| 1258 | Run(); |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | } |
| 1263 | |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 1264 | // Verifies that the event loop implementations detect when Channel is not a |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 1265 | // pointer into configuration(), or a name doesn't map to a channel in |
| 1266 | // configuration(). |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 1267 | TEST_P(AbstractEventLoopDeathTest, InvalidChannel) { |
| 1268 | auto loop = MakePrimary(); |
| 1269 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1270 | const Channel *channel = configuration::GetChannel( |
| 1271 | loop->configuration(), "/test", "aos.TestMessage", "", nullptr); |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 1272 | |
| 1273 | FlatbufferDetachedBuffer<Channel> channel_copy = CopyFlatBuffer(channel); |
| 1274 | |
| 1275 | EXPECT_DEATH( |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 1276 | loop->MakeRawSender(&channel_copy.message()), |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 1277 | "Channel pointer not found in configuration\\(\\)->channels\\(\\)"); |
| 1278 | |
| 1279 | EXPECT_DEATH( |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 1280 | loop->MakeSender<TestMessage>("/testbad"), |
| 1281 | "Channel \\{ \"name\": \"/testbad\", \"type\": \"aos.TestMessage\" \\}" |
| 1282 | " not found in config"); |
| 1283 | |
| 1284 | EXPECT_FALSE(loop->TryMakeSender<TestMessage>("/testbad")); |
| 1285 | |
| 1286 | EXPECT_DEATH( |
| 1287 | loop->MakeRawFetcher(&channel_copy.message()), |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 1288 | "Channel pointer not found in configuration\\(\\)->channels\\(\\)"); |
| 1289 | |
| 1290 | EXPECT_DEATH( |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 1291 | loop->MakeFetcher<TestMessage>("/testbad"), |
| 1292 | "Channel \\{ \"name\": \"/testbad\", \"type\": \"aos.TestMessage\" \\}" |
| 1293 | " not found in config"); |
| 1294 | |
| 1295 | EXPECT_FALSE(loop->TryMakeFetcher<TestMessage>("/testbad").valid()); |
| 1296 | |
| 1297 | EXPECT_DEATH( |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 1298 | { |
| 1299 | loop->MakeRawWatcher(&channel_copy.message(), |
| 1300 | [](const Context, const void *) {}); |
| 1301 | }, |
| 1302 | "Channel pointer not found in configuration\\(\\)->channels\\(\\)"); |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 1303 | |
| 1304 | EXPECT_DEATH( |
| 1305 | { loop->MakeWatcher("/testbad", [](const TestMessage &) {}); }, |
| 1306 | "Channel \\{ \"name\": \"/testbad\", \"type\": \"aos.TestMessage\" \\}" |
| 1307 | " not found in config"); |
| 1308 | } |
| 1309 | |
| 1310 | // Verifies that the event loop handles a channel which is not readable or |
| 1311 | // writable on the current node nicely. |
| 1312 | TEST_P(AbstractEventLoopDeathTest, InaccessibleChannel) { |
| 1313 | EnableNodes("me"); |
| 1314 | auto loop = MakePrimary("me"); |
| 1315 | auto loop2 = Make("them"); |
| 1316 | |
| 1317 | const Channel *channel = configuration::GetChannel( |
| 1318 | loop->configuration(), "/test_noforward", "aos.TestMessage", "", nullptr); |
| 1319 | |
| 1320 | FlatbufferDetachedBuffer<Channel> channel_copy = CopyFlatBuffer(channel); |
| 1321 | |
| 1322 | EXPECT_DEATH( |
| 1323 | loop2->MakeSender<TestMessage>("/test_forward"), |
| 1324 | "Channel" |
| 1325 | " \\{ \"name\": \"/test_forward\", \"type\": \"aos.TestMessage\" \\}" |
| 1326 | " is not able to be sent on this node"); |
| 1327 | |
| 1328 | EXPECT_FALSE(loop2->TryMakeSender<TestMessage>("/test_forward")); |
| 1329 | |
| 1330 | EXPECT_DEATH( |
| 1331 | loop2->MakeRawFetcher(channel), |
| 1332 | "Channel" |
| 1333 | " \\{ \"name\": \"/test_noforward\", \"type\": \"aos.TestMessage\" \\}" |
| 1334 | " is not able to be fetched on this node"); |
| 1335 | |
| 1336 | EXPECT_DEATH( |
| 1337 | loop2->MakeFetcher<TestMessage>("/test_noforward"), |
| 1338 | "Channel" |
| 1339 | " \\{ \"name\": \"/test_noforward\", \"type\": \"aos.TestMessage\" \\}" |
| 1340 | " is not able to be fetched on this node"); |
| 1341 | |
| 1342 | EXPECT_FALSE(loop2->TryMakeFetcher<TestMessage>("/test_noforward").valid()); |
| 1343 | |
| 1344 | EXPECT_DEATH( |
| 1345 | { loop2->MakeRawWatcher(channel, [](const Context, const void *) {}); }, |
| 1346 | "\\{ \"name\": \"/test_noforward\", \"type\": \"aos.TestMessage\", " |
| 1347 | "\"source_node\": \"them\" \\}" |
| 1348 | " is not able to be watched on this node"); |
| 1349 | |
| 1350 | EXPECT_DEATH( |
| 1351 | { loop2->MakeWatcher("/test_noforward", [](const TestMessage &) {}); }, |
| 1352 | "\\{ \"name\": \"/test_noforward\", \"type\": \"aos.TestMessage\", " |
| 1353 | "\"source_node\": \"them\" \\}" |
| 1354 | " is not able to be watched on this node"); |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 1355 | } |
| 1356 | |
Austin Schuh | d54780b | 2020-10-03 16:26:02 -0700 | [diff] [blame] | 1357 | // Verifies that the event loop implementations detect when Channel has an |
| 1358 | // invalid alignment. |
| 1359 | TEST_P(AbstractEventLoopDeathTest, InvalidChannelAlignment) { |
| 1360 | const char *const kError = "multiple of alignment"; |
| 1361 | InvalidChannelAlignment(); |
| 1362 | |
| 1363 | auto loop = MakePrimary(); |
| 1364 | |
| 1365 | const Channel *channel = configuration::GetChannel( |
| 1366 | loop->configuration(), "/test", "aos.TestMessage", "", nullptr); |
| 1367 | |
| 1368 | EXPECT_DEATH({ loop->MakeRawSender(channel); }, kError); |
| 1369 | EXPECT_DEATH({ loop->MakeSender<TestMessage>("/test"); }, kError); |
| 1370 | |
| 1371 | EXPECT_DEATH({ loop->MakeRawFetcher(channel); }, kError); |
| 1372 | EXPECT_DEATH({ loop->MakeFetcher<TestMessage>("/test"); }, kError); |
| 1373 | |
| 1374 | EXPECT_DEATH( |
| 1375 | { loop->MakeRawWatcher(channel, [](const Context &, const void *) {}); }, |
| 1376 | kError); |
| 1377 | EXPECT_DEATH({ loop->MakeRawNoArgWatcher(channel, [](const Context &) {}); }, |
| 1378 | kError); |
| 1379 | |
| 1380 | EXPECT_DEATH({ loop->MakeNoArgWatcher<TestMessage>("/test", []() {}); }, |
| 1381 | kError); |
| 1382 | EXPECT_DEATH({ loop->MakeWatcher("/test", [](const TestMessage &) {}); }, |
| 1383 | kError); |
| 1384 | } |
| 1385 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1386 | // 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] | 1387 | TEST_P(AbstractEventLoopTest, MessageSendTime) { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 1388 | auto loop1 = MakePrimary(); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1389 | auto loop2 = Make(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1390 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1391 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 1392 | |
| 1393 | auto test_timer = loop1->AddTimer([&sender]() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1394 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 1395 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 1396 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1397 | msg.CheckOk(msg.Send(builder.Finish())); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1398 | }); |
| 1399 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1400 | bool triggered = false; |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1401 | loop1->MakeWatcher("/test", [&](const TestMessage &msg) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1402 | // Confirm that the data pointer makes sense from a watcher, and all the |
| 1403 | // timestamps look right. |
| 1404 | EXPECT_GT(&msg, loop1->context().data); |
| 1405 | EXPECT_EQ(loop1->context().monotonic_remote_time, |
| 1406 | loop1->context().monotonic_event_time); |
| 1407 | EXPECT_EQ(loop1->context().realtime_remote_time, |
| 1408 | loop1->context().realtime_event_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1409 | EXPECT_EQ(loop1->context().source_boot_uuid, loop1->boot_uuid()); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1410 | |
| 1411 | const aos::monotonic_clock::time_point monotonic_now = |
| 1412 | loop1->monotonic_now(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1413 | const aos::realtime_clock::time_point realtime_now = loop1->realtime_now(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1414 | |
| 1415 | EXPECT_LE(loop1->context().monotonic_event_time, monotonic_now); |
| 1416 | EXPECT_LE(loop1->context().realtime_event_time, realtime_now); |
| 1417 | EXPECT_GE(loop1->context().monotonic_event_time + chrono::milliseconds(500), |
| 1418 | monotonic_now); |
| 1419 | EXPECT_GE(loop1->context().realtime_event_time + chrono::milliseconds(500), |
| 1420 | realtime_now); |
| 1421 | |
Brian Silverman | eaa41d6 | 2020-07-08 19:47:35 -0700 | [diff] [blame] | 1422 | EXPECT_LT(&msg, reinterpret_cast<const void *>( |
| 1423 | reinterpret_cast<const char *>(loop1->context().data) + |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1424 | loop1->context().size)); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 1425 | if (read_method() == ReadMethod::PIN) { |
| 1426 | EXPECT_GE(loop1->context().buffer_index, 0); |
| 1427 | EXPECT_LT(loop1->context().buffer_index, |
| 1428 | loop1->NumberBuffers( |
| 1429 | configuration::GetChannel(loop1->configuration(), "/test", |
| 1430 | "aos.TestMessage", "", nullptr))); |
| 1431 | } else { |
| 1432 | EXPECT_EQ(-1, loop1->context().buffer_index); |
| 1433 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1434 | triggered = true; |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1435 | }); |
| 1436 | |
| 1437 | test_timer->Setup(loop1->monotonic_now() + ::std::chrono::seconds(1)); |
| 1438 | |
| 1439 | EndEventLoop(loop1.get(), ::std::chrono::seconds(2)); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 1440 | Run(); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1441 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1442 | EXPECT_TRUE(triggered); |
| 1443 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1444 | ASSERT_TRUE(fetcher.Fetch()); |
| 1445 | |
| 1446 | monotonic_clock::duration monotonic_time_offset = |
| 1447 | fetcher.context().monotonic_event_time - |
| 1448 | (loop1->monotonic_now() - ::std::chrono::seconds(1)); |
| 1449 | realtime_clock::duration realtime_time_offset = |
| 1450 | fetcher.context().realtime_event_time - |
| 1451 | (loop1->realtime_now() - ::std::chrono::seconds(1)); |
| 1452 | |
| 1453 | EXPECT_EQ(fetcher.context().realtime_event_time, |
| 1454 | fetcher.context().realtime_remote_time); |
| 1455 | EXPECT_EQ(fetcher.context().monotonic_event_time, |
| 1456 | fetcher.context().monotonic_remote_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1457 | EXPECT_EQ(fetcher.context().source_boot_uuid, loop1->boot_uuid()); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1458 | |
| 1459 | EXPECT_TRUE(monotonic_time_offset > ::std::chrono::milliseconds(-500)) |
| 1460 | << ": Got " |
| 1461 | << fetcher.context().monotonic_event_time.time_since_epoch().count() |
| 1462 | << " expected " << loop1->monotonic_now().time_since_epoch().count(); |
| 1463 | // Confirm that the data pointer makes sense. |
| 1464 | EXPECT_GT(fetcher.get(), fetcher.context().data); |
| 1465 | EXPECT_LT(fetcher.get(), |
Brian Silverman | eaa41d6 | 2020-07-08 19:47:35 -0700 | [diff] [blame] | 1466 | reinterpret_cast<const void *>( |
| 1467 | reinterpret_cast<const char *>(fetcher.context().data) + |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1468 | fetcher.context().size)); |
| 1469 | EXPECT_TRUE(monotonic_time_offset < ::std::chrono::milliseconds(500)) |
| 1470 | << ": Got " |
| 1471 | << fetcher.context().monotonic_event_time.time_since_epoch().count() |
| 1472 | << " expected " << loop1->monotonic_now().time_since_epoch().count(); |
| 1473 | |
| 1474 | EXPECT_TRUE(realtime_time_offset > ::std::chrono::milliseconds(-500)) |
| 1475 | << ": Got " |
| 1476 | << fetcher.context().realtime_event_time.time_since_epoch().count() |
| 1477 | << " expected " << loop1->realtime_now().time_since_epoch().count(); |
| 1478 | EXPECT_TRUE(realtime_time_offset < ::std::chrono::milliseconds(500)) |
| 1479 | << ": Got " |
| 1480 | << fetcher.context().realtime_event_time.time_since_epoch().count() |
| 1481 | << " expected " << loop1->realtime_now().time_since_epoch().count(); |
| 1482 | } |
| 1483 | |
| 1484 | // Verify that the send time on a message is roughly right when using a no-arg |
| 1485 | // watcher. To get a message, we need to use a fetcher to actually access the |
| 1486 | // message. This is also the main use case for no-arg fetchers. |
| 1487 | TEST_P(AbstractEventLoopTest, MessageSendTimeNoArg) { |
| 1488 | auto loop1 = MakePrimary(); |
| 1489 | auto loop2 = Make(); |
| 1490 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 1491 | auto fetcher = loop1->MakeFetcher<TestMessage>("/test"); |
| 1492 | |
| 1493 | auto test_timer = loop1->AddTimer([&sender]() { |
| 1494 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 1495 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 1496 | builder.add_value(200); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1497 | msg.CheckOk(msg.Send(builder.Finish())); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1498 | }); |
| 1499 | |
| 1500 | bool triggered = false; |
| 1501 | loop1->MakeNoArgWatcher<TestMessage>("/test", [&]() { |
| 1502 | // Confirm that we can indeed use a fetcher on this channel from this |
| 1503 | // context, and it results in a sane data pointer and timestamps. |
| 1504 | ASSERT_TRUE(fetcher.Fetch()); |
| 1505 | |
| 1506 | EXPECT_EQ(loop1->context().monotonic_remote_time, |
| 1507 | loop1->context().monotonic_event_time); |
| 1508 | EXPECT_EQ(loop1->context().realtime_remote_time, |
| 1509 | loop1->context().realtime_event_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1510 | EXPECT_EQ(loop1->context().source_boot_uuid, loop1->boot_uuid()); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1511 | |
| 1512 | const aos::monotonic_clock::time_point monotonic_now = |
| 1513 | loop1->monotonic_now(); |
| 1514 | const aos::realtime_clock::time_point realtime_now = loop1->realtime_now(); |
| 1515 | |
| 1516 | EXPECT_LE(loop1->context().monotonic_event_time, monotonic_now); |
| 1517 | EXPECT_LE(loop1->context().realtime_event_time, realtime_now); |
| 1518 | EXPECT_GE(loop1->context().monotonic_event_time + chrono::milliseconds(500), |
| 1519 | monotonic_now); |
| 1520 | EXPECT_GE(loop1->context().realtime_event_time + chrono::milliseconds(500), |
| 1521 | realtime_now); |
| 1522 | |
| 1523 | triggered = true; |
| 1524 | }); |
| 1525 | |
| 1526 | test_timer->Setup(loop1->monotonic_now() + ::std::chrono::seconds(1)); |
| 1527 | |
| 1528 | EndEventLoop(loop1.get(), ::std::chrono::seconds(2)); |
| 1529 | Run(); |
| 1530 | |
| 1531 | ASSERT_TRUE(triggered); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1532 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1533 | monotonic_clock::duration monotonic_time_offset = |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1534 | fetcher.context().monotonic_event_time - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1535 | (loop1->monotonic_now() - ::std::chrono::seconds(1)); |
| 1536 | realtime_clock::duration realtime_time_offset = |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1537 | fetcher.context().realtime_event_time - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1538 | (loop1->realtime_now() - ::std::chrono::seconds(1)); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1539 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1540 | EXPECT_EQ(fetcher.context().realtime_event_time, |
| 1541 | fetcher.context().realtime_remote_time); |
| 1542 | EXPECT_EQ(fetcher.context().monotonic_event_time, |
| 1543 | fetcher.context().monotonic_remote_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1544 | EXPECT_EQ(fetcher.context().source_boot_uuid, loop1->boot_uuid()); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1545 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1546 | EXPECT_TRUE(monotonic_time_offset > ::std::chrono::milliseconds(-500)) |
| 1547 | << ": Got " |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1548 | << fetcher.context().monotonic_event_time.time_since_epoch().count() |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1549 | << " expected " << loop1->monotonic_now().time_since_epoch().count(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1550 | // Confirm that the data pointer makes sense. |
| 1551 | EXPECT_GT(fetcher.get(), fetcher.context().data); |
| 1552 | EXPECT_LT(fetcher.get(), |
Brian Silverman | eaa41d6 | 2020-07-08 19:47:35 -0700 | [diff] [blame] | 1553 | reinterpret_cast<const void *>( |
| 1554 | reinterpret_cast<const char *>(fetcher.context().data) + |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1555 | fetcher.context().size)); |
| 1556 | EXPECT_TRUE(monotonic_time_offset < ::std::chrono::milliseconds(500)) |
| 1557 | << ": Got " |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1558 | << fetcher.context().monotonic_event_time.time_since_epoch().count() |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1559 | << " expected " << loop1->monotonic_now().time_since_epoch().count(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1560 | |
| 1561 | EXPECT_TRUE(realtime_time_offset > ::std::chrono::milliseconds(-500)) |
| 1562 | << ": Got " |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1563 | << fetcher.context().realtime_event_time.time_since_epoch().count() |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1564 | << " expected " << loop1->realtime_now().time_since_epoch().count(); |
| 1565 | EXPECT_TRUE(realtime_time_offset < ::std::chrono::milliseconds(500)) |
| 1566 | << ": Got " |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1567 | << fetcher.context().realtime_event_time.time_since_epoch().count() |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1568 | << " expected " << loop1->realtime_now().time_since_epoch().count(); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1569 | } |
| 1570 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1571 | // Tests that a couple phased loops run in a row result in the correct offset |
| 1572 | // and period. |
| 1573 | TEST_P(AbstractEventLoopTest, PhasedLoopTest) { |
Stephan Pleines | 3dce7ea | 2021-06-22 13:19:26 -0700 | [diff] [blame] | 1574 | // 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] | 1575 | // loop. |
| 1576 | FLAGS_timing_report_ms = 2000; |
| 1577 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1578 | const chrono::milliseconds kOffset = chrono::milliseconds(400); |
| 1579 | const int kCount = 5; |
| 1580 | |
| 1581 | auto loop1 = MakePrimary(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1582 | auto loop2 = Make(); |
| 1583 | |
| 1584 | Fetcher<timing::Report> report_fetcher = |
| 1585 | loop2->MakeFetcher<timing::Report>("/aos"); |
| 1586 | EXPECT_FALSE(report_fetcher.Fetch()); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1587 | |
| 1588 | // Collect up a couple of samples. |
| 1589 | ::std::vector<::aos::monotonic_clock::time_point> times; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1590 | ::std::vector<::aos::monotonic_clock::time_point> expected_times; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1591 | |
| 1592 | // Run kCount iterations. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1593 | loop1 |
| 1594 | ->AddPhasedLoop( |
| 1595 | [×, &expected_times, &loop1, this](int count) { |
| 1596 | EXPECT_EQ(count, 1); |
| 1597 | times.push_back(loop1->monotonic_now()); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1598 | expected_times.push_back(loop1->context().monotonic_event_time); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1599 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1600 | EXPECT_EQ(loop1->context().monotonic_remote_time, |
| 1601 | monotonic_clock::min_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1602 | EXPECT_EQ(loop1->context().source_boot_uuid, loop1->boot_uuid()); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1603 | EXPECT_EQ(loop1->context().realtime_event_time, |
| 1604 | realtime_clock::min_time); |
| 1605 | EXPECT_EQ(loop1->context().realtime_remote_time, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1606 | realtime_clock::min_time); |
| 1607 | EXPECT_EQ(loop1->context().queue_index, 0xffffffffu); |
| 1608 | EXPECT_EQ(loop1->context().size, 0u); |
| 1609 | EXPECT_EQ(loop1->context().data, nullptr); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 1610 | EXPECT_EQ(loop1->context().buffer_index, -1); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1611 | |
| 1612 | if (times.size() == kCount) { |
| 1613 | LOG(INFO) << "Exiting"; |
| 1614 | this->Exit(); |
| 1615 | } |
| 1616 | }, |
| 1617 | chrono::seconds(1), kOffset) |
| 1618 | ->set_name("Test loop"); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1619 | |
| 1620 | // Add a delay to make sure that delay during startup doesn't result in a |
| 1621 | // "missed cycle". |
| 1622 | SleepFor(chrono::seconds(2)); |
| 1623 | |
| 1624 | Run(); |
| 1625 | |
| 1626 | // 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] | 1627 | ASSERT_EQ(times.size(), static_cast<size_t>(kCount)); |
| 1628 | ASSERT_EQ(times.size(), expected_times.size()); |
| 1629 | ASSERT_EQ((times.size() % 2), 1); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1630 | |
| 1631 | // Grab the middle sample. |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1632 | ::aos::monotonic_clock::time_point average_time = times[times.size() / 2]; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1633 | |
| 1634 | // Add up all the delays of all the times. |
| 1635 | ::aos::monotonic_clock::duration sum = chrono::seconds(0); |
| 1636 | for (const ::aos::monotonic_clock::time_point time : times) { |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1637 | sum += time - average_time; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | // Average and add to the middle to find the average time. |
| 1641 | sum /= times.size(); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1642 | average_time += sum; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1643 | |
| 1644 | // Compute the offset from the start of the second of the average time. This |
| 1645 | // should be pretty close to the offset. |
| 1646 | const ::aos::monotonic_clock::duration remainder = |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1647 | average_time.time_since_epoch() - |
| 1648 | chrono::duration_cast<chrono::seconds>(average_time.time_since_epoch()); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1649 | |
| 1650 | const chrono::milliseconds kEpsilon(100); |
| 1651 | EXPECT_LT(remainder, kOffset + kEpsilon); |
| 1652 | EXPECT_GT(remainder, kOffset - kEpsilon); |
| 1653 | |
| 1654 | // Make sure that the average duration is close to 1 second. |
| 1655 | EXPECT_NEAR(chrono::duration_cast<chrono::duration<double>>(times.back() - |
| 1656 | times.front()) |
| 1657 | .count() / |
| 1658 | static_cast<double>(times.size() - 1), |
| 1659 | 1.0, 0.1); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1660 | |
| 1661 | // Confirm that the ideal wakeup times increment correctly. |
| 1662 | for (size_t i = 1; i < expected_times.size(); ++i) { |
| 1663 | EXPECT_EQ(expected_times[i], expected_times[i - 1] + chrono::seconds(1)); |
| 1664 | } |
| 1665 | |
| 1666 | for (size_t i = 0; i < expected_times.size(); ++i) { |
| 1667 | EXPECT_EQ(expected_times[i].time_since_epoch() % chrono::seconds(1), |
| 1668 | kOffset); |
| 1669 | } |
| 1670 | |
| 1671 | EXPECT_LT(expected_times[expected_times.size() / 2], average_time + kEpsilon); |
| 1672 | EXPECT_GT(expected_times[expected_times.size() / 2], average_time - kEpsilon); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1673 | |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1674 | if (do_timing_reports() == DoTimingReports::kYes) { |
| 1675 | // And, since we are here, check that the timing report makes sense. |
| 1676 | // Start by looking for our event loop's timing. |
| 1677 | FlatbufferDetachedBuffer<timing::Report> report = |
| 1678 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 1679 | while (report_fetcher.FetchNext()) { |
| 1680 | if (report_fetcher->name()->string_view() == "primary") { |
| 1681 | report = CopyFlatBuffer(report_fetcher.get()); |
| 1682 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1683 | } |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1684 | |
| 1685 | VLOG(1) << FlatbufferToJson(report, {.multi_line = true}); |
| 1686 | |
| 1687 | EXPECT_EQ(report.message().name()->string_view(), "primary"); |
| 1688 | |
| 1689 | ASSERT_NE(report.message().senders(), nullptr); |
| 1690 | EXPECT_EQ(report.message().senders()->size(), 2); |
| 1691 | |
| 1692 | ASSERT_NE(report.message().timers(), nullptr); |
| 1693 | EXPECT_EQ(report.message().timers()->size(), 1); |
| 1694 | |
| 1695 | // Make sure there is a single phased loop report with our report in it. |
| 1696 | ASSERT_NE(report.message().phased_loops(), nullptr); |
| 1697 | ASSERT_EQ(report.message().phased_loops()->size(), 1); |
| 1698 | EXPECT_EQ(report.message().phased_loops()->Get(0)->name()->string_view(), |
| 1699 | "Test loop"); |
| 1700 | EXPECT_GE(report.message().phased_loops()->Get(0)->count(), 1); |
| 1701 | } else { |
| 1702 | ASSERT_FALSE(report_fetcher.Fetch()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1703 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1704 | } |
| 1705 | |
Milind Upadhyay | 42589bb | 2021-05-19 20:05:16 -0700 | [diff] [blame] | 1706 | // Tests that a phased loop responds correctly to a changing offset. |
| 1707 | TEST_P(AbstractEventLoopTest, PhasedLoopChangingOffsetTest) { |
| 1708 | // Force a slower rate so we are guaranteed to have reports for our phased |
| 1709 | // loop. |
| 1710 | FLAGS_timing_report_ms = 2000; |
| 1711 | |
| 1712 | const chrono::milliseconds kOffset = chrono::milliseconds(400); |
| 1713 | const chrono::milliseconds kInterval = chrono::milliseconds(1000); |
| 1714 | const int kCount = 5; |
| 1715 | |
| 1716 | auto loop1 = MakePrimary(); |
| 1717 | |
| 1718 | // Collect up a couple of samples. |
| 1719 | ::std::vector<::aos::monotonic_clock::time_point> times; |
| 1720 | ::std::vector<::aos::monotonic_clock::time_point> expected_times; |
| 1721 | |
| 1722 | PhasedLoopHandler *phased_loop; |
| 1723 | |
| 1724 | // Run kCount iterations. |
| 1725 | phased_loop = loop1->AddPhasedLoop( |
| 1726 | [&phased_loop, ×, &expected_times, &loop1, this, kOffset, |
| 1727 | kInterval](int count) { |
| 1728 | EXPECT_EQ(count, 1); |
| 1729 | times.push_back(loop1->monotonic_now()); |
| 1730 | |
| 1731 | expected_times.push_back(loop1->context().monotonic_event_time); |
| 1732 | |
| 1733 | phased_loop->set_interval_and_offset( |
| 1734 | kInterval, kOffset - chrono::milliseconds(times.size())); |
| 1735 | LOG(INFO) << "new offset: " |
| 1736 | << (kOffset - chrono::milliseconds(times.size())).count(); |
| 1737 | |
| 1738 | if (times.size() == kCount) { |
| 1739 | LOG(INFO) << "Exiting"; |
| 1740 | this->Exit(); |
| 1741 | } |
| 1742 | }, |
| 1743 | kInterval, kOffset); |
| 1744 | phased_loop->set_name("Test loop"); |
| 1745 | |
| 1746 | // Add a delay to make sure that delay during startup doesn't result in a |
| 1747 | // "missed cycle". |
| 1748 | SleepFor(chrono::seconds(2)); |
| 1749 | |
| 1750 | Run(); |
| 1751 | // Confirm that we got both the right number of samples, and it's odd. |
| 1752 | EXPECT_EQ(times.size(), static_cast<size_t>(kCount)); |
| 1753 | EXPECT_EQ(times.size(), expected_times.size()); |
| 1754 | EXPECT_EQ((times.size() % 2), 1); |
| 1755 | |
| 1756 | // Grab the middle sample. |
| 1757 | ::aos::monotonic_clock::time_point average_time = times[times.size() / 2]; |
| 1758 | |
| 1759 | // Add up all the delays of all the times. |
| 1760 | ::aos::monotonic_clock::duration sum = chrono::seconds(0); |
| 1761 | for (const ::aos::monotonic_clock::time_point time : times) { |
| 1762 | sum += time - average_time; |
| 1763 | } |
| 1764 | |
| 1765 | // Average and add to the middle to find the average time. |
| 1766 | sum /= times.size(); |
| 1767 | average_time += sum; |
| 1768 | |
| 1769 | // Compute the offset from the start of the second of the average time. This |
| 1770 | // should be pretty close to the offset. |
| 1771 | const ::aos::monotonic_clock::duration remainder = |
| 1772 | average_time.time_since_epoch() - |
| 1773 | chrono::duration_cast<chrono::seconds>(average_time.time_since_epoch()); |
| 1774 | |
| 1775 | const chrono::milliseconds kEpsilon(100); |
| 1776 | EXPECT_LT(remainder, kOffset + kEpsilon); |
| 1777 | EXPECT_GT(remainder, kOffset - kEpsilon); |
| 1778 | |
| 1779 | // Make sure that the average duration is close to 1 second. |
| 1780 | EXPECT_NEAR(chrono::duration_cast<chrono::duration<double>>(times.back() - |
| 1781 | times.front()) |
| 1782 | .count() / |
| 1783 | static_cast<double>(times.size() - 1), |
| 1784 | 1.0, 0.1); |
| 1785 | |
| 1786 | // Confirm that the ideal wakeup times increment correctly. |
| 1787 | for (size_t i = 1; i < expected_times.size(); ++i) { |
| 1788 | LOG(INFO) << i - 1 << ": " << expected_times[i - 1] << ", " << i << ": " |
| 1789 | << expected_times[i]; |
| 1790 | EXPECT_EQ(expected_times[i], expected_times[i - 1] + chrono::seconds(1) - |
| 1791 | chrono::milliseconds(1)); |
| 1792 | } |
| 1793 | |
| 1794 | for (size_t i = 0; i < expected_times.size(); ++i) { |
| 1795 | EXPECT_EQ(expected_times[i].time_since_epoch() % chrono::seconds(1), |
| 1796 | kOffset - chrono::milliseconds(i)); |
| 1797 | } |
| 1798 | |
| 1799 | EXPECT_LT(expected_times[expected_times.size() / 2], average_time + kEpsilon); |
| 1800 | EXPECT_GT(expected_times[expected_times.size() / 2], average_time - kEpsilon); |
| 1801 | } |
| 1802 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1803 | // Tests that senders count correctly in the timing report. |
| 1804 | TEST_P(AbstractEventLoopTest, SenderTimingReport) { |
| 1805 | FLAGS_timing_report_ms = 1000; |
| 1806 | auto loop1 = MakePrimary(); |
| 1807 | |
| 1808 | auto loop2 = Make("watcher_loop"); |
| 1809 | loop2->MakeWatcher("/test", [](const TestMessage &) {}); |
| 1810 | |
| 1811 | auto loop3 = Make(); |
| 1812 | |
| 1813 | Fetcher<timing::Report> report_fetcher = |
| 1814 | loop3->MakeFetcher<timing::Report>("/aos"); |
| 1815 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 1816 | |
| 1817 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 1818 | |
James Kuszmaul | 7851433 | 2022-04-06 15:08:34 -0700 | [diff] [blame^] | 1819 | // Sanity check channel frequencies to ensure that we've designed the test |
| 1820 | // correctly. |
| 1821 | ASSERT_EQ(800, sender.channel()->frequency()); |
| 1822 | ASSERT_EQ(2000000000, loop1->configuration()->channel_storage_duration()); |
| 1823 | constexpr int kMaxAllowedMessages = 800 * 2; |
| 1824 | constexpr int kSendMessages = kMaxAllowedMessages * 2; |
| 1825 | constexpr int kDroppedMessages = kSendMessages - kMaxAllowedMessages; |
| 1826 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1827 | // Add a timer to actually quit. |
| 1828 | auto test_timer = loop1->AddTimer([&sender]() { |
James Kuszmaul | 7851433 | 2022-04-06 15:08:34 -0700 | [diff] [blame^] | 1829 | for (int i = 0; i < kSendMessages; ++i) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1830 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 1831 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 1832 | builder.add_value(200 + i); |
James Kuszmaul | 7851433 | 2022-04-06 15:08:34 -0700 | [diff] [blame^] | 1833 | if (i < kMaxAllowedMessages) { |
| 1834 | msg.CheckOk(msg.Send(builder.Finish())); |
| 1835 | } else { |
| 1836 | EXPECT_EQ(RawSender::Error::kMessagesSentTooFast, |
| 1837 | msg.Send(builder.Finish())); |
| 1838 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1839 | } |
| 1840 | }); |
| 1841 | |
| 1842 | // Quit after 1 timing report, mid way through the next cycle. |
| 1843 | EndEventLoop(loop1.get(), chrono::milliseconds(2500)); |
| 1844 | |
| 1845 | loop1->OnRun([&test_timer, &loop1]() { |
| 1846 | test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1500)); |
| 1847 | }); |
| 1848 | |
| 1849 | Run(); |
| 1850 | |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1851 | if (do_timing_reports() == DoTimingReports::kYes) { |
| 1852 | // And, since we are here, check that the timing report makes sense. |
| 1853 | // Start by looking for our event loop's timing. |
| 1854 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 1855 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 1856 | while (report_fetcher.FetchNext()) { |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 1857 | VLOG(1) << "Report " << FlatbufferToJson(report_fetcher.get()); |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1858 | if (report_fetcher->name()->string_view() == "primary") { |
| 1859 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 1860 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1861 | } |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1862 | |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 1863 | VLOG(1) << FlatbufferToJson(primary_report, {.multi_line = true}); |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1864 | |
| 1865 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 1866 | |
| 1867 | ASSERT_NE(primary_report.message().senders(), nullptr); |
| 1868 | EXPECT_EQ(primary_report.message().senders()->size(), 3); |
| 1869 | |
| 1870 | // Confirm that the sender looks sane. |
| 1871 | EXPECT_EQ( |
| 1872 | loop1->configuration() |
| 1873 | ->channels() |
| 1874 | ->Get(primary_report.message().senders()->Get(0)->channel_index()) |
| 1875 | ->name() |
| 1876 | ->string_view(), |
| 1877 | "/test"); |
James Kuszmaul | 7851433 | 2022-04-06 15:08:34 -0700 | [diff] [blame^] | 1878 | EXPECT_EQ(primary_report.message().senders()->Get(0)->count(), |
| 1879 | kMaxAllowedMessages); |
| 1880 | ASSERT_TRUE(primary_report.message().senders()->Get(0)->has_error_counts()); |
| 1881 | ASSERT_EQ( |
| 1882 | primary_report.message().senders()->Get(0)->error_counts()->size(), 2u); |
| 1883 | EXPECT_EQ( |
| 1884 | primary_report.message() |
| 1885 | .senders() |
| 1886 | ->Get(0) |
| 1887 | ->error_counts() |
| 1888 | ->Get(static_cast<size_t>(timing::SendError::MESSAGE_SENT_TOO_FAST)) |
| 1889 | ->count(), |
| 1890 | kDroppedMessages) |
| 1891 | << aos::FlatbufferToJson(primary_report); |
| 1892 | EXPECT_EQ(primary_report.message() |
| 1893 | .senders() |
| 1894 | ->Get(0) |
| 1895 | ->error_counts() |
| 1896 | ->Get(static_cast<size_t>(timing::SendError::INVALID_REDZONE)) |
| 1897 | ->count(), |
| 1898 | 0); |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1899 | |
| 1900 | // Confirm that the timing primary_report sender looks sane. |
| 1901 | EXPECT_EQ( |
| 1902 | loop1->configuration() |
| 1903 | ->channels() |
| 1904 | ->Get(primary_report.message().senders()->Get(1)->channel_index()) |
| 1905 | ->name() |
| 1906 | ->string_view(), |
| 1907 | "/aos"); |
| 1908 | EXPECT_EQ(primary_report.message().senders()->Get(1)->count(), 1); |
| 1909 | |
| 1910 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 1911 | EXPECT_EQ(primary_report.message().timers()->size(), 3); |
| 1912 | |
| 1913 | // Make sure there are no phased loops or watchers. |
| 1914 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 1915 | ASSERT_EQ(primary_report.message().watchers(), nullptr); |
| 1916 | } else { |
| 1917 | ASSERT_FALSE(report_fetcher.Fetch()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1918 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | // Tests that senders count correctly in the timing report. |
| 1922 | TEST_P(AbstractEventLoopTest, WatcherTimingReport) { |
| 1923 | FLAGS_timing_report_ms = 1000; |
| 1924 | auto loop1 = MakePrimary(); |
| 1925 | loop1->MakeWatcher("/test", [](const TestMessage &) {}); |
| 1926 | |
| 1927 | auto loop2 = Make("sender_loop"); |
| 1928 | |
| 1929 | auto loop3 = Make(); |
| 1930 | |
| 1931 | Fetcher<timing::Report> report_fetcher = |
| 1932 | loop3->MakeFetcher<timing::Report>("/aos"); |
| 1933 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 1934 | |
| 1935 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 1936 | |
| 1937 | // Add a timer to actually quit. |
| 1938 | auto test_timer = loop1->AddTimer([&sender]() { |
| 1939 | for (int i = 0; i < 10; ++i) { |
| 1940 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 1941 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 1942 | builder.add_value(200 + i); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1943 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1944 | } |
| 1945 | }); |
| 1946 | |
| 1947 | // Quit after 1 timing report, mid way through the next cycle. |
| 1948 | EndEventLoop(loop1.get(), chrono::milliseconds(2500)); |
| 1949 | |
| 1950 | loop1->OnRun([&test_timer, &loop1]() { |
| 1951 | test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1500)); |
| 1952 | }); |
| 1953 | |
| 1954 | Run(); |
| 1955 | |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1956 | if (do_timing_reports() == DoTimingReports::kYes) { |
| 1957 | // And, since we are here, check that the timing report makes sense. |
| 1958 | // Start by looking for our event loop's timing. |
| 1959 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 1960 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 1961 | while (report_fetcher.FetchNext()) { |
| 1962 | LOG(INFO) << "Report " << FlatbufferToJson(report_fetcher.get()); |
| 1963 | if (report_fetcher->name()->string_view() == "primary") { |
| 1964 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 1965 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1966 | } |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1967 | |
| 1968 | // Check the watcher report. |
| 1969 | VLOG(1) << FlatbufferToJson(primary_report, {.multi_line = true}); |
| 1970 | |
| 1971 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 1972 | |
| 1973 | // Just the timing report timer. |
| 1974 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 1975 | EXPECT_EQ(primary_report.message().timers()->size(), 3); |
| 1976 | |
| 1977 | // No phased loops |
| 1978 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 1979 | |
| 1980 | ASSERT_NE(primary_report.message().watchers(), nullptr); |
| 1981 | ASSERT_EQ(primary_report.message().watchers()->size(), 1); |
| 1982 | EXPECT_EQ(primary_report.message().watchers()->Get(0)->count(), 10); |
| 1983 | } else { |
| 1984 | ASSERT_FALSE(report_fetcher.Fetch()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1985 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | // Tests that fetchers count correctly in the timing report. |
| 1989 | TEST_P(AbstractEventLoopTest, FetcherTimingReport) { |
| 1990 | FLAGS_timing_report_ms = 1000; |
| 1991 | auto loop1 = MakePrimary(); |
| 1992 | auto loop2 = Make("sender_loop"); |
| 1993 | |
| 1994 | auto loop3 = Make(); |
| 1995 | |
| 1996 | Fetcher<timing::Report> report_fetcher = |
| 1997 | loop3->MakeFetcher<timing::Report>("/aos"); |
| 1998 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 1999 | |
| 2000 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 2001 | auto fetcher1 = loop1->MakeFetcher<TestMessage>("/test"); |
| 2002 | auto fetcher2 = loop1->MakeFetcher<TestMessage>("/test"); |
| 2003 | fetcher1.Fetch(); |
| 2004 | fetcher2.Fetch(); |
| 2005 | |
| 2006 | // Add a timer to actually quit. |
| 2007 | auto test_timer = loop1->AddTimer([&sender]() { |
| 2008 | for (int i = 0; i < 10; ++i) { |
| 2009 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 2010 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 2011 | builder.add_value(200 + i); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2012 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2013 | } |
| 2014 | }); |
| 2015 | |
| 2016 | auto test_timer2 = loop1->AddTimer([&fetcher1, &fetcher2]() { |
| 2017 | fetcher1.Fetch(); |
| 2018 | while (fetcher2.FetchNext()) { |
| 2019 | } |
| 2020 | }); |
| 2021 | |
| 2022 | // Quit after 1 timing report, mid way through the next cycle. |
| 2023 | EndEventLoop(loop1.get(), chrono::milliseconds(2500)); |
| 2024 | |
| 2025 | loop1->OnRun([test_timer, test_timer2, &loop1]() { |
| 2026 | test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1400)); |
| 2027 | test_timer2->Setup(loop1->monotonic_now() + chrono::milliseconds(1600)); |
| 2028 | }); |
| 2029 | |
| 2030 | Run(); |
| 2031 | |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 2032 | if (do_timing_reports() == DoTimingReports::kYes) { |
| 2033 | // And, since we are here, check that the timing report makes sense. |
| 2034 | // Start by looking for our event loop's timing. |
| 2035 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 2036 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 2037 | while (report_fetcher.FetchNext()) { |
| 2038 | if (report_fetcher->name()->string_view() == "primary") { |
| 2039 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 2040 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2041 | } |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 2042 | |
| 2043 | VLOG(1) << FlatbufferToJson(primary_report, {.multi_line = true}); |
| 2044 | |
| 2045 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 2046 | |
| 2047 | ASSERT_NE(primary_report.message().senders(), nullptr); |
| 2048 | EXPECT_EQ(primary_report.message().senders()->size(), 2); |
| 2049 | |
| 2050 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 2051 | EXPECT_EQ(primary_report.message().timers()->size(), 4); |
| 2052 | |
| 2053 | // Make sure there are no phased loops or watchers. |
| 2054 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 2055 | ASSERT_EQ(primary_report.message().watchers(), nullptr); |
| 2056 | |
| 2057 | // Now look at the fetchrs. |
| 2058 | ASSERT_NE(primary_report.message().fetchers(), nullptr); |
| 2059 | ASSERT_EQ(primary_report.message().fetchers()->size(), 2); |
| 2060 | |
| 2061 | EXPECT_EQ(primary_report.message().fetchers()->Get(0)->count(), 1); |
| 2062 | EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->average(), |
| 2063 | 0.1); |
| 2064 | EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->min(), |
| 2065 | 0.1); |
| 2066 | EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->max(), |
| 2067 | 0.1); |
| 2068 | EXPECT_EQ(primary_report.message() |
| 2069 | .fetchers() |
| 2070 | ->Get(0) |
| 2071 | ->latency() |
| 2072 | ->standard_deviation(), |
| 2073 | 0.0); |
| 2074 | |
| 2075 | EXPECT_EQ(primary_report.message().fetchers()->Get(1)->count(), 10); |
| 2076 | } else { |
| 2077 | ASSERT_FALSE(report_fetcher.Fetch()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2078 | } |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2081 | // Tests that a raw watcher and raw fetcher can receive messages from a raw |
| 2082 | // sender without messing up offsets. |
| 2083 | TEST_P(AbstractEventLoopTest, RawBasic) { |
| 2084 | auto loop1 = Make(); |
| 2085 | auto loop2 = MakePrimary(); |
| 2086 | auto loop3 = Make(); |
| 2087 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2088 | const FlatbufferDetachedBuffer<TestMessage> kMessage = |
| 2089 | JsonToFlatbuffer<TestMessage>("{}"); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2090 | |
| 2091 | std::unique_ptr<aos::RawSender> sender = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2092 | loop1->MakeRawSender(configuration::GetChannel( |
| 2093 | loop1->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2094 | |
| 2095 | std::unique_ptr<aos::RawFetcher> fetcher = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2096 | loop3->MakeRawFetcher(configuration::GetChannel( |
| 2097 | loop3->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2098 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2099 | loop2->OnRun([&]() { |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2100 | EXPECT_EQ(sender->Send(kMessage.span().data(), kMessage.span().size()), |
| 2101 | RawSender::Error::kOk); |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2102 | }); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2103 | |
| 2104 | bool happened = false; |
| 2105 | loop2->MakeRawWatcher( |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2106 | configuration::GetChannel(loop2->configuration(), "/test", |
| 2107 | "aos.TestMessage", "", nullptr), |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2108 | [this, &kMessage, &fetcher, &happened](const Context &context, |
| 2109 | const void *message) { |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2110 | happened = true; |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2111 | EXPECT_EQ( |
| 2112 | kMessage.span(), |
| 2113 | absl::Span<const uint8_t>( |
| 2114 | reinterpret_cast<const uint8_t *>(message), context.size)); |
| 2115 | EXPECT_EQ(message, context.data); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2116 | |
| 2117 | ASSERT_TRUE(fetcher->Fetch()); |
| 2118 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2119 | EXPECT_EQ(kMessage.span(), |
| 2120 | absl::Span<const uint8_t>(reinterpret_cast<const uint8_t *>( |
| 2121 | fetcher->context().data), |
| 2122 | fetcher->context().size)); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2123 | |
| 2124 | this->Exit(); |
| 2125 | }); |
| 2126 | |
| 2127 | EXPECT_FALSE(happened); |
| 2128 | Run(); |
| 2129 | EXPECT_TRUE(happened); |
| 2130 | } |
| 2131 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2132 | // 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] | 2133 | // sender without messing up offsets, using the RawSpan overload. |
| 2134 | TEST_P(AbstractEventLoopTest, RawBasicSharedSpan) { |
| 2135 | auto loop1 = Make(); |
| 2136 | auto loop2 = MakePrimary(); |
| 2137 | auto loop3 = Make(); |
| 2138 | |
| 2139 | const FlatbufferDetachedBuffer<TestMessage> kMessage = |
| 2140 | JsonToFlatbuffer<TestMessage>("{}"); |
| 2141 | |
| 2142 | std::unique_ptr<aos::RawSender> sender = |
| 2143 | loop1->MakeRawSender(configuration::GetChannel( |
| 2144 | loop1->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
| 2145 | |
| 2146 | std::unique_ptr<aos::RawFetcher> fetcher = |
| 2147 | loop3->MakeRawFetcher(configuration::GetChannel( |
| 2148 | loop3->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
| 2149 | |
| 2150 | loop2->OnRun([&]() { |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2151 | EXPECT_EQ(sender->Send(std::make_shared<absl::Span<const uint8_t>>( |
| 2152 | kMessage.span().data(), kMessage.span().size())), |
| 2153 | RawSender::Error::kOk); |
Brian Silverman | bf88992 | 2021-11-10 12:41:57 -0800 | [diff] [blame] | 2154 | }); |
| 2155 | |
| 2156 | bool happened = false; |
| 2157 | loop2->MakeRawWatcher( |
| 2158 | configuration::GetChannel(loop2->configuration(), "/test", |
| 2159 | "aos.TestMessage", "", nullptr), |
| 2160 | [this, &kMessage, &fetcher, &happened](const Context &context, |
| 2161 | const void *message) { |
| 2162 | happened = true; |
| 2163 | EXPECT_EQ( |
| 2164 | kMessage.span(), |
| 2165 | absl::Span<const uint8_t>( |
| 2166 | reinterpret_cast<const uint8_t *>(message), context.size)); |
| 2167 | EXPECT_EQ(message, context.data); |
| 2168 | |
| 2169 | ASSERT_TRUE(fetcher->Fetch()); |
| 2170 | |
| 2171 | EXPECT_EQ(kMessage.span(), |
| 2172 | absl::Span<const uint8_t>(reinterpret_cast<const uint8_t *>( |
| 2173 | fetcher->context().data), |
| 2174 | fetcher->context().size)); |
| 2175 | |
| 2176 | this->Exit(); |
| 2177 | }); |
| 2178 | |
| 2179 | EXPECT_FALSE(happened); |
| 2180 | Run(); |
| 2181 | EXPECT_TRUE(happened); |
| 2182 | } |
| 2183 | |
| 2184 | // 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] | 2185 | // sender with remote times filled out. |
| 2186 | TEST_P(AbstractEventLoopTest, RawRemoteTimes) { |
| 2187 | auto loop1 = Make(); |
| 2188 | auto loop2 = MakePrimary(); |
| 2189 | auto loop3 = Make(); |
| 2190 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2191 | const FlatbufferDetachedBuffer<TestMessage> kMessage = |
| 2192 | JsonToFlatbuffer<TestMessage>("{}"); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2193 | |
| 2194 | const aos::monotonic_clock::time_point monotonic_remote_time = |
| 2195 | aos::monotonic_clock::time_point(chrono::seconds(1501)); |
| 2196 | const aos::realtime_clock::time_point realtime_remote_time = |
| 2197 | aos::realtime_clock::time_point(chrono::seconds(3132)); |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 2198 | const uint32_t remote_queue_index = 0x254971; |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 2199 | const UUID source_boot_uuid = UUID::Random(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2200 | |
| 2201 | std::unique_ptr<aos::RawSender> sender = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2202 | loop1->MakeRawSender(configuration::GetChannel( |
| 2203 | loop1->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2204 | |
| 2205 | std::unique_ptr<aos::RawFetcher> fetcher = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2206 | loop3->MakeRawFetcher(configuration::GetChannel( |
| 2207 | loop3->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2208 | |
| 2209 | loop2->OnRun([&]() { |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2210 | EXPECT_EQ(sender->Send(kMessage.span().data(), kMessage.span().size(), |
| 2211 | monotonic_remote_time, realtime_remote_time, |
| 2212 | remote_queue_index, source_boot_uuid), |
| 2213 | RawSender::Error::kOk); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2214 | }); |
| 2215 | |
| 2216 | bool happened = false; |
| 2217 | loop2->MakeRawWatcher( |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2218 | configuration::GetChannel(loop2->configuration(), "/test", |
| 2219 | "aos.TestMessage", "", nullptr), |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 2220 | [this, monotonic_remote_time, realtime_remote_time, source_boot_uuid, |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 2221 | remote_queue_index, &fetcher, |
| 2222 | &happened](const Context &context, const void * /*message*/) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2223 | happened = true; |
| 2224 | EXPECT_EQ(monotonic_remote_time, context.monotonic_remote_time); |
| 2225 | EXPECT_EQ(realtime_remote_time, context.realtime_remote_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 2226 | EXPECT_EQ(source_boot_uuid, context.source_boot_uuid); |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 2227 | EXPECT_EQ(remote_queue_index, context.remote_queue_index); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2228 | |
| 2229 | ASSERT_TRUE(fetcher->Fetch()); |
| 2230 | EXPECT_EQ(monotonic_remote_time, |
| 2231 | fetcher->context().monotonic_remote_time); |
| 2232 | EXPECT_EQ(realtime_remote_time, |
| 2233 | fetcher->context().realtime_remote_time); |
| 2234 | |
| 2235 | this->Exit(); |
| 2236 | }); |
| 2237 | |
| 2238 | EXPECT_FALSE(happened); |
| 2239 | Run(); |
| 2240 | EXPECT_TRUE(happened); |
| 2241 | } |
| 2242 | |
| 2243 | // Tests that a raw sender fills out sent data. |
| 2244 | TEST_P(AbstractEventLoopTest, RawSenderSentData) { |
| 2245 | auto loop1 = MakePrimary(); |
| 2246 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2247 | const FlatbufferDetachedBuffer<TestMessage> kMessage = |
| 2248 | JsonToFlatbuffer<TestMessage>("{}"); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2249 | |
| 2250 | std::unique_ptr<aos::RawSender> sender = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2251 | loop1->MakeRawSender(configuration::GetChannel( |
| 2252 | loop1->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2253 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2254 | const aos::monotonic_clock::time_point monotonic_now = loop1->monotonic_now(); |
| 2255 | const aos::realtime_clock::time_point realtime_now = loop1->realtime_now(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2256 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2257 | EXPECT_EQ(sender->Send(kMessage.span().data(), kMessage.span().size()), |
| 2258 | RawSender::Error::kOk); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2259 | |
| 2260 | EXPECT_GE(sender->monotonic_sent_time(), monotonic_now); |
| 2261 | EXPECT_LE(sender->monotonic_sent_time(), |
| 2262 | monotonic_now + chrono::milliseconds(100)); |
| 2263 | EXPECT_GE(sender->realtime_sent_time(), realtime_now); |
| 2264 | EXPECT_LE(sender->realtime_sent_time(), |
| 2265 | realtime_now + chrono::milliseconds(100)); |
| 2266 | EXPECT_EQ(sender->sent_queue_index(), 0u); |
| 2267 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2268 | EXPECT_EQ(sender->Send(kMessage.span().data(), kMessage.span().size()), |
| 2269 | RawSender::Error::kOk); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2270 | |
| 2271 | EXPECT_GE(sender->monotonic_sent_time(), monotonic_now); |
| 2272 | EXPECT_LE(sender->monotonic_sent_time(), |
| 2273 | monotonic_now + chrono::milliseconds(100)); |
| 2274 | EXPECT_GE(sender->realtime_sent_time(), realtime_now); |
| 2275 | EXPECT_LE(sender->realtime_sent_time(), |
| 2276 | realtime_now + chrono::milliseconds(100)); |
| 2277 | EXPECT_EQ(sender->sent_queue_index(), 1u); |
| 2278 | } |
| 2279 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 2280 | // Tests that not setting up nodes results in no node. |
| 2281 | TEST_P(AbstractEventLoopTest, NoNode) { |
| 2282 | auto loop1 = Make(); |
| 2283 | auto loop2 = MakePrimary(); |
| 2284 | |
| 2285 | EXPECT_EQ(loop1->node(), nullptr); |
| 2286 | EXPECT_EQ(loop2->node(), nullptr); |
| 2287 | } |
| 2288 | |
| 2289 | // Tests that setting up nodes results in node being set. |
| 2290 | TEST_P(AbstractEventLoopTest, Node) { |
| 2291 | EnableNodes("me"); |
| 2292 | |
| 2293 | auto loop1 = Make(); |
| 2294 | auto loop2 = MakePrimary(); |
| 2295 | |
| 2296 | EXPECT_NE(loop1->node(), nullptr); |
| 2297 | EXPECT_NE(loop2->node(), nullptr); |
| 2298 | } |
| 2299 | |
| 2300 | // Tests that watchers work with a node setup. |
| 2301 | TEST_P(AbstractEventLoopTest, NodeWatcher) { |
| 2302 | EnableNodes("me"); |
| 2303 | |
| 2304 | auto loop1 = Make(); |
| 2305 | auto loop2 = Make(); |
| 2306 | loop1->MakeWatcher("/test", [](const TestMessage &) {}); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2307 | loop2->MakeRawWatcher( |
| 2308 | configuration::GetChannel(configuration(), "/test", "aos.TestMessage", "", |
| 2309 | nullptr), |
| 2310 | [](const Context &, const void *) {}); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 2311 | } |
| 2312 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 2313 | // Tests that no-arg watchers work with a node setup. |
| 2314 | TEST_P(AbstractEventLoopTest, NodeNoArgWatcher) { |
| 2315 | EnableNodes("me"); |
| 2316 | |
| 2317 | auto loop1 = Make(); |
| 2318 | auto loop2 = Make(); |
| 2319 | loop1->MakeWatcher("/test", [](const TestMessage &) {}); |
| 2320 | loop2->MakeRawNoArgWatcher( |
| 2321 | configuration::GetChannel(configuration(), "/test", "aos.TestMessage", "", |
| 2322 | nullptr), |
| 2323 | [](const Context &) {}); |
| 2324 | } |
| 2325 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 2326 | // Tests that fetcher work with a node setup. |
| 2327 | TEST_P(AbstractEventLoopTest, NodeFetcher) { |
| 2328 | EnableNodes("me"); |
| 2329 | auto loop1 = Make(); |
| 2330 | |
| 2331 | auto fetcher = loop1->MakeFetcher<TestMessage>("/test"); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2332 | auto raw_fetcher = loop1->MakeRawFetcher(configuration::GetChannel( |
| 2333 | configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | // Tests that sender work with a node setup. |
| 2337 | TEST_P(AbstractEventLoopTest, NodeSender) { |
| 2338 | EnableNodes("me"); |
| 2339 | auto loop1 = Make(); |
| 2340 | |
| 2341 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
| 2342 | } |
| 2343 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 2344 | // Tests that a non-realtime event loop timer is marked non-realtime. |
| 2345 | TEST_P(AbstractEventLoopTest, NonRealtimeEventLoopTimer) { |
| 2346 | auto loop1 = MakePrimary(); |
| 2347 | |
| 2348 | // Add a timer to actually quit. |
| 2349 | auto test_timer = loop1->AddTimer([this]() { |
| 2350 | CheckNotRealtime(); |
| 2351 | this->Exit(); |
| 2352 | }); |
| 2353 | |
| 2354 | loop1->OnRun([&test_timer, &loop1]() { |
| 2355 | CheckNotRealtime(); |
| 2356 | test_timer->Setup(loop1->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 2357 | }); |
| 2358 | |
| 2359 | Run(); |
| 2360 | } |
| 2361 | |
| 2362 | // Tests that a realtime event loop timer is marked realtime. |
| 2363 | TEST_P(AbstractEventLoopTest, RealtimeEventLoopTimer) { |
| 2364 | auto loop1 = MakePrimary(); |
| 2365 | |
| 2366 | loop1->SetRuntimeRealtimePriority(1); |
| 2367 | |
| 2368 | // Add a timer to actually quit. |
| 2369 | auto test_timer = loop1->AddTimer([this]() { |
| 2370 | CheckRealtime(); |
| 2371 | this->Exit(); |
| 2372 | }); |
| 2373 | |
| 2374 | loop1->OnRun([&test_timer, &loop1]() { |
| 2375 | CheckRealtime(); |
| 2376 | test_timer->Setup(loop1->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 2377 | }); |
| 2378 | |
| 2379 | Run(); |
| 2380 | } |
| 2381 | |
| 2382 | // Tests that a non-realtime event loop phased loop is marked non-realtime. |
| 2383 | TEST_P(AbstractEventLoopTest, NonRealtimeEventLoopPhasedLoop) { |
| 2384 | auto loop1 = MakePrimary(); |
| 2385 | |
| 2386 | // Add a timer to actually quit. |
| 2387 | loop1->AddPhasedLoop( |
| 2388 | [this](int) { |
| 2389 | CheckNotRealtime(); |
| 2390 | this->Exit(); |
| 2391 | }, |
| 2392 | chrono::seconds(1), chrono::seconds(0)); |
| 2393 | |
| 2394 | Run(); |
| 2395 | } |
| 2396 | |
| 2397 | // Tests that a realtime event loop phased loop is marked realtime. |
| 2398 | TEST_P(AbstractEventLoopTest, RealtimeEventLoopPhasedLoop) { |
| 2399 | auto loop1 = MakePrimary(); |
| 2400 | |
| 2401 | loop1->SetRuntimeRealtimePriority(1); |
| 2402 | |
| 2403 | // Add a timer to actually quit. |
| 2404 | loop1->AddPhasedLoop( |
| 2405 | [this](int) { |
| 2406 | CheckRealtime(); |
| 2407 | this->Exit(); |
| 2408 | }, |
| 2409 | chrono::seconds(1), chrono::seconds(0)); |
| 2410 | |
| 2411 | Run(); |
| 2412 | } |
| 2413 | |
| 2414 | // Tests that a non-realtime event loop watcher is marked non-realtime. |
| 2415 | TEST_P(AbstractEventLoopTest, NonRealtimeEventLoopWatcher) { |
| 2416 | auto loop1 = MakePrimary(); |
| 2417 | auto loop2 = Make(); |
| 2418 | |
| 2419 | aos::Sender<TestMessage> sender = loop2->MakeSender<TestMessage>("/test"); |
| 2420 | |
| 2421 | loop1->OnRun([&]() { |
| 2422 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 2423 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2424 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 2425 | }); |
| 2426 | |
| 2427 | loop1->MakeWatcher("/test", [&](const TestMessage &) { |
| 2428 | CheckNotRealtime(); |
| 2429 | this->Exit(); |
| 2430 | }); |
| 2431 | |
| 2432 | Run(); |
| 2433 | } |
| 2434 | |
| 2435 | // Tests that a realtime event loop watcher is marked realtime. |
| 2436 | TEST_P(AbstractEventLoopTest, RealtimeEventLoopWatcher) { |
| 2437 | auto loop1 = MakePrimary(); |
| 2438 | auto loop2 = Make(); |
| 2439 | |
| 2440 | loop1->SetRuntimeRealtimePriority(1); |
| 2441 | |
| 2442 | aos::Sender<TestMessage> sender = loop2->MakeSender<TestMessage>("/test"); |
| 2443 | |
| 2444 | loop1->OnRun([&]() { |
| 2445 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 2446 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2447 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 2448 | }); |
| 2449 | |
| 2450 | loop1->MakeWatcher("/test", [&](const TestMessage &) { |
| 2451 | CheckRealtime(); |
| 2452 | this->Exit(); |
| 2453 | }); |
| 2454 | |
| 2455 | Run(); |
| 2456 | } |
| 2457 | |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 2458 | // Tests that event loop's context's monotonic time is set to a value on OnRun. |
| 2459 | TEST_P(AbstractEventLoopTest, SetContextOnRun) { |
| 2460 | auto loop = MakePrimary(); |
| 2461 | |
| 2462 | // We want to check that monotonic event time is before monotonic now |
| 2463 | // called inside of callback, but after time point obtained callback. |
| 2464 | aos::monotonic_clock::time_point monotonic_event_time_on_run; |
| 2465 | |
| 2466 | loop->OnRun([&]() { |
| 2467 | monotonic_event_time_on_run = loop->context().monotonic_event_time; |
| 2468 | EXPECT_LE(monotonic_event_time_on_run, loop->monotonic_now()); |
| 2469 | EXPECT_EQ(loop->context().monotonic_remote_time, monotonic_clock::min_time); |
| 2470 | EXPECT_EQ(loop->context().realtime_event_time, realtime_clock::min_time); |
| 2471 | EXPECT_EQ(loop->context().realtime_remote_time, realtime_clock::min_time); |
| 2472 | EXPECT_EQ(loop->context().source_boot_uuid, loop->boot_uuid()); |
| 2473 | EXPECT_EQ(loop->context().queue_index, 0xffffffffu); |
| 2474 | EXPECT_EQ(loop->context().size, 0u); |
| 2475 | EXPECT_EQ(loop->context().data, nullptr); |
| 2476 | EXPECT_EQ(loop->context().buffer_index, -1); |
| 2477 | }); |
| 2478 | |
| 2479 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(200)); |
| 2480 | |
| 2481 | const aos::monotonic_clock::time_point before_run_time = |
| 2482 | loop->monotonic_now(); |
| 2483 | Run(); |
| 2484 | EXPECT_GE(monotonic_event_time_on_run, before_run_time); |
| 2485 | } |
| 2486 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 2487 | // Tests that watchers fail when created on the wrong node. |
| 2488 | TEST_P(AbstractEventLoopDeathTest, NodeWatcher) { |
| 2489 | EnableNodes("them"); |
| 2490 | |
| 2491 | auto loop1 = Make(); |
| 2492 | auto loop2 = Make(); |
| 2493 | EXPECT_DEATH({ loop1->MakeWatcher("/test", [](const TestMessage &) {}); }, |
| 2494 | "node"); |
| 2495 | EXPECT_DEATH( |
| 2496 | { |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2497 | loop2->MakeRawWatcher( |
| 2498 | configuration::GetChannel(configuration(), "/test", |
| 2499 | "aos.TestMessage", "", nullptr), |
| 2500 | [](const Context &, const void *) {}); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 2501 | }, |
| 2502 | "node"); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 2503 | EXPECT_DEATH({ loop1->MakeNoArgWatcher<TestMessage>("/test", []() {}); }, |
| 2504 | "node"); |
| 2505 | EXPECT_DEATH( |
| 2506 | { |
| 2507 | loop2->MakeRawNoArgWatcher( |
| 2508 | configuration::GetChannel(configuration(), "/test", |
| 2509 | "aos.TestMessage", "", nullptr), |
| 2510 | [](const Context &) {}); |
| 2511 | }, |
| 2512 | "node"); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | // Tests that fetchers fail when created on the wrong node. |
| 2516 | TEST_P(AbstractEventLoopDeathTest, NodeFetcher) { |
| 2517 | EnableNodes("them"); |
| 2518 | auto loop1 = Make(); |
| 2519 | |
| 2520 | EXPECT_DEATH({ auto fetcher = loop1->MakeFetcher<TestMessage>("/test"); }, |
| 2521 | "node"); |
| 2522 | EXPECT_DEATH( |
| 2523 | { |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2524 | auto raw_fetcher = loop1->MakeRawFetcher(configuration::GetChannel( |
| 2525 | configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 2526 | }, |
| 2527 | "node"); |
| 2528 | } |
| 2529 | |
| 2530 | // Tests that senders fail when created on the wrong node. |
| 2531 | TEST_P(AbstractEventLoopDeathTest, NodeSender) { |
| 2532 | EnableNodes("them"); |
| 2533 | auto loop1 = Make(); |
| 2534 | |
| 2535 | EXPECT_DEATH( |
| 2536 | { |
| 2537 | aos::Sender<TestMessage> sender = |
| 2538 | loop1->MakeSender<TestMessage>("/test"); |
| 2539 | }, |
| 2540 | "node"); |
| 2541 | |
| 2542 | // Note: Creating raw senders is always supported. Right now, this lets us |
| 2543 | // use them to create message_gateway. |
| 2544 | } |
| 2545 | |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 2546 | // Tests creating multiple Builders from a single Sender at the same time. |
| 2547 | TEST_P(AbstractEventLoopDeathTest, MultipleBuilders) { |
| 2548 | auto loop1 = Make(); |
| 2549 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
| 2550 | |
| 2551 | { auto builder = sender.MakeBuilder(); } |
| 2552 | { |
| 2553 | auto builder = sender.MakeBuilder(); |
| 2554 | builder.MakeBuilder<TestMessage>().Finish(); |
| 2555 | } |
| 2556 | { |
| 2557 | // Creating this after the first one was destroyed should be fine. |
| 2558 | auto builder = sender.MakeBuilder(); |
| 2559 | builder.MakeBuilder<TestMessage>().Finish(); |
| 2560 | // But not a second one. |
| 2561 | EXPECT_DEATH(sender.MakeBuilder().MakeBuilder<TestMessage>().Finish(), |
| 2562 | "May not overwrite in-use allocator"); |
| 2563 | } |
| 2564 | |
| 2565 | FlatbufferDetachedBuffer<TestMessage> detached = |
| 2566 | flatbuffers::DetachedBuffer(); |
| 2567 | { |
| 2568 | auto builder = sender.MakeBuilder(); |
| 2569 | detached = builder.Detach(builder.MakeBuilder<TestMessage>().Finish()); |
| 2570 | } |
| 2571 | { |
| 2572 | // This is the second one, after the detached one, so it should fail. |
| 2573 | EXPECT_DEATH(sender.MakeBuilder().MakeBuilder<TestMessage>().Finish(), |
| 2574 | "May not overwrite in-use allocator"); |
| 2575 | } |
| 2576 | |
| 2577 | // Clear the detached one, and then we should be able to create another. |
| 2578 | detached = flatbuffers::DetachedBuffer(); |
| 2579 | { |
| 2580 | auto builder = sender.MakeBuilder(); |
| 2581 | builder.MakeBuilder<TestMessage>().Finish(); |
| 2582 | } |
| 2583 | |
| 2584 | // And then detach another one. |
| 2585 | { |
| 2586 | auto builder = sender.MakeBuilder(); |
| 2587 | detached = builder.Detach(builder.MakeBuilder<TestMessage>().Finish()); |
| 2588 | } |
| 2589 | } |
| 2590 | |
| 2591 | // Tests sending a buffer detached from a different builder. |
| 2592 | TEST_P(AbstractEventLoopDeathTest, WrongDetachedBuffer) { |
| 2593 | auto loop1 = Make(); |
| 2594 | aos::Sender<TestMessage> sender1 = loop1->MakeSender<TestMessage>("/test"); |
| 2595 | aos::Sender<TestMessage> sender2 = loop1->MakeSender<TestMessage>("/test"); |
| 2596 | |
| 2597 | auto builder = sender1.MakeBuilder(); |
| 2598 | FlatbufferDetachedBuffer<TestMessage> detached = |
| 2599 | builder.Detach(builder.MakeBuilder<TestMessage>().Finish()); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2600 | EXPECT_DEATH(sender2.CheckOk(sender2.SendDetached(std::move(detached))), |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 2601 | "May only send the buffer detached from this Sender"); |
| 2602 | } |
| 2603 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 2604 | int TestChannelFrequency(EventLoop *event_loop) { |
| 2605 | return event_loop->GetChannel<TestMessage>("/test")->frequency(); |
| 2606 | } |
| 2607 | |
| 2608 | int TestChannelQueueSize(EventLoop *event_loop) { |
| 2609 | const int frequency = TestChannelFrequency(event_loop); |
| 2610 | const auto channel_storage_duration = std::chrono::nanoseconds( |
| 2611 | event_loop->configuration()->channel_storage_duration()); |
| 2612 | const int queue_size = |
| 2613 | frequency * std::chrono::duration_cast<std::chrono::duration<double>>( |
| 2614 | channel_storage_duration) |
| 2615 | .count(); |
| 2616 | |
| 2617 | return queue_size; |
| 2618 | } |
| 2619 | |
| 2620 | RawSender::Error SendTestMessage(aos::Sender<TestMessage> &sender) { |
| 2621 | aos::Sender<TestMessage>::Builder builder = sender.MakeBuilder(); |
| 2622 | TestMessage::Builder test_message_builder = |
| 2623 | builder.MakeBuilder<TestMessage>(); |
| 2624 | test_message_builder.add_value(0); |
| 2625 | return builder.Send(test_message_builder.Finish()); |
| 2626 | } |
| 2627 | |
| 2628 | // Test that sending messages too fast returns |
| 2629 | // RawSender::Error::kMessagesSentTooFast. |
| 2630 | TEST_P(AbstractEventLoopTest, SendingMessagesTooFast) { |
| 2631 | auto event_loop = MakePrimary(); |
| 2632 | |
| 2633 | auto sender = event_loop->MakeSender<TestMessage>("/test"); |
| 2634 | |
| 2635 | // Send one message in the beginning, then wait until the |
| 2636 | // channel_storage_duration is almost done and start sending messages rapidly, |
| 2637 | // having some come in the next chanel_storage_duration. The queue_size is |
| 2638 | // 1600, so the 1601st message will be the last valid one (the initial message |
| 2639 | // having being sent more than a channel_storage_duration ago), and trying to |
| 2640 | // send the 1602nd message should return |
| 2641 | // RawSender::Error::kMessagesSentTooFast. |
| 2642 | EXPECT_EQ(SendTestMessage(sender), RawSender::Error::kOk); |
| 2643 | int msgs_sent = 1; |
| 2644 | const int queue_size = TestChannelQueueSize(event_loop.get()); |
| 2645 | |
| 2646 | const auto timer = event_loop->AddTimer([&]() { |
| 2647 | const bool done = (msgs_sent == queue_size + 1); |
| 2648 | ASSERT_EQ( |
| 2649 | SendTestMessage(sender), |
| 2650 | done ? RawSender::Error::kMessagesSentTooFast : RawSender::Error::kOk); |
| 2651 | msgs_sent++; |
| 2652 | if (done) { |
| 2653 | Exit(); |
| 2654 | } |
| 2655 | }); |
| 2656 | |
| 2657 | const auto kRepeatOffset = std::chrono::milliseconds(1); |
| 2658 | const auto base_offset = |
| 2659 | std::chrono::nanoseconds( |
| 2660 | event_loop->configuration()->channel_storage_duration()) - |
| 2661 | (kRepeatOffset * (queue_size / 2)); |
| 2662 | event_loop->OnRun([&event_loop, &timer, &base_offset, &kRepeatOffset]() { |
| 2663 | timer->Setup(event_loop->monotonic_now() + base_offset, kRepeatOffset); |
| 2664 | }); |
| 2665 | |
| 2666 | Run(); |
| 2667 | } |
| 2668 | |
| 2669 | // Tests that we are able to send messages successfully after sending messages |
| 2670 | // too fast and waiting while continuously attempting to send messages. |
| 2671 | // Also tests that SendFailureCounter is working correctly in this |
| 2672 | // situation |
| 2673 | TEST_P(AbstractEventLoopTest, SendingAfterSendingTooFast) { |
| 2674 | auto event_loop = MakePrimary(); |
| 2675 | |
| 2676 | auto sender = event_loop->MakeSender<TestMessage>("/test"); |
| 2677 | |
| 2678 | // We are sending messages at 1 kHz, so we will be sending too fast after |
| 2679 | // queue_size (1600) ms. After this, keep sending messages, and exactly a |
| 2680 | // channel storage duration (2s) after we send the first message we should |
| 2681 | // be able to successfully send a message. |
| 2682 | |
| 2683 | const monotonic_clock::duration kInterval = std::chrono::milliseconds(1); |
| 2684 | const monotonic_clock::duration channel_storage_duration = |
| 2685 | std::chrono::nanoseconds( |
| 2686 | event_loop->configuration()->channel_storage_duration()); |
| 2687 | const int queue_size = TestChannelQueueSize(event_loop.get()); |
| 2688 | |
| 2689 | int msgs_sent = 0; |
| 2690 | SendFailureCounter counter; |
| 2691 | auto start = monotonic_clock::min_time; |
| 2692 | |
| 2693 | event_loop->AddPhasedLoop( |
| 2694 | [&](int) { |
| 2695 | const auto actual_err = SendTestMessage(sender); |
| 2696 | const bool done_waiting = (start != monotonic_clock::min_time && |
| 2697 | sender.monotonic_sent_time() >= |
| 2698 | (start + channel_storage_duration)); |
| 2699 | const auto expected_err = |
| 2700 | (msgs_sent < queue_size || done_waiting |
| 2701 | ? RawSender::Error::kOk |
| 2702 | : RawSender::Error::kMessagesSentTooFast); |
| 2703 | |
| 2704 | if (start == monotonic_clock::min_time) { |
| 2705 | start = sender.monotonic_sent_time(); |
| 2706 | } |
| 2707 | |
| 2708 | ASSERT_EQ(actual_err, expected_err); |
| 2709 | counter.Count(actual_err); |
| 2710 | msgs_sent++; |
| 2711 | |
| 2712 | EXPECT_EQ(counter.failures(), |
| 2713 | msgs_sent <= queue_size |
| 2714 | ? 0 |
| 2715 | : (msgs_sent - queue_size) - |
| 2716 | (actual_err == RawSender::Error::kOk ? 1 : 0)); |
| 2717 | EXPECT_EQ(counter.just_failed(), actual_err != RawSender::Error::kOk); |
| 2718 | |
| 2719 | if (done_waiting) { |
| 2720 | Exit(); |
| 2721 | } |
| 2722 | }, |
| 2723 | kInterval); |
| 2724 | Run(); |
| 2725 | } |
| 2726 | |
| 2727 | // Tests that RawSender::Error::kMessagesSentTooFast is returned |
| 2728 | // when messages are sent too fast from senders in different loops |
| 2729 | TEST_P(AbstractEventLoopTest, SendingTooFastWithMultipleLoops) { |
| 2730 | auto loop1 = MakePrimary(); |
| 2731 | auto loop2 = Make(); |
| 2732 | |
| 2733 | auto sender1 = loop1->MakeSender<TestMessage>("/test"); |
| 2734 | auto sender2 = loop2->MakeSender<TestMessage>("/test"); |
| 2735 | |
| 2736 | // Send queue_size messages split between the senders. |
| 2737 | const int queue_size = TestChannelQueueSize(loop1.get()); |
| 2738 | for (int i = 0; i < queue_size / 2; i++) { |
| 2739 | ASSERT_EQ(SendTestMessage(sender1), RawSender::Error::kOk); |
| 2740 | ASSERT_EQ(SendTestMessage(sender2), RawSender::Error::kOk); |
| 2741 | } |
| 2742 | |
| 2743 | // Since queue_size messages have been sent, this should return an error |
| 2744 | EXPECT_EQ(SendTestMessage(sender2), RawSender::Error::kMessagesSentTooFast); |
| 2745 | } |
| 2746 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 2747 | } // namespace testing |
| 2748 | } // namespace aos |