blob: 71bef374db1cae4f6b2f5bab31a9b526a1f6cb37 [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>
4
Alex Perrycb7da4b2019-08-28 19:35:56 -07005#include "aos/events/test_message_generated.h"
Austin Schuh54cf95f2019-11-29 13:14:18 -08006#include "aos/flatbuffer_merge.h"
7#include "glog/logging.h"
Tyler Chatow67ddb032020-01-12 14:30:04 -08008#include "gmock/gmock.h"
9#include "gtest/gtest.h"
Austin Schuh9fe68f72019-08-10 19:32:03 -070010
Parker Schuhe4a70d62017-12-27 20:10:20 -080011namespace aos {
12namespace testing {
Austin Schuh52d325c2019-06-23 18:59:06 -070013namespace {
14namespace chrono = ::std::chrono;
15} // namespace
Parker Schuhe4a70d62017-12-27 20:10:20 -080016
Austin Schuh6b6dfa52019-06-12 20:16:20 -070017// Tests that watcher can receive messages from a sender.
Parker Schuhe4a70d62017-12-27 20:10:20 -080018// Also tests that OnRun() works.
19TEST_P(AbstractEventLoopTest, Basic) {
20 auto loop1 = Make();
Austin Schuh6b6dfa52019-06-12 20:16:20 -070021 auto loop2 = MakePrimary();
22
Alex Perrycb7da4b2019-08-28 19:35:56 -070023 aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test");
Austin Schuh6b6dfa52019-06-12 20:16:20 -070024
25 bool happened = false;
26
27 loop2->OnRun([&]() {
28 happened = true;
29
Alex Perrycb7da4b2019-08-28 19:35:56 -070030 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
31 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
32 builder.add_value(200);
33 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh6b6dfa52019-06-12 20:16:20 -070034 });
35
36 loop2->MakeWatcher("/test", [&](const TestMessage &message) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070037 EXPECT_EQ(message.value(), 200);
Austin Schuh9fe68f72019-08-10 19:32:03 -070038 this->Exit();
Austin Schuh6b6dfa52019-06-12 20:16:20 -070039 });
40
41 EXPECT_FALSE(happened);
42 Run();
43 EXPECT_TRUE(happened);
44}
45
Brian Silverman6b8a3c32020-03-06 11:26:14 -080046// Verifies that a no-arg watcher will not have a data pointer.
47TEST_P(AbstractEventLoopTest, NoArgNoData) {
48 auto loop1 = Make();
49 auto loop2 = MakePrimary();
50
51 aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test");
52
53 bool happened = false;
54
55 loop2->OnRun([&]() {
56 happened = true;
57
58 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
59 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
60 ASSERT_TRUE(msg.Send(builder.Finish()));
61 });
62
63 loop2->MakeNoArgWatcher<TestMessage>("/test", [&]() {
64 EXPECT_GT(loop2->context().size, 0u);
65 EXPECT_EQ(nullptr, loop2->context().data);
66 this->Exit();
67 });
68
69 EXPECT_FALSE(happened);
70 Run();
71 EXPECT_TRUE(happened);
72}
73
Brian Silverman454bc112020-03-05 14:21:25 -080074// Tests that no-arg watcher can receive messages from a sender.
75// Also tests that OnRun() works.
76TEST_P(AbstractEventLoopTest, BasicNoArg) {
77 auto loop1 = Make();
78 auto loop2 = MakePrimary();
79
80 aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test");
81
82 bool happened = false;
83
84 loop2->OnRun([&]() {
85 happened = true;
86
87 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
88 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
89 builder.add_value(200);
90 ASSERT_TRUE(msg.Send(builder.Finish()));
91 });
92
93 aos::Fetcher<TestMessage> fetcher = loop2->MakeFetcher<TestMessage>("/test");
94 loop2->MakeNoArgWatcher<TestMessage>("/test", [&]() {
95 ASSERT_TRUE(fetcher.Fetch());
96 EXPECT_EQ(fetcher->value(), 200);
97 this->Exit();
98 });
99
100 EXPECT_FALSE(happened);
101 Run();
102 EXPECT_TRUE(happened);
103}
104
105// Tests that a watcher can be created with an std::function.
106TEST_P(AbstractEventLoopTest, BasicFunction) {
107 auto loop1 = Make();
108 auto loop2 = MakePrimary();
109
110 aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test");
111
112 bool happened = false;
113
114 loop2->OnRun([&]() {
115 happened = true;
116
117 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
118 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
119 builder.add_value(200);
120 ASSERT_TRUE(msg.Send(builder.Finish()));
121 });
122
123 loop2->MakeWatcher("/test", std::function<void(const TestMessage &)>(
124 [&](const TestMessage &message) {
125 EXPECT_EQ(message.value(), 200);
126 this->Exit();
127 }));
128
129 EXPECT_FALSE(happened);
130 Run();
131 EXPECT_TRUE(happened);
132}
133
Brian Silverman0fc69932020-01-24 21:54:02 -0800134// Tests that watcher can receive messages from two senders.
135// Also tests that OnRun() works.
136TEST_P(AbstractEventLoopTest, BasicTwoSenders) {
137 auto loop1 = Make();
138 auto loop2 = MakePrimary();
139
140 aos::Sender<TestMessage> sender1 = loop1->MakeSender<TestMessage>("/test");
141 aos::Sender<TestMessage> sender2 = loop1->MakeSender<TestMessage>("/test");
142
143 bool happened = false;
144
145 loop2->OnRun([&]() {
146 happened = true;
147
148 {
149 aos::Sender<TestMessage>::Builder msg = sender1.MakeBuilder();
150 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
151 builder.add_value(200);
152 ASSERT_TRUE(msg.Send(builder.Finish()));
153 }
154 {
155 aos::Sender<TestMessage>::Builder msg = sender2.MakeBuilder();
156 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
157 builder.add_value(200);
158 ASSERT_TRUE(msg.Send(builder.Finish()));
159 }
160 });
161
162 int messages_received = 0;
163 loop2->MakeWatcher("/test", [&](const TestMessage &message) {
164 EXPECT_EQ(message.value(), 200);
165 this->Exit();
166 ++messages_received;
167 });
168
169 EXPECT_FALSE(happened);
170 Run();
171 EXPECT_TRUE(happened);
172 EXPECT_EQ(messages_received, 2);
173}
174
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700175// Tests that a fetcher can fetch from a sender.
176// Also tests that OnRun() works.
177TEST_P(AbstractEventLoopTest, FetchWithoutRun) {
178 auto loop1 = Make();
Parker Schuhe4a70d62017-12-27 20:10:20 -0800179 auto loop2 = Make();
Austin Schuh44019f92019-05-19 19:58:27 -0700180 auto loop3 = MakePrimary();
Parker Schuhe4a70d62017-12-27 20:10:20 -0800181
182 auto sender = loop1->MakeSender<TestMessage>("/test");
183
184 auto fetcher = loop2->MakeFetcher<TestMessage>("/test");
185
Austin Schuhbbce72d2019-05-26 15:11:46 -0700186 EXPECT_FALSE(fetcher.Fetch());
Austin Schuh39788ff2019-12-01 18:22:57 -0800187 EXPECT_EQ(fetcher.get(), nullptr);
188
Austin Schuhad154822019-12-27 15:45:13 -0800189 EXPECT_EQ(fetcher.context().monotonic_event_time, monotonic_clock::min_time);
190 EXPECT_EQ(fetcher.context().monotonic_remote_time, monotonic_clock::min_time);
191 EXPECT_EQ(fetcher.context().realtime_event_time, realtime_clock::min_time);
192 EXPECT_EQ(fetcher.context().realtime_remote_time, realtime_clock::min_time);
Austin Schuh39788ff2019-12-01 18:22:57 -0800193 EXPECT_EQ(fetcher.context().queue_index, 0xffffffffu);
194 EXPECT_EQ(fetcher.context().size, 0u);
195 EXPECT_EQ(fetcher.context().data, nullptr);
Austin Schuhbbce72d2019-05-26 15:11:46 -0700196
Alex Perrycb7da4b2019-08-28 19:35:56 -0700197 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
198 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
199 builder.add_value(200);
200 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh3578a2e2019-05-25 18:17:59 -0700201
202 EXPECT_TRUE(fetcher.Fetch());
203 ASSERT_FALSE(fetcher.get() == nullptr);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700204 EXPECT_EQ(fetcher.get()->value(), 200);
Austin Schuh39788ff2019-12-01 18:22:57 -0800205
206 const chrono::milliseconds kEpsilon(100);
207
Austin Schuhad154822019-12-27 15:45:13 -0800208 const aos::monotonic_clock::time_point monotonic_now = loop2->monotonic_now();
209 const aos::realtime_clock::time_point realtime_now = loop2->realtime_now();
210 EXPECT_EQ(fetcher.context().monotonic_event_time,
211 fetcher.context().monotonic_remote_time);
212 EXPECT_EQ(fetcher.context().realtime_event_time,
213 fetcher.context().realtime_remote_time);
214
215 EXPECT_GE(fetcher.context().monotonic_event_time, monotonic_now - kEpsilon);
216 EXPECT_LE(fetcher.context().monotonic_event_time, monotonic_now + kEpsilon);
217 EXPECT_GE(fetcher.context().realtime_event_time, realtime_now - kEpsilon);
218 EXPECT_LE(fetcher.context().realtime_event_time, realtime_now + kEpsilon);
Austin Schuh39788ff2019-12-01 18:22:57 -0800219 EXPECT_EQ(fetcher.context().queue_index, 0x0u);
220 EXPECT_EQ(fetcher.context().size, 20u);
221 EXPECT_NE(fetcher.context().data, nullptr);
Parker Schuhe4a70d62017-12-27 20:10:20 -0800222}
223
Austin Schuh3578a2e2019-05-25 18:17:59 -0700224// Tests that watcher will receive all messages sent if they are sent after
225// initialization and before running.
226TEST_P(AbstractEventLoopTest, DoubleSendAtStartup) {
227 auto loop1 = Make();
228 auto loop2 = MakePrimary();
229
230 auto sender = loop1->MakeSender<TestMessage>("/test");
231
232 ::std::vector<int> values;
233
234 loop2->MakeWatcher("/test", [&](const TestMessage &message) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700235 values.push_back(message.value());
Austin Schuh3578a2e2019-05-25 18:17:59 -0700236 if (values.size() == 2) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700237 this->Exit();
Austin Schuh3578a2e2019-05-25 18:17:59 -0700238 }
239 });
240
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700241 // Before Run, should be ignored.
Austin Schuh3578a2e2019-05-25 18:17:59 -0700242 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700243 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
244 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
245 builder.add_value(199);
246 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh3578a2e2019-05-25 18:17:59 -0700247 }
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700248
249 loop2->OnRun([&]() {
250 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700251 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
252 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
253 builder.add_value(200);
254 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700255 }
256 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700257 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
258 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
259 builder.add_value(201);
260 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700261 }
262 });
Austin Schuh3578a2e2019-05-25 18:17:59 -0700263
264 Run();
265
266 EXPECT_THAT(values, ::testing::ElementsAreArray({200, 201}));
267}
268
269// Tests that watcher will not receive messages sent before the watcher is
270// created.
271TEST_P(AbstractEventLoopTest, DoubleSendAfterStartup) {
272 auto loop1 = Make();
273 auto loop2 = MakePrimary();
274
275 auto sender = loop1->MakeSender<TestMessage>("/test");
276
277 ::std::vector<int> values;
278
279 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700280 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
281 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
282 builder.add_value(200);
283 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh3578a2e2019-05-25 18:17:59 -0700284 }
285 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700286 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
287 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
288 builder.add_value(201);
289 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh3578a2e2019-05-25 18:17:59 -0700290 }
291
292 loop2->MakeWatcher("/test", [&](const TestMessage &message) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700293 values.push_back(message.value());
Austin Schuh3578a2e2019-05-25 18:17:59 -0700294 });
295
296 // Add a timer to actually quit.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700297 auto test_timer = loop2->AddTimer([this]() { this->Exit(); });
Austin Schuh3578a2e2019-05-25 18:17:59 -0700298 loop2->OnRun([&test_timer, &loop2]() {
299 test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100));
300 });
301
302 Run();
303 EXPECT_EQ(0, values.size());
304}
305
Austin Schuhbbce72d2019-05-26 15:11:46 -0700306// Tests that FetchNext gets all the messages sent after it is constructed.
307TEST_P(AbstractEventLoopTest, FetchNext) {
308 auto loop1 = Make();
309 auto loop2 = MakePrimary();
310
311 auto sender = loop1->MakeSender<TestMessage>("/test");
312 auto fetcher = loop2->MakeFetcher<TestMessage>("/test");
313
314 ::std::vector<int> values;
315
316 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700317 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
318 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
319 builder.add_value(200);
320 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700321 }
322 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700323 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
324 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
325 builder.add_value(201);
326 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700327 }
328
329 // Add a timer to actually quit.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700330 auto test_timer = loop2->AddTimer([&fetcher, &values, this]() {
Austin Schuhbbce72d2019-05-26 15:11:46 -0700331 while (fetcher.FetchNext()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700332 values.push_back(fetcher.get()->value());
Austin Schuhbbce72d2019-05-26 15:11:46 -0700333 }
Austin Schuh9fe68f72019-08-10 19:32:03 -0700334 this->Exit();
Austin Schuhbbce72d2019-05-26 15:11:46 -0700335 });
336
337 loop2->OnRun([&test_timer, &loop2]() {
338 test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100));
339 });
340
341 Run();
342 EXPECT_THAT(values, ::testing::ElementsAreArray({200, 201}));
343}
344
345// Tests that FetchNext gets no messages sent before it is constructed.
346TEST_P(AbstractEventLoopTest, FetchNextAfterSend) {
347 auto loop1 = Make();
348 auto loop2 = MakePrimary();
349
350 auto sender = loop1->MakeSender<TestMessage>("/test");
351
352 ::std::vector<int> values;
353
354 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700355 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
356 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
357 builder.add_value(200);
358 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700359 }
360 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700361 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
362 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
363 builder.add_value(201);
364 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700365 }
366
367 auto fetcher = loop2->MakeFetcher<TestMessage>("/test");
368
369 // Add a timer to actually quit.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700370 auto test_timer = loop2->AddTimer([&fetcher, &values, this]() {
Austin Schuhbbce72d2019-05-26 15:11:46 -0700371 while (fetcher.FetchNext()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700372 values.push_back(fetcher.get()->value());
Austin Schuhbbce72d2019-05-26 15:11:46 -0700373 }
Austin Schuh9fe68f72019-08-10 19:32:03 -0700374 this->Exit();
Austin Schuhbbce72d2019-05-26 15:11:46 -0700375 });
376
377 loop2->OnRun([&test_timer, &loop2]() {
378 test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100));
379 });
380
381 Run();
382 EXPECT_THAT(0, values.size());
383}
384
385// Tests that Fetch returns the last message created before the loop was
386// started.
387TEST_P(AbstractEventLoopTest, FetchDataFromBeforeCreation) {
388 auto loop1 = Make();
389 auto loop2 = MakePrimary();
390
391 auto sender = loop1->MakeSender<TestMessage>("/test");
392
393 ::std::vector<int> values;
394
395 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700396 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
397 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
398 builder.add_value(200);
399 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700400 }
401 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700402 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
403 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
404 builder.add_value(201);
405 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700406 }
407
408 auto fetcher = loop2->MakeFetcher<TestMessage>("/test");
409
410 // Add a timer to actually quit.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700411 auto test_timer = loop2->AddTimer([&fetcher, &values, this]() {
Austin Schuhbbce72d2019-05-26 15:11:46 -0700412 if (fetcher.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700413 values.push_back(fetcher.get()->value());
Austin Schuhbbce72d2019-05-26 15:11:46 -0700414 }
415 // Do it again to make sure we don't double fetch.
416 if (fetcher.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700417 values.push_back(fetcher.get()->value());
Austin Schuhbbce72d2019-05-26 15:11:46 -0700418 }
Austin Schuh9fe68f72019-08-10 19:32:03 -0700419 this->Exit();
Austin Schuhbbce72d2019-05-26 15:11:46 -0700420 });
421
422 loop2->OnRun([&test_timer, &loop2]() {
423 test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100));
424 });
425
426 Run();
427 EXPECT_THAT(values, ::testing::ElementsAreArray({201}));
428}
429
430// Tests that Fetch and FetchNext interleave as expected.
431TEST_P(AbstractEventLoopTest, FetchAndFetchNextTogether) {
432 auto loop1 = Make();
433 auto loop2 = MakePrimary();
434
435 auto sender = loop1->MakeSender<TestMessage>("/test");
436
437 ::std::vector<int> values;
438
439 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700440 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
441 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
442 builder.add_value(200);
443 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700444 }
445 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700446 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
447 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
448 builder.add_value(201);
449 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700450 }
451
452 auto fetcher = loop2->MakeFetcher<TestMessage>("/test");
453
454 // Add a timer to actually quit.
Austin Schuh9fe68f72019-08-10 19:32:03 -0700455 auto test_timer = loop2->AddTimer([&fetcher, &values, &sender, this]() {
Austin Schuhbbce72d2019-05-26 15:11:46 -0700456 if (fetcher.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700457 values.push_back(fetcher.get()->value());
Austin Schuhbbce72d2019-05-26 15:11:46 -0700458 }
459
460 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700461 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
462 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
463 builder.add_value(202);
464 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700465 }
466 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700467 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
468 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
469 builder.add_value(203);
470 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700471 }
472 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700473 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
474 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
475 builder.add_value(204);
476 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuhbbce72d2019-05-26 15:11:46 -0700477 }
478
479 if (fetcher.FetchNext()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700480 values.push_back(fetcher.get()->value());
Austin Schuhbbce72d2019-05-26 15:11:46 -0700481 }
482
483 if (fetcher.Fetch()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700484 values.push_back(fetcher.get()->value());
Austin Schuhbbce72d2019-05-26 15:11:46 -0700485 }
486
Austin Schuh9fe68f72019-08-10 19:32:03 -0700487 this->Exit();
Austin Schuhbbce72d2019-05-26 15:11:46 -0700488 });
489
490 loop2->OnRun([&test_timer, &loop2]() {
491 test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100));
492 });
493
494 Run();
495 EXPECT_THAT(values, ::testing::ElementsAreArray({201, 202, 204}));
496}
497
Austin Schuh3115a202019-05-27 21:02:14 -0700498// Tests that FetchNext behaves correctly when we get two messages in the queue
499// but don't consume the first until after the second has been sent.
500TEST_P(AbstractEventLoopTest, FetchNextTest) {
Austin Schuh3115a202019-05-27 21:02:14 -0700501 auto send_loop = Make();
502 auto fetch_loop = Make();
503 auto sender = send_loop->MakeSender<TestMessage>("/test");
504 Fetcher<TestMessage> fetcher = fetch_loop->MakeFetcher<TestMessage>("/test");
505
506 {
Tyler Chatow67ddb032020-01-12 14:30:04 -0800507 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
508 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
509 builder.add_value(100);
510 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh3115a202019-05-27 21:02:14 -0700511 }
512
513 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700514 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
515 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
516 builder.add_value(200);
517 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh3115a202019-05-27 21:02:14 -0700518 }
519
520 ASSERT_TRUE(fetcher.FetchNext());
521 ASSERT_NE(nullptr, fetcher.get());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700522 EXPECT_EQ(100, fetcher.get()->value());
Austin Schuh3115a202019-05-27 21:02:14 -0700523
524 ASSERT_TRUE(fetcher.FetchNext());
525 ASSERT_NE(nullptr, fetcher.get());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700526 EXPECT_EQ(200, fetcher.get()->value());
Austin Schuh3115a202019-05-27 21:02:14 -0700527
528 // When we run off the end of the queue, expect to still have the old message:
529 ASSERT_FALSE(fetcher.FetchNext());
530 ASSERT_NE(nullptr, fetcher.get());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700531 EXPECT_EQ(200, fetcher.get()->value());
Austin Schuh3115a202019-05-27 21:02:14 -0700532}
533
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800534// Verify that making a fetcher and watcher for "/test" succeeds.
535TEST_P(AbstractEventLoopTest, FetcherAndWatcher) {
Parker Schuhe4a70d62017-12-27 20:10:20 -0800536 auto loop = Make();
537 auto fetcher = loop->MakeFetcher<TestMessage>("/test");
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800538 loop->MakeWatcher("/test", [&](const TestMessage &) {});
Parker Schuhe4a70d62017-12-27 20:10:20 -0800539}
540
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800541// Verify that making 2 fetchers for "/test" succeeds.
Parker Schuhe4a70d62017-12-27 20:10:20 -0800542TEST_P(AbstractEventLoopTest, TwoFetcher) {
543 auto loop = Make();
544 auto fetcher = loop->MakeFetcher<TestMessage>("/test");
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800545 auto fetcher2 = loop->MakeFetcher<TestMessage>("/test");
Parker Schuhe4a70d62017-12-27 20:10:20 -0800546}
547
Alex Perrycb7da4b2019-08-28 19:35:56 -0700548// Verify that registering a watcher for an invalid channel name dies.
549TEST_P(AbstractEventLoopDeathTest, InvalidChannelName) {
550 auto loop = Make();
551 EXPECT_DEATH(
552 { loop->MakeWatcher("/test/invalid", [&](const TestMessage &) {}); },
553 "/test/invalid");
Brian Silverman454bc112020-03-05 14:21:25 -0800554 EXPECT_DEATH(
555 { loop->MakeNoArgWatcher<TestMessage>("/test/invalid", [&]() {}); },
556 "/test/invalid");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700557}
558
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800559// Verify that registering a watcher twice for "/test" fails.
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700560TEST_P(AbstractEventLoopDeathTest, TwoWatcher) {
Parker Schuhe4a70d62017-12-27 20:10:20 -0800561 auto loop = Make();
562 loop->MakeWatcher("/test", [&](const TestMessage &) {});
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800563 EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}),
564 "/test");
Brian Silverman454bc112020-03-05 14:21:25 -0800565 EXPECT_DEATH(loop->MakeNoArgWatcher<TestMessage>("/test", [&]() {}), "/test");
566}
567
568// Verify that registering a no-arg watcher twice for "/test" fails.
569TEST_P(AbstractEventLoopDeathTest, TwoNoArgWatcher) {
570 auto loop = Make();
571 loop->MakeNoArgWatcher<TestMessage>("/test", [&]() {});
572 EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}),
573 "/test");
574 EXPECT_DEATH(loop->MakeNoArgWatcher<TestMessage>("/test", [&]() {}), "/test");
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800575}
576
Austin Schuh3115a202019-05-27 21:02:14 -0700577// Verify that SetRuntimeRealtimePriority fails while running.
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700578TEST_P(AbstractEventLoopDeathTest, SetRuntimeRealtimePriority) {
Austin Schuh3115a202019-05-27 21:02:14 -0700579 auto loop = MakePrimary();
580 // Confirm that runtime priority calls work when not realtime.
581 loop->SetRuntimeRealtimePriority(5);
582
583 loop->OnRun([&]() { loop->SetRuntimeRealtimePriority(5); });
584
585 EXPECT_DEATH(Run(), "realtime");
586}
587
Brian Silverman6a54ff32020-04-28 16:41:39 -0700588// Verify that SetRuntimeAffinity fails while running.
589TEST_P(AbstractEventLoopDeathTest, SetRuntimeAffinity) {
590 auto loop = MakePrimary();
591 // Confirm that runtime priority calls work when not running.
592 loop->SetRuntimeAffinity(MakeCpusetFromCpus({0}));
593
594 loop->OnRun([&]() { loop->SetRuntimeAffinity(MakeCpusetFromCpus({1})); });
595
596 EXPECT_DEATH(Run(), "Cannot set affinity while running");
597}
598
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800599// Verify that registering a watcher and a sender for "/test" fails.
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700600TEST_P(AbstractEventLoopDeathTest, WatcherAndSender) {
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800601 auto loop = Make();
602 auto sender = loop->MakeSender<TestMessage>("/test");
603 EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}),
604 "/test");
Parker Schuhe4a70d62017-12-27 20:10:20 -0800605}
606
Austin Schuhe516ab02020-05-06 21:37:04 -0700607// Verify that creating too many senders fails.
608TEST_P(AbstractEventLoopDeathTest, TooManySenders) {
609 auto loop = Make();
610 std::vector<aos::Sender<TestMessage>> senders;
611 for (int i = 0; i < 10; ++i) {
612 senders.emplace_back(loop->MakeSender<TestMessage>("/test"));
613 }
614 EXPECT_DEATH({ loop->MakeSender<TestMessage>("/test"); },
615 "Failed to create sender on \\{ \"name\": \"/test\", \"type\": "
616 "\"aos.TestMessage\" \\}, too many senders.");
617}
618
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700619// Verify that we can't create a sender inside OnRun.
620TEST_P(AbstractEventLoopDeathTest, SenderInOnRun) {
621 auto loop1 = MakePrimary();
622
623 loop1->OnRun(
624 [&]() { auto sender = loop1->MakeSender<TestMessage>("/test2"); });
625
626 EXPECT_DEATH(Run(), "running");
627}
628
629// Verify that we can't create a watcher inside OnRun.
630TEST_P(AbstractEventLoopDeathTest, WatcherInOnRun) {
631 auto loop1 = MakePrimary();
632
633 loop1->OnRun(
634 [&]() { loop1->MakeWatcher("/test", [&](const TestMessage &) {}); });
635
636 EXPECT_DEATH(Run(), "running");
637}
638
Brian Silverman454bc112020-03-05 14:21:25 -0800639// Verify that we can't create a no-arg watcher inside OnRun.
640TEST_P(AbstractEventLoopDeathTest, NoArgWatcherInOnRun) {
641 auto loop1 = MakePrimary();
642
643 loop1->OnRun(
644 [&]() { loop1->MakeNoArgWatcher<TestMessage>("/test", [&]() {}); });
645
646 EXPECT_DEATH(Run(), "running");
647}
648
Parker Schuhe4a70d62017-12-27 20:10:20 -0800649// Verify that Quit() works when there are multiple watchers.
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800650TEST_P(AbstractEventLoopTest, MultipleWatcherQuit) {
651 auto loop1 = Make();
Austin Schuh44019f92019-05-19 19:58:27 -0700652 auto loop2 = MakePrimary();
Parker Schuhe4a70d62017-12-27 20:10:20 -0800653
Austin Schuh3578a2e2019-05-25 18:17:59 -0700654 loop2->MakeWatcher("/test1", [&](const TestMessage &) {});
655 loop2->MakeWatcher("/test2", [&](const TestMessage &message) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700656 EXPECT_EQ(message.value(), 200);
Austin Schuh9fe68f72019-08-10 19:32:03 -0700657 this->Exit();
Austin Schuh3578a2e2019-05-25 18:17:59 -0700658 });
659
Austin Schuh81fc9cc2019-02-02 23:25:47 -0800660 auto sender = loop1->MakeSender<TestMessage>("/test2");
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700661
662 loop2->OnRun([&]() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700663 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
664 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
665 builder.add_value(200);
666 ASSERT_TRUE(msg.Send(builder.Finish()));
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700667 });
Parker Schuhe4a70d62017-12-27 20:10:20 -0800668
Austin Schuh44019f92019-05-19 19:58:27 -0700669 Run();
Parker Schuhe4a70d62017-12-27 20:10:20 -0800670}
671
Neil Balch229001a2018-01-07 18:22:52 -0800672// Verify that timer intervals and duration function properly.
673TEST_P(AbstractEventLoopTest, TimerIntervalAndDuration) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800674 // Force a slower rate so we are guarenteed to have reports for our timer.
675 FLAGS_timing_report_ms = 2000;
676
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800677 const int kCount = 5;
Neil Balch229001a2018-01-07 18:22:52 -0800678
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800679 auto loop = MakePrimary();
Austin Schuh39788ff2019-12-01 18:22:57 -0800680 auto loop2 = Make();
681
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800682 ::std::vector<::aos::monotonic_clock::time_point> times;
683 ::std::vector<::aos::monotonic_clock::time_point> expected_times;
684
Austin Schuh39788ff2019-12-01 18:22:57 -0800685 Fetcher<timing::Report> report_fetcher =
686 loop2->MakeFetcher<timing::Report>("/aos");
687 EXPECT_FALSE(report_fetcher.Fetch());
688
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800689 auto test_timer = loop->AddTimer([this, &times, &expected_times, &loop]() {
690 times.push_back(loop->monotonic_now());
Austin Schuhad154822019-12-27 15:45:13 -0800691 EXPECT_EQ(loop->context().monotonic_remote_time, monotonic_clock::min_time);
692 EXPECT_EQ(loop->context().realtime_event_time, realtime_clock::min_time);
693 EXPECT_EQ(loop->context().realtime_remote_time, realtime_clock::min_time);
Austin Schuh39788ff2019-12-01 18:22:57 -0800694 EXPECT_EQ(loop->context().queue_index, 0xffffffffu);
695 EXPECT_EQ(loop->context().size, 0u);
696 EXPECT_EQ(loop->context().data, nullptr);
697
Austin Schuhad154822019-12-27 15:45:13 -0800698 expected_times.push_back(loop->context().monotonic_event_time);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800699 if (times.size() == kCount) {
700 this->Exit();
701 }
Neil Balch229001a2018-01-07 18:22:52 -0800702 });
Austin Schuh39788ff2019-12-01 18:22:57 -0800703 test_timer->set_name("Test loop");
Neil Balch229001a2018-01-07 18:22:52 -0800704
Austin Schuh39788ff2019-12-01 18:22:57 -0800705 const monotonic_clock::time_point start_time = loop->monotonic_now();
Austin Schuh52d325c2019-06-23 18:59:06 -0700706 // TODO(austin): This should be an error... Should be done in OnRun only.
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800707 test_timer->Setup(start_time + chrono::seconds(1), chrono::seconds(1));
708
Austin Schuh44019f92019-05-19 19:58:27 -0700709 Run();
Neil Balch229001a2018-01-07 18:22:52 -0800710
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800711 // Confirm that we got both the right number of samples, and it's odd.
712 EXPECT_EQ(times.size(), static_cast<size_t>(kCount));
713 EXPECT_EQ(times.size(), expected_times.size());
714 EXPECT_EQ((times.size() % 2), 1);
715
716 // Grab the middle sample.
717 ::aos::monotonic_clock::time_point average_time = times[times.size() / 2];
718
719 // Add up all the delays of all the times.
720 ::aos::monotonic_clock::duration sum = chrono::seconds(0);
721 for (const ::aos::monotonic_clock::time_point time : times) {
722 sum += time - average_time;
723 }
724
725 // Average and add to the middle to find the average time.
726 sum /= times.size();
727 average_time += sum;
728
729 // Compute the offset from the average and the expected average. It
730 // should be pretty close to 0.
731 const ::aos::monotonic_clock::duration remainder =
732 average_time - start_time - chrono::seconds(times.size() / 2 + 1);
733
734 const chrono::milliseconds kEpsilon(100);
735 EXPECT_LT(remainder, +kEpsilon);
736 EXPECT_GT(remainder, -kEpsilon);
737
738 // Make sure that the average duration is close to 1 second.
739 EXPECT_NEAR(chrono::duration_cast<chrono::duration<double>>(times.back() -
740 times.front())
741 .count() /
742 static_cast<double>(times.size() - 1),
743 1.0, 0.1);
744
745 // Confirm that the ideal wakeup times increment correctly.
746 for (size_t i = 1; i < expected_times.size(); ++i) {
747 EXPECT_EQ(expected_times[i], expected_times[i - 1] + chrono::seconds(1));
748 }
749
750 for (size_t i = 0; i < expected_times.size(); ++i) {
751 EXPECT_EQ((expected_times[i] - start_time) % chrono::seconds(1),
752 chrono::seconds(0));
753 }
754
755 EXPECT_LT(expected_times[expected_times.size() / 2], average_time + kEpsilon);
756 EXPECT_GT(expected_times[expected_times.size() / 2], average_time - kEpsilon);
Austin Schuh39788ff2019-12-01 18:22:57 -0800757
758 // And, since we are here, check that the timing report makes sense.
759 // Start by looking for our event loop's timing.
760 FlatbufferDetachedBuffer<timing::Report> report =
761 FlatbufferDetachedBuffer<timing::Report>::Empty();
762 while (report_fetcher.FetchNext()) {
763 if (report_fetcher->name()->string_view() == "primary") {
764 report = CopyFlatBuffer(report_fetcher.get());
765 }
766 }
767
768 // Confirm that we have the right number of reports, and the contents are
769 // sane.
770 VLOG(1) << FlatbufferToJson(report, true);
771
772 EXPECT_EQ(report.message().name()->string_view(), "primary");
773
774 ASSERT_NE(report.message().senders(), nullptr);
Tyler Chatow67ddb032020-01-12 14:30:04 -0800775 EXPECT_EQ(report.message().senders()->size(), 2);
Austin Schuh39788ff2019-12-01 18:22:57 -0800776
777 ASSERT_NE(report.message().timers(), nullptr);
778 EXPECT_EQ(report.message().timers()->size(), 2);
779
780 EXPECT_EQ(report.message().timers()->Get(0)->name()->string_view(),
781 "Test loop");
782 EXPECT_GE(report.message().timers()->Get(0)->count(), 1);
783
784 EXPECT_EQ(report.message().timers()->Get(1)->name()->string_view(),
785 "timing_reports");
786 EXPECT_EQ(report.message().timers()->Get(1)->count(), 1);
787
788 // Make sure there is a single phased loop report with our report in it.
789 ASSERT_EQ(report.message().phased_loops(), nullptr);
Neil Balch229001a2018-01-07 18:22:52 -0800790}
791
792// Verify that we can change a timer's parameters during execution.
793TEST_P(AbstractEventLoopTest, TimerChangeParameters) {
Austin Schuh44019f92019-05-19 19:58:27 -0700794 auto loop = MakePrimary();
Neil Balch229001a2018-01-07 18:22:52 -0800795 ::std::vector<::aos::monotonic_clock::time_point> iteration_list;
796
797 auto test_timer = loop->AddTimer([&iteration_list, &loop]() {
798 iteration_list.push_back(loop->monotonic_now());
799 });
800
801 auto modifier_timer = loop->AddTimer([&loop, &test_timer]() {
802 test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(30));
803 });
804
Neil Balch229001a2018-01-07 18:22:52 -0800805 test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(20));
806 modifier_timer->Setup(loop->monotonic_now() +
807 ::std::chrono::milliseconds(45));
808 EndEventLoop(loop.get(), ::std::chrono::milliseconds(150));
Austin Schuh44019f92019-05-19 19:58:27 -0700809 Run();
Neil Balch229001a2018-01-07 18:22:52 -0800810
811 EXPECT_EQ(iteration_list.size(), 7);
812}
813
814// Verify that we can disable a timer during execution.
815TEST_P(AbstractEventLoopTest, TimerDisable) {
Austin Schuh44019f92019-05-19 19:58:27 -0700816 auto loop = MakePrimary();
Neil Balch229001a2018-01-07 18:22:52 -0800817 ::std::vector<::aos::monotonic_clock::time_point> iteration_list;
818
819 auto test_timer = loop->AddTimer([&iteration_list, &loop]() {
820 iteration_list.push_back(loop->monotonic_now());
821 });
822
Tyler Chatow67ddb032020-01-12 14:30:04 -0800823 auto ender_timer = loop->AddTimer([&test_timer]() { test_timer->Disable(); });
Neil Balch229001a2018-01-07 18:22:52 -0800824
825 test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(20));
Tyler Chatow67ddb032020-01-12 14:30:04 -0800826 ender_timer->Setup(loop->monotonic_now() + ::std::chrono::milliseconds(45));
Neil Balch229001a2018-01-07 18:22:52 -0800827 EndEventLoop(loop.get(), ::std::chrono::milliseconds(150));
Austin Schuh44019f92019-05-19 19:58:27 -0700828 Run();
Neil Balch229001a2018-01-07 18:22:52 -0800829
830 EXPECT_EQ(iteration_list.size(), 3);
831}
Austin Schuh7267c532019-05-19 19:55:53 -0700832
Austin Schuh54cf95f2019-11-29 13:14:18 -0800833// Verifies that the event loop implementations detect when Channel is not a
834// pointer into confguration()
835TEST_P(AbstractEventLoopDeathTest, InvalidChannel) {
836 auto loop = MakePrimary();
837
Tyler Chatow67ddb032020-01-12 14:30:04 -0800838 const Channel *channel = configuration::GetChannel(
839 loop->configuration(), "/test", "aos.TestMessage", "", nullptr);
Austin Schuh54cf95f2019-11-29 13:14:18 -0800840
841 FlatbufferDetachedBuffer<Channel> channel_copy = CopyFlatBuffer(channel);
842
843 EXPECT_DEATH(
844 { loop->MakeRawSender(&channel_copy.message()); },
845 "Channel pointer not found in configuration\\(\\)->channels\\(\\)");
846
847 EXPECT_DEATH(
848 { loop->MakeRawFetcher(&channel_copy.message()); },
849 "Channel pointer not found in configuration\\(\\)->channels\\(\\)");
850
851 EXPECT_DEATH(
852 {
853 loop->MakeRawWatcher(&channel_copy.message(),
854 [](const Context, const void *) {});
855 },
856 "Channel pointer not found in configuration\\(\\)->channels\\(\\)");
857}
858
Brian Silverman454bc112020-03-05 14:21:25 -0800859// Verify that the send time on a message is roughly right when using a watcher.
Austin Schuh7267c532019-05-19 19:55:53 -0700860TEST_P(AbstractEventLoopTest, MessageSendTime) {
Austin Schuh44019f92019-05-19 19:58:27 -0700861 auto loop1 = MakePrimary();
Austin Schuh7267c532019-05-19 19:55:53 -0700862 auto loop2 = Make();
Austin Schuhad154822019-12-27 15:45:13 -0800863 auto sender = loop2->MakeSender<TestMessage>("/test");
Austin Schuh7267c532019-05-19 19:55:53 -0700864 auto fetcher = loop2->MakeFetcher<TestMessage>("/test");
865
866 auto test_timer = loop1->AddTimer([&sender]() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700867 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
868 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
869 builder.add_value(200);
870 ASSERT_TRUE(msg.Send(builder.Finish()));
871 });
872
Austin Schuhad154822019-12-27 15:45:13 -0800873 bool triggered = false;
Brian Silverman454bc112020-03-05 14:21:25 -0800874 loop1->MakeWatcher("/test", [&](const TestMessage &msg) {
Austin Schuhad154822019-12-27 15:45:13 -0800875 // Confirm that the data pointer makes sense from a watcher, and all the
876 // timestamps look right.
877 EXPECT_GT(&msg, loop1->context().data);
878 EXPECT_EQ(loop1->context().monotonic_remote_time,
879 loop1->context().monotonic_event_time);
880 EXPECT_EQ(loop1->context().realtime_remote_time,
881 loop1->context().realtime_event_time);
882
883 const aos::monotonic_clock::time_point monotonic_now =
884 loop1->monotonic_now();
Tyler Chatow67ddb032020-01-12 14:30:04 -0800885 const aos::realtime_clock::time_point realtime_now = loop1->realtime_now();
Austin Schuhad154822019-12-27 15:45:13 -0800886
887 EXPECT_LE(loop1->context().monotonic_event_time, monotonic_now);
888 EXPECT_LE(loop1->context().realtime_event_time, realtime_now);
889 EXPECT_GE(loop1->context().monotonic_event_time + chrono::milliseconds(500),
890 monotonic_now);
891 EXPECT_GE(loop1->context().realtime_event_time + chrono::milliseconds(500),
892 realtime_now);
893
Alex Perrycb7da4b2019-08-28 19:35:56 -0700894 EXPECT_LT(&msg, reinterpret_cast<void *>(
Austin Schuhad154822019-12-27 15:45:13 -0800895 reinterpret_cast<char *>(loop1->context().data) +
896 loop1->context().size));
897 triggered = true;
Austin Schuh7267c532019-05-19 19:55:53 -0700898 });
899
900 test_timer->Setup(loop1->monotonic_now() + ::std::chrono::seconds(1));
901
902 EndEventLoop(loop1.get(), ::std::chrono::seconds(2));
Austin Schuh44019f92019-05-19 19:58:27 -0700903 Run();
Austin Schuh7267c532019-05-19 19:55:53 -0700904
Austin Schuhad154822019-12-27 15:45:13 -0800905 EXPECT_TRUE(triggered);
906
Brian Silverman454bc112020-03-05 14:21:25 -0800907 ASSERT_TRUE(fetcher.Fetch());
908
909 monotonic_clock::duration monotonic_time_offset =
910 fetcher.context().monotonic_event_time -
911 (loop1->monotonic_now() - ::std::chrono::seconds(1));
912 realtime_clock::duration realtime_time_offset =
913 fetcher.context().realtime_event_time -
914 (loop1->realtime_now() - ::std::chrono::seconds(1));
915
916 EXPECT_EQ(fetcher.context().realtime_event_time,
917 fetcher.context().realtime_remote_time);
918 EXPECT_EQ(fetcher.context().monotonic_event_time,
919 fetcher.context().monotonic_remote_time);
920
921 EXPECT_TRUE(monotonic_time_offset > ::std::chrono::milliseconds(-500))
922 << ": Got "
923 << fetcher.context().monotonic_event_time.time_since_epoch().count()
924 << " expected " << loop1->monotonic_now().time_since_epoch().count();
925 // Confirm that the data pointer makes sense.
926 EXPECT_GT(fetcher.get(), fetcher.context().data);
927 EXPECT_LT(fetcher.get(),
928 reinterpret_cast<void *>(
929 reinterpret_cast<char *>(fetcher.context().data) +
930 fetcher.context().size));
931 EXPECT_TRUE(monotonic_time_offset < ::std::chrono::milliseconds(500))
932 << ": Got "
933 << fetcher.context().monotonic_event_time.time_since_epoch().count()
934 << " expected " << loop1->monotonic_now().time_since_epoch().count();
935
936 EXPECT_TRUE(realtime_time_offset > ::std::chrono::milliseconds(-500))
937 << ": Got "
938 << fetcher.context().realtime_event_time.time_since_epoch().count()
939 << " expected " << loop1->realtime_now().time_since_epoch().count();
940 EXPECT_TRUE(realtime_time_offset < ::std::chrono::milliseconds(500))
941 << ": Got "
942 << fetcher.context().realtime_event_time.time_since_epoch().count()
943 << " expected " << loop1->realtime_now().time_since_epoch().count();
944}
945
946// Verify that the send time on a message is roughly right when using a no-arg
947// watcher. To get a message, we need to use a fetcher to actually access the
948// message. This is also the main use case for no-arg fetchers.
949TEST_P(AbstractEventLoopTest, MessageSendTimeNoArg) {
950 auto loop1 = MakePrimary();
951 auto loop2 = Make();
952 auto sender = loop2->MakeSender<TestMessage>("/test");
953 auto fetcher = loop1->MakeFetcher<TestMessage>("/test");
954
955 auto test_timer = loop1->AddTimer([&sender]() {
956 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
957 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
958 builder.add_value(200);
959 ASSERT_TRUE(msg.Send(builder.Finish()));
960 });
961
962 bool triggered = false;
963 loop1->MakeNoArgWatcher<TestMessage>("/test", [&]() {
964 // Confirm that we can indeed use a fetcher on this channel from this
965 // context, and it results in a sane data pointer and timestamps.
966 ASSERT_TRUE(fetcher.Fetch());
967
968 EXPECT_EQ(loop1->context().monotonic_remote_time,
969 loop1->context().monotonic_event_time);
970 EXPECT_EQ(loop1->context().realtime_remote_time,
971 loop1->context().realtime_event_time);
972
973 const aos::monotonic_clock::time_point monotonic_now =
974 loop1->monotonic_now();
975 const aos::realtime_clock::time_point realtime_now = loop1->realtime_now();
976
977 EXPECT_LE(loop1->context().monotonic_event_time, monotonic_now);
978 EXPECT_LE(loop1->context().realtime_event_time, realtime_now);
979 EXPECT_GE(loop1->context().monotonic_event_time + chrono::milliseconds(500),
980 monotonic_now);
981 EXPECT_GE(loop1->context().realtime_event_time + chrono::milliseconds(500),
982 realtime_now);
983
984 triggered = true;
985 });
986
987 test_timer->Setup(loop1->monotonic_now() + ::std::chrono::seconds(1));
988
989 EndEventLoop(loop1.get(), ::std::chrono::seconds(2));
990 Run();
991
992 ASSERT_TRUE(triggered);
Austin Schuh7267c532019-05-19 19:55:53 -0700993
Alex Perrycb7da4b2019-08-28 19:35:56 -0700994 monotonic_clock::duration monotonic_time_offset =
Austin Schuhad154822019-12-27 15:45:13 -0800995 fetcher.context().monotonic_event_time -
Alex Perrycb7da4b2019-08-28 19:35:56 -0700996 (loop1->monotonic_now() - ::std::chrono::seconds(1));
997 realtime_clock::duration realtime_time_offset =
Austin Schuhad154822019-12-27 15:45:13 -0800998 fetcher.context().realtime_event_time -
Alex Perrycb7da4b2019-08-28 19:35:56 -0700999 (loop1->realtime_now() - ::std::chrono::seconds(1));
Austin Schuh7267c532019-05-19 19:55:53 -07001000
Austin Schuhad154822019-12-27 15:45:13 -08001001 EXPECT_EQ(fetcher.context().realtime_event_time,
1002 fetcher.context().realtime_remote_time);
1003 EXPECT_EQ(fetcher.context().monotonic_event_time,
1004 fetcher.context().monotonic_remote_time);
1005
Alex Perrycb7da4b2019-08-28 19:35:56 -07001006 EXPECT_TRUE(monotonic_time_offset > ::std::chrono::milliseconds(-500))
1007 << ": Got "
Austin Schuhad154822019-12-27 15:45:13 -08001008 << fetcher.context().monotonic_event_time.time_since_epoch().count()
Austin Schuh52d325c2019-06-23 18:59:06 -07001009 << " expected " << loop1->monotonic_now().time_since_epoch().count();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001010 // Confirm that the data pointer makes sense.
1011 EXPECT_GT(fetcher.get(), fetcher.context().data);
1012 EXPECT_LT(fetcher.get(),
1013 reinterpret_cast<void *>(
1014 reinterpret_cast<char *>(fetcher.context().data) +
1015 fetcher.context().size));
1016 EXPECT_TRUE(monotonic_time_offset < ::std::chrono::milliseconds(500))
1017 << ": Got "
Austin Schuhad154822019-12-27 15:45:13 -08001018 << fetcher.context().monotonic_event_time.time_since_epoch().count()
Austin Schuh7267c532019-05-19 19:55:53 -07001019 << " expected " << loop1->monotonic_now().time_since_epoch().count();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001020
1021 EXPECT_TRUE(realtime_time_offset > ::std::chrono::milliseconds(-500))
1022 << ": Got "
Austin Schuhad154822019-12-27 15:45:13 -08001023 << fetcher.context().realtime_event_time.time_since_epoch().count()
Alex Perrycb7da4b2019-08-28 19:35:56 -07001024 << " expected " << loop1->realtime_now().time_since_epoch().count();
1025 EXPECT_TRUE(realtime_time_offset < ::std::chrono::milliseconds(500))
1026 << ": Got "
Austin Schuhad154822019-12-27 15:45:13 -08001027 << fetcher.context().realtime_event_time.time_since_epoch().count()
Alex Perrycb7da4b2019-08-28 19:35:56 -07001028 << " expected " << loop1->realtime_now().time_since_epoch().count();
Austin Schuh7267c532019-05-19 19:55:53 -07001029}
1030
Austin Schuh52d325c2019-06-23 18:59:06 -07001031// Tests that a couple phased loops run in a row result in the correct offset
1032// and period.
1033TEST_P(AbstractEventLoopTest, PhasedLoopTest) {
Austin Schuh39788ff2019-12-01 18:22:57 -08001034 // Force a slower rate so we are guarenteed to have reports for our phased
1035 // loop.
1036 FLAGS_timing_report_ms = 2000;
1037
Austin Schuh52d325c2019-06-23 18:59:06 -07001038 const chrono::milliseconds kOffset = chrono::milliseconds(400);
1039 const int kCount = 5;
1040
1041 auto loop1 = MakePrimary();
Austin Schuh39788ff2019-12-01 18:22:57 -08001042 auto loop2 = Make();
1043
1044 Fetcher<timing::Report> report_fetcher =
1045 loop2->MakeFetcher<timing::Report>("/aos");
1046 EXPECT_FALSE(report_fetcher.Fetch());
Austin Schuh52d325c2019-06-23 18:59:06 -07001047
1048 // Collect up a couple of samples.
1049 ::std::vector<::aos::monotonic_clock::time_point> times;
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001050 ::std::vector<::aos::monotonic_clock::time_point> expected_times;
Austin Schuh52d325c2019-06-23 18:59:06 -07001051
1052 // Run kCount iterations.
Austin Schuh39788ff2019-12-01 18:22:57 -08001053 loop1
1054 ->AddPhasedLoop(
1055 [&times, &expected_times, &loop1, this](int count) {
1056 EXPECT_EQ(count, 1);
1057 times.push_back(loop1->monotonic_now());
Austin Schuhad154822019-12-27 15:45:13 -08001058 expected_times.push_back(loop1->context().monotonic_event_time);
Austin Schuh39788ff2019-12-01 18:22:57 -08001059
Austin Schuhad154822019-12-27 15:45:13 -08001060 EXPECT_EQ(loop1->context().monotonic_remote_time,
1061 monotonic_clock::min_time);
1062 EXPECT_EQ(loop1->context().realtime_event_time,
1063 realtime_clock::min_time);
1064 EXPECT_EQ(loop1->context().realtime_remote_time,
Austin Schuh39788ff2019-12-01 18:22:57 -08001065 realtime_clock::min_time);
1066 EXPECT_EQ(loop1->context().queue_index, 0xffffffffu);
1067 EXPECT_EQ(loop1->context().size, 0u);
1068 EXPECT_EQ(loop1->context().data, nullptr);
1069
1070 if (times.size() == kCount) {
1071 LOG(INFO) << "Exiting";
1072 this->Exit();
1073 }
1074 },
1075 chrono::seconds(1), kOffset)
1076 ->set_name("Test loop");
Austin Schuh52d325c2019-06-23 18:59:06 -07001077
1078 // Add a delay to make sure that delay during startup doesn't result in a
1079 // "missed cycle".
1080 SleepFor(chrono::seconds(2));
1081
1082 Run();
1083
1084 // Confirm that we got both the right number of samples, and it's odd.
1085 EXPECT_EQ(times.size(), static_cast<size_t>(kCount));
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001086 EXPECT_EQ(times.size(), expected_times.size());
Austin Schuh52d325c2019-06-23 18:59:06 -07001087 EXPECT_EQ((times.size() % 2), 1);
1088
1089 // Grab the middle sample.
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001090 ::aos::monotonic_clock::time_point average_time = times[times.size() / 2];
Austin Schuh52d325c2019-06-23 18:59:06 -07001091
1092 // Add up all the delays of all the times.
1093 ::aos::monotonic_clock::duration sum = chrono::seconds(0);
1094 for (const ::aos::monotonic_clock::time_point time : times) {
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001095 sum += time - average_time;
Austin Schuh52d325c2019-06-23 18:59:06 -07001096 }
1097
1098 // Average and add to the middle to find the average time.
1099 sum /= times.size();
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001100 average_time += sum;
Austin Schuh52d325c2019-06-23 18:59:06 -07001101
1102 // Compute the offset from the start of the second of the average time. This
1103 // should be pretty close to the offset.
1104 const ::aos::monotonic_clock::duration remainder =
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001105 average_time.time_since_epoch() -
1106 chrono::duration_cast<chrono::seconds>(average_time.time_since_epoch());
Austin Schuh52d325c2019-06-23 18:59:06 -07001107
1108 const chrono::milliseconds kEpsilon(100);
1109 EXPECT_LT(remainder, kOffset + kEpsilon);
1110 EXPECT_GT(remainder, kOffset - kEpsilon);
1111
1112 // Make sure that the average duration is close to 1 second.
1113 EXPECT_NEAR(chrono::duration_cast<chrono::duration<double>>(times.back() -
1114 times.front())
1115 .count() /
1116 static_cast<double>(times.size() - 1),
1117 1.0, 0.1);
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001118
1119 // Confirm that the ideal wakeup times increment correctly.
1120 for (size_t i = 1; i < expected_times.size(); ++i) {
1121 EXPECT_EQ(expected_times[i], expected_times[i - 1] + chrono::seconds(1));
1122 }
1123
1124 for (size_t i = 0; i < expected_times.size(); ++i) {
1125 EXPECT_EQ(expected_times[i].time_since_epoch() % chrono::seconds(1),
1126 kOffset);
1127 }
1128
1129 EXPECT_LT(expected_times[expected_times.size() / 2], average_time + kEpsilon);
1130 EXPECT_GT(expected_times[expected_times.size() / 2], average_time - kEpsilon);
Austin Schuh39788ff2019-12-01 18:22:57 -08001131
1132 // And, since we are here, check that the timing report makes sense.
1133 // Start by looking for our event loop's timing.
1134 FlatbufferDetachedBuffer<timing::Report> report =
1135 FlatbufferDetachedBuffer<timing::Report>::Empty();
1136 while (report_fetcher.FetchNext()) {
1137 if (report_fetcher->name()->string_view() == "primary") {
1138 report = CopyFlatBuffer(report_fetcher.get());
1139 }
1140 }
1141
1142 VLOG(1) << FlatbufferToJson(report, true);
1143
1144 EXPECT_EQ(report.message().name()->string_view(), "primary");
1145
1146 ASSERT_NE(report.message().senders(), nullptr);
Tyler Chatow67ddb032020-01-12 14:30:04 -08001147 EXPECT_EQ(report.message().senders()->size(), 2);
Austin Schuh39788ff2019-12-01 18:22:57 -08001148
1149 ASSERT_NE(report.message().timers(), nullptr);
1150 EXPECT_EQ(report.message().timers()->size(), 1);
1151
1152 // Make sure there is a single phased loop report with our report in it.
1153 ASSERT_NE(report.message().phased_loops(), nullptr);
1154 ASSERT_EQ(report.message().phased_loops()->size(), 1);
1155 EXPECT_EQ(report.message().phased_loops()->Get(0)->name()->string_view(),
1156 "Test loop");
1157 EXPECT_GE(report.message().phased_loops()->Get(0)->count(), 1);
1158}
1159
1160// Tests that senders count correctly in the timing report.
1161TEST_P(AbstractEventLoopTest, SenderTimingReport) {
1162 FLAGS_timing_report_ms = 1000;
1163 auto loop1 = MakePrimary();
1164
1165 auto loop2 = Make("watcher_loop");
1166 loop2->MakeWatcher("/test", [](const TestMessage &) {});
1167
1168 auto loop3 = Make();
1169
1170 Fetcher<timing::Report> report_fetcher =
1171 loop3->MakeFetcher<timing::Report>("/aos");
1172 EXPECT_FALSE(report_fetcher.Fetch());
1173
1174 auto sender = loop1->MakeSender<TestMessage>("/test");
1175
1176 // Add a timer to actually quit.
1177 auto test_timer = loop1->AddTimer([&sender]() {
1178 for (int i = 0; i < 10; ++i) {
1179 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
1180 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
1181 builder.add_value(200 + i);
1182 ASSERT_TRUE(msg.Send(builder.Finish()));
1183 }
1184 });
1185
1186 // Quit after 1 timing report, mid way through the next cycle.
1187 EndEventLoop(loop1.get(), chrono::milliseconds(2500));
1188
1189 loop1->OnRun([&test_timer, &loop1]() {
1190 test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1500));
1191 });
1192
1193 Run();
1194
1195 // And, since we are here, check that the timing report makes sense.
1196 // Start by looking for our event loop's timing.
1197 FlatbufferDetachedBuffer<timing::Report> primary_report =
1198 FlatbufferDetachedBuffer<timing::Report>::Empty();
1199 while (report_fetcher.FetchNext()) {
1200 LOG(INFO) << "Report " << FlatbufferToJson(report_fetcher.get());
1201 if (report_fetcher->name()->string_view() == "primary") {
1202 primary_report = CopyFlatBuffer(report_fetcher.get());
1203 }
1204 }
1205
1206 LOG(INFO) << FlatbufferToJson(primary_report, true);
1207
1208 EXPECT_EQ(primary_report.message().name()->string_view(), "primary");
1209
1210 ASSERT_NE(primary_report.message().senders(), nullptr);
Tyler Chatow67ddb032020-01-12 14:30:04 -08001211 EXPECT_EQ(primary_report.message().senders()->size(), 3);
Austin Schuh39788ff2019-12-01 18:22:57 -08001212
1213 // Confirm that the sender looks sane.
1214 EXPECT_EQ(
1215 loop1->configuration()
1216 ->channels()
1217 ->Get(primary_report.message().senders()->Get(0)->channel_index())
1218 ->name()
1219 ->string_view(),
1220 "/test");
1221 EXPECT_EQ(primary_report.message().senders()->Get(0)->count(), 10);
1222
1223 // Confirm that the timing primary_report sender looks sane.
1224 EXPECT_EQ(
1225 loop1->configuration()
1226 ->channels()
1227 ->Get(primary_report.message().senders()->Get(1)->channel_index())
1228 ->name()
1229 ->string_view(),
1230 "/aos");
1231 EXPECT_EQ(primary_report.message().senders()->Get(1)->count(), 1);
1232
1233 ASSERT_NE(primary_report.message().timers(), nullptr);
1234 EXPECT_EQ(primary_report.message().timers()->size(), 3);
1235
1236 // Make sure there are no phased loops or watchers.
1237 ASSERT_EQ(primary_report.message().phased_loops(), nullptr);
1238 ASSERT_EQ(primary_report.message().watchers(), nullptr);
1239}
1240
1241// Tests that senders count correctly in the timing report.
1242TEST_P(AbstractEventLoopTest, WatcherTimingReport) {
1243 FLAGS_timing_report_ms = 1000;
1244 auto loop1 = MakePrimary();
1245 loop1->MakeWatcher("/test", [](const TestMessage &) {});
1246
1247 auto loop2 = Make("sender_loop");
1248
1249 auto loop3 = Make();
1250
1251 Fetcher<timing::Report> report_fetcher =
1252 loop3->MakeFetcher<timing::Report>("/aos");
1253 EXPECT_FALSE(report_fetcher.Fetch());
1254
1255 auto sender = loop2->MakeSender<TestMessage>("/test");
1256
1257 // Add a timer to actually quit.
1258 auto test_timer = loop1->AddTimer([&sender]() {
1259 for (int i = 0; i < 10; ++i) {
1260 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
1261 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
1262 builder.add_value(200 + i);
1263 ASSERT_TRUE(msg.Send(builder.Finish()));
1264 }
1265 });
1266
1267 // Quit after 1 timing report, mid way through the next cycle.
1268 EndEventLoop(loop1.get(), chrono::milliseconds(2500));
1269
1270 loop1->OnRun([&test_timer, &loop1]() {
1271 test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1500));
1272 });
1273
1274 Run();
1275
1276 // And, since we are here, check that the timing report makes sense.
1277 // Start by looking for our event loop's timing.
1278 FlatbufferDetachedBuffer<timing::Report> primary_report =
1279 FlatbufferDetachedBuffer<timing::Report>::Empty();
1280 while (report_fetcher.FetchNext()) {
1281 LOG(INFO) << "Report " << FlatbufferToJson(report_fetcher.get());
1282 if (report_fetcher->name()->string_view() == "primary") {
1283 primary_report = CopyFlatBuffer(report_fetcher.get());
1284 }
1285 }
1286
1287 // Check the watcher report.
1288 VLOG(1) << FlatbufferToJson(primary_report, true);
1289
1290 EXPECT_EQ(primary_report.message().name()->string_view(), "primary");
1291
1292 // Just the timing report timer.
1293 ASSERT_NE(primary_report.message().timers(), nullptr);
1294 EXPECT_EQ(primary_report.message().timers()->size(), 3);
1295
1296 // No phased loops
1297 ASSERT_EQ(primary_report.message().phased_loops(), nullptr);
1298
1299 ASSERT_NE(primary_report.message().watchers(), nullptr);
1300 ASSERT_EQ(primary_report.message().watchers()->size(), 1);
1301 EXPECT_EQ(primary_report.message().watchers()->Get(0)->count(), 10);
1302}
1303
1304// Tests that fetchers count correctly in the timing report.
1305TEST_P(AbstractEventLoopTest, FetcherTimingReport) {
1306 FLAGS_timing_report_ms = 1000;
1307 auto loop1 = MakePrimary();
1308 auto loop2 = Make("sender_loop");
1309
1310 auto loop3 = Make();
1311
1312 Fetcher<timing::Report> report_fetcher =
1313 loop3->MakeFetcher<timing::Report>("/aos");
1314 EXPECT_FALSE(report_fetcher.Fetch());
1315
1316 auto sender = loop2->MakeSender<TestMessage>("/test");
1317 auto fetcher1 = loop1->MakeFetcher<TestMessage>("/test");
1318 auto fetcher2 = loop1->MakeFetcher<TestMessage>("/test");
1319 fetcher1.Fetch();
1320 fetcher2.Fetch();
1321
1322 // Add a timer to actually quit.
1323 auto test_timer = loop1->AddTimer([&sender]() {
1324 for (int i = 0; i < 10; ++i) {
1325 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
1326 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
1327 builder.add_value(200 + i);
1328 ASSERT_TRUE(msg.Send(builder.Finish()));
1329 }
1330 });
1331
1332 auto test_timer2 = loop1->AddTimer([&fetcher1, &fetcher2]() {
1333 fetcher1.Fetch();
1334 while (fetcher2.FetchNext()) {
1335 }
1336 });
1337
1338 // Quit after 1 timing report, mid way through the next cycle.
1339 EndEventLoop(loop1.get(), chrono::milliseconds(2500));
1340
1341 loop1->OnRun([test_timer, test_timer2, &loop1]() {
1342 test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1400));
1343 test_timer2->Setup(loop1->monotonic_now() + chrono::milliseconds(1600));
1344 });
1345
1346 Run();
1347
1348 // And, since we are here, check that the timing report makes sense.
1349 // Start by looking for our event loop's timing.
1350 FlatbufferDetachedBuffer<timing::Report> primary_report =
1351 FlatbufferDetachedBuffer<timing::Report>::Empty();
1352 while (report_fetcher.FetchNext()) {
1353 if (report_fetcher->name()->string_view() == "primary") {
1354 primary_report = CopyFlatBuffer(report_fetcher.get());
1355 }
1356 }
1357
1358 VLOG(1) << FlatbufferToJson(primary_report, true);
1359
1360 EXPECT_EQ(primary_report.message().name()->string_view(), "primary");
1361
1362 ASSERT_NE(primary_report.message().senders(), nullptr);
Tyler Chatow67ddb032020-01-12 14:30:04 -08001363 EXPECT_EQ(primary_report.message().senders()->size(), 2);
Austin Schuh39788ff2019-12-01 18:22:57 -08001364
1365 ASSERT_NE(primary_report.message().timers(), nullptr);
1366 EXPECT_EQ(primary_report.message().timers()->size(), 4);
1367
1368 // Make sure there are no phased loops or watchers.
1369 ASSERT_EQ(primary_report.message().phased_loops(), nullptr);
1370 ASSERT_EQ(primary_report.message().watchers(), nullptr);
1371
1372 // Now look at the fetchrs.
1373 ASSERT_NE(primary_report.message().fetchers(), nullptr);
1374 ASSERT_EQ(primary_report.message().fetchers()->size(), 2);
1375
1376 EXPECT_EQ(primary_report.message().fetchers()->Get(0)->count(), 1);
1377 EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->average(),
1378 0.1);
Tyler Chatow67ddb032020-01-12 14:30:04 -08001379 EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->min(), 0.1);
1380 EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->max(), 0.1);
Austin Schuh39788ff2019-12-01 18:22:57 -08001381 EXPECT_EQ(primary_report.message()
1382 .fetchers()
1383 ->Get(0)
1384 ->latency()
1385 ->standard_deviation(),
1386 0.0);
1387
1388 EXPECT_EQ(primary_report.message().fetchers()->Get(1)->count(), 10);
Austin Schuh52d325c2019-06-23 18:59:06 -07001389}
1390
Austin Schuh67420a42019-12-21 21:55:04 -08001391// Tests that a raw watcher and raw fetcher can receive messages from a raw
1392// sender without messing up offsets.
1393TEST_P(AbstractEventLoopTest, RawBasic) {
1394 auto loop1 = Make();
1395 auto loop2 = MakePrimary();
1396 auto loop3 = Make();
1397
1398 const std::string kData("971 is the best");
1399
1400 std::unique_ptr<aos::RawSender> sender =
Tyler Chatow67ddb032020-01-12 14:30:04 -08001401 loop1->MakeRawSender(configuration::GetChannel(
1402 loop1->configuration(), "/test", "aos.TestMessage", "", nullptr));
Austin Schuh67420a42019-12-21 21:55:04 -08001403
1404 std::unique_ptr<aos::RawFetcher> fetcher =
Tyler Chatow67ddb032020-01-12 14:30:04 -08001405 loop3->MakeRawFetcher(configuration::GetChannel(
1406 loop3->configuration(), "/test", "aos.TestMessage", "", nullptr));
Austin Schuh67420a42019-12-21 21:55:04 -08001407
1408 loop2->OnRun(
1409 [&]() { EXPECT_TRUE(sender->Send(kData.data(), kData.size())); });
1410
1411 bool happened = false;
1412 loop2->MakeRawWatcher(
Tyler Chatow67ddb032020-01-12 14:30:04 -08001413 configuration::GetChannel(loop2->configuration(), "/test",
1414 "aos.TestMessage", "", nullptr),
Austin Schuh67420a42019-12-21 21:55:04 -08001415 [this, &kData, &fetcher, &happened](const Context &context,
1416 const void *message) {
1417 happened = true;
1418 EXPECT_EQ(std::string_view(kData),
1419 std::string_view(reinterpret_cast<const char *>(message),
1420 context.size));
1421 EXPECT_EQ(std::string_view(kData),
1422 std::string_view(reinterpret_cast<const char *>(context.data),
1423 context.size));
1424
1425 ASSERT_TRUE(fetcher->Fetch());
1426
1427 EXPECT_EQ(std::string_view(kData),
1428 std::string_view(
1429 reinterpret_cast<const char *>(fetcher->context().data),
1430 fetcher->context().size));
1431
1432 this->Exit();
1433 });
1434
1435 EXPECT_FALSE(happened);
1436 Run();
1437 EXPECT_TRUE(happened);
1438}
1439
Austin Schuhad154822019-12-27 15:45:13 -08001440// Tests that a raw watcher and raw fetcher can receive messages from a raw
1441// sender with remote times filled out.
1442TEST_P(AbstractEventLoopTest, RawRemoteTimes) {
1443 auto loop1 = Make();
1444 auto loop2 = MakePrimary();
1445 auto loop3 = Make();
1446
1447 const std::string kData("971 is the best");
1448
1449 const aos::monotonic_clock::time_point monotonic_remote_time =
1450 aos::monotonic_clock::time_point(chrono::seconds(1501));
1451 const aos::realtime_clock::time_point realtime_remote_time =
1452 aos::realtime_clock::time_point(chrono::seconds(3132));
1453
1454 std::unique_ptr<aos::RawSender> sender =
Tyler Chatow67ddb032020-01-12 14:30:04 -08001455 loop1->MakeRawSender(configuration::GetChannel(
1456 loop1->configuration(), "/test", "aos.TestMessage", "", nullptr));
Austin Schuhad154822019-12-27 15:45:13 -08001457
1458 std::unique_ptr<aos::RawFetcher> fetcher =
Tyler Chatow67ddb032020-01-12 14:30:04 -08001459 loop3->MakeRawFetcher(configuration::GetChannel(
1460 loop3->configuration(), "/test", "aos.TestMessage", "", nullptr));
Austin Schuhad154822019-12-27 15:45:13 -08001461
1462 loop2->OnRun([&]() {
1463 EXPECT_TRUE(sender->Send(kData.data(), kData.size(), monotonic_remote_time,
1464 realtime_remote_time));
1465 });
1466
1467 bool happened = false;
1468 loop2->MakeRawWatcher(
Tyler Chatow67ddb032020-01-12 14:30:04 -08001469 configuration::GetChannel(loop2->configuration(), "/test",
1470 "aos.TestMessage", "", nullptr),
Austin Schuhad154822019-12-27 15:45:13 -08001471 [this, monotonic_remote_time, realtime_remote_time, &fetcher, &happened](
1472 const Context &context, const void * /*message*/) {
1473 happened = true;
1474 EXPECT_EQ(monotonic_remote_time, context.monotonic_remote_time);
1475 EXPECT_EQ(realtime_remote_time, context.realtime_remote_time);
1476
1477 ASSERT_TRUE(fetcher->Fetch());
1478 EXPECT_EQ(monotonic_remote_time,
1479 fetcher->context().monotonic_remote_time);
1480 EXPECT_EQ(realtime_remote_time,
1481 fetcher->context().realtime_remote_time);
1482
1483 this->Exit();
1484 });
1485
1486 EXPECT_FALSE(happened);
1487 Run();
1488 EXPECT_TRUE(happened);
1489}
1490
1491// Tests that a raw sender fills out sent data.
1492TEST_P(AbstractEventLoopTest, RawSenderSentData) {
1493 auto loop1 = MakePrimary();
1494
1495 const std::string kData("971 is the best");
1496
1497 std::unique_ptr<aos::RawSender> sender =
Tyler Chatow67ddb032020-01-12 14:30:04 -08001498 loop1->MakeRawSender(configuration::GetChannel(
1499 loop1->configuration(), "/test", "aos.TestMessage", "", nullptr));
Austin Schuhad154822019-12-27 15:45:13 -08001500
Tyler Chatow67ddb032020-01-12 14:30:04 -08001501 const aos::monotonic_clock::time_point monotonic_now = loop1->monotonic_now();
1502 const aos::realtime_clock::time_point realtime_now = loop1->realtime_now();
Austin Schuhad154822019-12-27 15:45:13 -08001503
1504 EXPECT_TRUE(sender->Send(kData.data(), kData.size()));
1505
1506 EXPECT_GE(sender->monotonic_sent_time(), monotonic_now);
1507 EXPECT_LE(sender->monotonic_sent_time(),
1508 monotonic_now + chrono::milliseconds(100));
1509 EXPECT_GE(sender->realtime_sent_time(), realtime_now);
1510 EXPECT_LE(sender->realtime_sent_time(),
1511 realtime_now + chrono::milliseconds(100));
1512 EXPECT_EQ(sender->sent_queue_index(), 0u);
1513
1514 EXPECT_TRUE(sender->Send(kData.data(), kData.size()));
1515
1516 EXPECT_GE(sender->monotonic_sent_time(), monotonic_now);
1517 EXPECT_LE(sender->monotonic_sent_time(),
1518 monotonic_now + chrono::milliseconds(100));
1519 EXPECT_GE(sender->realtime_sent_time(), realtime_now);
1520 EXPECT_LE(sender->realtime_sent_time(),
1521 realtime_now + chrono::milliseconds(100));
1522 EXPECT_EQ(sender->sent_queue_index(), 1u);
1523}
1524
Austin Schuh217a9782019-12-21 23:02:50 -08001525// Tests that not setting up nodes results in no node.
1526TEST_P(AbstractEventLoopTest, NoNode) {
1527 auto loop1 = Make();
1528 auto loop2 = MakePrimary();
1529
1530 EXPECT_EQ(loop1->node(), nullptr);
1531 EXPECT_EQ(loop2->node(), nullptr);
1532}
1533
1534// Tests that setting up nodes results in node being set.
1535TEST_P(AbstractEventLoopTest, Node) {
1536 EnableNodes("me");
1537
1538 auto loop1 = Make();
1539 auto loop2 = MakePrimary();
1540
1541 EXPECT_NE(loop1->node(), nullptr);
1542 EXPECT_NE(loop2->node(), nullptr);
1543}
1544
1545// Tests that watchers work with a node setup.
1546TEST_P(AbstractEventLoopTest, NodeWatcher) {
1547 EnableNodes("me");
1548
1549 auto loop1 = Make();
1550 auto loop2 = Make();
1551 loop1->MakeWatcher("/test", [](const TestMessage &) {});
Tyler Chatow67ddb032020-01-12 14:30:04 -08001552 loop2->MakeRawWatcher(
1553 configuration::GetChannel(configuration(), "/test", "aos.TestMessage", "",
1554 nullptr),
1555 [](const Context &, const void *) {});
Austin Schuh217a9782019-12-21 23:02:50 -08001556}
1557
Brian Silverman454bc112020-03-05 14:21:25 -08001558// Tests that no-arg watchers work with a node setup.
1559TEST_P(AbstractEventLoopTest, NodeNoArgWatcher) {
1560 EnableNodes("me");
1561
1562 auto loop1 = Make();
1563 auto loop2 = Make();
1564 loop1->MakeWatcher("/test", [](const TestMessage &) {});
1565 loop2->MakeRawNoArgWatcher(
1566 configuration::GetChannel(configuration(), "/test", "aos.TestMessage", "",
1567 nullptr),
1568 [](const Context &) {});
1569}
1570
Austin Schuh217a9782019-12-21 23:02:50 -08001571// Tests that fetcher work with a node setup.
1572TEST_P(AbstractEventLoopTest, NodeFetcher) {
1573 EnableNodes("me");
1574 auto loop1 = Make();
1575
1576 auto fetcher = loop1->MakeFetcher<TestMessage>("/test");
Tyler Chatow67ddb032020-01-12 14:30:04 -08001577 auto raw_fetcher = loop1->MakeRawFetcher(configuration::GetChannel(
1578 configuration(), "/test", "aos.TestMessage", "", nullptr));
Austin Schuh217a9782019-12-21 23:02:50 -08001579}
1580
1581// Tests that sender work with a node setup.
1582TEST_P(AbstractEventLoopTest, NodeSender) {
1583 EnableNodes("me");
1584 auto loop1 = Make();
1585
1586 aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test");
1587}
1588
1589// Tests that watchers fail when created on the wrong node.
1590TEST_P(AbstractEventLoopDeathTest, NodeWatcher) {
1591 EnableNodes("them");
1592
1593 auto loop1 = Make();
1594 auto loop2 = Make();
1595 EXPECT_DEATH({ loop1->MakeWatcher("/test", [](const TestMessage &) {}); },
1596 "node");
1597 EXPECT_DEATH(
1598 {
Tyler Chatow67ddb032020-01-12 14:30:04 -08001599 loop2->MakeRawWatcher(
1600 configuration::GetChannel(configuration(), "/test",
1601 "aos.TestMessage", "", nullptr),
1602 [](const Context &, const void *) {});
Austin Schuh217a9782019-12-21 23:02:50 -08001603 },
1604 "node");
Brian Silverman454bc112020-03-05 14:21:25 -08001605 EXPECT_DEATH({ loop1->MakeNoArgWatcher<TestMessage>("/test", []() {}); },
1606 "node");
1607 EXPECT_DEATH(
1608 {
1609 loop2->MakeRawNoArgWatcher(
1610 configuration::GetChannel(configuration(), "/test",
1611 "aos.TestMessage", "", nullptr),
1612 [](const Context &) {});
1613 },
1614 "node");
Austin Schuh217a9782019-12-21 23:02:50 -08001615}
1616
1617// Tests that fetchers fail when created on the wrong node.
1618TEST_P(AbstractEventLoopDeathTest, NodeFetcher) {
1619 EnableNodes("them");
1620 auto loop1 = Make();
1621
1622 EXPECT_DEATH({ auto fetcher = loop1->MakeFetcher<TestMessage>("/test"); },
1623 "node");
1624 EXPECT_DEATH(
1625 {
Tyler Chatow67ddb032020-01-12 14:30:04 -08001626 auto raw_fetcher = loop1->MakeRawFetcher(configuration::GetChannel(
1627 configuration(), "/test", "aos.TestMessage", "", nullptr));
Austin Schuh217a9782019-12-21 23:02:50 -08001628 },
1629 "node");
1630}
1631
1632// Tests that senders fail when created on the wrong node.
1633TEST_P(AbstractEventLoopDeathTest, NodeSender) {
1634 EnableNodes("them");
1635 auto loop1 = Make();
1636
1637 EXPECT_DEATH(
1638 {
1639 aos::Sender<TestMessage> sender =
1640 loop1->MakeSender<TestMessage>("/test");
1641 },
1642 "node");
1643
1644 // Note: Creating raw senders is always supported. Right now, this lets us
1645 // use them to create message_gateway.
1646}
1647
Parker Schuhe4a70d62017-12-27 20:10:20 -08001648} // namespace testing
1649} // namespace aos