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