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