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 | |
| 1819 | // Add a timer to actually quit. |
| 1820 | auto test_timer = loop1->AddTimer([&sender]() { |
| 1821 | for (int i = 0; i < 10; ++i) { |
| 1822 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 1823 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 1824 | builder.add_value(200 + i); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1825 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1826 | } |
| 1827 | }); |
| 1828 | |
| 1829 | // Quit after 1 timing report, mid way through the next cycle. |
| 1830 | EndEventLoop(loop1.get(), chrono::milliseconds(2500)); |
| 1831 | |
| 1832 | loop1->OnRun([&test_timer, &loop1]() { |
| 1833 | test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1500)); |
| 1834 | }); |
| 1835 | |
| 1836 | Run(); |
| 1837 | |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1838 | if (do_timing_reports() == DoTimingReports::kYes) { |
| 1839 | // And, since we are here, check that the timing report makes sense. |
| 1840 | // Start by looking for our event loop's timing. |
| 1841 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 1842 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 1843 | while (report_fetcher.FetchNext()) { |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 1844 | VLOG(1) << "Report " << FlatbufferToJson(report_fetcher.get()); |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1845 | if (report_fetcher->name()->string_view() == "primary") { |
| 1846 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 1847 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1848 | } |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1849 | |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 1850 | VLOG(1) << FlatbufferToJson(primary_report, {.multi_line = true}); |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1851 | |
| 1852 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 1853 | |
| 1854 | ASSERT_NE(primary_report.message().senders(), nullptr); |
| 1855 | EXPECT_EQ(primary_report.message().senders()->size(), 3); |
| 1856 | |
| 1857 | // Confirm that the sender looks sane. |
| 1858 | EXPECT_EQ( |
| 1859 | loop1->configuration() |
| 1860 | ->channels() |
| 1861 | ->Get(primary_report.message().senders()->Get(0)->channel_index()) |
| 1862 | ->name() |
| 1863 | ->string_view(), |
| 1864 | "/test"); |
| 1865 | EXPECT_EQ(primary_report.message().senders()->Get(0)->count(), 10); |
| 1866 | |
| 1867 | // Confirm that the timing primary_report sender looks sane. |
| 1868 | EXPECT_EQ( |
| 1869 | loop1->configuration() |
| 1870 | ->channels() |
| 1871 | ->Get(primary_report.message().senders()->Get(1)->channel_index()) |
| 1872 | ->name() |
| 1873 | ->string_view(), |
| 1874 | "/aos"); |
| 1875 | EXPECT_EQ(primary_report.message().senders()->Get(1)->count(), 1); |
| 1876 | |
| 1877 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 1878 | EXPECT_EQ(primary_report.message().timers()->size(), 3); |
| 1879 | |
| 1880 | // Make sure there are no phased loops or watchers. |
| 1881 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 1882 | ASSERT_EQ(primary_report.message().watchers(), nullptr); |
| 1883 | } else { |
| 1884 | ASSERT_FALSE(report_fetcher.Fetch()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1885 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | // Tests that senders count correctly in the timing report. |
| 1889 | TEST_P(AbstractEventLoopTest, WatcherTimingReport) { |
| 1890 | FLAGS_timing_report_ms = 1000; |
| 1891 | auto loop1 = MakePrimary(); |
| 1892 | loop1->MakeWatcher("/test", [](const TestMessage &) {}); |
| 1893 | |
| 1894 | auto loop2 = Make("sender_loop"); |
| 1895 | |
| 1896 | auto loop3 = Make(); |
| 1897 | |
| 1898 | Fetcher<timing::Report> report_fetcher = |
| 1899 | loop3->MakeFetcher<timing::Report>("/aos"); |
| 1900 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 1901 | |
| 1902 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 1903 | |
| 1904 | // Add a timer to actually quit. |
| 1905 | auto test_timer = loop1->AddTimer([&sender]() { |
| 1906 | for (int i = 0; i < 10; ++i) { |
| 1907 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 1908 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 1909 | builder.add_value(200 + i); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1910 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1911 | } |
| 1912 | }); |
| 1913 | |
| 1914 | // Quit after 1 timing report, mid way through the next cycle. |
| 1915 | EndEventLoop(loop1.get(), chrono::milliseconds(2500)); |
| 1916 | |
| 1917 | loop1->OnRun([&test_timer, &loop1]() { |
| 1918 | test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1500)); |
| 1919 | }); |
| 1920 | |
| 1921 | Run(); |
| 1922 | |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1923 | if (do_timing_reports() == DoTimingReports::kYes) { |
| 1924 | // And, since we are here, check that the timing report makes sense. |
| 1925 | // Start by looking for our event loop's timing. |
| 1926 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 1927 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 1928 | while (report_fetcher.FetchNext()) { |
| 1929 | LOG(INFO) << "Report " << FlatbufferToJson(report_fetcher.get()); |
| 1930 | if (report_fetcher->name()->string_view() == "primary") { |
| 1931 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 1932 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1933 | } |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1934 | |
| 1935 | // Check the watcher report. |
| 1936 | VLOG(1) << FlatbufferToJson(primary_report, {.multi_line = true}); |
| 1937 | |
| 1938 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 1939 | |
| 1940 | // Just the timing report timer. |
| 1941 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 1942 | EXPECT_EQ(primary_report.message().timers()->size(), 3); |
| 1943 | |
| 1944 | // No phased loops |
| 1945 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 1946 | |
| 1947 | ASSERT_NE(primary_report.message().watchers(), nullptr); |
| 1948 | ASSERT_EQ(primary_report.message().watchers()->size(), 1); |
| 1949 | EXPECT_EQ(primary_report.message().watchers()->Get(0)->count(), 10); |
| 1950 | } else { |
| 1951 | ASSERT_FALSE(report_fetcher.Fetch()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1952 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | // Tests that fetchers count correctly in the timing report. |
| 1956 | TEST_P(AbstractEventLoopTest, FetcherTimingReport) { |
| 1957 | FLAGS_timing_report_ms = 1000; |
| 1958 | auto loop1 = MakePrimary(); |
| 1959 | auto loop2 = Make("sender_loop"); |
| 1960 | |
| 1961 | auto loop3 = Make(); |
| 1962 | |
| 1963 | Fetcher<timing::Report> report_fetcher = |
| 1964 | loop3->MakeFetcher<timing::Report>("/aos"); |
| 1965 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 1966 | |
| 1967 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 1968 | auto fetcher1 = loop1->MakeFetcher<TestMessage>("/test"); |
| 1969 | auto fetcher2 = loop1->MakeFetcher<TestMessage>("/test"); |
| 1970 | fetcher1.Fetch(); |
| 1971 | fetcher2.Fetch(); |
| 1972 | |
| 1973 | // Add a timer to actually quit. |
| 1974 | auto test_timer = loop1->AddTimer([&sender]() { |
| 1975 | for (int i = 0; i < 10; ++i) { |
| 1976 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 1977 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 1978 | builder.add_value(200 + i); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1979 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1980 | } |
| 1981 | }); |
| 1982 | |
| 1983 | auto test_timer2 = loop1->AddTimer([&fetcher1, &fetcher2]() { |
| 1984 | fetcher1.Fetch(); |
| 1985 | while (fetcher2.FetchNext()) { |
| 1986 | } |
| 1987 | }); |
| 1988 | |
| 1989 | // Quit after 1 timing report, mid way through the next cycle. |
| 1990 | EndEventLoop(loop1.get(), chrono::milliseconds(2500)); |
| 1991 | |
| 1992 | loop1->OnRun([test_timer, test_timer2, &loop1]() { |
| 1993 | test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1400)); |
| 1994 | test_timer2->Setup(loop1->monotonic_now() + chrono::milliseconds(1600)); |
| 1995 | }); |
| 1996 | |
| 1997 | Run(); |
| 1998 | |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 1999 | if (do_timing_reports() == DoTimingReports::kYes) { |
| 2000 | // And, since we are here, check that the timing report makes sense. |
| 2001 | // Start by looking for our event loop's timing. |
| 2002 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 2003 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 2004 | while (report_fetcher.FetchNext()) { |
| 2005 | if (report_fetcher->name()->string_view() == "primary") { |
| 2006 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 2007 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2008 | } |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 2009 | |
| 2010 | VLOG(1) << FlatbufferToJson(primary_report, {.multi_line = true}); |
| 2011 | |
| 2012 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 2013 | |
| 2014 | ASSERT_NE(primary_report.message().senders(), nullptr); |
| 2015 | EXPECT_EQ(primary_report.message().senders()->size(), 2); |
| 2016 | |
| 2017 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 2018 | EXPECT_EQ(primary_report.message().timers()->size(), 4); |
| 2019 | |
| 2020 | // Make sure there are no phased loops or watchers. |
| 2021 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 2022 | ASSERT_EQ(primary_report.message().watchers(), nullptr); |
| 2023 | |
| 2024 | // Now look at the fetchrs. |
| 2025 | ASSERT_NE(primary_report.message().fetchers(), nullptr); |
| 2026 | ASSERT_EQ(primary_report.message().fetchers()->size(), 2); |
| 2027 | |
| 2028 | EXPECT_EQ(primary_report.message().fetchers()->Get(0)->count(), 1); |
| 2029 | EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->average(), |
| 2030 | 0.1); |
| 2031 | EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->min(), |
| 2032 | 0.1); |
| 2033 | EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->max(), |
| 2034 | 0.1); |
| 2035 | EXPECT_EQ(primary_report.message() |
| 2036 | .fetchers() |
| 2037 | ->Get(0) |
| 2038 | ->latency() |
| 2039 | ->standard_deviation(), |
| 2040 | 0.0); |
| 2041 | |
| 2042 | EXPECT_EQ(primary_report.message().fetchers()->Get(1)->count(), 10); |
| 2043 | } else { |
| 2044 | ASSERT_FALSE(report_fetcher.Fetch()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 2045 | } |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 2046 | } |
| 2047 | |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2048 | // Tests that a raw watcher and raw fetcher can receive messages from a raw |
| 2049 | // sender without messing up offsets. |
| 2050 | TEST_P(AbstractEventLoopTest, RawBasic) { |
| 2051 | auto loop1 = Make(); |
| 2052 | auto loop2 = MakePrimary(); |
| 2053 | auto loop3 = Make(); |
| 2054 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2055 | const FlatbufferDetachedBuffer<TestMessage> kMessage = |
| 2056 | JsonToFlatbuffer<TestMessage>("{}"); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2057 | |
| 2058 | std::unique_ptr<aos::RawSender> sender = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2059 | loop1->MakeRawSender(configuration::GetChannel( |
| 2060 | loop1->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2061 | |
| 2062 | std::unique_ptr<aos::RawFetcher> fetcher = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2063 | loop3->MakeRawFetcher(configuration::GetChannel( |
| 2064 | loop3->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2065 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2066 | loop2->OnRun([&]() { |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2067 | EXPECT_EQ(sender->Send(kMessage.span().data(), kMessage.span().size()), |
| 2068 | RawSender::Error::kOk); |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2069 | }); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2070 | |
| 2071 | bool happened = false; |
| 2072 | loop2->MakeRawWatcher( |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2073 | configuration::GetChannel(loop2->configuration(), "/test", |
| 2074 | "aos.TestMessage", "", nullptr), |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2075 | [this, &kMessage, &fetcher, &happened](const Context &context, |
| 2076 | const void *message) { |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2077 | happened = true; |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2078 | EXPECT_EQ( |
| 2079 | kMessage.span(), |
| 2080 | absl::Span<const uint8_t>( |
| 2081 | reinterpret_cast<const uint8_t *>(message), context.size)); |
| 2082 | EXPECT_EQ(message, context.data); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2083 | |
| 2084 | ASSERT_TRUE(fetcher->Fetch()); |
| 2085 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2086 | EXPECT_EQ(kMessage.span(), |
| 2087 | absl::Span<const uint8_t>(reinterpret_cast<const uint8_t *>( |
| 2088 | fetcher->context().data), |
| 2089 | fetcher->context().size)); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 2090 | |
| 2091 | this->Exit(); |
| 2092 | }); |
| 2093 | |
| 2094 | EXPECT_FALSE(happened); |
| 2095 | Run(); |
| 2096 | EXPECT_TRUE(happened); |
| 2097 | } |
| 2098 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2099 | // 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] | 2100 | // sender without messing up offsets, using the RawSpan overload. |
| 2101 | TEST_P(AbstractEventLoopTest, RawBasicSharedSpan) { |
| 2102 | auto loop1 = Make(); |
| 2103 | auto loop2 = MakePrimary(); |
| 2104 | auto loop3 = Make(); |
| 2105 | |
| 2106 | const FlatbufferDetachedBuffer<TestMessage> kMessage = |
| 2107 | JsonToFlatbuffer<TestMessage>("{}"); |
| 2108 | |
| 2109 | std::unique_ptr<aos::RawSender> sender = |
| 2110 | loop1->MakeRawSender(configuration::GetChannel( |
| 2111 | loop1->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
| 2112 | |
| 2113 | std::unique_ptr<aos::RawFetcher> fetcher = |
| 2114 | loop3->MakeRawFetcher(configuration::GetChannel( |
| 2115 | loop3->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
| 2116 | |
| 2117 | loop2->OnRun([&]() { |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2118 | EXPECT_EQ(sender->Send(std::make_shared<absl::Span<const uint8_t>>( |
| 2119 | kMessage.span().data(), kMessage.span().size())), |
| 2120 | RawSender::Error::kOk); |
Brian Silverman | bf88992 | 2021-11-10 12:41:57 -0800 | [diff] [blame] | 2121 | }); |
| 2122 | |
| 2123 | bool happened = false; |
| 2124 | loop2->MakeRawWatcher( |
| 2125 | configuration::GetChannel(loop2->configuration(), "/test", |
| 2126 | "aos.TestMessage", "", nullptr), |
| 2127 | [this, &kMessage, &fetcher, &happened](const Context &context, |
| 2128 | const void *message) { |
| 2129 | happened = true; |
| 2130 | EXPECT_EQ( |
| 2131 | kMessage.span(), |
| 2132 | absl::Span<const uint8_t>( |
| 2133 | reinterpret_cast<const uint8_t *>(message), context.size)); |
| 2134 | EXPECT_EQ(message, context.data); |
| 2135 | |
| 2136 | ASSERT_TRUE(fetcher->Fetch()); |
| 2137 | |
| 2138 | EXPECT_EQ(kMessage.span(), |
| 2139 | absl::Span<const uint8_t>(reinterpret_cast<const uint8_t *>( |
| 2140 | fetcher->context().data), |
| 2141 | fetcher->context().size)); |
| 2142 | |
| 2143 | this->Exit(); |
| 2144 | }); |
| 2145 | |
| 2146 | EXPECT_FALSE(happened); |
| 2147 | Run(); |
| 2148 | EXPECT_TRUE(happened); |
| 2149 | } |
| 2150 | |
| 2151 | // 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] | 2152 | // sender with remote times filled out. |
| 2153 | TEST_P(AbstractEventLoopTest, RawRemoteTimes) { |
| 2154 | auto loop1 = Make(); |
| 2155 | auto loop2 = MakePrimary(); |
| 2156 | auto loop3 = Make(); |
| 2157 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2158 | const FlatbufferDetachedBuffer<TestMessage> kMessage = |
| 2159 | JsonToFlatbuffer<TestMessage>("{}"); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2160 | |
| 2161 | const aos::monotonic_clock::time_point monotonic_remote_time = |
| 2162 | aos::monotonic_clock::time_point(chrono::seconds(1501)); |
| 2163 | const aos::realtime_clock::time_point realtime_remote_time = |
| 2164 | aos::realtime_clock::time_point(chrono::seconds(3132)); |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 2165 | const uint32_t remote_queue_index = 0x254971; |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 2166 | const UUID source_boot_uuid = UUID::Random(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2167 | |
| 2168 | std::unique_ptr<aos::RawSender> sender = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2169 | loop1->MakeRawSender(configuration::GetChannel( |
| 2170 | loop1->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2171 | |
| 2172 | std::unique_ptr<aos::RawFetcher> fetcher = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2173 | loop3->MakeRawFetcher(configuration::GetChannel( |
| 2174 | loop3->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2175 | |
| 2176 | loop2->OnRun([&]() { |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2177 | EXPECT_EQ(sender->Send(kMessage.span().data(), kMessage.span().size(), |
| 2178 | monotonic_remote_time, realtime_remote_time, |
| 2179 | remote_queue_index, source_boot_uuid), |
| 2180 | RawSender::Error::kOk); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2181 | }); |
| 2182 | |
| 2183 | bool happened = false; |
| 2184 | loop2->MakeRawWatcher( |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2185 | configuration::GetChannel(loop2->configuration(), "/test", |
| 2186 | "aos.TestMessage", "", nullptr), |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 2187 | [this, monotonic_remote_time, realtime_remote_time, source_boot_uuid, |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 2188 | remote_queue_index, &fetcher, |
| 2189 | &happened](const Context &context, const void * /*message*/) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2190 | happened = true; |
| 2191 | EXPECT_EQ(monotonic_remote_time, context.monotonic_remote_time); |
| 2192 | EXPECT_EQ(realtime_remote_time, context.realtime_remote_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 2193 | EXPECT_EQ(source_boot_uuid, context.source_boot_uuid); |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 2194 | EXPECT_EQ(remote_queue_index, context.remote_queue_index); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2195 | |
| 2196 | ASSERT_TRUE(fetcher->Fetch()); |
| 2197 | EXPECT_EQ(monotonic_remote_time, |
| 2198 | fetcher->context().monotonic_remote_time); |
| 2199 | EXPECT_EQ(realtime_remote_time, |
| 2200 | fetcher->context().realtime_remote_time); |
| 2201 | |
| 2202 | this->Exit(); |
| 2203 | }); |
| 2204 | |
| 2205 | EXPECT_FALSE(happened); |
| 2206 | Run(); |
| 2207 | EXPECT_TRUE(happened); |
| 2208 | } |
| 2209 | |
| 2210 | // Tests that a raw sender fills out sent data. |
| 2211 | TEST_P(AbstractEventLoopTest, RawSenderSentData) { |
| 2212 | auto loop1 = MakePrimary(); |
| 2213 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 2214 | const FlatbufferDetachedBuffer<TestMessage> kMessage = |
| 2215 | JsonToFlatbuffer<TestMessage>("{}"); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2216 | |
| 2217 | std::unique_ptr<aos::RawSender> sender = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2218 | loop1->MakeRawSender(configuration::GetChannel( |
| 2219 | loop1->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2220 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2221 | const aos::monotonic_clock::time_point monotonic_now = loop1->monotonic_now(); |
| 2222 | const aos::realtime_clock::time_point realtime_now = loop1->realtime_now(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2223 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2224 | EXPECT_EQ(sender->Send(kMessage.span().data(), kMessage.span().size()), |
| 2225 | RawSender::Error::kOk); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2226 | |
| 2227 | EXPECT_GE(sender->monotonic_sent_time(), monotonic_now); |
| 2228 | EXPECT_LE(sender->monotonic_sent_time(), |
| 2229 | monotonic_now + chrono::milliseconds(100)); |
| 2230 | EXPECT_GE(sender->realtime_sent_time(), realtime_now); |
| 2231 | EXPECT_LE(sender->realtime_sent_time(), |
| 2232 | realtime_now + chrono::milliseconds(100)); |
| 2233 | EXPECT_EQ(sender->sent_queue_index(), 0u); |
| 2234 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2235 | EXPECT_EQ(sender->Send(kMessage.span().data(), kMessage.span().size()), |
| 2236 | RawSender::Error::kOk); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 2237 | |
| 2238 | EXPECT_GE(sender->monotonic_sent_time(), monotonic_now); |
| 2239 | EXPECT_LE(sender->monotonic_sent_time(), |
| 2240 | monotonic_now + chrono::milliseconds(100)); |
| 2241 | EXPECT_GE(sender->realtime_sent_time(), realtime_now); |
| 2242 | EXPECT_LE(sender->realtime_sent_time(), |
| 2243 | realtime_now + chrono::milliseconds(100)); |
| 2244 | EXPECT_EQ(sender->sent_queue_index(), 1u); |
| 2245 | } |
| 2246 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 2247 | // Tests that not setting up nodes results in no node. |
| 2248 | TEST_P(AbstractEventLoopTest, NoNode) { |
| 2249 | auto loop1 = Make(); |
| 2250 | auto loop2 = MakePrimary(); |
| 2251 | |
| 2252 | EXPECT_EQ(loop1->node(), nullptr); |
| 2253 | EXPECT_EQ(loop2->node(), nullptr); |
| 2254 | } |
| 2255 | |
| 2256 | // Tests that setting up nodes results in node being set. |
| 2257 | TEST_P(AbstractEventLoopTest, Node) { |
| 2258 | EnableNodes("me"); |
| 2259 | |
| 2260 | auto loop1 = Make(); |
| 2261 | auto loop2 = MakePrimary(); |
| 2262 | |
| 2263 | EXPECT_NE(loop1->node(), nullptr); |
| 2264 | EXPECT_NE(loop2->node(), nullptr); |
| 2265 | } |
| 2266 | |
| 2267 | // Tests that watchers work with a node setup. |
| 2268 | TEST_P(AbstractEventLoopTest, NodeWatcher) { |
| 2269 | EnableNodes("me"); |
| 2270 | |
| 2271 | auto loop1 = Make(); |
| 2272 | auto loop2 = Make(); |
| 2273 | loop1->MakeWatcher("/test", [](const TestMessage &) {}); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2274 | loop2->MakeRawWatcher( |
| 2275 | configuration::GetChannel(configuration(), "/test", "aos.TestMessage", "", |
| 2276 | nullptr), |
| 2277 | [](const Context &, const void *) {}); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 2278 | } |
| 2279 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 2280 | // Tests that no-arg watchers work with a node setup. |
| 2281 | TEST_P(AbstractEventLoopTest, NodeNoArgWatcher) { |
| 2282 | EnableNodes("me"); |
| 2283 | |
| 2284 | auto loop1 = Make(); |
| 2285 | auto loop2 = Make(); |
| 2286 | loop1->MakeWatcher("/test", [](const TestMessage &) {}); |
| 2287 | loop2->MakeRawNoArgWatcher( |
| 2288 | configuration::GetChannel(configuration(), "/test", "aos.TestMessage", "", |
| 2289 | nullptr), |
| 2290 | [](const Context &) {}); |
| 2291 | } |
| 2292 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 2293 | // Tests that fetcher work with a node setup. |
| 2294 | TEST_P(AbstractEventLoopTest, NodeFetcher) { |
| 2295 | EnableNodes("me"); |
| 2296 | auto loop1 = Make(); |
| 2297 | |
| 2298 | auto fetcher = loop1->MakeFetcher<TestMessage>("/test"); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2299 | auto raw_fetcher = loop1->MakeRawFetcher(configuration::GetChannel( |
| 2300 | configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | // Tests that sender work with a node setup. |
| 2304 | TEST_P(AbstractEventLoopTest, NodeSender) { |
| 2305 | EnableNodes("me"); |
| 2306 | auto loop1 = Make(); |
| 2307 | |
| 2308 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
| 2309 | } |
| 2310 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 2311 | // Tests that a non-realtime event loop timer is marked non-realtime. |
| 2312 | TEST_P(AbstractEventLoopTest, NonRealtimeEventLoopTimer) { |
| 2313 | auto loop1 = MakePrimary(); |
| 2314 | |
| 2315 | // Add a timer to actually quit. |
| 2316 | auto test_timer = loop1->AddTimer([this]() { |
| 2317 | CheckNotRealtime(); |
| 2318 | this->Exit(); |
| 2319 | }); |
| 2320 | |
| 2321 | loop1->OnRun([&test_timer, &loop1]() { |
| 2322 | CheckNotRealtime(); |
| 2323 | test_timer->Setup(loop1->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 2324 | }); |
| 2325 | |
| 2326 | Run(); |
| 2327 | } |
| 2328 | |
| 2329 | // Tests that a realtime event loop timer is marked realtime. |
| 2330 | TEST_P(AbstractEventLoopTest, RealtimeEventLoopTimer) { |
| 2331 | auto loop1 = MakePrimary(); |
| 2332 | |
| 2333 | loop1->SetRuntimeRealtimePriority(1); |
| 2334 | |
| 2335 | // Add a timer to actually quit. |
| 2336 | auto test_timer = loop1->AddTimer([this]() { |
| 2337 | CheckRealtime(); |
| 2338 | this->Exit(); |
| 2339 | }); |
| 2340 | |
| 2341 | loop1->OnRun([&test_timer, &loop1]() { |
| 2342 | CheckRealtime(); |
| 2343 | test_timer->Setup(loop1->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 2344 | }); |
| 2345 | |
| 2346 | Run(); |
| 2347 | } |
| 2348 | |
| 2349 | // Tests that a non-realtime event loop phased loop is marked non-realtime. |
| 2350 | TEST_P(AbstractEventLoopTest, NonRealtimeEventLoopPhasedLoop) { |
| 2351 | auto loop1 = MakePrimary(); |
| 2352 | |
| 2353 | // Add a timer to actually quit. |
| 2354 | loop1->AddPhasedLoop( |
| 2355 | [this](int) { |
| 2356 | CheckNotRealtime(); |
| 2357 | this->Exit(); |
| 2358 | }, |
| 2359 | chrono::seconds(1), chrono::seconds(0)); |
| 2360 | |
| 2361 | Run(); |
| 2362 | } |
| 2363 | |
| 2364 | // Tests that a realtime event loop phased loop is marked realtime. |
| 2365 | TEST_P(AbstractEventLoopTest, RealtimeEventLoopPhasedLoop) { |
| 2366 | auto loop1 = MakePrimary(); |
| 2367 | |
| 2368 | loop1->SetRuntimeRealtimePriority(1); |
| 2369 | |
| 2370 | // Add a timer to actually quit. |
| 2371 | loop1->AddPhasedLoop( |
| 2372 | [this](int) { |
| 2373 | CheckRealtime(); |
| 2374 | this->Exit(); |
| 2375 | }, |
| 2376 | chrono::seconds(1), chrono::seconds(0)); |
| 2377 | |
| 2378 | Run(); |
| 2379 | } |
| 2380 | |
| 2381 | // Tests that a non-realtime event loop watcher is marked non-realtime. |
| 2382 | TEST_P(AbstractEventLoopTest, NonRealtimeEventLoopWatcher) { |
| 2383 | auto loop1 = MakePrimary(); |
| 2384 | auto loop2 = Make(); |
| 2385 | |
| 2386 | aos::Sender<TestMessage> sender = loop2->MakeSender<TestMessage>("/test"); |
| 2387 | |
| 2388 | loop1->OnRun([&]() { |
| 2389 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 2390 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2391 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 2392 | }); |
| 2393 | |
| 2394 | loop1->MakeWatcher("/test", [&](const TestMessage &) { |
| 2395 | CheckNotRealtime(); |
| 2396 | this->Exit(); |
| 2397 | }); |
| 2398 | |
| 2399 | Run(); |
| 2400 | } |
| 2401 | |
| 2402 | // Tests that a realtime event loop watcher is marked realtime. |
| 2403 | TEST_P(AbstractEventLoopTest, RealtimeEventLoopWatcher) { |
| 2404 | auto loop1 = MakePrimary(); |
| 2405 | auto loop2 = Make(); |
| 2406 | |
| 2407 | loop1->SetRuntimeRealtimePriority(1); |
| 2408 | |
| 2409 | aos::Sender<TestMessage> sender = loop2->MakeSender<TestMessage>("/test"); |
| 2410 | |
| 2411 | loop1->OnRun([&]() { |
| 2412 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 2413 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2414 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 2415 | }); |
| 2416 | |
| 2417 | loop1->MakeWatcher("/test", [&](const TestMessage &) { |
| 2418 | CheckRealtime(); |
| 2419 | this->Exit(); |
| 2420 | }); |
| 2421 | |
| 2422 | Run(); |
| 2423 | } |
| 2424 | |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 2425 | // Tests that event loop's context's monotonic time is set to a value on OnRun. |
| 2426 | TEST_P(AbstractEventLoopTest, SetContextOnRun) { |
| 2427 | auto loop = MakePrimary(); |
| 2428 | |
| 2429 | // We want to check that monotonic event time is before monotonic now |
| 2430 | // called inside of callback, but after time point obtained callback. |
| 2431 | aos::monotonic_clock::time_point monotonic_event_time_on_run; |
| 2432 | |
| 2433 | loop->OnRun([&]() { |
| 2434 | monotonic_event_time_on_run = loop->context().monotonic_event_time; |
| 2435 | EXPECT_LE(monotonic_event_time_on_run, loop->monotonic_now()); |
| 2436 | EXPECT_EQ(loop->context().monotonic_remote_time, monotonic_clock::min_time); |
| 2437 | EXPECT_EQ(loop->context().realtime_event_time, realtime_clock::min_time); |
| 2438 | EXPECT_EQ(loop->context().realtime_remote_time, realtime_clock::min_time); |
| 2439 | EXPECT_EQ(loop->context().source_boot_uuid, loop->boot_uuid()); |
| 2440 | EXPECT_EQ(loop->context().queue_index, 0xffffffffu); |
| 2441 | EXPECT_EQ(loop->context().size, 0u); |
| 2442 | EXPECT_EQ(loop->context().data, nullptr); |
| 2443 | EXPECT_EQ(loop->context().buffer_index, -1); |
| 2444 | }); |
| 2445 | |
| 2446 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(200)); |
| 2447 | |
| 2448 | const aos::monotonic_clock::time_point before_run_time = |
| 2449 | loop->monotonic_now(); |
| 2450 | Run(); |
| 2451 | EXPECT_GE(monotonic_event_time_on_run, before_run_time); |
| 2452 | } |
| 2453 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 2454 | // Tests that watchers fail when created on the wrong node. |
| 2455 | TEST_P(AbstractEventLoopDeathTest, NodeWatcher) { |
| 2456 | EnableNodes("them"); |
| 2457 | |
| 2458 | auto loop1 = Make(); |
| 2459 | auto loop2 = Make(); |
| 2460 | EXPECT_DEATH({ loop1->MakeWatcher("/test", [](const TestMessage &) {}); }, |
| 2461 | "node"); |
| 2462 | EXPECT_DEATH( |
| 2463 | { |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2464 | loop2->MakeRawWatcher( |
| 2465 | configuration::GetChannel(configuration(), "/test", |
| 2466 | "aos.TestMessage", "", nullptr), |
| 2467 | [](const Context &, const void *) {}); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 2468 | }, |
| 2469 | "node"); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 2470 | EXPECT_DEATH({ loop1->MakeNoArgWatcher<TestMessage>("/test", []() {}); }, |
| 2471 | "node"); |
| 2472 | EXPECT_DEATH( |
| 2473 | { |
| 2474 | loop2->MakeRawNoArgWatcher( |
| 2475 | configuration::GetChannel(configuration(), "/test", |
| 2476 | "aos.TestMessage", "", nullptr), |
| 2477 | [](const Context &) {}); |
| 2478 | }, |
| 2479 | "node"); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | // Tests that fetchers fail when created on the wrong node. |
| 2483 | TEST_P(AbstractEventLoopDeathTest, NodeFetcher) { |
| 2484 | EnableNodes("them"); |
| 2485 | auto loop1 = Make(); |
| 2486 | |
| 2487 | EXPECT_DEATH({ auto fetcher = loop1->MakeFetcher<TestMessage>("/test"); }, |
| 2488 | "node"); |
| 2489 | EXPECT_DEATH( |
| 2490 | { |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 2491 | auto raw_fetcher = loop1->MakeRawFetcher(configuration::GetChannel( |
| 2492 | configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 2493 | }, |
| 2494 | "node"); |
| 2495 | } |
| 2496 | |
| 2497 | // Tests that senders fail when created on the wrong node. |
| 2498 | TEST_P(AbstractEventLoopDeathTest, NodeSender) { |
| 2499 | EnableNodes("them"); |
| 2500 | auto loop1 = Make(); |
| 2501 | |
| 2502 | EXPECT_DEATH( |
| 2503 | { |
| 2504 | aos::Sender<TestMessage> sender = |
| 2505 | loop1->MakeSender<TestMessage>("/test"); |
| 2506 | }, |
| 2507 | "node"); |
| 2508 | |
| 2509 | // Note: Creating raw senders is always supported. Right now, this lets us |
| 2510 | // use them to create message_gateway. |
| 2511 | } |
| 2512 | |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 2513 | // Tests creating multiple Builders from a single Sender at the same time. |
| 2514 | TEST_P(AbstractEventLoopDeathTest, MultipleBuilders) { |
| 2515 | auto loop1 = Make(); |
| 2516 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
| 2517 | |
| 2518 | { auto builder = sender.MakeBuilder(); } |
| 2519 | { |
| 2520 | auto builder = sender.MakeBuilder(); |
| 2521 | builder.MakeBuilder<TestMessage>().Finish(); |
| 2522 | } |
| 2523 | { |
| 2524 | // Creating this after the first one was destroyed should be fine. |
| 2525 | auto builder = sender.MakeBuilder(); |
| 2526 | builder.MakeBuilder<TestMessage>().Finish(); |
| 2527 | // But not a second one. |
| 2528 | EXPECT_DEATH(sender.MakeBuilder().MakeBuilder<TestMessage>().Finish(), |
| 2529 | "May not overwrite in-use allocator"); |
| 2530 | } |
| 2531 | |
| 2532 | FlatbufferDetachedBuffer<TestMessage> detached = |
| 2533 | flatbuffers::DetachedBuffer(); |
| 2534 | { |
| 2535 | auto builder = sender.MakeBuilder(); |
| 2536 | detached = builder.Detach(builder.MakeBuilder<TestMessage>().Finish()); |
| 2537 | } |
| 2538 | { |
| 2539 | // This is the second one, after the detached one, so it should fail. |
| 2540 | EXPECT_DEATH(sender.MakeBuilder().MakeBuilder<TestMessage>().Finish(), |
| 2541 | "May not overwrite in-use allocator"); |
| 2542 | } |
| 2543 | |
| 2544 | // Clear the detached one, and then we should be able to create another. |
| 2545 | detached = flatbuffers::DetachedBuffer(); |
| 2546 | { |
| 2547 | auto builder = sender.MakeBuilder(); |
| 2548 | builder.MakeBuilder<TestMessage>().Finish(); |
| 2549 | } |
| 2550 | |
| 2551 | // And then detach another one. |
| 2552 | { |
| 2553 | auto builder = sender.MakeBuilder(); |
| 2554 | detached = builder.Detach(builder.MakeBuilder<TestMessage>().Finish()); |
| 2555 | } |
| 2556 | } |
| 2557 | |
| 2558 | // Tests sending a buffer detached from a different builder. |
| 2559 | TEST_P(AbstractEventLoopDeathTest, WrongDetachedBuffer) { |
| 2560 | auto loop1 = Make(); |
| 2561 | aos::Sender<TestMessage> sender1 = loop1->MakeSender<TestMessage>("/test"); |
| 2562 | aos::Sender<TestMessage> sender2 = loop1->MakeSender<TestMessage>("/test"); |
| 2563 | |
| 2564 | auto builder = sender1.MakeBuilder(); |
| 2565 | FlatbufferDetachedBuffer<TestMessage> detached = |
| 2566 | builder.Detach(builder.MakeBuilder<TestMessage>().Finish()); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 2567 | EXPECT_DEATH(sender2.CheckOk(sender2.SendDetached(std::move(detached))), |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 2568 | "May only send the buffer detached from this Sender"); |
| 2569 | } |
| 2570 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame^] | 2571 | int TestChannelFrequency(EventLoop *event_loop) { |
| 2572 | return event_loop->GetChannel<TestMessage>("/test")->frequency(); |
| 2573 | } |
| 2574 | |
| 2575 | int TestChannelQueueSize(EventLoop *event_loop) { |
| 2576 | const int frequency = TestChannelFrequency(event_loop); |
| 2577 | const auto channel_storage_duration = std::chrono::nanoseconds( |
| 2578 | event_loop->configuration()->channel_storage_duration()); |
| 2579 | const int queue_size = |
| 2580 | frequency * std::chrono::duration_cast<std::chrono::duration<double>>( |
| 2581 | channel_storage_duration) |
| 2582 | .count(); |
| 2583 | |
| 2584 | return queue_size; |
| 2585 | } |
| 2586 | |
| 2587 | RawSender::Error SendTestMessage(aos::Sender<TestMessage> &sender) { |
| 2588 | aos::Sender<TestMessage>::Builder builder = sender.MakeBuilder(); |
| 2589 | TestMessage::Builder test_message_builder = |
| 2590 | builder.MakeBuilder<TestMessage>(); |
| 2591 | test_message_builder.add_value(0); |
| 2592 | return builder.Send(test_message_builder.Finish()); |
| 2593 | } |
| 2594 | |
| 2595 | // Test that sending messages too fast returns |
| 2596 | // RawSender::Error::kMessagesSentTooFast. |
| 2597 | TEST_P(AbstractEventLoopTest, SendingMessagesTooFast) { |
| 2598 | auto event_loop = MakePrimary(); |
| 2599 | |
| 2600 | auto sender = event_loop->MakeSender<TestMessage>("/test"); |
| 2601 | |
| 2602 | // Send one message in the beginning, then wait until the |
| 2603 | // channel_storage_duration is almost done and start sending messages rapidly, |
| 2604 | // having some come in the next chanel_storage_duration. The queue_size is |
| 2605 | // 1600, so the 1601st message will be the last valid one (the initial message |
| 2606 | // having being sent more than a channel_storage_duration ago), and trying to |
| 2607 | // send the 1602nd message should return |
| 2608 | // RawSender::Error::kMessagesSentTooFast. |
| 2609 | EXPECT_EQ(SendTestMessage(sender), RawSender::Error::kOk); |
| 2610 | int msgs_sent = 1; |
| 2611 | const int queue_size = TestChannelQueueSize(event_loop.get()); |
| 2612 | |
| 2613 | const auto timer = event_loop->AddTimer([&]() { |
| 2614 | const bool done = (msgs_sent == queue_size + 1); |
| 2615 | ASSERT_EQ( |
| 2616 | SendTestMessage(sender), |
| 2617 | done ? RawSender::Error::kMessagesSentTooFast : RawSender::Error::kOk); |
| 2618 | msgs_sent++; |
| 2619 | if (done) { |
| 2620 | Exit(); |
| 2621 | } |
| 2622 | }); |
| 2623 | |
| 2624 | const auto kRepeatOffset = std::chrono::milliseconds(1); |
| 2625 | const auto base_offset = |
| 2626 | std::chrono::nanoseconds( |
| 2627 | event_loop->configuration()->channel_storage_duration()) - |
| 2628 | (kRepeatOffset * (queue_size / 2)); |
| 2629 | event_loop->OnRun([&event_loop, &timer, &base_offset, &kRepeatOffset]() { |
| 2630 | timer->Setup(event_loop->monotonic_now() + base_offset, kRepeatOffset); |
| 2631 | }); |
| 2632 | |
| 2633 | Run(); |
| 2634 | } |
| 2635 | |
| 2636 | // Tests that we are able to send messages successfully after sending messages |
| 2637 | // too fast and waiting while continuously attempting to send messages. |
| 2638 | // Also tests that SendFailureCounter is working correctly in this |
| 2639 | // situation |
| 2640 | TEST_P(AbstractEventLoopTest, SendingAfterSendingTooFast) { |
| 2641 | auto event_loop = MakePrimary(); |
| 2642 | |
| 2643 | auto sender = event_loop->MakeSender<TestMessage>("/test"); |
| 2644 | |
| 2645 | // We are sending messages at 1 kHz, so we will be sending too fast after |
| 2646 | // queue_size (1600) ms. After this, keep sending messages, and exactly a |
| 2647 | // channel storage duration (2s) after we send the first message we should |
| 2648 | // be able to successfully send a message. |
| 2649 | |
| 2650 | const monotonic_clock::duration kInterval = std::chrono::milliseconds(1); |
| 2651 | const monotonic_clock::duration channel_storage_duration = |
| 2652 | std::chrono::nanoseconds( |
| 2653 | event_loop->configuration()->channel_storage_duration()); |
| 2654 | const int queue_size = TestChannelQueueSize(event_loop.get()); |
| 2655 | |
| 2656 | int msgs_sent = 0; |
| 2657 | SendFailureCounter counter; |
| 2658 | auto start = monotonic_clock::min_time; |
| 2659 | |
| 2660 | event_loop->AddPhasedLoop( |
| 2661 | [&](int) { |
| 2662 | const auto actual_err = SendTestMessage(sender); |
| 2663 | const bool done_waiting = (start != monotonic_clock::min_time && |
| 2664 | sender.monotonic_sent_time() >= |
| 2665 | (start + channel_storage_duration)); |
| 2666 | const auto expected_err = |
| 2667 | (msgs_sent < queue_size || done_waiting |
| 2668 | ? RawSender::Error::kOk |
| 2669 | : RawSender::Error::kMessagesSentTooFast); |
| 2670 | |
| 2671 | if (start == monotonic_clock::min_time) { |
| 2672 | start = sender.monotonic_sent_time(); |
| 2673 | } |
| 2674 | |
| 2675 | ASSERT_EQ(actual_err, expected_err); |
| 2676 | counter.Count(actual_err); |
| 2677 | msgs_sent++; |
| 2678 | |
| 2679 | EXPECT_EQ(counter.failures(), |
| 2680 | msgs_sent <= queue_size |
| 2681 | ? 0 |
| 2682 | : (msgs_sent - queue_size) - |
| 2683 | (actual_err == RawSender::Error::kOk ? 1 : 0)); |
| 2684 | EXPECT_EQ(counter.just_failed(), actual_err != RawSender::Error::kOk); |
| 2685 | |
| 2686 | if (done_waiting) { |
| 2687 | Exit(); |
| 2688 | } |
| 2689 | }, |
| 2690 | kInterval); |
| 2691 | Run(); |
| 2692 | } |
| 2693 | |
| 2694 | // Tests that RawSender::Error::kMessagesSentTooFast is returned |
| 2695 | // when messages are sent too fast from senders in different loops |
| 2696 | TEST_P(AbstractEventLoopTest, SendingTooFastWithMultipleLoops) { |
| 2697 | auto loop1 = MakePrimary(); |
| 2698 | auto loop2 = Make(); |
| 2699 | |
| 2700 | auto sender1 = loop1->MakeSender<TestMessage>("/test"); |
| 2701 | auto sender2 = loop2->MakeSender<TestMessage>("/test"); |
| 2702 | |
| 2703 | // Send queue_size messages split between the senders. |
| 2704 | const int queue_size = TestChannelQueueSize(loop1.get()); |
| 2705 | for (int i = 0; i < queue_size / 2; i++) { |
| 2706 | ASSERT_EQ(SendTestMessage(sender1), RawSender::Error::kOk); |
| 2707 | ASSERT_EQ(SendTestMessage(sender2), RawSender::Error::kOk); |
| 2708 | } |
| 2709 | |
| 2710 | // Since queue_size messages have been sent, this should return an error |
| 2711 | EXPECT_EQ(SendTestMessage(sender2), RawSender::Error::kMessagesSentTooFast); |
| 2712 | } |
| 2713 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 2714 | } // namespace testing |
| 2715 | } // namespace aos |