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