blob: b976d8ff23f5abcab9e464cd38f822cfcfc2aa6d [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#include "aos/events/event_loop_param_test.h"
Parker Schuhe4a70d62017-12-27 20:10:20 -08002
Austin Schuh52d325c2019-06-23 18:59:06 -07003#include <chrono>
Brian Silverman4f4e0612020-08-12 19:54:41 -07004#include <unordered_map>
5#include <unordered_set>
Austin Schuh52d325c2019-06-23 18:59:06 -07006
Austin Schuh54cf95f2019-11-29 13:14:18 -08007#include "aos/flatbuffer_merge.h"
Austin Schuhcc6070c2020-10-10 20:25:56 -07008#include "aos/realtime.h"
Austin Schuh54cf95f2019-11-29 13:14:18 -08009#include "glog/logging.h"
Tyler Chatow67ddb032020-01-12 14:30:04 -080010#include "gmock/gmock.h"
11#include "gtest/gtest.h"
Austin Schuh9fe68f72019-08-10 19:32:03 -070012
Parker Schuhe4a70d62017-12-27 20:10:20 -080013namespace aos {
14namespace testing {
Austin Schuh52d325c2019-06-23 18:59:06 -070015namespace {
16namespace chrono = ::std::chrono;
17} // namespace
Parker Schuhe4a70d62017-12-27 20:10:20 -080018
Brian Silverman4f4e0612020-08-12 19:54:41 -070019::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 Schuh6bae8252021-02-07 22:01:49 -080027 auto result = factory_->Make(name_copy);
28 if (do_timing_reports() == DoTimingReports::kNo) {
29 result->SkipTimingReport();
30 }
31 return result;
Brian Silverman4f4e0612020-08-12 19:54:41 -070032}
33
34void 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 Schuh6b6dfa52019-06-12 20:16:20 -070071// Tests that watcher can receive messages from a sender.
Parker Schuhe4a70d62017-12-27 20:10:20 -080072// Also tests that OnRun() works.
73TEST_P(AbstractEventLoopTest, Basic) {
74 auto loop1 = Make();
Austin Schuh6b6dfa52019-06-12 20:16:20 -070075 auto loop2 = MakePrimary();
76
Alex Perrycb7da4b2019-08-28 19:35:56 -070077 aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test");
Austin Schuh6b6dfa52019-06-12 20:16:20 -070078
79 bool happened = false;
80
81 loop2->OnRun([&]() {
82 happened = true;
83
Alex Perrycb7da4b2019-08-28 19:35:56 -070084 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 Schuh6b6dfa52019-06-12 20:16:20 -070088 });
89
90 loop2->MakeWatcher("/test", [&](const TestMessage &message) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070091 EXPECT_EQ(message.value(), 200);
Austin Schuh9fe68f72019-08-10 19:32:03 -070092 this->Exit();
Austin Schuh6b6dfa52019-06-12 20:16:20 -070093 });
94
95 EXPECT_FALSE(happened);
96 Run();
97 EXPECT_TRUE(happened);
98}
99
Brian Silverman341b57e2020-06-23 16:23:18 -0700100// Tests that watcher can receive messages from a sender, sent via SendDetached.
101TEST_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 Silverman6b8a3c32020-03-06 11:26:14 -0800129// Verifies that a no-arg watcher will not have a data pointer.
130TEST_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 Silverman4f4e0612020-08-12 19:54:41 -0700149 EXPECT_EQ(-1, loop2->context().buffer_index);
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800150 this->Exit();
151 });
152
153 EXPECT_FALSE(happened);
154 Run();
155 EXPECT_TRUE(happened);
156}
157
Brian Silverman454bc112020-03-05 14:21:25 -0800158// Tests that no-arg watcher can receive messages from a sender.
159// Also tests that OnRun() works.
160TEST_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.
190TEST_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 Silverman0fc69932020-01-24 21:54:02 -0800218// Tests that watcher can receive messages from two senders.
219// Also tests that OnRun() works.
220TEST_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 Schuh6b6dfa52019-06-12 20:16:20 -0700259// Tests that a fetcher can fetch from a sender.
260// Also tests that OnRun() works.
261TEST_P(AbstractEventLoopTest, FetchWithoutRun) {
262 auto loop1 = Make();
Parker Schuhe4a70d62017-12-27 20:10:20 -0800263 auto loop2 = Make();
Austin Schuh44019f92019-05-19 19:58:27 -0700264 auto loop3 = MakePrimary();
Parker Schuhe4a70d62017-12-27 20:10:20 -0800265
266 auto sender = loop1->MakeSender<TestMessage>("/test");
267
268 auto fetcher = loop2->MakeFetcher<TestMessage>("/test");
269
Austin Schuhbbce72d2019-05-26 15:11:46 -0700270 EXPECT_FALSE(fetcher.Fetch());
Austin Schuh39788ff2019-12-01 18:22:57 -0800271 EXPECT_EQ(fetcher.get(), nullptr);
272
Austin Schuhad154822019-12-27 15:45:13 -0800273 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 Schuha9012be2021-07-21 15:19:11 -0700277 EXPECT_EQ(fetcher.context().source_boot_uuid, UUID::Zero());
Austin Schuh39788ff2019-12-01 18:22:57 -0800278 EXPECT_EQ(fetcher.context().queue_index, 0xffffffffu);
279 EXPECT_EQ(fetcher.context().size, 0u);
280 EXPECT_EQ(fetcher.context().data, nullptr);
Brian Silverman4f4e0612020-08-12 19:54:41 -0700281 EXPECT_EQ(fetcher.context().buffer_index, -1);
Austin Schuhbbce72d2019-05-26 15:11:46 -0700282
Alex Perrycb7da4b2019-08-28 19:35:56 -0700283 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
284 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
285 builder.add_value(200);
286 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh3578a2e2019-05-25 18:17:59 -0700287
288 EXPECT_TRUE(fetcher.Fetch());
289 ASSERT_FALSE(fetcher.get() == nullptr);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700290 EXPECT_EQ(fetcher.get()->value(), 200);
Austin Schuh39788ff2019-12-01 18:22:57 -0800291
292 const chrono::milliseconds kEpsilon(100);
293
Austin Schuhad154822019-12-27 15:45:13 -0800294 const aos::monotonic_clock::time_point monotonic_now = loop2->monotonic_now();
295 const aos::realtime_clock::time_point realtime_now = loop2->realtime_now();
296 EXPECT_EQ(fetcher.context().monotonic_event_time,
297 fetcher.context().monotonic_remote_time);
298 EXPECT_EQ(fetcher.context().realtime_event_time,
299 fetcher.context().realtime_remote_time);
300
301 EXPECT_GE(fetcher.context().monotonic_event_time, monotonic_now - kEpsilon);
302 EXPECT_LE(fetcher.context().monotonic_event_time, monotonic_now + kEpsilon);
303 EXPECT_GE(fetcher.context().realtime_event_time, realtime_now - kEpsilon);
304 EXPECT_LE(fetcher.context().realtime_event_time, realtime_now + kEpsilon);
Austin Schuha9012be2021-07-21 15:19:11 -0700305 EXPECT_EQ(fetcher.context().source_boot_uuid, loop2->boot_uuid());
Austin Schuh39788ff2019-12-01 18:22:57 -0800306 EXPECT_EQ(fetcher.context().queue_index, 0x0u);
307 EXPECT_EQ(fetcher.context().size, 20u);
308 EXPECT_NE(fetcher.context().data, nullptr);
Brian Silverman4f4e0612020-08-12 19:54:41 -0700309 if (read_method() == ReadMethod::PIN) {
310 EXPECT_GE(fetcher.context().buffer_index, 0);
311 EXPECT_LT(fetcher.context().buffer_index,
312 loop2->NumberBuffers(fetcher.channel()));
313 } else {
314 EXPECT_EQ(fetcher.context().buffer_index, -1);
315 }
Parker Schuhe4a70d62017-12-27 20:10:20 -0800316}
317
Austin Schuh3578a2e2019-05-25 18:17:59 -0700318// Tests that watcher will receive all messages sent if they are sent after
319// initialization and before running.
320TEST_P(AbstractEventLoopTest, DoubleSendAtStartup) {
321 auto loop1 = Make();
322 auto loop2 = MakePrimary();
323
324 auto sender = loop1->MakeSender<TestMessage>("/test");
325
326 ::std::vector<int> values;
327
328 loop2->MakeWatcher("/test", [&](const TestMessage &message) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700329 values.push_back(message.value());
Austin Schuh3578a2e2019-05-25 18:17:59 -0700330 if (values.size() == 2) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700331 this->Exit();
Austin Schuh3578a2e2019-05-25 18:17:59 -0700332 }
333 });
334
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700335 // Before Run, should be ignored.
Austin Schuh3578a2e2019-05-25 18:17:59 -0700336 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700337 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
338 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
339 builder.add_value(199);
340 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh3578a2e2019-05-25 18:17:59 -0700341 }
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700342
343 loop2->OnRun([&]() {
344 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700345 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
346 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
347 builder.add_value(200);
348 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700349 }
350 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700351 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
352 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
353 builder.add_value(201);
354 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700355 }
356 });
Austin Schuh3578a2e2019-05-25 18:17:59 -0700357
358 Run();
359
360 EXPECT_THAT(values, ::testing::ElementsAreArray({200, 201}));
361}
362
363// Tests that watcher will not receive messages sent before the watcher is
364// created.
365TEST_P(AbstractEventLoopTest, DoubleSendAfterStartup) {
366 auto loop1 = Make();
367 auto loop2 = MakePrimary();
368
369 auto sender = loop1->MakeSender<TestMessage>("/test");
370
371 ::std::vector<int> values;
372
373 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700374 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
375 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
376 builder.add_value(200);
377 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh3578a2e2019-05-25 18:17:59 -0700378 }
379 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700380 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
381 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
382 builder.add_value(201);
383 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh3578a2e2019-05-25 18:17:59 -0700384 }
385
386 loop2->MakeWatcher("/test", [&](const TestMessage &message) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700387 values.push_back(message.value());
Austin Schuh3578a2e2019-05-25 18:17:59 -0700388 });
389
390 // Add a timer to actually quit.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700391 auto test_timer = loop2->AddTimer([this]() { this->Exit(); });
Austin Schuh3578a2e2019-05-25 18:17:59 -0700392 loop2->OnRun([&test_timer, &loop2]() {
393 test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100));
394 });
395
396 Run();
397 EXPECT_EQ(0, values.size());
398}
399
Austin Schuhbbce72d2019-05-26 15:11:46 -0700400// Tests that FetchNext gets all the messages sent after it is constructed.
401TEST_P(AbstractEventLoopTest, FetchNext) {
402 auto loop1 = Make();
403 auto loop2 = MakePrimary();
404
405 auto sender = loop1->MakeSender<TestMessage>("/test");
406 auto fetcher = loop2->MakeFetcher<TestMessage>("/test");
407
408 ::std::vector<int> values;
409
410 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700411 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
412 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
413 builder.add_value(200);
414 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700415 }
416 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700417 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
418 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
419 builder.add_value(201);
420 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700421 }
422
423 // Add a timer to actually quit.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700424 auto test_timer = loop2->AddTimer([&fetcher, &values, this]() {
Austin Schuhbbce72d2019-05-26 15:11:46 -0700425 while (fetcher.FetchNext()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700426 values.push_back(fetcher.get()->value());
Austin Schuhbbce72d2019-05-26 15:11:46 -0700427 }
Austin Schuh9fe68f72019-08-10 19:32:03 -0700428 this->Exit();
Austin Schuhbbce72d2019-05-26 15:11:46 -0700429 });
430
431 loop2->OnRun([&test_timer, &loop2]() {
432 test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100));
433 });
434
435 Run();
436 EXPECT_THAT(values, ::testing::ElementsAreArray({200, 201}));
437}
438
439// Tests that FetchNext gets no messages sent before it is constructed.
440TEST_P(AbstractEventLoopTest, FetchNextAfterSend) {
441 auto loop1 = Make();
442 auto loop2 = MakePrimary();
443
444 auto sender = loop1->MakeSender<TestMessage>("/test");
445
446 ::std::vector<int> values;
447
448 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700449 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
450 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
451 builder.add_value(200);
452 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700453 }
454 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700455 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
456 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
457 builder.add_value(201);
458 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700459 }
460
461 auto fetcher = loop2->MakeFetcher<TestMessage>("/test");
462
463 // Add a timer to actually quit.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700464 auto test_timer = loop2->AddTimer([&fetcher, &values, this]() {
Austin Schuhbbce72d2019-05-26 15:11:46 -0700465 while (fetcher.FetchNext()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700466 values.push_back(fetcher.get()->value());
Austin Schuhbbce72d2019-05-26 15:11:46 -0700467 }
Austin Schuh9fe68f72019-08-10 19:32:03 -0700468 this->Exit();
Austin Schuhbbce72d2019-05-26 15:11:46 -0700469 });
470
471 loop2->OnRun([&test_timer, &loop2]() {
472 test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100));
473 });
474
475 Run();
476 EXPECT_THAT(0, values.size());
477}
478
479// Tests that Fetch returns the last message created before the loop was
480// started.
481TEST_P(AbstractEventLoopTest, FetchDataFromBeforeCreation) {
482 auto loop1 = Make();
483 auto loop2 = MakePrimary();
484
485 auto sender = loop1->MakeSender<TestMessage>("/test");
486
487 ::std::vector<int> values;
488
489 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700490 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
491 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
492 builder.add_value(200);
493 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700494 }
495 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700496 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
497 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
498 builder.add_value(201);
499 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700500 }
501
502 auto fetcher = loop2->MakeFetcher<TestMessage>("/test");
503
504 // Add a timer to actually quit.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700505 auto test_timer = loop2->AddTimer([&fetcher, &values, this]() {
Austin Schuhbbce72d2019-05-26 15:11:46 -0700506 if (fetcher.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700507 values.push_back(fetcher.get()->value());
Austin Schuhbbce72d2019-05-26 15:11:46 -0700508 }
509 // Do it again to make sure we don't double fetch.
510 if (fetcher.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700511 values.push_back(fetcher.get()->value());
Austin Schuhbbce72d2019-05-26 15:11:46 -0700512 }
Austin Schuh9fe68f72019-08-10 19:32:03 -0700513 this->Exit();
Austin Schuhbbce72d2019-05-26 15:11:46 -0700514 });
515
516 loop2->OnRun([&test_timer, &loop2]() {
517 test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100));
518 });
519
520 Run();
521 EXPECT_THAT(values, ::testing::ElementsAreArray({201}));
522}
523
524// Tests that Fetch and FetchNext interleave as expected.
525TEST_P(AbstractEventLoopTest, FetchAndFetchNextTogether) {
526 auto loop1 = Make();
527 auto loop2 = MakePrimary();
528
529 auto sender = loop1->MakeSender<TestMessage>("/test");
530
531 ::std::vector<int> values;
532
533 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700534 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
535 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
536 builder.add_value(200);
537 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700538 }
539 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700540 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
541 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
542 builder.add_value(201);
543 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700544 }
545
546 auto fetcher = loop2->MakeFetcher<TestMessage>("/test");
547
548 // Add a timer to actually quit.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700549 auto test_timer = loop2->AddTimer([&fetcher, &values, &sender, this]() {
Austin Schuhbbce72d2019-05-26 15:11:46 -0700550 if (fetcher.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700551 values.push_back(fetcher.get()->value());
Austin Schuhbbce72d2019-05-26 15:11:46 -0700552 }
553
554 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700555 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
556 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
557 builder.add_value(202);
558 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700559 }
560 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700561 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
562 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
563 builder.add_value(203);
564 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700565 }
566 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700567 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
568 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
569 builder.add_value(204);
570 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700571 }
572
573 if (fetcher.FetchNext()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700574 values.push_back(fetcher.get()->value());
Austin Schuhbbce72d2019-05-26 15:11:46 -0700575 }
576
577 if (fetcher.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700578 values.push_back(fetcher.get()->value());
Austin Schuhbbce72d2019-05-26 15:11:46 -0700579 }
580
Austin Schuh9fe68f72019-08-10 19:32:03 -0700581 this->Exit();
Austin Schuhbbce72d2019-05-26 15:11:46 -0700582 });
583
584 loop2->OnRun([&test_timer, &loop2]() {
585 test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100));
586 });
587
588 Run();
589 EXPECT_THAT(values, ::testing::ElementsAreArray({201, 202, 204}));
590}
591
Austin Schuh3115a202019-05-27 21:02:14 -0700592// Tests that FetchNext behaves correctly when we get two messages in the queue
593// but don't consume the first until after the second has been sent.
594TEST_P(AbstractEventLoopTest, FetchNextTest) {
Austin Schuh3115a202019-05-27 21:02:14 -0700595 auto send_loop = Make();
596 auto fetch_loop = Make();
597 auto sender = send_loop->MakeSender<TestMessage>("/test");
598 Fetcher<TestMessage> fetcher = fetch_loop->MakeFetcher<TestMessage>("/test");
599
600 {
Tyler Chatow67ddb032020-01-12 14:30:04 -0800601 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
602 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
603 builder.add_value(100);
604 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh3115a202019-05-27 21:02:14 -0700605 }
606
607 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700608 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
609 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
610 builder.add_value(200);
611 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh3115a202019-05-27 21:02:14 -0700612 }
613
614 ASSERT_TRUE(fetcher.FetchNext());
615 ASSERT_NE(nullptr, fetcher.get());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700616 EXPECT_EQ(100, fetcher.get()->value());
Austin Schuh3115a202019-05-27 21:02:14 -0700617
618 ASSERT_TRUE(fetcher.FetchNext());
619 ASSERT_NE(nullptr, fetcher.get());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700620 EXPECT_EQ(200, fetcher.get()->value());
Austin Schuh3115a202019-05-27 21:02:14 -0700621
622 // When we run off the end of the queue, expect to still have the old message:
623 ASSERT_FALSE(fetcher.FetchNext());
624 ASSERT_NE(nullptr, fetcher.get());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700625 EXPECT_EQ(200, fetcher.get()->value());
Austin Schuh3115a202019-05-27 21:02:14 -0700626}
627
Brian Silverman77162972020-08-12 19:52:40 -0700628// Verify that a fetcher still holds its data, even after falling behind.
629TEST_P(AbstractEventLoopTest, FetcherBehindData) {
630 auto send_loop = Make();
631 auto fetch_loop = Make();
632 auto sender = send_loop->MakeSender<TestMessage>("/test");
633 Fetcher<TestMessage> fetcher = fetch_loop->MakeFetcher<TestMessage>("/test");
634 {
635 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
636 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
637 builder.add_value(1);
638 ASSERT_TRUE(msg.Send(builder.Finish()));
639 }
640 ASSERT_TRUE(fetcher.Fetch());
641 EXPECT_EQ(1, fetcher.get()->value());
642 for (int i = 0; i < 300; ++i) {
643 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
644 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
645 builder.add_value(i + 2);
646 ASSERT_TRUE(msg.Send(builder.Finish()));
647 }
648 EXPECT_EQ(1, fetcher.get()->value());
649}
650
651// Try a bunch of orderings of operations with fetchers and senders. Verify that
652// all the fetchers have the correct data at each step.
653TEST_P(AbstractEventLoopTest, FetcherPermutations) {
654 for (int max_save = 0; max_save < 5; ++max_save) {
655 SCOPED_TRACE("max_save=" + std::to_string(max_save));
656
657 auto send_loop = Make();
658 auto fetch_loop = Make();
659 auto sender = send_loop->MakeSender<TestMessage>("/test");
660 const auto send_message = [&sender](int i) {
661 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
662 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
663 builder.add_value(i);
664 ASSERT_TRUE(msg.Send(builder.Finish()));
665 };
666 std::vector<Fetcher<TestMessage>> fetchers;
667 for (int i = 0; i < 10; ++i) {
668 fetchers.emplace_back(fetch_loop->MakeFetcher<TestMessage>("/test"));
669 }
670 send_message(1);
Brian Silverman4f4e0612020-08-12 19:54:41 -0700671 const auto verify_buffers = [&]() {
672 std::vector<std::reference_wrapper<const Fetcher<TestMessage>>>
673 fetchers_copy;
674 for (const auto &fetcher : fetchers) {
675 fetchers_copy.emplace_back(fetcher);
676 }
677 std::vector<std::reference_wrapper<const Sender<TestMessage>>>
678 senders_copy;
679 senders_copy.emplace_back(sender);
680 VerifyBuffers(send_loop->NumberBuffers(sender.channel()), fetchers_copy,
681 senders_copy);
682 };
Brian Silverman77162972020-08-12 19:52:40 -0700683 for (auto &fetcher : fetchers) {
684 ASSERT_TRUE(fetcher.Fetch());
Brian Silverman4f4e0612020-08-12 19:54:41 -0700685 verify_buffers();
Brian Silverman77162972020-08-12 19:52:40 -0700686 EXPECT_EQ(1, fetcher.get()->value());
687 }
688
689 for (int save = 1; save <= max_save; ++save) {
690 SCOPED_TRACE("save=" + std::to_string(save));
691 send_message(100 + save);
Brian Silverman4f4e0612020-08-12 19:54:41 -0700692 verify_buffers();
Brian Silverman77162972020-08-12 19:52:40 -0700693 for (size_t i = 0; i < fetchers.size() - save; ++i) {
694 SCOPED_TRACE("fetcher=" + std::to_string(i));
695 ASSERT_TRUE(fetchers[i].Fetch());
Brian Silverman4f4e0612020-08-12 19:54:41 -0700696 verify_buffers();
Brian Silverman77162972020-08-12 19:52:40 -0700697 EXPECT_EQ(100 + save, fetchers[i].get()->value());
698 }
699 for (size_t i = fetchers.size() - save; i < fetchers.size() - 1; ++i) {
700 SCOPED_TRACE("fetcher=" + std::to_string(i));
701 EXPECT_EQ(100 + (fetchers.size() - 1 - i), fetchers[i].get()->value());
702 }
703 EXPECT_EQ(1, fetchers.back().get()->value());
704 }
705
706 for (int i = 0; i < 300; ++i) {
707 send_message(200 + i);
Brian Silverman4f4e0612020-08-12 19:54:41 -0700708 verify_buffers();
Brian Silverman77162972020-08-12 19:52:40 -0700709 }
710
711 for (size_t i = 0; i < fetchers.size() - max_save; ++i) {
712 SCOPED_TRACE("fetcher=" + std::to_string(i));
713 if (max_save > 0) {
714 EXPECT_EQ(100 + max_save, fetchers[i].get()->value());
715 } else {
716 EXPECT_EQ(1, fetchers[i].get()->value());
717 }
718 }
719 for (size_t i = fetchers.size() - max_save; i < fetchers.size() - 1; ++i) {
720 SCOPED_TRACE("fetcher=" + std::to_string(i));
721 EXPECT_EQ(100 + (fetchers.size() - 1 - i), fetchers[i].get()->value());
722 }
723 EXPECT_EQ(1, fetchers.back().get()->value());
724 }
725}
726
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800727// Verify that making a fetcher and watcher for "/test" succeeds.
728TEST_P(AbstractEventLoopTest, FetcherAndWatcher) {
Parker Schuhe4a70d62017-12-27 20:10:20 -0800729 auto loop = Make();
730 auto fetcher = loop->MakeFetcher<TestMessage>("/test");
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800731 loop->MakeWatcher("/test", [&](const TestMessage &) {});
Parker Schuhe4a70d62017-12-27 20:10:20 -0800732}
733
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800734// Verify that making 2 fetchers for "/test" succeeds.
Parker Schuhe4a70d62017-12-27 20:10:20 -0800735TEST_P(AbstractEventLoopTest, TwoFetcher) {
736 auto loop = Make();
737 auto fetcher = loop->MakeFetcher<TestMessage>("/test");
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800738 auto fetcher2 = loop->MakeFetcher<TestMessage>("/test");
Parker Schuhe4a70d62017-12-27 20:10:20 -0800739}
740
Alex Perrycb7da4b2019-08-28 19:35:56 -0700741// Verify that registering a watcher for an invalid channel name dies.
742TEST_P(AbstractEventLoopDeathTest, InvalidChannelName) {
743 auto loop = Make();
744 EXPECT_DEATH(
745 { loop->MakeWatcher("/test/invalid", [&](const TestMessage &) {}); },
746 "/test/invalid");
Brian Silverman454bc112020-03-05 14:21:25 -0800747 EXPECT_DEATH(
748 { loop->MakeNoArgWatcher<TestMessage>("/test/invalid", [&]() {}); },
749 "/test/invalid");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700750}
751
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800752// Verify that registering a watcher twice for "/test" fails.
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700753TEST_P(AbstractEventLoopDeathTest, TwoWatcher) {
Parker Schuhe4a70d62017-12-27 20:10:20 -0800754 auto loop = Make();
755 loop->MakeWatcher("/test", [&](const TestMessage &) {});
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800756 EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}),
757 "/test");
Brian Silverman454bc112020-03-05 14:21:25 -0800758 EXPECT_DEATH(loop->MakeNoArgWatcher<TestMessage>("/test", [&]() {}), "/test");
759}
760
761// Verify that registering a no-arg watcher twice for "/test" fails.
762TEST_P(AbstractEventLoopDeathTest, TwoNoArgWatcher) {
763 auto loop = Make();
764 loop->MakeNoArgWatcher<TestMessage>("/test", [&]() {});
765 EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}),
766 "/test");
767 EXPECT_DEATH(loop->MakeNoArgWatcher<TestMessage>("/test", [&]() {}), "/test");
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800768}
769
Austin Schuh3115a202019-05-27 21:02:14 -0700770// Verify that SetRuntimeRealtimePriority fails while running.
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700771TEST_P(AbstractEventLoopDeathTest, SetRuntimeRealtimePriority) {
Austin Schuh3115a202019-05-27 21:02:14 -0700772 auto loop = MakePrimary();
773 // Confirm that runtime priority calls work when not realtime.
774 loop->SetRuntimeRealtimePriority(5);
775
776 loop->OnRun([&]() { loop->SetRuntimeRealtimePriority(5); });
777
778 EXPECT_DEATH(Run(), "realtime");
779}
780
Brian Silverman6a54ff32020-04-28 16:41:39 -0700781// Verify that SetRuntimeAffinity fails while running.
782TEST_P(AbstractEventLoopDeathTest, SetRuntimeAffinity) {
Austin Schuhde973292021-10-12 18:09:49 -0700783 const cpu_set_t available = GetCurrentThreadAffinity();
784 int first_cpu = -1;
785 for (int i = 0; i < CPU_SETSIZE; ++i) {
786 if (CPU_ISSET(i, &available)) {
787 first_cpu = i;
788 break;
789 continue;
790 }
791 }
792 CHECK_NE(first_cpu, -1) << ": Default affinity has no CPUs?";
793
Brian Silverman6a54ff32020-04-28 16:41:39 -0700794 auto loop = MakePrimary();
795 // Confirm that runtime priority calls work when not running.
Austin Schuhde973292021-10-12 18:09:49 -0700796 loop->SetRuntimeAffinity(MakeCpusetFromCpus({first_cpu}));
Brian Silverman6a54ff32020-04-28 16:41:39 -0700797
Austin Schuhde973292021-10-12 18:09:49 -0700798 loop->OnRun(
799 [&]() { loop->SetRuntimeAffinity(MakeCpusetFromCpus({first_cpu})); });
Brian Silverman6a54ff32020-04-28 16:41:39 -0700800
801 EXPECT_DEATH(Run(), "Cannot set affinity while running");
802}
803
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800804// Verify that registering a watcher and a sender for "/test" fails.
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700805TEST_P(AbstractEventLoopDeathTest, WatcherAndSender) {
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800806 auto loop = Make();
807 auto sender = loop->MakeSender<TestMessage>("/test");
808 EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}),
809 "/test");
Parker Schuhe4a70d62017-12-27 20:10:20 -0800810}
811
Austin Schuhe516ab02020-05-06 21:37:04 -0700812// Verify that creating too many senders fails.
813TEST_P(AbstractEventLoopDeathTest, TooManySenders) {
814 auto loop = Make();
815 std::vector<aos::Sender<TestMessage>> senders;
816 for (int i = 0; i < 10; ++i) {
817 senders.emplace_back(loop->MakeSender<TestMessage>("/test"));
818 }
819 EXPECT_DEATH({ loop->MakeSender<TestMessage>("/test"); },
820 "Failed to create sender on \\{ \"name\": \"/test\", \"type\": "
Brian Silverman77162972020-08-12 19:52:40 -0700821 "\"aos.TestMessage\"[^}]*\\ }, too many senders.");
822}
823
824// Verify that creating too many fetchers fails.
825TEST_P(AbstractEventLoopDeathTest, TooManyFetchers) {
826 if (read_method() != ReadMethod::PIN) {
827 // Other read methods don't limit the number of readers, so just skip this.
828 return;
829 }
830
831 auto loop = Make();
832 std::vector<aos::Fetcher<TestMessage>> fetchers;
833 for (int i = 0; i < 10; ++i) {
834 fetchers.emplace_back(loop->MakeFetcher<TestMessage>("/test"));
835 }
836 EXPECT_DEATH({ loop->MakeFetcher<TestMessage>("/test"); },
837 "Failed to create reader on \\{ \"name\": \"/test\", \"type\": "
838 "\"aos.TestMessage\"[^}]*\\ }, too many readers.");
839}
840
841// Verify that creating too many fetchers, split between two event loops, fails.
842TEST_P(AbstractEventLoopDeathTest, TooManyFetchersTwoLoops) {
843 if (read_method() != ReadMethod::PIN) {
844 // Other read methods don't limit the number of readers, so just skip this.
845 return;
846 }
847
848 auto loop = Make();
849 auto loop2 = Make();
850 std::vector<aos::Fetcher<TestMessage>> fetchers;
851 for (int i = 0; i < 5; ++i) {
852 fetchers.emplace_back(loop->MakeFetcher<TestMessage>("/test"));
853 fetchers.emplace_back(loop2->MakeFetcher<TestMessage>("/test"));
854 }
855 EXPECT_DEATH({ loop->MakeFetcher<TestMessage>("/test"); },
856 "Failed to create reader on \\{ \"name\": \"/test\", \"type\": "
857 "\"aos.TestMessage\"[^}]*\\ }, too many readers.");
858}
859
860// Verify that creating too many watchers fails.
861TEST_P(AbstractEventLoopDeathTest, TooManyWatchers) {
862 if (read_method() != ReadMethod::PIN) {
863 // Other read methods don't limit the number of readers, so just skip this.
864 return;
865 }
866
867 std::vector<std::unique_ptr<EventLoop>> loops;
868 for (int i = 0; i < 10; ++i) {
869 loops.emplace_back(Make());
870 loops.back()->MakeWatcher("/test", [](const TestMessage &) {});
871 }
872 EXPECT_DEATH({ Make()->MakeWatcher("/test", [](const TestMessage &) {}); },
873 "Failed to create reader on \\{ \"name\": \"/test\", \"type\": "
874 "\"aos.TestMessage\"[^}]*\\ }, too many readers.");
875}
876
877// Verify that creating too many watchers and fetchers combined fails.
878TEST_P(AbstractEventLoopDeathTest, TooManyWatchersAndFetchers) {
879 if (read_method() != ReadMethod::PIN) {
880 // Other read methods don't limit the number of readers, so just skip this.
881 return;
882 }
883
884 auto loop = Make();
885 std::vector<aos::Fetcher<TestMessage>> fetchers;
886 std::vector<std::unique_ptr<EventLoop>> loops;
887 for (int i = 0; i < 5; ++i) {
888 fetchers.emplace_back(loop->MakeFetcher<TestMessage>("/test"));
889 loops.emplace_back(Make());
890 loops.back()->MakeWatcher("/test", [](const TestMessage &) {});
891 }
892 EXPECT_DEATH({ loop->MakeFetcher<TestMessage>("/test"); },
893 "Failed to create reader on \\{ \"name\": \"/test\", \"type\": "
894 "\"aos.TestMessage\"[^}]*\\ }, too many readers.");
Austin Schuhe516ab02020-05-06 21:37:04 -0700895}
896
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700897// Verify that we can't create a sender inside OnRun.
898TEST_P(AbstractEventLoopDeathTest, SenderInOnRun) {
899 auto loop1 = MakePrimary();
900
901 loop1->OnRun(
902 [&]() { auto sender = loop1->MakeSender<TestMessage>("/test2"); });
903
904 EXPECT_DEATH(Run(), "running");
905}
906
907// Verify that we can't create a watcher inside OnRun.
908TEST_P(AbstractEventLoopDeathTest, WatcherInOnRun) {
909 auto loop1 = MakePrimary();
910
911 loop1->OnRun(
912 [&]() { loop1->MakeWatcher("/test", [&](const TestMessage &) {}); });
913
914 EXPECT_DEATH(Run(), "running");
915}
916
Brian Silverman454bc112020-03-05 14:21:25 -0800917// Verify that we can't create a no-arg watcher inside OnRun.
918TEST_P(AbstractEventLoopDeathTest, NoArgWatcherInOnRun) {
919 auto loop1 = MakePrimary();
920
921 loop1->OnRun(
922 [&]() { loop1->MakeNoArgWatcher<TestMessage>("/test", [&]() {}); });
923
924 EXPECT_DEATH(Run(), "running");
925}
926
Parker Schuhe4a70d62017-12-27 20:10:20 -0800927// Verify that Quit() works when there are multiple watchers.
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800928TEST_P(AbstractEventLoopTest, MultipleWatcherQuit) {
929 auto loop1 = Make();
Austin Schuh44019f92019-05-19 19:58:27 -0700930 auto loop2 = MakePrimary();
Parker Schuhe4a70d62017-12-27 20:10:20 -0800931
Austin Schuh3578a2e2019-05-25 18:17:59 -0700932 loop2->MakeWatcher("/test1", [&](const TestMessage &) {});
933 loop2->MakeWatcher("/test2", [&](const TestMessage &message) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700934 EXPECT_EQ(message.value(), 200);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700935 this->Exit();
Austin Schuh3578a2e2019-05-25 18:17:59 -0700936 });
937
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800938 auto sender = loop1->MakeSender<TestMessage>("/test2");
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700939
940 loop2->OnRun([&]() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700941 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
942 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
943 builder.add_value(200);
944 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700945 });
Parker Schuhe4a70d62017-12-27 20:10:20 -0800946
Austin Schuh44019f92019-05-19 19:58:27 -0700947 Run();
Parker Schuhe4a70d62017-12-27 20:10:20 -0800948}
949
Neil Balch229001a2018-01-07 18:22:52 -0800950// Verify that timer intervals and duration function properly.
951TEST_P(AbstractEventLoopTest, TimerIntervalAndDuration) {
Stephan Pleines3dce7ea2021-06-22 13:19:26 -0700952 // Force a slower rate so we are guaranteed to have reports for our timer.
Austin Schuh39788ff2019-12-01 18:22:57 -0800953 FLAGS_timing_report_ms = 2000;
954
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800955 const int kCount = 5;
Neil Balch229001a2018-01-07 18:22:52 -0800956
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800957 auto loop = MakePrimary();
Austin Schuh39788ff2019-12-01 18:22:57 -0800958 auto loop2 = Make();
959
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800960 ::std::vector<::aos::monotonic_clock::time_point> times;
961 ::std::vector<::aos::monotonic_clock::time_point> expected_times;
962
Austin Schuh39788ff2019-12-01 18:22:57 -0800963 Fetcher<timing::Report> report_fetcher =
964 loop2->MakeFetcher<timing::Report>("/aos");
965 EXPECT_FALSE(report_fetcher.Fetch());
966
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800967 auto test_timer = loop->AddTimer([this, &times, &expected_times, &loop]() {
968 times.push_back(loop->monotonic_now());
Austin Schuhad154822019-12-27 15:45:13 -0800969 EXPECT_EQ(loop->context().monotonic_remote_time, monotonic_clock::min_time);
970 EXPECT_EQ(loop->context().realtime_event_time, realtime_clock::min_time);
971 EXPECT_EQ(loop->context().realtime_remote_time, realtime_clock::min_time);
Austin Schuha9012be2021-07-21 15:19:11 -0700972 EXPECT_EQ(loop->context().source_boot_uuid, loop->boot_uuid());
Austin Schuh39788ff2019-12-01 18:22:57 -0800973 EXPECT_EQ(loop->context().queue_index, 0xffffffffu);
974 EXPECT_EQ(loop->context().size, 0u);
975 EXPECT_EQ(loop->context().data, nullptr);
Brian Silverman4f4e0612020-08-12 19:54:41 -0700976 EXPECT_EQ(loop->context().buffer_index, -1);
Austin Schuh39788ff2019-12-01 18:22:57 -0800977
Austin Schuhad154822019-12-27 15:45:13 -0800978 expected_times.push_back(loop->context().monotonic_event_time);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800979 if (times.size() == kCount) {
980 this->Exit();
981 }
Neil Balch229001a2018-01-07 18:22:52 -0800982 });
Austin Schuh39788ff2019-12-01 18:22:57 -0800983 test_timer->set_name("Test loop");
Neil Balch229001a2018-01-07 18:22:52 -0800984
Austin Schuh39788ff2019-12-01 18:22:57 -0800985 const monotonic_clock::time_point start_time = loop->monotonic_now();
Austin Schuh52d325c2019-06-23 18:59:06 -0700986 // TODO(austin): This should be an error... Should be done in OnRun only.
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800987 test_timer->Setup(start_time + chrono::seconds(1), chrono::seconds(1));
988
Austin Schuh44019f92019-05-19 19:58:27 -0700989 Run();
Neil Balch229001a2018-01-07 18:22:52 -0800990
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800991 // Confirm that we got both the right number of samples, and it's odd.
Stephan Pleines3dce7ea2021-06-22 13:19:26 -0700992 ASSERT_EQ(times.size(), static_cast<size_t>(kCount));
993 ASSERT_EQ(times.size(), expected_times.size());
994 ASSERT_EQ((times.size() % 2), 1);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800995
996 // Grab the middle sample.
997 ::aos::monotonic_clock::time_point average_time = times[times.size() / 2];
998
999 // Add up all the delays of all the times.
1000 ::aos::monotonic_clock::duration sum = chrono::seconds(0);
1001 for (const ::aos::monotonic_clock::time_point time : times) {
1002 sum += time - average_time;
1003 }
1004
1005 // Average and add to the middle to find the average time.
1006 sum /= times.size();
1007 average_time += sum;
1008
1009 // Compute the offset from the average and the expected average. It
1010 // should be pretty close to 0.
1011 const ::aos::monotonic_clock::duration remainder =
1012 average_time - start_time - chrono::seconds(times.size() / 2 + 1);
1013
1014 const chrono::milliseconds kEpsilon(100);
1015 EXPECT_LT(remainder, +kEpsilon);
1016 EXPECT_GT(remainder, -kEpsilon);
1017
1018 // Make sure that the average duration is close to 1 second.
1019 EXPECT_NEAR(chrono::duration_cast<chrono::duration<double>>(times.back() -
1020 times.front())
1021 .count() /
1022 static_cast<double>(times.size() - 1),
1023 1.0, 0.1);
1024
1025 // Confirm that the ideal wakeup times increment correctly.
1026 for (size_t i = 1; i < expected_times.size(); ++i) {
1027 EXPECT_EQ(expected_times[i], expected_times[i - 1] + chrono::seconds(1));
1028 }
1029
1030 for (size_t i = 0; i < expected_times.size(); ++i) {
1031 EXPECT_EQ((expected_times[i] - start_time) % chrono::seconds(1),
1032 chrono::seconds(0));
1033 }
1034
1035 EXPECT_LT(expected_times[expected_times.size() / 2], average_time + kEpsilon);
1036 EXPECT_GT(expected_times[expected_times.size() / 2], average_time - kEpsilon);
Austin Schuh39788ff2019-12-01 18:22:57 -08001037
Austin Schuh6bae8252021-02-07 22:01:49 -08001038 if (do_timing_reports() == DoTimingReports::kYes) {
1039 // And, since we are here, check that the timing report makes sense.
1040 // Start by looking for our event loop's timing.
1041 FlatbufferDetachedBuffer<timing::Report> report =
1042 FlatbufferDetachedBuffer<timing::Report>::Empty();
1043 while (report_fetcher.FetchNext()) {
1044 if (report_fetcher->name()->string_view() == "primary") {
1045 report = CopyFlatBuffer(report_fetcher.get());
1046 }
Austin Schuh39788ff2019-12-01 18:22:57 -08001047 }
Austin Schuh6bae8252021-02-07 22:01:49 -08001048
1049 // Confirm that we have the right number of reports, and the contents are
1050 // sane.
1051 VLOG(1) << FlatbufferToJson(report, {.multi_line = true});
1052
1053 EXPECT_EQ(report.message().name()->string_view(), "primary");
1054
1055 ASSERT_NE(report.message().senders(), nullptr);
1056 EXPECT_EQ(report.message().senders()->size(), 2);
1057
1058 ASSERT_NE(report.message().timers(), nullptr);
1059 EXPECT_EQ(report.message().timers()->size(), 2);
1060
1061 EXPECT_EQ(report.message().timers()->Get(0)->name()->string_view(),
1062 "Test loop");
1063 EXPECT_GE(report.message().timers()->Get(0)->count(), 1);
1064
1065 EXPECT_EQ(report.message().timers()->Get(1)->name()->string_view(),
1066 "timing_reports");
1067 EXPECT_EQ(report.message().timers()->Get(1)->count(), 1);
1068
1069 // Make sure there is a single phased loop report with our report in it.
1070 ASSERT_EQ(report.message().phased_loops(), nullptr);
1071 } else {
1072 ASSERT_FALSE(report_fetcher.Fetch());
Austin Schuh39788ff2019-12-01 18:22:57 -08001073 }
Neil Balch229001a2018-01-07 18:22:52 -08001074}
1075
1076// Verify that we can change a timer's parameters during execution.
1077TEST_P(AbstractEventLoopTest, TimerChangeParameters) {
Austin Schuh44019f92019-05-19 19:58:27 -07001078 auto loop = MakePrimary();
Austin Schuhd892f102021-10-12 18:01:46 -07001079 loop->SetRuntimeRealtimePriority(1);
Austin Schuh7f20f512021-01-31 17:56:16 -08001080 std::vector<monotonic_clock::time_point> iteration_list;
Neil Balch229001a2018-01-07 18:22:52 -08001081
1082 auto test_timer = loop->AddTimer([&iteration_list, &loop]() {
Austin Schuh7f20f512021-01-31 17:56:16 -08001083 iteration_list.push_back(loop->context().monotonic_event_time);
Neil Balch229001a2018-01-07 18:22:52 -08001084 });
1085
Austin Schuh7f20f512021-01-31 17:56:16 -08001086 monotonic_clock::time_point s;
1087 auto modifier_timer = loop->AddTimer([&test_timer, &s]() {
Austin Schuhd892f102021-10-12 18:01:46 -07001088 test_timer->Setup(s + chrono::milliseconds(1750),
1089 chrono::milliseconds(600));
Neil Balch229001a2018-01-07 18:22:52 -08001090 });
1091
Austin Schuh7f20f512021-01-31 17:56:16 -08001092 s = loop->monotonic_now();
Austin Schuhd892f102021-10-12 18:01:46 -07001093 test_timer->Setup(s, chrono::milliseconds(500));
1094 modifier_timer->Setup(s + chrono::milliseconds(1250));
1095 EndEventLoop(loop.get(), chrono::milliseconds(3950));
Austin Schuh44019f92019-05-19 19:58:27 -07001096 Run();
Neil Balch229001a2018-01-07 18:22:52 -08001097
Austin Schuhd892f102021-10-12 18:01:46 -07001098 EXPECT_THAT(
1099 iteration_list,
1100 ::testing::ElementsAre(
1101 s, s + chrono::milliseconds(500), s + chrono::milliseconds(1000),
1102 s + chrono::milliseconds(1750), s + chrono::milliseconds(2350),
1103 s + chrono::milliseconds(2950), s + chrono::milliseconds(3550)));
Neil Balch229001a2018-01-07 18:22:52 -08001104}
1105
1106// Verify that we can disable a timer during execution.
1107TEST_P(AbstractEventLoopTest, TimerDisable) {
Austin Schuh44019f92019-05-19 19:58:27 -07001108 auto loop = MakePrimary();
Austin Schuhd892f102021-10-12 18:01:46 -07001109 loop->SetRuntimeRealtimePriority(1);
Neil Balch229001a2018-01-07 18:22:52 -08001110 ::std::vector<::aos::monotonic_clock::time_point> iteration_list;
1111
1112 auto test_timer = loop->AddTimer([&iteration_list, &loop]() {
Austin Schuhd892f102021-10-12 18:01:46 -07001113 iteration_list.push_back(loop->context().monotonic_event_time);
Neil Balch229001a2018-01-07 18:22:52 -08001114 });
1115
Tyler Chatow67ddb032020-01-12 14:30:04 -08001116 auto ender_timer = loop->AddTimer([&test_timer]() { test_timer->Disable(); });
Neil Balch229001a2018-01-07 18:22:52 -08001117
Austin Schuhd892f102021-10-12 18:01:46 -07001118 monotonic_clock::time_point s = loop->monotonic_now();
1119 test_timer->Setup(s, ::std::chrono::milliseconds(70));
1120 ender_timer->Setup(s + ::std::chrono::milliseconds(200));
1121 EndEventLoop(loop.get(), ::std::chrono::milliseconds(350));
Austin Schuh44019f92019-05-19 19:58:27 -07001122 Run();
Neil Balch229001a2018-01-07 18:22:52 -08001123
Austin Schuhd892f102021-10-12 18:01:46 -07001124 EXPECT_THAT(iteration_list,
1125 ::testing::ElementsAre(s, s + chrono::milliseconds(70),
1126 s + chrono::milliseconds(140)));
Neil Balch229001a2018-01-07 18:22:52 -08001127}
Austin Schuh7267c532019-05-19 19:55:53 -07001128
Brian Silvermanaf9a4d82020-10-06 15:10:58 -07001129// Verify that a timer can disable itself.
1130//
1131// TODO(Brian): Do something similar with phased loops, both with a quick
1132// handler and a handler that would miss a cycle except it got deferred. Current
1133// behavior doing that is a mess.
1134TEST_P(AbstractEventLoopTest, TimerDisableSelf) {
1135 auto loop = MakePrimary();
1136
1137 int count = 0;
1138 aos::TimerHandler *test_timer;
1139 test_timer = loop->AddTimer([&count, &test_timer]() {
1140 ++count;
1141 test_timer->Disable();
1142 });
1143
1144 test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(20));
1145 EndEventLoop(loop.get(), ::std::chrono::milliseconds(80));
1146 Run();
1147
1148 EXPECT_EQ(count, 1);
1149}
1150
Brian Silvermanbd405c02020-06-23 16:25:23 -07001151// Verify that we can disable a timer during execution of another timer
1152// scheduled for the same time, with one ordering of creation for the timers.
1153//
1154// Also schedule some more events to reshuffle the heap in EventLoop used for
1155// tracking events to change up the order. This used to segfault
1156// SimulatedEventLoop.
1157TEST_P(AbstractEventLoopTest, TimerDisableOther) {
1158 for (bool creation_order : {true, false}) {
1159 for (bool setup_order : {true, false}) {
1160 for (int shuffle_events = 0; shuffle_events < 5; ++shuffle_events) {
1161 auto loop = MakePrimary();
1162 aos::TimerHandler *test_timer, *ender_timer;
1163 if (creation_order) {
1164 test_timer = loop->AddTimer([]() {});
1165 ender_timer =
1166 loop->AddTimer([&test_timer]() { test_timer->Disable(); });
1167 } else {
1168 ender_timer =
1169 loop->AddTimer([&test_timer]() { test_timer->Disable(); });
1170 test_timer = loop->AddTimer([]() {});
1171 }
1172
1173 const auto start = loop->monotonic_now();
1174
1175 for (int i = 0; i < shuffle_events; ++i) {
1176 loop->AddTimer([]() {})->Setup(start + std::chrono::milliseconds(10));
1177 }
1178
1179 if (setup_order) {
1180 test_timer->Setup(start + ::std::chrono::milliseconds(20));
1181 ender_timer->Setup(start + ::std::chrono::milliseconds(20));
1182 } else {
1183 ender_timer->Setup(start + ::std::chrono::milliseconds(20));
1184 test_timer->Setup(start + ::std::chrono::milliseconds(20));
1185 }
1186 EndEventLoop(loop.get(), ::std::chrono::milliseconds(40));
1187 Run();
1188 }
1189 }
1190 }
1191}
1192
Austin Schuh54cf95f2019-11-29 13:14:18 -08001193// Verifies that the event loop implementations detect when Channel is not a
Stephan Pleines3dce7ea2021-06-22 13:19:26 -07001194// pointer into configuration()
Austin Schuh54cf95f2019-11-29 13:14:18 -08001195TEST_P(AbstractEventLoopDeathTest, InvalidChannel) {
1196 auto loop = MakePrimary();
1197
Tyler Chatow67ddb032020-01-12 14:30:04 -08001198 const Channel *channel = configuration::GetChannel(
1199 loop->configuration(), "/test", "aos.TestMessage", "", nullptr);
Austin Schuh54cf95f2019-11-29 13:14:18 -08001200
1201 FlatbufferDetachedBuffer<Channel> channel_copy = CopyFlatBuffer(channel);
1202
1203 EXPECT_DEATH(
1204 { loop->MakeRawSender(&channel_copy.message()); },
1205 "Channel pointer not found in configuration\\(\\)->channels\\(\\)");
1206
1207 EXPECT_DEATH(
1208 { loop->MakeRawFetcher(&channel_copy.message()); },
1209 "Channel pointer not found in configuration\\(\\)->channels\\(\\)");
1210
1211 EXPECT_DEATH(
1212 {
1213 loop->MakeRawWatcher(&channel_copy.message(),
1214 [](const Context, const void *) {});
1215 },
1216 "Channel pointer not found in configuration\\(\\)->channels\\(\\)");
1217}
1218
Austin Schuhd54780b2020-10-03 16:26:02 -07001219// Verifies that the event loop implementations detect when Channel has an
1220// invalid alignment.
1221TEST_P(AbstractEventLoopDeathTest, InvalidChannelAlignment) {
1222 const char *const kError = "multiple of alignment";
1223 InvalidChannelAlignment();
1224
1225 auto loop = MakePrimary();
1226
1227 const Channel *channel = configuration::GetChannel(
1228 loop->configuration(), "/test", "aos.TestMessage", "", nullptr);
1229
1230 EXPECT_DEATH({ loop->MakeRawSender(channel); }, kError);
1231 EXPECT_DEATH({ loop->MakeSender<TestMessage>("/test"); }, kError);
1232
1233 EXPECT_DEATH({ loop->MakeRawFetcher(channel); }, kError);
1234 EXPECT_DEATH({ loop->MakeFetcher<TestMessage>("/test"); }, kError);
1235
1236 EXPECT_DEATH(
1237 { loop->MakeRawWatcher(channel, [](const Context &, const void *) {}); },
1238 kError);
1239 EXPECT_DEATH({ loop->MakeRawNoArgWatcher(channel, [](const Context &) {}); },
1240 kError);
1241
1242 EXPECT_DEATH({ loop->MakeNoArgWatcher<TestMessage>("/test", []() {}); },
1243 kError);
1244 EXPECT_DEATH({ loop->MakeWatcher("/test", [](const TestMessage &) {}); },
1245 kError);
1246}
1247
Brian Silverman454bc112020-03-05 14:21:25 -08001248// Verify that the send time on a message is roughly right when using a watcher.
Austin Schuh7267c532019-05-19 19:55:53 -07001249TEST_P(AbstractEventLoopTest, MessageSendTime) {
Austin Schuh44019f92019-05-19 19:58:27 -07001250 auto loop1 = MakePrimary();
Austin Schuh7267c532019-05-19 19:55:53 -07001251 auto loop2 = Make();
Austin Schuhad154822019-12-27 15:45:13 -08001252 auto sender = loop2->MakeSender<TestMessage>("/test");
Austin Schuh7267c532019-05-19 19:55:53 -07001253 auto fetcher = loop2->MakeFetcher<TestMessage>("/test");
1254
1255 auto test_timer = loop1->AddTimer([&sender]() {
Alex Perrycb7da4b2019-08-28 19:35:56 -07001256 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
1257 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
1258 builder.add_value(200);
1259 ASSERT_TRUE(msg.Send(builder.Finish()));
1260 });
1261
Austin Schuhad154822019-12-27 15:45:13 -08001262 bool triggered = false;
Brian Silverman454bc112020-03-05 14:21:25 -08001263 loop1->MakeWatcher("/test", [&](const TestMessage &msg) {
Austin Schuhad154822019-12-27 15:45:13 -08001264 // Confirm that the data pointer makes sense from a watcher, and all the
1265 // timestamps look right.
1266 EXPECT_GT(&msg, loop1->context().data);
1267 EXPECT_EQ(loop1->context().monotonic_remote_time,
1268 loop1->context().monotonic_event_time);
1269 EXPECT_EQ(loop1->context().realtime_remote_time,
1270 loop1->context().realtime_event_time);
Austin Schuha9012be2021-07-21 15:19:11 -07001271 EXPECT_EQ(loop1->context().source_boot_uuid, loop1->boot_uuid());
Austin Schuhad154822019-12-27 15:45:13 -08001272
1273 const aos::monotonic_clock::time_point monotonic_now =
1274 loop1->monotonic_now();
Tyler Chatow67ddb032020-01-12 14:30:04 -08001275 const aos::realtime_clock::time_point realtime_now = loop1->realtime_now();
Austin Schuhad154822019-12-27 15:45:13 -08001276
1277 EXPECT_LE(loop1->context().monotonic_event_time, monotonic_now);
1278 EXPECT_LE(loop1->context().realtime_event_time, realtime_now);
1279 EXPECT_GE(loop1->context().monotonic_event_time + chrono::milliseconds(500),
1280 monotonic_now);
1281 EXPECT_GE(loop1->context().realtime_event_time + chrono::milliseconds(500),
1282 realtime_now);
1283
Brian Silvermaneaa41d62020-07-08 19:47:35 -07001284 EXPECT_LT(&msg, reinterpret_cast<const void *>(
1285 reinterpret_cast<const char *>(loop1->context().data) +
Austin Schuhad154822019-12-27 15:45:13 -08001286 loop1->context().size));
Brian Silverman4f4e0612020-08-12 19:54:41 -07001287 if (read_method() == ReadMethod::PIN) {
1288 EXPECT_GE(loop1->context().buffer_index, 0);
1289 EXPECT_LT(loop1->context().buffer_index,
1290 loop1->NumberBuffers(
1291 configuration::GetChannel(loop1->configuration(), "/test",
1292 "aos.TestMessage", "", nullptr)));
1293 } else {
1294 EXPECT_EQ(-1, loop1->context().buffer_index);
1295 }
Austin Schuhad154822019-12-27 15:45:13 -08001296 triggered = true;
Austin Schuh7267c532019-05-19 19:55:53 -07001297 });
1298
1299 test_timer->Setup(loop1->monotonic_now() + ::std::chrono::seconds(1));
1300
1301 EndEventLoop(loop1.get(), ::std::chrono::seconds(2));
Austin Schuh44019f92019-05-19 19:58:27 -07001302 Run();
Austin Schuh7267c532019-05-19 19:55:53 -07001303
Austin Schuhad154822019-12-27 15:45:13 -08001304 EXPECT_TRUE(triggered);
1305
Brian Silverman454bc112020-03-05 14:21:25 -08001306 ASSERT_TRUE(fetcher.Fetch());
1307
1308 monotonic_clock::duration monotonic_time_offset =
1309 fetcher.context().monotonic_event_time -
1310 (loop1->monotonic_now() - ::std::chrono::seconds(1));
1311 realtime_clock::duration realtime_time_offset =
1312 fetcher.context().realtime_event_time -
1313 (loop1->realtime_now() - ::std::chrono::seconds(1));
1314
1315 EXPECT_EQ(fetcher.context().realtime_event_time,
1316 fetcher.context().realtime_remote_time);
1317 EXPECT_EQ(fetcher.context().monotonic_event_time,
1318 fetcher.context().monotonic_remote_time);
Austin Schuha9012be2021-07-21 15:19:11 -07001319 EXPECT_EQ(fetcher.context().source_boot_uuid, loop1->boot_uuid());
Brian Silverman454bc112020-03-05 14:21:25 -08001320
1321 EXPECT_TRUE(monotonic_time_offset > ::std::chrono::milliseconds(-500))
1322 << ": Got "
1323 << fetcher.context().monotonic_event_time.time_since_epoch().count()
1324 << " expected " << loop1->monotonic_now().time_since_epoch().count();
1325 // Confirm that the data pointer makes sense.
1326 EXPECT_GT(fetcher.get(), fetcher.context().data);
1327 EXPECT_LT(fetcher.get(),
Brian Silvermaneaa41d62020-07-08 19:47:35 -07001328 reinterpret_cast<const void *>(
1329 reinterpret_cast<const char *>(fetcher.context().data) +
Brian Silverman454bc112020-03-05 14:21:25 -08001330 fetcher.context().size));
1331 EXPECT_TRUE(monotonic_time_offset < ::std::chrono::milliseconds(500))
1332 << ": Got "
1333 << fetcher.context().monotonic_event_time.time_since_epoch().count()
1334 << " expected " << loop1->monotonic_now().time_since_epoch().count();
1335
1336 EXPECT_TRUE(realtime_time_offset > ::std::chrono::milliseconds(-500))
1337 << ": Got "
1338 << fetcher.context().realtime_event_time.time_since_epoch().count()
1339 << " expected " << loop1->realtime_now().time_since_epoch().count();
1340 EXPECT_TRUE(realtime_time_offset < ::std::chrono::milliseconds(500))
1341 << ": Got "
1342 << fetcher.context().realtime_event_time.time_since_epoch().count()
1343 << " expected " << loop1->realtime_now().time_since_epoch().count();
1344}
1345
1346// Verify that the send time on a message is roughly right when using a no-arg
1347// watcher. To get a message, we need to use a fetcher to actually access the
1348// message. This is also the main use case for no-arg fetchers.
1349TEST_P(AbstractEventLoopTest, MessageSendTimeNoArg) {
1350 auto loop1 = MakePrimary();
1351 auto loop2 = Make();
1352 auto sender = loop2->MakeSender<TestMessage>("/test");
1353 auto fetcher = loop1->MakeFetcher<TestMessage>("/test");
1354
1355 auto test_timer = loop1->AddTimer([&sender]() {
1356 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
1357 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
1358 builder.add_value(200);
1359 ASSERT_TRUE(msg.Send(builder.Finish()));
1360 });
1361
1362 bool triggered = false;
1363 loop1->MakeNoArgWatcher<TestMessage>("/test", [&]() {
1364 // Confirm that we can indeed use a fetcher on this channel from this
1365 // context, and it results in a sane data pointer and timestamps.
1366 ASSERT_TRUE(fetcher.Fetch());
1367
1368 EXPECT_EQ(loop1->context().monotonic_remote_time,
1369 loop1->context().monotonic_event_time);
1370 EXPECT_EQ(loop1->context().realtime_remote_time,
1371 loop1->context().realtime_event_time);
Austin Schuha9012be2021-07-21 15:19:11 -07001372 EXPECT_EQ(loop1->context().source_boot_uuid, loop1->boot_uuid());
Brian Silverman454bc112020-03-05 14:21:25 -08001373
1374 const aos::monotonic_clock::time_point monotonic_now =
1375 loop1->monotonic_now();
1376 const aos::realtime_clock::time_point realtime_now = loop1->realtime_now();
1377
1378 EXPECT_LE(loop1->context().monotonic_event_time, monotonic_now);
1379 EXPECT_LE(loop1->context().realtime_event_time, realtime_now);
1380 EXPECT_GE(loop1->context().monotonic_event_time + chrono::milliseconds(500),
1381 monotonic_now);
1382 EXPECT_GE(loop1->context().realtime_event_time + chrono::milliseconds(500),
1383 realtime_now);
1384
1385 triggered = true;
1386 });
1387
1388 test_timer->Setup(loop1->monotonic_now() + ::std::chrono::seconds(1));
1389
1390 EndEventLoop(loop1.get(), ::std::chrono::seconds(2));
1391 Run();
1392
1393 ASSERT_TRUE(triggered);
Austin Schuh7267c532019-05-19 19:55:53 -07001394
Alex Perrycb7da4b2019-08-28 19:35:56 -07001395 monotonic_clock::duration monotonic_time_offset =
Austin Schuhad154822019-12-27 15:45:13 -08001396 fetcher.context().monotonic_event_time -
Alex Perrycb7da4b2019-08-28 19:35:56 -07001397 (loop1->monotonic_now() - ::std::chrono::seconds(1));
1398 realtime_clock::duration realtime_time_offset =
Austin Schuhad154822019-12-27 15:45:13 -08001399 fetcher.context().realtime_event_time -
Alex Perrycb7da4b2019-08-28 19:35:56 -07001400 (loop1->realtime_now() - ::std::chrono::seconds(1));
Austin Schuh7267c532019-05-19 19:55:53 -07001401
Austin Schuhad154822019-12-27 15:45:13 -08001402 EXPECT_EQ(fetcher.context().realtime_event_time,
1403 fetcher.context().realtime_remote_time);
1404 EXPECT_EQ(fetcher.context().monotonic_event_time,
1405 fetcher.context().monotonic_remote_time);
Austin Schuha9012be2021-07-21 15:19:11 -07001406 EXPECT_EQ(fetcher.context().source_boot_uuid, loop1->boot_uuid());
Austin Schuhad154822019-12-27 15:45:13 -08001407
Alex Perrycb7da4b2019-08-28 19:35:56 -07001408 EXPECT_TRUE(monotonic_time_offset > ::std::chrono::milliseconds(-500))
1409 << ": Got "
Austin Schuhad154822019-12-27 15:45:13 -08001410 << fetcher.context().monotonic_event_time.time_since_epoch().count()
Austin Schuh52d325c2019-06-23 18:59:06 -07001411 << " expected " << loop1->monotonic_now().time_since_epoch().count();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001412 // Confirm that the data pointer makes sense.
1413 EXPECT_GT(fetcher.get(), fetcher.context().data);
1414 EXPECT_LT(fetcher.get(),
Brian Silvermaneaa41d62020-07-08 19:47:35 -07001415 reinterpret_cast<const void *>(
1416 reinterpret_cast<const char *>(fetcher.context().data) +
Alex Perrycb7da4b2019-08-28 19:35:56 -07001417 fetcher.context().size));
1418 EXPECT_TRUE(monotonic_time_offset < ::std::chrono::milliseconds(500))
1419 << ": Got "
Austin Schuhad154822019-12-27 15:45:13 -08001420 << fetcher.context().monotonic_event_time.time_since_epoch().count()
Austin Schuh7267c532019-05-19 19:55:53 -07001421 << " expected " << loop1->monotonic_now().time_since_epoch().count();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001422
1423 EXPECT_TRUE(realtime_time_offset > ::std::chrono::milliseconds(-500))
1424 << ": Got "
Austin Schuhad154822019-12-27 15:45:13 -08001425 << fetcher.context().realtime_event_time.time_since_epoch().count()
Alex Perrycb7da4b2019-08-28 19:35:56 -07001426 << " expected " << loop1->realtime_now().time_since_epoch().count();
1427 EXPECT_TRUE(realtime_time_offset < ::std::chrono::milliseconds(500))
1428 << ": Got "
Austin Schuhad154822019-12-27 15:45:13 -08001429 << fetcher.context().realtime_event_time.time_since_epoch().count()
Alex Perrycb7da4b2019-08-28 19:35:56 -07001430 << " expected " << loop1->realtime_now().time_since_epoch().count();
Austin Schuh7267c532019-05-19 19:55:53 -07001431}
1432
Austin Schuh52d325c2019-06-23 18:59:06 -07001433// Tests that a couple phased loops run in a row result in the correct offset
1434// and period.
1435TEST_P(AbstractEventLoopTest, PhasedLoopTest) {
Stephan Pleines3dce7ea2021-06-22 13:19:26 -07001436 // Force a slower rate so we are guaranteed to have reports for our phased
Austin Schuh39788ff2019-12-01 18:22:57 -08001437 // loop.
1438 FLAGS_timing_report_ms = 2000;
1439
Austin Schuh52d325c2019-06-23 18:59:06 -07001440 const chrono::milliseconds kOffset = chrono::milliseconds(400);
1441 const int kCount = 5;
1442
1443 auto loop1 = MakePrimary();
Austin Schuh39788ff2019-12-01 18:22:57 -08001444 auto loop2 = Make();
1445
1446 Fetcher<timing::Report> report_fetcher =
1447 loop2->MakeFetcher<timing::Report>("/aos");
1448 EXPECT_FALSE(report_fetcher.Fetch());
Austin Schuh52d325c2019-06-23 18:59:06 -07001449
1450 // Collect up a couple of samples.
1451 ::std::vector<::aos::monotonic_clock::time_point> times;
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001452 ::std::vector<::aos::monotonic_clock::time_point> expected_times;
Austin Schuh52d325c2019-06-23 18:59:06 -07001453
1454 // Run kCount iterations.
Austin Schuh39788ff2019-12-01 18:22:57 -08001455 loop1
1456 ->AddPhasedLoop(
1457 [&times, &expected_times, &loop1, this](int count) {
1458 EXPECT_EQ(count, 1);
1459 times.push_back(loop1->monotonic_now());
Austin Schuhad154822019-12-27 15:45:13 -08001460 expected_times.push_back(loop1->context().monotonic_event_time);
Austin Schuh39788ff2019-12-01 18:22:57 -08001461
Austin Schuhad154822019-12-27 15:45:13 -08001462 EXPECT_EQ(loop1->context().monotonic_remote_time,
1463 monotonic_clock::min_time);
Austin Schuha9012be2021-07-21 15:19:11 -07001464 EXPECT_EQ(loop1->context().source_boot_uuid, loop1->boot_uuid());
Austin Schuhad154822019-12-27 15:45:13 -08001465 EXPECT_EQ(loop1->context().realtime_event_time,
1466 realtime_clock::min_time);
1467 EXPECT_EQ(loop1->context().realtime_remote_time,
Austin Schuh39788ff2019-12-01 18:22:57 -08001468 realtime_clock::min_time);
1469 EXPECT_EQ(loop1->context().queue_index, 0xffffffffu);
1470 EXPECT_EQ(loop1->context().size, 0u);
1471 EXPECT_EQ(loop1->context().data, nullptr);
Brian Silverman4f4e0612020-08-12 19:54:41 -07001472 EXPECT_EQ(loop1->context().buffer_index, -1);
Austin Schuh39788ff2019-12-01 18:22:57 -08001473
1474 if (times.size() == kCount) {
1475 LOG(INFO) << "Exiting";
1476 this->Exit();
1477 }
1478 },
1479 chrono::seconds(1), kOffset)
1480 ->set_name("Test loop");
Austin Schuh52d325c2019-06-23 18:59:06 -07001481
1482 // Add a delay to make sure that delay during startup doesn't result in a
1483 // "missed cycle".
1484 SleepFor(chrono::seconds(2));
1485
1486 Run();
1487
1488 // Confirm that we got both the right number of samples, and it's odd.
Stephan Pleines3dce7ea2021-06-22 13:19:26 -07001489 ASSERT_EQ(times.size(), static_cast<size_t>(kCount));
1490 ASSERT_EQ(times.size(), expected_times.size());
1491 ASSERT_EQ((times.size() % 2), 1);
Austin Schuh52d325c2019-06-23 18:59:06 -07001492
1493 // Grab the middle sample.
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001494 ::aos::monotonic_clock::time_point average_time = times[times.size() / 2];
Austin Schuh52d325c2019-06-23 18:59:06 -07001495
1496 // Add up all the delays of all the times.
1497 ::aos::monotonic_clock::duration sum = chrono::seconds(0);
1498 for (const ::aos::monotonic_clock::time_point time : times) {
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001499 sum += time - average_time;
Austin Schuh52d325c2019-06-23 18:59:06 -07001500 }
1501
1502 // Average and add to the middle to find the average time.
1503 sum /= times.size();
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001504 average_time += sum;
Austin Schuh52d325c2019-06-23 18:59:06 -07001505
1506 // Compute the offset from the start of the second of the average time. This
1507 // should be pretty close to the offset.
1508 const ::aos::monotonic_clock::duration remainder =
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001509 average_time.time_since_epoch() -
1510 chrono::duration_cast<chrono::seconds>(average_time.time_since_epoch());
Austin Schuh52d325c2019-06-23 18:59:06 -07001511
1512 const chrono::milliseconds kEpsilon(100);
1513 EXPECT_LT(remainder, kOffset + kEpsilon);
1514 EXPECT_GT(remainder, kOffset - kEpsilon);
1515
1516 // Make sure that the average duration is close to 1 second.
1517 EXPECT_NEAR(chrono::duration_cast<chrono::duration<double>>(times.back() -
1518 times.front())
1519 .count() /
1520 static_cast<double>(times.size() - 1),
1521 1.0, 0.1);
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001522
1523 // Confirm that the ideal wakeup times increment correctly.
1524 for (size_t i = 1; i < expected_times.size(); ++i) {
1525 EXPECT_EQ(expected_times[i], expected_times[i - 1] + chrono::seconds(1));
1526 }
1527
1528 for (size_t i = 0; i < expected_times.size(); ++i) {
1529 EXPECT_EQ(expected_times[i].time_since_epoch() % chrono::seconds(1),
1530 kOffset);
1531 }
1532
1533 EXPECT_LT(expected_times[expected_times.size() / 2], average_time + kEpsilon);
1534 EXPECT_GT(expected_times[expected_times.size() / 2], average_time - kEpsilon);
Austin Schuh39788ff2019-12-01 18:22:57 -08001535
Austin Schuh6bae8252021-02-07 22:01:49 -08001536 if (do_timing_reports() == DoTimingReports::kYes) {
1537 // And, since we are here, check that the timing report makes sense.
1538 // Start by looking for our event loop's timing.
1539 FlatbufferDetachedBuffer<timing::Report> report =
1540 FlatbufferDetachedBuffer<timing::Report>::Empty();
1541 while (report_fetcher.FetchNext()) {
1542 if (report_fetcher->name()->string_view() == "primary") {
1543 report = CopyFlatBuffer(report_fetcher.get());
1544 }
Austin Schuh39788ff2019-12-01 18:22:57 -08001545 }
Austin Schuh6bae8252021-02-07 22:01:49 -08001546
1547 VLOG(1) << FlatbufferToJson(report, {.multi_line = true});
1548
1549 EXPECT_EQ(report.message().name()->string_view(), "primary");
1550
1551 ASSERT_NE(report.message().senders(), nullptr);
1552 EXPECT_EQ(report.message().senders()->size(), 2);
1553
1554 ASSERT_NE(report.message().timers(), nullptr);
1555 EXPECT_EQ(report.message().timers()->size(), 1);
1556
1557 // Make sure there is a single phased loop report with our report in it.
1558 ASSERT_NE(report.message().phased_loops(), nullptr);
1559 ASSERT_EQ(report.message().phased_loops()->size(), 1);
1560 EXPECT_EQ(report.message().phased_loops()->Get(0)->name()->string_view(),
1561 "Test loop");
1562 EXPECT_GE(report.message().phased_loops()->Get(0)->count(), 1);
1563 } else {
1564 ASSERT_FALSE(report_fetcher.Fetch());
Austin Schuh39788ff2019-12-01 18:22:57 -08001565 }
Austin Schuh39788ff2019-12-01 18:22:57 -08001566}
1567
Milind Upadhyay42589bb2021-05-19 20:05:16 -07001568// Tests that a phased loop responds correctly to a changing offset.
1569TEST_P(AbstractEventLoopTest, PhasedLoopChangingOffsetTest) {
1570 // Force a slower rate so we are guaranteed to have reports for our phased
1571 // loop.
1572 FLAGS_timing_report_ms = 2000;
1573
1574 const chrono::milliseconds kOffset = chrono::milliseconds(400);
1575 const chrono::milliseconds kInterval = chrono::milliseconds(1000);
1576 const int kCount = 5;
1577
1578 auto loop1 = MakePrimary();
1579
1580 // Collect up a couple of samples.
1581 ::std::vector<::aos::monotonic_clock::time_point> times;
1582 ::std::vector<::aos::monotonic_clock::time_point> expected_times;
1583
1584 PhasedLoopHandler *phased_loop;
1585
1586 // Run kCount iterations.
1587 phased_loop = loop1->AddPhasedLoop(
1588 [&phased_loop, &times, &expected_times, &loop1, this, kOffset,
1589 kInterval](int count) {
1590 EXPECT_EQ(count, 1);
1591 times.push_back(loop1->monotonic_now());
1592
1593 expected_times.push_back(loop1->context().monotonic_event_time);
1594
1595 phased_loop->set_interval_and_offset(
1596 kInterval, kOffset - chrono::milliseconds(times.size()));
1597 LOG(INFO) << "new offset: "
1598 << (kOffset - chrono::milliseconds(times.size())).count();
1599
1600 if (times.size() == kCount) {
1601 LOG(INFO) << "Exiting";
1602 this->Exit();
1603 }
1604 },
1605 kInterval, kOffset);
1606 phased_loop->set_name("Test loop");
1607
1608 // Add a delay to make sure that delay during startup doesn't result in a
1609 // "missed cycle".
1610 SleepFor(chrono::seconds(2));
1611
1612 Run();
1613 // Confirm that we got both the right number of samples, and it's odd.
1614 EXPECT_EQ(times.size(), static_cast<size_t>(kCount));
1615 EXPECT_EQ(times.size(), expected_times.size());
1616 EXPECT_EQ((times.size() % 2), 1);
1617
1618 // Grab the middle sample.
1619 ::aos::monotonic_clock::time_point average_time = times[times.size() / 2];
1620
1621 // Add up all the delays of all the times.
1622 ::aos::monotonic_clock::duration sum = chrono::seconds(0);
1623 for (const ::aos::monotonic_clock::time_point time : times) {
1624 sum += time - average_time;
1625 }
1626
1627 // Average and add to the middle to find the average time.
1628 sum /= times.size();
1629 average_time += sum;
1630
1631 // Compute the offset from the start of the second of the average time. This
1632 // should be pretty close to the offset.
1633 const ::aos::monotonic_clock::duration remainder =
1634 average_time.time_since_epoch() -
1635 chrono::duration_cast<chrono::seconds>(average_time.time_since_epoch());
1636
1637 const chrono::milliseconds kEpsilon(100);
1638 EXPECT_LT(remainder, kOffset + kEpsilon);
1639 EXPECT_GT(remainder, kOffset - kEpsilon);
1640
1641 // Make sure that the average duration is close to 1 second.
1642 EXPECT_NEAR(chrono::duration_cast<chrono::duration<double>>(times.back() -
1643 times.front())
1644 .count() /
1645 static_cast<double>(times.size() - 1),
1646 1.0, 0.1);
1647
1648 // Confirm that the ideal wakeup times increment correctly.
1649 for (size_t i = 1; i < expected_times.size(); ++i) {
1650 LOG(INFO) << i - 1 << ": " << expected_times[i - 1] << ", " << i << ": "
1651 << expected_times[i];
1652 EXPECT_EQ(expected_times[i], expected_times[i - 1] + chrono::seconds(1) -
1653 chrono::milliseconds(1));
1654 }
1655
1656 for (size_t i = 0; i < expected_times.size(); ++i) {
1657 EXPECT_EQ(expected_times[i].time_since_epoch() % chrono::seconds(1),
1658 kOffset - chrono::milliseconds(i));
1659 }
1660
1661 EXPECT_LT(expected_times[expected_times.size() / 2], average_time + kEpsilon);
1662 EXPECT_GT(expected_times[expected_times.size() / 2], average_time - kEpsilon);
1663}
1664
Austin Schuh39788ff2019-12-01 18:22:57 -08001665// Tests that senders count correctly in the timing report.
1666TEST_P(AbstractEventLoopTest, SenderTimingReport) {
1667 FLAGS_timing_report_ms = 1000;
1668 auto loop1 = MakePrimary();
1669
1670 auto loop2 = Make("watcher_loop");
1671 loop2->MakeWatcher("/test", [](const TestMessage &) {});
1672
1673 auto loop3 = Make();
1674
1675 Fetcher<timing::Report> report_fetcher =
1676 loop3->MakeFetcher<timing::Report>("/aos");
1677 EXPECT_FALSE(report_fetcher.Fetch());
1678
1679 auto sender = loop1->MakeSender<TestMessage>("/test");
1680
1681 // Add a timer to actually quit.
1682 auto test_timer = loop1->AddTimer([&sender]() {
1683 for (int i = 0; i < 10; ++i) {
1684 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
1685 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
1686 builder.add_value(200 + i);
1687 ASSERT_TRUE(msg.Send(builder.Finish()));
1688 }
1689 });
1690
1691 // Quit after 1 timing report, mid way through the next cycle.
1692 EndEventLoop(loop1.get(), chrono::milliseconds(2500));
1693
1694 loop1->OnRun([&test_timer, &loop1]() {
1695 test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1500));
1696 });
1697
1698 Run();
1699
Austin Schuh6bae8252021-02-07 22:01:49 -08001700 if (do_timing_reports() == DoTimingReports::kYes) {
1701 // And, since we are here, check that the timing report makes sense.
1702 // Start by looking for our event loop's timing.
1703 FlatbufferDetachedBuffer<timing::Report> primary_report =
1704 FlatbufferDetachedBuffer<timing::Report>::Empty();
1705 while (report_fetcher.FetchNext()) {
Austin Schuh8902fa52021-03-14 22:39:24 -07001706 VLOG(1) << "Report " << FlatbufferToJson(report_fetcher.get());
Austin Schuh6bae8252021-02-07 22:01:49 -08001707 if (report_fetcher->name()->string_view() == "primary") {
1708 primary_report = CopyFlatBuffer(report_fetcher.get());
1709 }
Austin Schuh39788ff2019-12-01 18:22:57 -08001710 }
Austin Schuh6bae8252021-02-07 22:01:49 -08001711
Austin Schuh8902fa52021-03-14 22:39:24 -07001712 VLOG(1) << FlatbufferToJson(primary_report, {.multi_line = true});
Austin Schuh6bae8252021-02-07 22:01:49 -08001713
1714 EXPECT_EQ(primary_report.message().name()->string_view(), "primary");
1715
1716 ASSERT_NE(primary_report.message().senders(), nullptr);
1717 EXPECT_EQ(primary_report.message().senders()->size(), 3);
1718
1719 // Confirm that the sender looks sane.
1720 EXPECT_EQ(
1721 loop1->configuration()
1722 ->channels()
1723 ->Get(primary_report.message().senders()->Get(0)->channel_index())
1724 ->name()
1725 ->string_view(),
1726 "/test");
1727 EXPECT_EQ(primary_report.message().senders()->Get(0)->count(), 10);
1728
1729 // Confirm that the timing primary_report sender looks sane.
1730 EXPECT_EQ(
1731 loop1->configuration()
1732 ->channels()
1733 ->Get(primary_report.message().senders()->Get(1)->channel_index())
1734 ->name()
1735 ->string_view(),
1736 "/aos");
1737 EXPECT_EQ(primary_report.message().senders()->Get(1)->count(), 1);
1738
1739 ASSERT_NE(primary_report.message().timers(), nullptr);
1740 EXPECT_EQ(primary_report.message().timers()->size(), 3);
1741
1742 // Make sure there are no phased loops or watchers.
1743 ASSERT_EQ(primary_report.message().phased_loops(), nullptr);
1744 ASSERT_EQ(primary_report.message().watchers(), nullptr);
1745 } else {
1746 ASSERT_FALSE(report_fetcher.Fetch());
Austin Schuh39788ff2019-12-01 18:22:57 -08001747 }
Austin Schuh39788ff2019-12-01 18:22:57 -08001748}
1749
1750// Tests that senders count correctly in the timing report.
1751TEST_P(AbstractEventLoopTest, WatcherTimingReport) {
1752 FLAGS_timing_report_ms = 1000;
1753 auto loop1 = MakePrimary();
1754 loop1->MakeWatcher("/test", [](const TestMessage &) {});
1755
1756 auto loop2 = Make("sender_loop");
1757
1758 auto loop3 = Make();
1759
1760 Fetcher<timing::Report> report_fetcher =
1761 loop3->MakeFetcher<timing::Report>("/aos");
1762 EXPECT_FALSE(report_fetcher.Fetch());
1763
1764 auto sender = loop2->MakeSender<TestMessage>("/test");
1765
1766 // Add a timer to actually quit.
1767 auto test_timer = loop1->AddTimer([&sender]() {
1768 for (int i = 0; i < 10; ++i) {
1769 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
1770 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
1771 builder.add_value(200 + i);
1772 ASSERT_TRUE(msg.Send(builder.Finish()));
1773 }
1774 });
1775
1776 // Quit after 1 timing report, mid way through the next cycle.
1777 EndEventLoop(loop1.get(), chrono::milliseconds(2500));
1778
1779 loop1->OnRun([&test_timer, &loop1]() {
1780 test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1500));
1781 });
1782
1783 Run();
1784
Austin Schuh6bae8252021-02-07 22:01:49 -08001785 if (do_timing_reports() == DoTimingReports::kYes) {
1786 // And, since we are here, check that the timing report makes sense.
1787 // Start by looking for our event loop's timing.
1788 FlatbufferDetachedBuffer<timing::Report> primary_report =
1789 FlatbufferDetachedBuffer<timing::Report>::Empty();
1790 while (report_fetcher.FetchNext()) {
1791 LOG(INFO) << "Report " << FlatbufferToJson(report_fetcher.get());
1792 if (report_fetcher->name()->string_view() == "primary") {
1793 primary_report = CopyFlatBuffer(report_fetcher.get());
1794 }
Austin Schuh39788ff2019-12-01 18:22:57 -08001795 }
Austin Schuh6bae8252021-02-07 22:01:49 -08001796
1797 // Check the watcher report.
1798 VLOG(1) << FlatbufferToJson(primary_report, {.multi_line = true});
1799
1800 EXPECT_EQ(primary_report.message().name()->string_view(), "primary");
1801
1802 // Just the timing report timer.
1803 ASSERT_NE(primary_report.message().timers(), nullptr);
1804 EXPECT_EQ(primary_report.message().timers()->size(), 3);
1805
1806 // No phased loops
1807 ASSERT_EQ(primary_report.message().phased_loops(), nullptr);
1808
1809 ASSERT_NE(primary_report.message().watchers(), nullptr);
1810 ASSERT_EQ(primary_report.message().watchers()->size(), 1);
1811 EXPECT_EQ(primary_report.message().watchers()->Get(0)->count(), 10);
1812 } else {
1813 ASSERT_FALSE(report_fetcher.Fetch());
Austin Schuh39788ff2019-12-01 18:22:57 -08001814 }
Austin Schuh39788ff2019-12-01 18:22:57 -08001815}
1816
1817// Tests that fetchers count correctly in the timing report.
1818TEST_P(AbstractEventLoopTest, FetcherTimingReport) {
1819 FLAGS_timing_report_ms = 1000;
1820 auto loop1 = MakePrimary();
1821 auto loop2 = Make("sender_loop");
1822
1823 auto loop3 = Make();
1824
1825 Fetcher<timing::Report> report_fetcher =
1826 loop3->MakeFetcher<timing::Report>("/aos");
1827 EXPECT_FALSE(report_fetcher.Fetch());
1828
1829 auto sender = loop2->MakeSender<TestMessage>("/test");
1830 auto fetcher1 = loop1->MakeFetcher<TestMessage>("/test");
1831 auto fetcher2 = loop1->MakeFetcher<TestMessage>("/test");
1832 fetcher1.Fetch();
1833 fetcher2.Fetch();
1834
1835 // Add a timer to actually quit.
1836 auto test_timer = loop1->AddTimer([&sender]() {
1837 for (int i = 0; i < 10; ++i) {
1838 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
1839 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
1840 builder.add_value(200 + i);
1841 ASSERT_TRUE(msg.Send(builder.Finish()));
1842 }
1843 });
1844
1845 auto test_timer2 = loop1->AddTimer([&fetcher1, &fetcher2]() {
1846 fetcher1.Fetch();
1847 while (fetcher2.FetchNext()) {
1848 }
1849 });
1850
1851 // Quit after 1 timing report, mid way through the next cycle.
1852 EndEventLoop(loop1.get(), chrono::milliseconds(2500));
1853
1854 loop1->OnRun([test_timer, test_timer2, &loop1]() {
1855 test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1400));
1856 test_timer2->Setup(loop1->monotonic_now() + chrono::milliseconds(1600));
1857 });
1858
1859 Run();
1860
Austin Schuh6bae8252021-02-07 22:01:49 -08001861 if (do_timing_reports() == DoTimingReports::kYes) {
1862 // And, since we are here, check that the timing report makes sense.
1863 // Start by looking for our event loop's timing.
1864 FlatbufferDetachedBuffer<timing::Report> primary_report =
1865 FlatbufferDetachedBuffer<timing::Report>::Empty();
1866 while (report_fetcher.FetchNext()) {
1867 if (report_fetcher->name()->string_view() == "primary") {
1868 primary_report = CopyFlatBuffer(report_fetcher.get());
1869 }
Austin Schuh39788ff2019-12-01 18:22:57 -08001870 }
Austin Schuh6bae8252021-02-07 22:01:49 -08001871
1872 VLOG(1) << FlatbufferToJson(primary_report, {.multi_line = true});
1873
1874 EXPECT_EQ(primary_report.message().name()->string_view(), "primary");
1875
1876 ASSERT_NE(primary_report.message().senders(), nullptr);
1877 EXPECT_EQ(primary_report.message().senders()->size(), 2);
1878
1879 ASSERT_NE(primary_report.message().timers(), nullptr);
1880 EXPECT_EQ(primary_report.message().timers()->size(), 4);
1881
1882 // Make sure there are no phased loops or watchers.
1883 ASSERT_EQ(primary_report.message().phased_loops(), nullptr);
1884 ASSERT_EQ(primary_report.message().watchers(), nullptr);
1885
1886 // Now look at the fetchrs.
1887 ASSERT_NE(primary_report.message().fetchers(), nullptr);
1888 ASSERT_EQ(primary_report.message().fetchers()->size(), 2);
1889
1890 EXPECT_EQ(primary_report.message().fetchers()->Get(0)->count(), 1);
1891 EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->average(),
1892 0.1);
1893 EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->min(),
1894 0.1);
1895 EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->max(),
1896 0.1);
1897 EXPECT_EQ(primary_report.message()
1898 .fetchers()
1899 ->Get(0)
1900 ->latency()
1901 ->standard_deviation(),
1902 0.0);
1903
1904 EXPECT_EQ(primary_report.message().fetchers()->Get(1)->count(), 10);
1905 } else {
1906 ASSERT_FALSE(report_fetcher.Fetch());
Austin Schuh39788ff2019-12-01 18:22:57 -08001907 }
Austin Schuh52d325c2019-06-23 18:59:06 -07001908}
1909
Austin Schuh67420a42019-12-21 21:55:04 -08001910// Tests that a raw watcher and raw fetcher can receive messages from a raw
1911// sender without messing up offsets.
1912TEST_P(AbstractEventLoopTest, RawBasic) {
1913 auto loop1 = Make();
1914 auto loop2 = MakePrimary();
1915 auto loop3 = Make();
1916
Austin Schuha9df9ad2021-06-16 14:49:39 -07001917 const FlatbufferDetachedBuffer<TestMessage> kMessage =
1918 JsonToFlatbuffer<TestMessage>("{}");
Austin Schuh67420a42019-12-21 21:55:04 -08001919
1920 std::unique_ptr<aos::RawSender> sender =
Tyler Chatow67ddb032020-01-12 14:30:04 -08001921 loop1->MakeRawSender(configuration::GetChannel(
1922 loop1->configuration(), "/test", "aos.TestMessage", "", nullptr));
Austin Schuh67420a42019-12-21 21:55:04 -08001923
1924 std::unique_ptr<aos::RawFetcher> fetcher =
Tyler Chatow67ddb032020-01-12 14:30:04 -08001925 loop3->MakeRawFetcher(configuration::GetChannel(
1926 loop3->configuration(), "/test", "aos.TestMessage", "", nullptr));
Austin Schuh67420a42019-12-21 21:55:04 -08001927
Austin Schuha9df9ad2021-06-16 14:49:39 -07001928 loop2->OnRun([&]() {
1929 EXPECT_TRUE(sender->Send(kMessage.span().data(), kMessage.span().size()));
1930 });
Austin Schuh67420a42019-12-21 21:55:04 -08001931
1932 bool happened = false;
1933 loop2->MakeRawWatcher(
Tyler Chatow67ddb032020-01-12 14:30:04 -08001934 configuration::GetChannel(loop2->configuration(), "/test",
1935 "aos.TestMessage", "", nullptr),
Austin Schuha9df9ad2021-06-16 14:49:39 -07001936 [this, &kMessage, &fetcher, &happened](const Context &context,
1937 const void *message) {
Austin Schuh67420a42019-12-21 21:55:04 -08001938 happened = true;
Austin Schuha9df9ad2021-06-16 14:49:39 -07001939 EXPECT_EQ(
1940 kMessage.span(),
1941 absl::Span<const uint8_t>(
1942 reinterpret_cast<const uint8_t *>(message), context.size));
1943 EXPECT_EQ(message, context.data);
Austin Schuh67420a42019-12-21 21:55:04 -08001944
1945 ASSERT_TRUE(fetcher->Fetch());
1946
Austin Schuha9df9ad2021-06-16 14:49:39 -07001947 EXPECT_EQ(kMessage.span(),
1948 absl::Span<const uint8_t>(reinterpret_cast<const uint8_t *>(
1949 fetcher->context().data),
1950 fetcher->context().size));
Austin Schuh67420a42019-12-21 21:55:04 -08001951
1952 this->Exit();
1953 });
1954
1955 EXPECT_FALSE(happened);
1956 Run();
1957 EXPECT_TRUE(happened);
1958}
1959
Austin Schuhad154822019-12-27 15:45:13 -08001960// Tests that a raw watcher and raw fetcher can receive messages from a raw
1961// sender with remote times filled out.
1962TEST_P(AbstractEventLoopTest, RawRemoteTimes) {
1963 auto loop1 = Make();
1964 auto loop2 = MakePrimary();
1965 auto loop3 = Make();
1966
Austin Schuha9df9ad2021-06-16 14:49:39 -07001967 const FlatbufferDetachedBuffer<TestMessage> kMessage =
1968 JsonToFlatbuffer<TestMessage>("{}");
Austin Schuhad154822019-12-27 15:45:13 -08001969
1970 const aos::monotonic_clock::time_point monotonic_remote_time =
1971 aos::monotonic_clock::time_point(chrono::seconds(1501));
1972 const aos::realtime_clock::time_point realtime_remote_time =
1973 aos::realtime_clock::time_point(chrono::seconds(3132));
Austin Schuhb5c6f972021-03-14 21:53:07 -07001974 const uint32_t remote_queue_index = 0x254971;
Austin Schuha9012be2021-07-21 15:19:11 -07001975 const UUID source_boot_uuid = UUID::Random();
Austin Schuhad154822019-12-27 15:45:13 -08001976
1977 std::unique_ptr<aos::RawSender> sender =
Tyler Chatow67ddb032020-01-12 14:30:04 -08001978 loop1->MakeRawSender(configuration::GetChannel(
1979 loop1->configuration(), "/test", "aos.TestMessage", "", nullptr));
Austin Schuhad154822019-12-27 15:45:13 -08001980
1981 std::unique_ptr<aos::RawFetcher> fetcher =
Tyler Chatow67ddb032020-01-12 14:30:04 -08001982 loop3->MakeRawFetcher(configuration::GetChannel(
1983 loop3->configuration(), "/test", "aos.TestMessage", "", nullptr));
Austin Schuhad154822019-12-27 15:45:13 -08001984
1985 loop2->OnRun([&]() {
Austin Schuha9df9ad2021-06-16 14:49:39 -07001986 EXPECT_TRUE(sender->Send(kMessage.span().data(), kMessage.span().size(),
1987 monotonic_remote_time, realtime_remote_time,
Austin Schuha9012be2021-07-21 15:19:11 -07001988 remote_queue_index, source_boot_uuid));
Austin Schuhad154822019-12-27 15:45:13 -08001989 });
1990
1991 bool happened = false;
1992 loop2->MakeRawWatcher(
Tyler Chatow67ddb032020-01-12 14:30:04 -08001993 configuration::GetChannel(loop2->configuration(), "/test",
1994 "aos.TestMessage", "", nullptr),
Austin Schuha9012be2021-07-21 15:19:11 -07001995 [this, monotonic_remote_time, realtime_remote_time, source_boot_uuid,
Austin Schuhb5c6f972021-03-14 21:53:07 -07001996 remote_queue_index, &fetcher,
1997 &happened](const Context &context, const void * /*message*/) {
Austin Schuhad154822019-12-27 15:45:13 -08001998 happened = true;
1999 EXPECT_EQ(monotonic_remote_time, context.monotonic_remote_time);
2000 EXPECT_EQ(realtime_remote_time, context.realtime_remote_time);
Austin Schuha9012be2021-07-21 15:19:11 -07002001 EXPECT_EQ(source_boot_uuid, context.source_boot_uuid);
Austin Schuhb5c6f972021-03-14 21:53:07 -07002002 EXPECT_EQ(remote_queue_index, context.remote_queue_index);
Austin Schuhad154822019-12-27 15:45:13 -08002003
2004 ASSERT_TRUE(fetcher->Fetch());
2005 EXPECT_EQ(monotonic_remote_time,
2006 fetcher->context().monotonic_remote_time);
2007 EXPECT_EQ(realtime_remote_time,
2008 fetcher->context().realtime_remote_time);
2009
2010 this->Exit();
2011 });
2012
2013 EXPECT_FALSE(happened);
2014 Run();
2015 EXPECT_TRUE(happened);
2016}
2017
2018// Tests that a raw sender fills out sent data.
2019TEST_P(AbstractEventLoopTest, RawSenderSentData) {
2020 auto loop1 = MakePrimary();
2021
Austin Schuha9df9ad2021-06-16 14:49:39 -07002022 const FlatbufferDetachedBuffer<TestMessage> kMessage =
2023 JsonToFlatbuffer<TestMessage>("{}");
Austin Schuhad154822019-12-27 15:45:13 -08002024
2025 std::unique_ptr<aos::RawSender> sender =
Tyler Chatow67ddb032020-01-12 14:30:04 -08002026 loop1->MakeRawSender(configuration::GetChannel(
2027 loop1->configuration(), "/test", "aos.TestMessage", "", nullptr));
Austin Schuhad154822019-12-27 15:45:13 -08002028
Tyler Chatow67ddb032020-01-12 14:30:04 -08002029 const aos::monotonic_clock::time_point monotonic_now = loop1->monotonic_now();
2030 const aos::realtime_clock::time_point realtime_now = loop1->realtime_now();
Austin Schuhad154822019-12-27 15:45:13 -08002031
Austin Schuha9df9ad2021-06-16 14:49:39 -07002032 EXPECT_TRUE(sender->Send(kMessage.span().data(), kMessage.span().size()));
Austin Schuhad154822019-12-27 15:45:13 -08002033
2034 EXPECT_GE(sender->monotonic_sent_time(), monotonic_now);
2035 EXPECT_LE(sender->monotonic_sent_time(),
2036 monotonic_now + chrono::milliseconds(100));
2037 EXPECT_GE(sender->realtime_sent_time(), realtime_now);
2038 EXPECT_LE(sender->realtime_sent_time(),
2039 realtime_now + chrono::milliseconds(100));
2040 EXPECT_EQ(sender->sent_queue_index(), 0u);
2041
Austin Schuha9df9ad2021-06-16 14:49:39 -07002042 EXPECT_TRUE(sender->Send(kMessage.span().data(), kMessage.span().size()));
Austin Schuhad154822019-12-27 15:45:13 -08002043
2044 EXPECT_GE(sender->monotonic_sent_time(), monotonic_now);
2045 EXPECT_LE(sender->monotonic_sent_time(),
2046 monotonic_now + chrono::milliseconds(100));
2047 EXPECT_GE(sender->realtime_sent_time(), realtime_now);
2048 EXPECT_LE(sender->realtime_sent_time(),
2049 realtime_now + chrono::milliseconds(100));
2050 EXPECT_EQ(sender->sent_queue_index(), 1u);
2051}
2052
Austin Schuh217a9782019-12-21 23:02:50 -08002053// Tests that not setting up nodes results in no node.
2054TEST_P(AbstractEventLoopTest, NoNode) {
2055 auto loop1 = Make();
2056 auto loop2 = MakePrimary();
2057
2058 EXPECT_EQ(loop1->node(), nullptr);
2059 EXPECT_EQ(loop2->node(), nullptr);
2060}
2061
2062// Tests that setting up nodes results in node being set.
2063TEST_P(AbstractEventLoopTest, Node) {
2064 EnableNodes("me");
2065
2066 auto loop1 = Make();
2067 auto loop2 = MakePrimary();
2068
2069 EXPECT_NE(loop1->node(), nullptr);
2070 EXPECT_NE(loop2->node(), nullptr);
2071}
2072
2073// Tests that watchers work with a node setup.
2074TEST_P(AbstractEventLoopTest, NodeWatcher) {
2075 EnableNodes("me");
2076
2077 auto loop1 = Make();
2078 auto loop2 = Make();
2079 loop1->MakeWatcher("/test", [](const TestMessage &) {});
Tyler Chatow67ddb032020-01-12 14:30:04 -08002080 loop2->MakeRawWatcher(
2081 configuration::GetChannel(configuration(), "/test", "aos.TestMessage", "",
2082 nullptr),
2083 [](const Context &, const void *) {});
Austin Schuh217a9782019-12-21 23:02:50 -08002084}
2085
Brian Silverman454bc112020-03-05 14:21:25 -08002086// Tests that no-arg watchers work with a node setup.
2087TEST_P(AbstractEventLoopTest, NodeNoArgWatcher) {
2088 EnableNodes("me");
2089
2090 auto loop1 = Make();
2091 auto loop2 = Make();
2092 loop1->MakeWatcher("/test", [](const TestMessage &) {});
2093 loop2->MakeRawNoArgWatcher(
2094 configuration::GetChannel(configuration(), "/test", "aos.TestMessage", "",
2095 nullptr),
2096 [](const Context &) {});
2097}
2098
Austin Schuh217a9782019-12-21 23:02:50 -08002099// Tests that fetcher work with a node setup.
2100TEST_P(AbstractEventLoopTest, NodeFetcher) {
2101 EnableNodes("me");
2102 auto loop1 = Make();
2103
2104 auto fetcher = loop1->MakeFetcher<TestMessage>("/test");
Tyler Chatow67ddb032020-01-12 14:30:04 -08002105 auto raw_fetcher = loop1->MakeRawFetcher(configuration::GetChannel(
2106 configuration(), "/test", "aos.TestMessage", "", nullptr));
Austin Schuh217a9782019-12-21 23:02:50 -08002107}
2108
2109// Tests that sender work with a node setup.
2110TEST_P(AbstractEventLoopTest, NodeSender) {
2111 EnableNodes("me");
2112 auto loop1 = Make();
2113
2114 aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test");
2115}
2116
Austin Schuhcc6070c2020-10-10 20:25:56 -07002117// Tests that a non-realtime event loop timer is marked non-realtime.
2118TEST_P(AbstractEventLoopTest, NonRealtimeEventLoopTimer) {
2119 auto loop1 = MakePrimary();
2120
2121 // Add a timer to actually quit.
2122 auto test_timer = loop1->AddTimer([this]() {
2123 CheckNotRealtime();
2124 this->Exit();
2125 });
2126
2127 loop1->OnRun([&test_timer, &loop1]() {
2128 CheckNotRealtime();
2129 test_timer->Setup(loop1->monotonic_now(), ::std::chrono::milliseconds(100));
2130 });
2131
2132 Run();
2133}
2134
2135// Tests that a realtime event loop timer is marked realtime.
2136TEST_P(AbstractEventLoopTest, RealtimeEventLoopTimer) {
2137 auto loop1 = MakePrimary();
2138
2139 loop1->SetRuntimeRealtimePriority(1);
2140
2141 // Add a timer to actually quit.
2142 auto test_timer = loop1->AddTimer([this]() {
2143 CheckRealtime();
2144 this->Exit();
2145 });
2146
2147 loop1->OnRun([&test_timer, &loop1]() {
2148 CheckRealtime();
2149 test_timer->Setup(loop1->monotonic_now(), ::std::chrono::milliseconds(100));
2150 });
2151
2152 Run();
2153}
2154
2155// Tests that a non-realtime event loop phased loop is marked non-realtime.
2156TEST_P(AbstractEventLoopTest, NonRealtimeEventLoopPhasedLoop) {
2157 auto loop1 = MakePrimary();
2158
2159 // Add a timer to actually quit.
2160 loop1->AddPhasedLoop(
2161 [this](int) {
2162 CheckNotRealtime();
2163 this->Exit();
2164 },
2165 chrono::seconds(1), chrono::seconds(0));
2166
2167 Run();
2168}
2169
2170// Tests that a realtime event loop phased loop is marked realtime.
2171TEST_P(AbstractEventLoopTest, RealtimeEventLoopPhasedLoop) {
2172 auto loop1 = MakePrimary();
2173
2174 loop1->SetRuntimeRealtimePriority(1);
2175
2176 // Add a timer to actually quit.
2177 loop1->AddPhasedLoop(
2178 [this](int) {
2179 CheckRealtime();
2180 this->Exit();
2181 },
2182 chrono::seconds(1), chrono::seconds(0));
2183
2184 Run();
2185}
2186
2187// Tests that a non-realtime event loop watcher is marked non-realtime.
2188TEST_P(AbstractEventLoopTest, NonRealtimeEventLoopWatcher) {
2189 auto loop1 = MakePrimary();
2190 auto loop2 = Make();
2191
2192 aos::Sender<TestMessage> sender = loop2->MakeSender<TestMessage>("/test");
2193
2194 loop1->OnRun([&]() {
2195 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
2196 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
2197 ASSERT_TRUE(msg.Send(builder.Finish()));
2198 });
2199
2200 loop1->MakeWatcher("/test", [&](const TestMessage &) {
2201 CheckNotRealtime();
2202 this->Exit();
2203 });
2204
2205 Run();
2206}
2207
2208// Tests that a realtime event loop watcher is marked realtime.
2209TEST_P(AbstractEventLoopTest, RealtimeEventLoopWatcher) {
2210 auto loop1 = MakePrimary();
2211 auto loop2 = Make();
2212
2213 loop1->SetRuntimeRealtimePriority(1);
2214
2215 aos::Sender<TestMessage> sender = loop2->MakeSender<TestMessage>("/test");
2216
2217 loop1->OnRun([&]() {
2218 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
2219 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
2220 ASSERT_TRUE(msg.Send(builder.Finish()));
2221 });
2222
2223 loop1->MakeWatcher("/test", [&](const TestMessage &) {
2224 CheckRealtime();
2225 this->Exit();
2226 });
2227
2228 Run();
2229}
2230
Austin Schuha9012be2021-07-21 15:19:11 -07002231// Tests that event loop's context's monotonic time is set to a value on OnRun.
2232TEST_P(AbstractEventLoopTest, SetContextOnRun) {
2233 auto loop = MakePrimary();
2234
2235 // We want to check that monotonic event time is before monotonic now
2236 // called inside of callback, but after time point obtained callback.
2237 aos::monotonic_clock::time_point monotonic_event_time_on_run;
2238
2239 loop->OnRun([&]() {
2240 monotonic_event_time_on_run = loop->context().monotonic_event_time;
2241 EXPECT_LE(monotonic_event_time_on_run, loop->monotonic_now());
2242 EXPECT_EQ(loop->context().monotonic_remote_time, monotonic_clock::min_time);
2243 EXPECT_EQ(loop->context().realtime_event_time, realtime_clock::min_time);
2244 EXPECT_EQ(loop->context().realtime_remote_time, realtime_clock::min_time);
2245 EXPECT_EQ(loop->context().source_boot_uuid, loop->boot_uuid());
2246 EXPECT_EQ(loop->context().queue_index, 0xffffffffu);
2247 EXPECT_EQ(loop->context().size, 0u);
2248 EXPECT_EQ(loop->context().data, nullptr);
2249 EXPECT_EQ(loop->context().buffer_index, -1);
2250 });
2251
2252 EndEventLoop(loop.get(), ::std::chrono::milliseconds(200));
2253
2254 const aos::monotonic_clock::time_point before_run_time =
2255 loop->monotonic_now();
2256 Run();
2257 EXPECT_GE(monotonic_event_time_on_run, before_run_time);
2258}
2259
Austin Schuh217a9782019-12-21 23:02:50 -08002260// Tests that watchers fail when created on the wrong node.
2261TEST_P(AbstractEventLoopDeathTest, NodeWatcher) {
2262 EnableNodes("them");
2263
2264 auto loop1 = Make();
2265 auto loop2 = Make();
2266 EXPECT_DEATH({ loop1->MakeWatcher("/test", [](const TestMessage &) {}); },
2267 "node");
2268 EXPECT_DEATH(
2269 {
Tyler Chatow67ddb032020-01-12 14:30:04 -08002270 loop2->MakeRawWatcher(
2271 configuration::GetChannel(configuration(), "/test",
2272 "aos.TestMessage", "", nullptr),
2273 [](const Context &, const void *) {});
Austin Schuh217a9782019-12-21 23:02:50 -08002274 },
2275 "node");
Brian Silverman454bc112020-03-05 14:21:25 -08002276 EXPECT_DEATH({ loop1->MakeNoArgWatcher<TestMessage>("/test", []() {}); },
2277 "node");
2278 EXPECT_DEATH(
2279 {
2280 loop2->MakeRawNoArgWatcher(
2281 configuration::GetChannel(configuration(), "/test",
2282 "aos.TestMessage", "", nullptr),
2283 [](const Context &) {});
2284 },
2285 "node");
Austin Schuh217a9782019-12-21 23:02:50 -08002286}
2287
2288// Tests that fetchers fail when created on the wrong node.
2289TEST_P(AbstractEventLoopDeathTest, NodeFetcher) {
2290 EnableNodes("them");
2291 auto loop1 = Make();
2292
2293 EXPECT_DEATH({ auto fetcher = loop1->MakeFetcher<TestMessage>("/test"); },
2294 "node");
2295 EXPECT_DEATH(
2296 {
Tyler Chatow67ddb032020-01-12 14:30:04 -08002297 auto raw_fetcher = loop1->MakeRawFetcher(configuration::GetChannel(
2298 configuration(), "/test", "aos.TestMessage", "", nullptr));
Austin Schuh217a9782019-12-21 23:02:50 -08002299 },
2300 "node");
2301}
2302
2303// Tests that senders fail when created on the wrong node.
2304TEST_P(AbstractEventLoopDeathTest, NodeSender) {
2305 EnableNodes("them");
2306 auto loop1 = Make();
2307
2308 EXPECT_DEATH(
2309 {
2310 aos::Sender<TestMessage> sender =
2311 loop1->MakeSender<TestMessage>("/test");
2312 },
2313 "node");
2314
2315 // Note: Creating raw senders is always supported. Right now, this lets us
2316 // use them to create message_gateway.
2317}
2318
Brian Silverman341b57e2020-06-23 16:23:18 -07002319// Tests creating multiple Builders from a single Sender at the same time.
2320TEST_P(AbstractEventLoopDeathTest, MultipleBuilders) {
2321 auto loop1 = Make();
2322 aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test");
2323
2324 { auto builder = sender.MakeBuilder(); }
2325 {
2326 auto builder = sender.MakeBuilder();
2327 builder.MakeBuilder<TestMessage>().Finish();
2328 }
2329 {
2330 // Creating this after the first one was destroyed should be fine.
2331 auto builder = sender.MakeBuilder();
2332 builder.MakeBuilder<TestMessage>().Finish();
2333 // But not a second one.
2334 EXPECT_DEATH(sender.MakeBuilder().MakeBuilder<TestMessage>().Finish(),
2335 "May not overwrite in-use allocator");
2336 }
2337
2338 FlatbufferDetachedBuffer<TestMessage> detached =
2339 flatbuffers::DetachedBuffer();
2340 {
2341 auto builder = sender.MakeBuilder();
2342 detached = builder.Detach(builder.MakeBuilder<TestMessage>().Finish());
2343 }
2344 {
2345 // This is the second one, after the detached one, so it should fail.
2346 EXPECT_DEATH(sender.MakeBuilder().MakeBuilder<TestMessage>().Finish(),
2347 "May not overwrite in-use allocator");
2348 }
2349
2350 // Clear the detached one, and then we should be able to create another.
2351 detached = flatbuffers::DetachedBuffer();
2352 {
2353 auto builder = sender.MakeBuilder();
2354 builder.MakeBuilder<TestMessage>().Finish();
2355 }
2356
2357 // And then detach another one.
2358 {
2359 auto builder = sender.MakeBuilder();
2360 detached = builder.Detach(builder.MakeBuilder<TestMessage>().Finish());
2361 }
2362}
2363
2364// Tests sending a buffer detached from a different builder.
2365TEST_P(AbstractEventLoopDeathTest, WrongDetachedBuffer) {
2366 auto loop1 = Make();
2367 aos::Sender<TestMessage> sender1 = loop1->MakeSender<TestMessage>("/test");
2368 aos::Sender<TestMessage> sender2 = loop1->MakeSender<TestMessage>("/test");
2369
2370 auto builder = sender1.MakeBuilder();
2371 FlatbufferDetachedBuffer<TestMessage> detached =
2372 builder.Detach(builder.MakeBuilder<TestMessage>().Finish());
2373 EXPECT_DEATH(sender2.SendDetached(std::move(detached)),
2374 "May only send the buffer detached from this Sender");
2375}
2376
Parker Schuhe4a70d62017-12-27 20:10:20 -08002377} // namespace testing
2378} // namespace aos