Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include "aos/events/event_loop_param_test.h" |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 2 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 3 | #include <chrono> |
| 4 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 5 | #include "aos/events/test_message_generated.h" |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 6 | #include "aos/flatbuffer_merge.h" |
| 7 | #include "glog/logging.h" |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 8 | #include "gmock/gmock.h" |
| 9 | #include "gtest/gtest.h" |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 10 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 11 | namespace aos { |
| 12 | namespace testing { |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 13 | namespace { |
| 14 | namespace chrono = ::std::chrono; |
| 15 | } // namespace |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 16 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 17 | // Tests that watcher can receive messages from a sender. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 18 | // Also tests that OnRun() works. |
| 19 | TEST_P(AbstractEventLoopTest, Basic) { |
| 20 | auto loop1 = Make(); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 21 | auto loop2 = MakePrimary(); |
| 22 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 23 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 24 | |
| 25 | bool happened = false; |
| 26 | |
| 27 | loop2->OnRun([&]() { |
| 28 | happened = true; |
| 29 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 30 | 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 Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 34 | }); |
| 35 | |
| 36 | loop2->MakeWatcher("/test", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 37 | EXPECT_EQ(message.value(), 200); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 38 | this->Exit(); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 39 | }); |
| 40 | |
| 41 | EXPECT_FALSE(happened); |
| 42 | Run(); |
| 43 | EXPECT_TRUE(happened); |
| 44 | } |
| 45 | |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 46 | // Verifies that a no-arg watcher will not have a data pointer. |
| 47 | TEST_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 Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 74 | // Tests that no-arg watcher can receive messages from a sender. |
| 75 | // Also tests that OnRun() works. |
| 76 | TEST_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. |
| 106 | TEST_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 Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 134 | // Tests that watcher can receive messages from two senders. |
| 135 | // Also tests that OnRun() works. |
| 136 | TEST_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 Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 175 | // Tests that a fetcher can fetch from a sender. |
| 176 | // Also tests that OnRun() works. |
| 177 | TEST_P(AbstractEventLoopTest, FetchWithoutRun) { |
| 178 | auto loop1 = Make(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 179 | auto loop2 = Make(); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 180 | auto loop3 = MakePrimary(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 181 | |
| 182 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 183 | |
| 184 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 185 | |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 186 | EXPECT_FALSE(fetcher.Fetch()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 187 | EXPECT_EQ(fetcher.get(), nullptr); |
| 188 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 189 | 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 Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 193 | EXPECT_EQ(fetcher.context().queue_index, 0xffffffffu); |
| 194 | EXPECT_EQ(fetcher.context().size, 0u); |
| 195 | EXPECT_EQ(fetcher.context().data, nullptr); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 196 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 197 | 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 Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 201 | |
| 202 | EXPECT_TRUE(fetcher.Fetch()); |
| 203 | ASSERT_FALSE(fetcher.get() == nullptr); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 204 | EXPECT_EQ(fetcher.get()->value(), 200); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 205 | |
| 206 | const chrono::milliseconds kEpsilon(100); |
| 207 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 208 | 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 Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 219 | EXPECT_EQ(fetcher.context().queue_index, 0x0u); |
| 220 | EXPECT_EQ(fetcher.context().size, 20u); |
| 221 | EXPECT_NE(fetcher.context().data, nullptr); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 224 | // Tests that watcher will receive all messages sent if they are sent after |
| 225 | // initialization and before running. |
| 226 | TEST_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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 235 | values.push_back(message.value()); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 236 | if (values.size() == 2) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 237 | this->Exit(); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 238 | } |
| 239 | }); |
| 240 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 241 | // Before Run, should be ignored. |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 242 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 243 | 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 Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 247 | } |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 248 | |
| 249 | loop2->OnRun([&]() { |
| 250 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 251 | 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 Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 255 | } |
| 256 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 257 | 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 Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 261 | } |
| 262 | }); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 263 | |
| 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. |
| 271 | TEST_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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 280 | 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 Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 284 | } |
| 285 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 286 | 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 Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | loop2->MakeWatcher("/test", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 293 | values.push_back(message.value()); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 294 | }); |
| 295 | |
| 296 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 297 | auto test_timer = loop2->AddTimer([this]() { this->Exit(); }); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 298 | 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 Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 306 | // Tests that FetchNext gets all the messages sent after it is constructed. |
| 307 | TEST_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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 317 | 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 Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 321 | } |
| 322 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 323 | 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 Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 330 | auto test_timer = loop2->AddTimer([&fetcher, &values, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 331 | while (fetcher.FetchNext()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 332 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 333 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 334 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 335 | }); |
| 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. |
| 346 | TEST_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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 355 | 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 Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 359 | } |
| 360 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 361 | 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 Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 368 | |
| 369 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 370 | auto test_timer = loop2->AddTimer([&fetcher, &values, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 371 | while (fetcher.FetchNext()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 372 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 373 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 374 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 375 | }); |
| 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. |
| 387 | TEST_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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 396 | 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 Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 400 | } |
| 401 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 402 | 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 Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 409 | |
| 410 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 411 | auto test_timer = loop2->AddTimer([&fetcher, &values, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 412 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 413 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 414 | } |
| 415 | // Do it again to make sure we don't double fetch. |
| 416 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 417 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 418 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 419 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 420 | }); |
| 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. |
| 431 | TEST_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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 440 | 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 Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 444 | } |
| 445 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 446 | 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 Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 453 | |
| 454 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 455 | auto test_timer = loop2->AddTimer([&fetcher, &values, &sender, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 456 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 457 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 461 | 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 Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 465 | } |
| 466 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 467 | 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 Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 471 | } |
| 472 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 473 | 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 Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | if (fetcher.FetchNext()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 480 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 484 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 487 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 488 | }); |
| 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 Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 498 | // 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. |
| 500 | TEST_P(AbstractEventLoopTest, FetchNextTest) { |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 501 | 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 Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 507 | 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 Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 514 | 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 Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | ASSERT_TRUE(fetcher.FetchNext()); |
| 521 | ASSERT_NE(nullptr, fetcher.get()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 522 | EXPECT_EQ(100, fetcher.get()->value()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 523 | |
| 524 | ASSERT_TRUE(fetcher.FetchNext()); |
| 525 | ASSERT_NE(nullptr, fetcher.get()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 526 | EXPECT_EQ(200, fetcher.get()->value()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 527 | |
| 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 531 | EXPECT_EQ(200, fetcher.get()->value()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 534 | // Verify that making a fetcher and watcher for "/test" succeeds. |
| 535 | TEST_P(AbstractEventLoopTest, FetcherAndWatcher) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 536 | auto loop = Make(); |
| 537 | auto fetcher = loop->MakeFetcher<TestMessage>("/test"); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 538 | loop->MakeWatcher("/test", [&](const TestMessage &) {}); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 539 | } |
| 540 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 541 | // Verify that making 2 fetchers for "/test" succeeds. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 542 | TEST_P(AbstractEventLoopTest, TwoFetcher) { |
| 543 | auto loop = Make(); |
| 544 | auto fetcher = loop->MakeFetcher<TestMessage>("/test"); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 545 | auto fetcher2 = loop->MakeFetcher<TestMessage>("/test"); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 548 | // Verify that registering a watcher for an invalid channel name dies. |
| 549 | TEST_P(AbstractEventLoopDeathTest, InvalidChannelName) { |
| 550 | auto loop = Make(); |
| 551 | EXPECT_DEATH( |
| 552 | { loop->MakeWatcher("/test/invalid", [&](const TestMessage &) {}); }, |
| 553 | "/test/invalid"); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 554 | EXPECT_DEATH( |
| 555 | { loop->MakeNoArgWatcher<TestMessage>("/test/invalid", [&]() {}); }, |
| 556 | "/test/invalid"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 559 | // Verify that registering a watcher twice for "/test" fails. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 560 | TEST_P(AbstractEventLoopDeathTest, TwoWatcher) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 561 | auto loop = Make(); |
| 562 | loop->MakeWatcher("/test", [&](const TestMessage &) {}); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 563 | EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}), |
| 564 | "/test"); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 565 | EXPECT_DEATH(loop->MakeNoArgWatcher<TestMessage>("/test", [&]() {}), "/test"); |
| 566 | } |
| 567 | |
| 568 | // Verify that registering a no-arg watcher twice for "/test" fails. |
| 569 | TEST_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 Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 575 | } |
| 576 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 577 | // Verify that SetRuntimeRealtimePriority fails while running. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 578 | TEST_P(AbstractEventLoopDeathTest, SetRuntimeRealtimePriority) { |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 579 | 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 Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame^] | 588 | // Verify that SetRuntimeAffinity fails while running. |
| 589 | TEST_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 Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 599 | // Verify that registering a watcher and a sender for "/test" fails. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 600 | TEST_P(AbstractEventLoopDeathTest, WatcherAndSender) { |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 601 | auto loop = Make(); |
| 602 | auto sender = loop->MakeSender<TestMessage>("/test"); |
| 603 | EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}), |
| 604 | "/test"); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 605 | } |
| 606 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 607 | // Verify that we can't create a sender inside OnRun. |
| 608 | TEST_P(AbstractEventLoopDeathTest, SenderInOnRun) { |
| 609 | auto loop1 = MakePrimary(); |
| 610 | |
| 611 | loop1->OnRun( |
| 612 | [&]() { auto sender = loop1->MakeSender<TestMessage>("/test2"); }); |
| 613 | |
| 614 | EXPECT_DEATH(Run(), "running"); |
| 615 | } |
| 616 | |
| 617 | // Verify that we can't create a watcher inside OnRun. |
| 618 | TEST_P(AbstractEventLoopDeathTest, WatcherInOnRun) { |
| 619 | auto loop1 = MakePrimary(); |
| 620 | |
| 621 | loop1->OnRun( |
| 622 | [&]() { loop1->MakeWatcher("/test", [&](const TestMessage &) {}); }); |
| 623 | |
| 624 | EXPECT_DEATH(Run(), "running"); |
| 625 | } |
| 626 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 627 | // Verify that we can't create a no-arg watcher inside OnRun. |
| 628 | TEST_P(AbstractEventLoopDeathTest, NoArgWatcherInOnRun) { |
| 629 | auto loop1 = MakePrimary(); |
| 630 | |
| 631 | loop1->OnRun( |
| 632 | [&]() { loop1->MakeNoArgWatcher<TestMessage>("/test", [&]() {}); }); |
| 633 | |
| 634 | EXPECT_DEATH(Run(), "running"); |
| 635 | } |
| 636 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 637 | // Verify that Quit() works when there are multiple watchers. |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 638 | TEST_P(AbstractEventLoopTest, MultipleWatcherQuit) { |
| 639 | auto loop1 = Make(); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 640 | auto loop2 = MakePrimary(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 641 | |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 642 | loop2->MakeWatcher("/test1", [&](const TestMessage &) {}); |
| 643 | loop2->MakeWatcher("/test2", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 644 | EXPECT_EQ(message.value(), 200); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 645 | this->Exit(); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 646 | }); |
| 647 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 648 | auto sender = loop1->MakeSender<TestMessage>("/test2"); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 649 | |
| 650 | loop2->OnRun([&]() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 651 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 652 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 653 | builder.add_value(200); |
| 654 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 655 | }); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 656 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 657 | Run(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 658 | } |
| 659 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 660 | // Verify that timer intervals and duration function properly. |
| 661 | TEST_P(AbstractEventLoopTest, TimerIntervalAndDuration) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 662 | // Force a slower rate so we are guarenteed to have reports for our timer. |
| 663 | FLAGS_timing_report_ms = 2000; |
| 664 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 665 | const int kCount = 5; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 666 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 667 | auto loop = MakePrimary(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 668 | auto loop2 = Make(); |
| 669 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 670 | ::std::vector<::aos::monotonic_clock::time_point> times; |
| 671 | ::std::vector<::aos::monotonic_clock::time_point> expected_times; |
| 672 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 673 | Fetcher<timing::Report> report_fetcher = |
| 674 | loop2->MakeFetcher<timing::Report>("/aos"); |
| 675 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 676 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 677 | auto test_timer = loop->AddTimer([this, ×, &expected_times, &loop]() { |
| 678 | times.push_back(loop->monotonic_now()); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 679 | EXPECT_EQ(loop->context().monotonic_remote_time, monotonic_clock::min_time); |
| 680 | EXPECT_EQ(loop->context().realtime_event_time, realtime_clock::min_time); |
| 681 | EXPECT_EQ(loop->context().realtime_remote_time, realtime_clock::min_time); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 682 | EXPECT_EQ(loop->context().queue_index, 0xffffffffu); |
| 683 | EXPECT_EQ(loop->context().size, 0u); |
| 684 | EXPECT_EQ(loop->context().data, nullptr); |
| 685 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 686 | expected_times.push_back(loop->context().monotonic_event_time); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 687 | if (times.size() == kCount) { |
| 688 | this->Exit(); |
| 689 | } |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 690 | }); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 691 | test_timer->set_name("Test loop"); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 692 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 693 | const monotonic_clock::time_point start_time = loop->monotonic_now(); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 694 | // TODO(austin): This should be an error... Should be done in OnRun only. |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 695 | test_timer->Setup(start_time + chrono::seconds(1), chrono::seconds(1)); |
| 696 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 697 | Run(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 698 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 699 | // Confirm that we got both the right number of samples, and it's odd. |
| 700 | EXPECT_EQ(times.size(), static_cast<size_t>(kCount)); |
| 701 | EXPECT_EQ(times.size(), expected_times.size()); |
| 702 | EXPECT_EQ((times.size() % 2), 1); |
| 703 | |
| 704 | // Grab the middle sample. |
| 705 | ::aos::monotonic_clock::time_point average_time = times[times.size() / 2]; |
| 706 | |
| 707 | // Add up all the delays of all the times. |
| 708 | ::aos::monotonic_clock::duration sum = chrono::seconds(0); |
| 709 | for (const ::aos::monotonic_clock::time_point time : times) { |
| 710 | sum += time - average_time; |
| 711 | } |
| 712 | |
| 713 | // Average and add to the middle to find the average time. |
| 714 | sum /= times.size(); |
| 715 | average_time += sum; |
| 716 | |
| 717 | // Compute the offset from the average and the expected average. It |
| 718 | // should be pretty close to 0. |
| 719 | const ::aos::monotonic_clock::duration remainder = |
| 720 | average_time - start_time - chrono::seconds(times.size() / 2 + 1); |
| 721 | |
| 722 | const chrono::milliseconds kEpsilon(100); |
| 723 | EXPECT_LT(remainder, +kEpsilon); |
| 724 | EXPECT_GT(remainder, -kEpsilon); |
| 725 | |
| 726 | // Make sure that the average duration is close to 1 second. |
| 727 | EXPECT_NEAR(chrono::duration_cast<chrono::duration<double>>(times.back() - |
| 728 | times.front()) |
| 729 | .count() / |
| 730 | static_cast<double>(times.size() - 1), |
| 731 | 1.0, 0.1); |
| 732 | |
| 733 | // Confirm that the ideal wakeup times increment correctly. |
| 734 | for (size_t i = 1; i < expected_times.size(); ++i) { |
| 735 | EXPECT_EQ(expected_times[i], expected_times[i - 1] + chrono::seconds(1)); |
| 736 | } |
| 737 | |
| 738 | for (size_t i = 0; i < expected_times.size(); ++i) { |
| 739 | EXPECT_EQ((expected_times[i] - start_time) % chrono::seconds(1), |
| 740 | chrono::seconds(0)); |
| 741 | } |
| 742 | |
| 743 | EXPECT_LT(expected_times[expected_times.size() / 2], average_time + kEpsilon); |
| 744 | EXPECT_GT(expected_times[expected_times.size() / 2], average_time - kEpsilon); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 745 | |
| 746 | // And, since we are here, check that the timing report makes sense. |
| 747 | // Start by looking for our event loop's timing. |
| 748 | FlatbufferDetachedBuffer<timing::Report> report = |
| 749 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 750 | while (report_fetcher.FetchNext()) { |
| 751 | if (report_fetcher->name()->string_view() == "primary") { |
| 752 | report = CopyFlatBuffer(report_fetcher.get()); |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | // Confirm that we have the right number of reports, and the contents are |
| 757 | // sane. |
| 758 | VLOG(1) << FlatbufferToJson(report, true); |
| 759 | |
| 760 | EXPECT_EQ(report.message().name()->string_view(), "primary"); |
| 761 | |
| 762 | ASSERT_NE(report.message().senders(), nullptr); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 763 | EXPECT_EQ(report.message().senders()->size(), 2); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 764 | |
| 765 | ASSERT_NE(report.message().timers(), nullptr); |
| 766 | EXPECT_EQ(report.message().timers()->size(), 2); |
| 767 | |
| 768 | EXPECT_EQ(report.message().timers()->Get(0)->name()->string_view(), |
| 769 | "Test loop"); |
| 770 | EXPECT_GE(report.message().timers()->Get(0)->count(), 1); |
| 771 | |
| 772 | EXPECT_EQ(report.message().timers()->Get(1)->name()->string_view(), |
| 773 | "timing_reports"); |
| 774 | EXPECT_EQ(report.message().timers()->Get(1)->count(), 1); |
| 775 | |
| 776 | // Make sure there is a single phased loop report with our report in it. |
| 777 | ASSERT_EQ(report.message().phased_loops(), nullptr); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | // Verify that we can change a timer's parameters during execution. |
| 781 | TEST_P(AbstractEventLoopTest, TimerChangeParameters) { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 782 | auto loop = MakePrimary(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 783 | ::std::vector<::aos::monotonic_clock::time_point> iteration_list; |
| 784 | |
| 785 | auto test_timer = loop->AddTimer([&iteration_list, &loop]() { |
| 786 | iteration_list.push_back(loop->monotonic_now()); |
| 787 | }); |
| 788 | |
| 789 | auto modifier_timer = loop->AddTimer([&loop, &test_timer]() { |
| 790 | test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(30)); |
| 791 | }); |
| 792 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 793 | test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(20)); |
| 794 | modifier_timer->Setup(loop->monotonic_now() + |
| 795 | ::std::chrono::milliseconds(45)); |
| 796 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(150)); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 797 | Run(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 798 | |
| 799 | EXPECT_EQ(iteration_list.size(), 7); |
| 800 | } |
| 801 | |
| 802 | // Verify that we can disable a timer during execution. |
| 803 | TEST_P(AbstractEventLoopTest, TimerDisable) { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 804 | auto loop = MakePrimary(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 805 | ::std::vector<::aos::monotonic_clock::time_point> iteration_list; |
| 806 | |
| 807 | auto test_timer = loop->AddTimer([&iteration_list, &loop]() { |
| 808 | iteration_list.push_back(loop->monotonic_now()); |
| 809 | }); |
| 810 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 811 | auto ender_timer = loop->AddTimer([&test_timer]() { test_timer->Disable(); }); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 812 | |
| 813 | test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(20)); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 814 | ender_timer->Setup(loop->monotonic_now() + ::std::chrono::milliseconds(45)); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 815 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(150)); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 816 | Run(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 817 | |
| 818 | EXPECT_EQ(iteration_list.size(), 3); |
| 819 | } |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 820 | |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 821 | // Verifies that the event loop implementations detect when Channel is not a |
| 822 | // pointer into confguration() |
| 823 | TEST_P(AbstractEventLoopDeathTest, InvalidChannel) { |
| 824 | auto loop = MakePrimary(); |
| 825 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 826 | const Channel *channel = configuration::GetChannel( |
| 827 | loop->configuration(), "/test", "aos.TestMessage", "", nullptr); |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 828 | |
| 829 | FlatbufferDetachedBuffer<Channel> channel_copy = CopyFlatBuffer(channel); |
| 830 | |
| 831 | EXPECT_DEATH( |
| 832 | { loop->MakeRawSender(&channel_copy.message()); }, |
| 833 | "Channel pointer not found in configuration\\(\\)->channels\\(\\)"); |
| 834 | |
| 835 | EXPECT_DEATH( |
| 836 | { loop->MakeRawFetcher(&channel_copy.message()); }, |
| 837 | "Channel pointer not found in configuration\\(\\)->channels\\(\\)"); |
| 838 | |
| 839 | EXPECT_DEATH( |
| 840 | { |
| 841 | loop->MakeRawWatcher(&channel_copy.message(), |
| 842 | [](const Context, const void *) {}); |
| 843 | }, |
| 844 | "Channel pointer not found in configuration\\(\\)->channels\\(\\)"); |
| 845 | } |
| 846 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 847 | // Verify that the send time on a message is roughly right when using a watcher. |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 848 | TEST_P(AbstractEventLoopTest, MessageSendTime) { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 849 | auto loop1 = MakePrimary(); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 850 | auto loop2 = Make(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 851 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 852 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 853 | |
| 854 | auto test_timer = loop1->AddTimer([&sender]() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 855 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 856 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 857 | builder.add_value(200); |
| 858 | ASSERT_TRUE(msg.Send(builder.Finish())); |
| 859 | }); |
| 860 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 861 | bool triggered = false; |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 862 | loop1->MakeWatcher("/test", [&](const TestMessage &msg) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 863 | // Confirm that the data pointer makes sense from a watcher, and all the |
| 864 | // timestamps look right. |
| 865 | EXPECT_GT(&msg, loop1->context().data); |
| 866 | EXPECT_EQ(loop1->context().monotonic_remote_time, |
| 867 | loop1->context().monotonic_event_time); |
| 868 | EXPECT_EQ(loop1->context().realtime_remote_time, |
| 869 | loop1->context().realtime_event_time); |
| 870 | |
| 871 | const aos::monotonic_clock::time_point monotonic_now = |
| 872 | loop1->monotonic_now(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 873 | const aos::realtime_clock::time_point realtime_now = loop1->realtime_now(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 874 | |
| 875 | EXPECT_LE(loop1->context().monotonic_event_time, monotonic_now); |
| 876 | EXPECT_LE(loop1->context().realtime_event_time, realtime_now); |
| 877 | EXPECT_GE(loop1->context().monotonic_event_time + chrono::milliseconds(500), |
| 878 | monotonic_now); |
| 879 | EXPECT_GE(loop1->context().realtime_event_time + chrono::milliseconds(500), |
| 880 | realtime_now); |
| 881 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 882 | EXPECT_LT(&msg, reinterpret_cast<void *>( |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 883 | reinterpret_cast<char *>(loop1->context().data) + |
| 884 | loop1->context().size)); |
| 885 | triggered = true; |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 886 | }); |
| 887 | |
| 888 | test_timer->Setup(loop1->monotonic_now() + ::std::chrono::seconds(1)); |
| 889 | |
| 890 | EndEventLoop(loop1.get(), ::std::chrono::seconds(2)); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 891 | Run(); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 892 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 893 | EXPECT_TRUE(triggered); |
| 894 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 895 | ASSERT_TRUE(fetcher.Fetch()); |
| 896 | |
| 897 | monotonic_clock::duration monotonic_time_offset = |
| 898 | fetcher.context().monotonic_event_time - |
| 899 | (loop1->monotonic_now() - ::std::chrono::seconds(1)); |
| 900 | realtime_clock::duration realtime_time_offset = |
| 901 | fetcher.context().realtime_event_time - |
| 902 | (loop1->realtime_now() - ::std::chrono::seconds(1)); |
| 903 | |
| 904 | EXPECT_EQ(fetcher.context().realtime_event_time, |
| 905 | fetcher.context().realtime_remote_time); |
| 906 | EXPECT_EQ(fetcher.context().monotonic_event_time, |
| 907 | fetcher.context().monotonic_remote_time); |
| 908 | |
| 909 | EXPECT_TRUE(monotonic_time_offset > ::std::chrono::milliseconds(-500)) |
| 910 | << ": Got " |
| 911 | << fetcher.context().monotonic_event_time.time_since_epoch().count() |
| 912 | << " expected " << loop1->monotonic_now().time_since_epoch().count(); |
| 913 | // Confirm that the data pointer makes sense. |
| 914 | EXPECT_GT(fetcher.get(), fetcher.context().data); |
| 915 | EXPECT_LT(fetcher.get(), |
| 916 | reinterpret_cast<void *>( |
| 917 | reinterpret_cast<char *>(fetcher.context().data) + |
| 918 | fetcher.context().size)); |
| 919 | EXPECT_TRUE(monotonic_time_offset < ::std::chrono::milliseconds(500)) |
| 920 | << ": Got " |
| 921 | << fetcher.context().monotonic_event_time.time_since_epoch().count() |
| 922 | << " expected " << loop1->monotonic_now().time_since_epoch().count(); |
| 923 | |
| 924 | EXPECT_TRUE(realtime_time_offset > ::std::chrono::milliseconds(-500)) |
| 925 | << ": Got " |
| 926 | << fetcher.context().realtime_event_time.time_since_epoch().count() |
| 927 | << " expected " << loop1->realtime_now().time_since_epoch().count(); |
| 928 | EXPECT_TRUE(realtime_time_offset < ::std::chrono::milliseconds(500)) |
| 929 | << ": Got " |
| 930 | << fetcher.context().realtime_event_time.time_since_epoch().count() |
| 931 | << " expected " << loop1->realtime_now().time_since_epoch().count(); |
| 932 | } |
| 933 | |
| 934 | // Verify that the send time on a message is roughly right when using a no-arg |
| 935 | // watcher. To get a message, we need to use a fetcher to actually access the |
| 936 | // message. This is also the main use case for no-arg fetchers. |
| 937 | TEST_P(AbstractEventLoopTest, MessageSendTimeNoArg) { |
| 938 | auto loop1 = MakePrimary(); |
| 939 | auto loop2 = Make(); |
| 940 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 941 | auto fetcher = loop1->MakeFetcher<TestMessage>("/test"); |
| 942 | |
| 943 | auto test_timer = loop1->AddTimer([&sender]() { |
| 944 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 945 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 946 | builder.add_value(200); |
| 947 | ASSERT_TRUE(msg.Send(builder.Finish())); |
| 948 | }); |
| 949 | |
| 950 | bool triggered = false; |
| 951 | loop1->MakeNoArgWatcher<TestMessage>("/test", [&]() { |
| 952 | // Confirm that we can indeed use a fetcher on this channel from this |
| 953 | // context, and it results in a sane data pointer and timestamps. |
| 954 | ASSERT_TRUE(fetcher.Fetch()); |
| 955 | |
| 956 | EXPECT_EQ(loop1->context().monotonic_remote_time, |
| 957 | loop1->context().monotonic_event_time); |
| 958 | EXPECT_EQ(loop1->context().realtime_remote_time, |
| 959 | loop1->context().realtime_event_time); |
| 960 | |
| 961 | const aos::monotonic_clock::time_point monotonic_now = |
| 962 | loop1->monotonic_now(); |
| 963 | const aos::realtime_clock::time_point realtime_now = loop1->realtime_now(); |
| 964 | |
| 965 | EXPECT_LE(loop1->context().monotonic_event_time, monotonic_now); |
| 966 | EXPECT_LE(loop1->context().realtime_event_time, realtime_now); |
| 967 | EXPECT_GE(loop1->context().monotonic_event_time + chrono::milliseconds(500), |
| 968 | monotonic_now); |
| 969 | EXPECT_GE(loop1->context().realtime_event_time + chrono::milliseconds(500), |
| 970 | realtime_now); |
| 971 | |
| 972 | triggered = true; |
| 973 | }); |
| 974 | |
| 975 | test_timer->Setup(loop1->monotonic_now() + ::std::chrono::seconds(1)); |
| 976 | |
| 977 | EndEventLoop(loop1.get(), ::std::chrono::seconds(2)); |
| 978 | Run(); |
| 979 | |
| 980 | ASSERT_TRUE(triggered); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 981 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 982 | monotonic_clock::duration monotonic_time_offset = |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 983 | fetcher.context().monotonic_event_time - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 984 | (loop1->monotonic_now() - ::std::chrono::seconds(1)); |
| 985 | realtime_clock::duration realtime_time_offset = |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 986 | fetcher.context().realtime_event_time - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 987 | (loop1->realtime_now() - ::std::chrono::seconds(1)); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 988 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 989 | EXPECT_EQ(fetcher.context().realtime_event_time, |
| 990 | fetcher.context().realtime_remote_time); |
| 991 | EXPECT_EQ(fetcher.context().monotonic_event_time, |
| 992 | fetcher.context().monotonic_remote_time); |
| 993 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 994 | EXPECT_TRUE(monotonic_time_offset > ::std::chrono::milliseconds(-500)) |
| 995 | << ": Got " |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 996 | << fetcher.context().monotonic_event_time.time_since_epoch().count() |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 997 | << " expected " << loop1->monotonic_now().time_since_epoch().count(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 998 | // Confirm that the data pointer makes sense. |
| 999 | EXPECT_GT(fetcher.get(), fetcher.context().data); |
| 1000 | EXPECT_LT(fetcher.get(), |
| 1001 | reinterpret_cast<void *>( |
| 1002 | reinterpret_cast<char *>(fetcher.context().data) + |
| 1003 | fetcher.context().size)); |
| 1004 | EXPECT_TRUE(monotonic_time_offset < ::std::chrono::milliseconds(500)) |
| 1005 | << ": Got " |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1006 | << fetcher.context().monotonic_event_time.time_since_epoch().count() |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1007 | << " expected " << loop1->monotonic_now().time_since_epoch().count(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1008 | |
| 1009 | EXPECT_TRUE(realtime_time_offset > ::std::chrono::milliseconds(-500)) |
| 1010 | << ": Got " |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1011 | << fetcher.context().realtime_event_time.time_since_epoch().count() |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1012 | << " expected " << loop1->realtime_now().time_since_epoch().count(); |
| 1013 | EXPECT_TRUE(realtime_time_offset < ::std::chrono::milliseconds(500)) |
| 1014 | << ": Got " |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1015 | << fetcher.context().realtime_event_time.time_since_epoch().count() |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1016 | << " expected " << loop1->realtime_now().time_since_epoch().count(); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1019 | // Tests that a couple phased loops run in a row result in the correct offset |
| 1020 | // and period. |
| 1021 | TEST_P(AbstractEventLoopTest, PhasedLoopTest) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1022 | // Force a slower rate so we are guarenteed to have reports for our phased |
| 1023 | // loop. |
| 1024 | FLAGS_timing_report_ms = 2000; |
| 1025 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1026 | const chrono::milliseconds kOffset = chrono::milliseconds(400); |
| 1027 | const int kCount = 5; |
| 1028 | |
| 1029 | auto loop1 = MakePrimary(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1030 | auto loop2 = Make(); |
| 1031 | |
| 1032 | Fetcher<timing::Report> report_fetcher = |
| 1033 | loop2->MakeFetcher<timing::Report>("/aos"); |
| 1034 | EXPECT_FALSE(report_fetcher.Fetch()); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1035 | |
| 1036 | // Collect up a couple of samples. |
| 1037 | ::std::vector<::aos::monotonic_clock::time_point> times; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1038 | ::std::vector<::aos::monotonic_clock::time_point> expected_times; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1039 | |
| 1040 | // Run kCount iterations. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1041 | loop1 |
| 1042 | ->AddPhasedLoop( |
| 1043 | [×, &expected_times, &loop1, this](int count) { |
| 1044 | EXPECT_EQ(count, 1); |
| 1045 | times.push_back(loop1->monotonic_now()); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1046 | expected_times.push_back(loop1->context().monotonic_event_time); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1047 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1048 | EXPECT_EQ(loop1->context().monotonic_remote_time, |
| 1049 | monotonic_clock::min_time); |
| 1050 | EXPECT_EQ(loop1->context().realtime_event_time, |
| 1051 | realtime_clock::min_time); |
| 1052 | EXPECT_EQ(loop1->context().realtime_remote_time, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1053 | realtime_clock::min_time); |
| 1054 | EXPECT_EQ(loop1->context().queue_index, 0xffffffffu); |
| 1055 | EXPECT_EQ(loop1->context().size, 0u); |
| 1056 | EXPECT_EQ(loop1->context().data, nullptr); |
| 1057 | |
| 1058 | if (times.size() == kCount) { |
| 1059 | LOG(INFO) << "Exiting"; |
| 1060 | this->Exit(); |
| 1061 | } |
| 1062 | }, |
| 1063 | chrono::seconds(1), kOffset) |
| 1064 | ->set_name("Test loop"); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1065 | |
| 1066 | // Add a delay to make sure that delay during startup doesn't result in a |
| 1067 | // "missed cycle". |
| 1068 | SleepFor(chrono::seconds(2)); |
| 1069 | |
| 1070 | Run(); |
| 1071 | |
| 1072 | // Confirm that we got both the right number of samples, and it's odd. |
| 1073 | EXPECT_EQ(times.size(), static_cast<size_t>(kCount)); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1074 | EXPECT_EQ(times.size(), expected_times.size()); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1075 | EXPECT_EQ((times.size() % 2), 1); |
| 1076 | |
| 1077 | // Grab the middle sample. |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1078 | ::aos::monotonic_clock::time_point average_time = times[times.size() / 2]; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1079 | |
| 1080 | // Add up all the delays of all the times. |
| 1081 | ::aos::monotonic_clock::duration sum = chrono::seconds(0); |
| 1082 | for (const ::aos::monotonic_clock::time_point time : times) { |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1083 | sum += time - average_time; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | // Average and add to the middle to find the average time. |
| 1087 | sum /= times.size(); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1088 | average_time += sum; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1089 | |
| 1090 | // Compute the offset from the start of the second of the average time. This |
| 1091 | // should be pretty close to the offset. |
| 1092 | const ::aos::monotonic_clock::duration remainder = |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1093 | average_time.time_since_epoch() - |
| 1094 | chrono::duration_cast<chrono::seconds>(average_time.time_since_epoch()); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1095 | |
| 1096 | const chrono::milliseconds kEpsilon(100); |
| 1097 | EXPECT_LT(remainder, kOffset + kEpsilon); |
| 1098 | EXPECT_GT(remainder, kOffset - kEpsilon); |
| 1099 | |
| 1100 | // Make sure that the average duration is close to 1 second. |
| 1101 | EXPECT_NEAR(chrono::duration_cast<chrono::duration<double>>(times.back() - |
| 1102 | times.front()) |
| 1103 | .count() / |
| 1104 | static_cast<double>(times.size() - 1), |
| 1105 | 1.0, 0.1); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1106 | |
| 1107 | // Confirm that the ideal wakeup times increment correctly. |
| 1108 | for (size_t i = 1; i < expected_times.size(); ++i) { |
| 1109 | EXPECT_EQ(expected_times[i], expected_times[i - 1] + chrono::seconds(1)); |
| 1110 | } |
| 1111 | |
| 1112 | for (size_t i = 0; i < expected_times.size(); ++i) { |
| 1113 | EXPECT_EQ(expected_times[i].time_since_epoch() % chrono::seconds(1), |
| 1114 | kOffset); |
| 1115 | } |
| 1116 | |
| 1117 | EXPECT_LT(expected_times[expected_times.size() / 2], average_time + kEpsilon); |
| 1118 | EXPECT_GT(expected_times[expected_times.size() / 2], average_time - kEpsilon); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1119 | |
| 1120 | // And, since we are here, check that the timing report makes sense. |
| 1121 | // Start by looking for our event loop's timing. |
| 1122 | FlatbufferDetachedBuffer<timing::Report> report = |
| 1123 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 1124 | while (report_fetcher.FetchNext()) { |
| 1125 | if (report_fetcher->name()->string_view() == "primary") { |
| 1126 | report = CopyFlatBuffer(report_fetcher.get()); |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | VLOG(1) << FlatbufferToJson(report, true); |
| 1131 | |
| 1132 | EXPECT_EQ(report.message().name()->string_view(), "primary"); |
| 1133 | |
| 1134 | ASSERT_NE(report.message().senders(), nullptr); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1135 | EXPECT_EQ(report.message().senders()->size(), 2); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1136 | |
| 1137 | ASSERT_NE(report.message().timers(), nullptr); |
| 1138 | EXPECT_EQ(report.message().timers()->size(), 1); |
| 1139 | |
| 1140 | // Make sure there is a single phased loop report with our report in it. |
| 1141 | ASSERT_NE(report.message().phased_loops(), nullptr); |
| 1142 | ASSERT_EQ(report.message().phased_loops()->size(), 1); |
| 1143 | EXPECT_EQ(report.message().phased_loops()->Get(0)->name()->string_view(), |
| 1144 | "Test loop"); |
| 1145 | EXPECT_GE(report.message().phased_loops()->Get(0)->count(), 1); |
| 1146 | } |
| 1147 | |
| 1148 | // Tests that senders count correctly in the timing report. |
| 1149 | TEST_P(AbstractEventLoopTest, SenderTimingReport) { |
| 1150 | FLAGS_timing_report_ms = 1000; |
| 1151 | auto loop1 = MakePrimary(); |
| 1152 | |
| 1153 | auto loop2 = Make("watcher_loop"); |
| 1154 | loop2->MakeWatcher("/test", [](const TestMessage &) {}); |
| 1155 | |
| 1156 | auto loop3 = Make(); |
| 1157 | |
| 1158 | Fetcher<timing::Report> report_fetcher = |
| 1159 | loop3->MakeFetcher<timing::Report>("/aos"); |
| 1160 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 1161 | |
| 1162 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 1163 | |
| 1164 | // Add a timer to actually quit. |
| 1165 | auto test_timer = loop1->AddTimer([&sender]() { |
| 1166 | for (int i = 0; i < 10; ++i) { |
| 1167 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 1168 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 1169 | builder.add_value(200 + i); |
| 1170 | ASSERT_TRUE(msg.Send(builder.Finish())); |
| 1171 | } |
| 1172 | }); |
| 1173 | |
| 1174 | // Quit after 1 timing report, mid way through the next cycle. |
| 1175 | EndEventLoop(loop1.get(), chrono::milliseconds(2500)); |
| 1176 | |
| 1177 | loop1->OnRun([&test_timer, &loop1]() { |
| 1178 | test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1500)); |
| 1179 | }); |
| 1180 | |
| 1181 | Run(); |
| 1182 | |
| 1183 | // And, since we are here, check that the timing report makes sense. |
| 1184 | // Start by looking for our event loop's timing. |
| 1185 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 1186 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 1187 | while (report_fetcher.FetchNext()) { |
| 1188 | LOG(INFO) << "Report " << FlatbufferToJson(report_fetcher.get()); |
| 1189 | if (report_fetcher->name()->string_view() == "primary") { |
| 1190 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | LOG(INFO) << FlatbufferToJson(primary_report, true); |
| 1195 | |
| 1196 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 1197 | |
| 1198 | ASSERT_NE(primary_report.message().senders(), nullptr); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1199 | EXPECT_EQ(primary_report.message().senders()->size(), 3); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1200 | |
| 1201 | // Confirm that the sender looks sane. |
| 1202 | EXPECT_EQ( |
| 1203 | loop1->configuration() |
| 1204 | ->channels() |
| 1205 | ->Get(primary_report.message().senders()->Get(0)->channel_index()) |
| 1206 | ->name() |
| 1207 | ->string_view(), |
| 1208 | "/test"); |
| 1209 | EXPECT_EQ(primary_report.message().senders()->Get(0)->count(), 10); |
| 1210 | |
| 1211 | // Confirm that the timing primary_report sender looks sane. |
| 1212 | EXPECT_EQ( |
| 1213 | loop1->configuration() |
| 1214 | ->channels() |
| 1215 | ->Get(primary_report.message().senders()->Get(1)->channel_index()) |
| 1216 | ->name() |
| 1217 | ->string_view(), |
| 1218 | "/aos"); |
| 1219 | EXPECT_EQ(primary_report.message().senders()->Get(1)->count(), 1); |
| 1220 | |
| 1221 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 1222 | EXPECT_EQ(primary_report.message().timers()->size(), 3); |
| 1223 | |
| 1224 | // Make sure there are no phased loops or watchers. |
| 1225 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 1226 | ASSERT_EQ(primary_report.message().watchers(), nullptr); |
| 1227 | } |
| 1228 | |
| 1229 | // Tests that senders count correctly in the timing report. |
| 1230 | TEST_P(AbstractEventLoopTest, WatcherTimingReport) { |
| 1231 | FLAGS_timing_report_ms = 1000; |
| 1232 | auto loop1 = MakePrimary(); |
| 1233 | loop1->MakeWatcher("/test", [](const TestMessage &) {}); |
| 1234 | |
| 1235 | auto loop2 = Make("sender_loop"); |
| 1236 | |
| 1237 | auto loop3 = Make(); |
| 1238 | |
| 1239 | Fetcher<timing::Report> report_fetcher = |
| 1240 | loop3->MakeFetcher<timing::Report>("/aos"); |
| 1241 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 1242 | |
| 1243 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 1244 | |
| 1245 | // Add a timer to actually quit. |
| 1246 | auto test_timer = loop1->AddTimer([&sender]() { |
| 1247 | for (int i = 0; i < 10; ++i) { |
| 1248 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 1249 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 1250 | builder.add_value(200 + i); |
| 1251 | ASSERT_TRUE(msg.Send(builder.Finish())); |
| 1252 | } |
| 1253 | }); |
| 1254 | |
| 1255 | // Quit after 1 timing report, mid way through the next cycle. |
| 1256 | EndEventLoop(loop1.get(), chrono::milliseconds(2500)); |
| 1257 | |
| 1258 | loop1->OnRun([&test_timer, &loop1]() { |
| 1259 | test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1500)); |
| 1260 | }); |
| 1261 | |
| 1262 | Run(); |
| 1263 | |
| 1264 | // And, since we are here, check that the timing report makes sense. |
| 1265 | // Start by looking for our event loop's timing. |
| 1266 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 1267 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 1268 | while (report_fetcher.FetchNext()) { |
| 1269 | LOG(INFO) << "Report " << FlatbufferToJson(report_fetcher.get()); |
| 1270 | if (report_fetcher->name()->string_view() == "primary") { |
| 1271 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | // Check the watcher report. |
| 1276 | VLOG(1) << FlatbufferToJson(primary_report, true); |
| 1277 | |
| 1278 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 1279 | |
| 1280 | // Just the timing report timer. |
| 1281 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 1282 | EXPECT_EQ(primary_report.message().timers()->size(), 3); |
| 1283 | |
| 1284 | // No phased loops |
| 1285 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 1286 | |
| 1287 | ASSERT_NE(primary_report.message().watchers(), nullptr); |
| 1288 | ASSERT_EQ(primary_report.message().watchers()->size(), 1); |
| 1289 | EXPECT_EQ(primary_report.message().watchers()->Get(0)->count(), 10); |
| 1290 | } |
| 1291 | |
| 1292 | // Tests that fetchers count correctly in the timing report. |
| 1293 | TEST_P(AbstractEventLoopTest, FetcherTimingReport) { |
| 1294 | FLAGS_timing_report_ms = 1000; |
| 1295 | auto loop1 = MakePrimary(); |
| 1296 | auto loop2 = Make("sender_loop"); |
| 1297 | |
| 1298 | auto loop3 = Make(); |
| 1299 | |
| 1300 | Fetcher<timing::Report> report_fetcher = |
| 1301 | loop3->MakeFetcher<timing::Report>("/aos"); |
| 1302 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 1303 | |
| 1304 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 1305 | auto fetcher1 = loop1->MakeFetcher<TestMessage>("/test"); |
| 1306 | auto fetcher2 = loop1->MakeFetcher<TestMessage>("/test"); |
| 1307 | fetcher1.Fetch(); |
| 1308 | fetcher2.Fetch(); |
| 1309 | |
| 1310 | // Add a timer to actually quit. |
| 1311 | auto test_timer = loop1->AddTimer([&sender]() { |
| 1312 | for (int i = 0; i < 10; ++i) { |
| 1313 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 1314 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 1315 | builder.add_value(200 + i); |
| 1316 | ASSERT_TRUE(msg.Send(builder.Finish())); |
| 1317 | } |
| 1318 | }); |
| 1319 | |
| 1320 | auto test_timer2 = loop1->AddTimer([&fetcher1, &fetcher2]() { |
| 1321 | fetcher1.Fetch(); |
| 1322 | while (fetcher2.FetchNext()) { |
| 1323 | } |
| 1324 | }); |
| 1325 | |
| 1326 | // Quit after 1 timing report, mid way through the next cycle. |
| 1327 | EndEventLoop(loop1.get(), chrono::milliseconds(2500)); |
| 1328 | |
| 1329 | loop1->OnRun([test_timer, test_timer2, &loop1]() { |
| 1330 | test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1400)); |
| 1331 | test_timer2->Setup(loop1->monotonic_now() + chrono::milliseconds(1600)); |
| 1332 | }); |
| 1333 | |
| 1334 | Run(); |
| 1335 | |
| 1336 | // And, since we are here, check that the timing report makes sense. |
| 1337 | // Start by looking for our event loop's timing. |
| 1338 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 1339 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 1340 | while (report_fetcher.FetchNext()) { |
| 1341 | if (report_fetcher->name()->string_view() == "primary") { |
| 1342 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | VLOG(1) << FlatbufferToJson(primary_report, true); |
| 1347 | |
| 1348 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 1349 | |
| 1350 | ASSERT_NE(primary_report.message().senders(), nullptr); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1351 | EXPECT_EQ(primary_report.message().senders()->size(), 2); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1352 | |
| 1353 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 1354 | EXPECT_EQ(primary_report.message().timers()->size(), 4); |
| 1355 | |
| 1356 | // Make sure there are no phased loops or watchers. |
| 1357 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 1358 | ASSERT_EQ(primary_report.message().watchers(), nullptr); |
| 1359 | |
| 1360 | // Now look at the fetchrs. |
| 1361 | ASSERT_NE(primary_report.message().fetchers(), nullptr); |
| 1362 | ASSERT_EQ(primary_report.message().fetchers()->size(), 2); |
| 1363 | |
| 1364 | EXPECT_EQ(primary_report.message().fetchers()->Get(0)->count(), 1); |
| 1365 | EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->average(), |
| 1366 | 0.1); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1367 | EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->min(), 0.1); |
| 1368 | EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->max(), 0.1); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1369 | EXPECT_EQ(primary_report.message() |
| 1370 | .fetchers() |
| 1371 | ->Get(0) |
| 1372 | ->latency() |
| 1373 | ->standard_deviation(), |
| 1374 | 0.0); |
| 1375 | |
| 1376 | EXPECT_EQ(primary_report.message().fetchers()->Get(1)->count(), 10); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 1379 | // Tests that a raw watcher and raw fetcher can receive messages from a raw |
| 1380 | // sender without messing up offsets. |
| 1381 | TEST_P(AbstractEventLoopTest, RawBasic) { |
| 1382 | auto loop1 = Make(); |
| 1383 | auto loop2 = MakePrimary(); |
| 1384 | auto loop3 = Make(); |
| 1385 | |
| 1386 | const std::string kData("971 is the best"); |
| 1387 | |
| 1388 | std::unique_ptr<aos::RawSender> sender = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1389 | loop1->MakeRawSender(configuration::GetChannel( |
| 1390 | loop1->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 1391 | |
| 1392 | std::unique_ptr<aos::RawFetcher> fetcher = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1393 | loop3->MakeRawFetcher(configuration::GetChannel( |
| 1394 | loop3->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 1395 | |
| 1396 | loop2->OnRun( |
| 1397 | [&]() { EXPECT_TRUE(sender->Send(kData.data(), kData.size())); }); |
| 1398 | |
| 1399 | bool happened = false; |
| 1400 | loop2->MakeRawWatcher( |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1401 | configuration::GetChannel(loop2->configuration(), "/test", |
| 1402 | "aos.TestMessage", "", nullptr), |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 1403 | [this, &kData, &fetcher, &happened](const Context &context, |
| 1404 | const void *message) { |
| 1405 | happened = true; |
| 1406 | EXPECT_EQ(std::string_view(kData), |
| 1407 | std::string_view(reinterpret_cast<const char *>(message), |
| 1408 | context.size)); |
| 1409 | EXPECT_EQ(std::string_view(kData), |
| 1410 | std::string_view(reinterpret_cast<const char *>(context.data), |
| 1411 | context.size)); |
| 1412 | |
| 1413 | ASSERT_TRUE(fetcher->Fetch()); |
| 1414 | |
| 1415 | EXPECT_EQ(std::string_view(kData), |
| 1416 | std::string_view( |
| 1417 | reinterpret_cast<const char *>(fetcher->context().data), |
| 1418 | fetcher->context().size)); |
| 1419 | |
| 1420 | this->Exit(); |
| 1421 | }); |
| 1422 | |
| 1423 | EXPECT_FALSE(happened); |
| 1424 | Run(); |
| 1425 | EXPECT_TRUE(happened); |
| 1426 | } |
| 1427 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1428 | // Tests that a raw watcher and raw fetcher can receive messages from a raw |
| 1429 | // sender with remote times filled out. |
| 1430 | TEST_P(AbstractEventLoopTest, RawRemoteTimes) { |
| 1431 | auto loop1 = Make(); |
| 1432 | auto loop2 = MakePrimary(); |
| 1433 | auto loop3 = Make(); |
| 1434 | |
| 1435 | const std::string kData("971 is the best"); |
| 1436 | |
| 1437 | const aos::monotonic_clock::time_point monotonic_remote_time = |
| 1438 | aos::monotonic_clock::time_point(chrono::seconds(1501)); |
| 1439 | const aos::realtime_clock::time_point realtime_remote_time = |
| 1440 | aos::realtime_clock::time_point(chrono::seconds(3132)); |
| 1441 | |
| 1442 | std::unique_ptr<aos::RawSender> sender = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1443 | loop1->MakeRawSender(configuration::GetChannel( |
| 1444 | loop1->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1445 | |
| 1446 | std::unique_ptr<aos::RawFetcher> fetcher = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1447 | loop3->MakeRawFetcher(configuration::GetChannel( |
| 1448 | loop3->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1449 | |
| 1450 | loop2->OnRun([&]() { |
| 1451 | EXPECT_TRUE(sender->Send(kData.data(), kData.size(), monotonic_remote_time, |
| 1452 | realtime_remote_time)); |
| 1453 | }); |
| 1454 | |
| 1455 | bool happened = false; |
| 1456 | loop2->MakeRawWatcher( |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1457 | configuration::GetChannel(loop2->configuration(), "/test", |
| 1458 | "aos.TestMessage", "", nullptr), |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1459 | [this, monotonic_remote_time, realtime_remote_time, &fetcher, &happened]( |
| 1460 | const Context &context, const void * /*message*/) { |
| 1461 | happened = true; |
| 1462 | EXPECT_EQ(monotonic_remote_time, context.monotonic_remote_time); |
| 1463 | EXPECT_EQ(realtime_remote_time, context.realtime_remote_time); |
| 1464 | |
| 1465 | ASSERT_TRUE(fetcher->Fetch()); |
| 1466 | EXPECT_EQ(monotonic_remote_time, |
| 1467 | fetcher->context().monotonic_remote_time); |
| 1468 | EXPECT_EQ(realtime_remote_time, |
| 1469 | fetcher->context().realtime_remote_time); |
| 1470 | |
| 1471 | this->Exit(); |
| 1472 | }); |
| 1473 | |
| 1474 | EXPECT_FALSE(happened); |
| 1475 | Run(); |
| 1476 | EXPECT_TRUE(happened); |
| 1477 | } |
| 1478 | |
| 1479 | // Tests that a raw sender fills out sent data. |
| 1480 | TEST_P(AbstractEventLoopTest, RawSenderSentData) { |
| 1481 | auto loop1 = MakePrimary(); |
| 1482 | |
| 1483 | const std::string kData("971 is the best"); |
| 1484 | |
| 1485 | std::unique_ptr<aos::RawSender> sender = |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1486 | loop1->MakeRawSender(configuration::GetChannel( |
| 1487 | loop1->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1488 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1489 | const aos::monotonic_clock::time_point monotonic_now = loop1->monotonic_now(); |
| 1490 | const aos::realtime_clock::time_point realtime_now = loop1->realtime_now(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1491 | |
| 1492 | EXPECT_TRUE(sender->Send(kData.data(), kData.size())); |
| 1493 | |
| 1494 | EXPECT_GE(sender->monotonic_sent_time(), monotonic_now); |
| 1495 | EXPECT_LE(sender->monotonic_sent_time(), |
| 1496 | monotonic_now + chrono::milliseconds(100)); |
| 1497 | EXPECT_GE(sender->realtime_sent_time(), realtime_now); |
| 1498 | EXPECT_LE(sender->realtime_sent_time(), |
| 1499 | realtime_now + chrono::milliseconds(100)); |
| 1500 | EXPECT_EQ(sender->sent_queue_index(), 0u); |
| 1501 | |
| 1502 | EXPECT_TRUE(sender->Send(kData.data(), kData.size())); |
| 1503 | |
| 1504 | EXPECT_GE(sender->monotonic_sent_time(), monotonic_now); |
| 1505 | EXPECT_LE(sender->monotonic_sent_time(), |
| 1506 | monotonic_now + chrono::milliseconds(100)); |
| 1507 | EXPECT_GE(sender->realtime_sent_time(), realtime_now); |
| 1508 | EXPECT_LE(sender->realtime_sent_time(), |
| 1509 | realtime_now + chrono::milliseconds(100)); |
| 1510 | EXPECT_EQ(sender->sent_queue_index(), 1u); |
| 1511 | } |
| 1512 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1513 | // Tests that not setting up nodes results in no node. |
| 1514 | TEST_P(AbstractEventLoopTest, NoNode) { |
| 1515 | auto loop1 = Make(); |
| 1516 | auto loop2 = MakePrimary(); |
| 1517 | |
| 1518 | EXPECT_EQ(loop1->node(), nullptr); |
| 1519 | EXPECT_EQ(loop2->node(), nullptr); |
| 1520 | } |
| 1521 | |
| 1522 | // Tests that setting up nodes results in node being set. |
| 1523 | TEST_P(AbstractEventLoopTest, Node) { |
| 1524 | EnableNodes("me"); |
| 1525 | |
| 1526 | auto loop1 = Make(); |
| 1527 | auto loop2 = MakePrimary(); |
| 1528 | |
| 1529 | EXPECT_NE(loop1->node(), nullptr); |
| 1530 | EXPECT_NE(loop2->node(), nullptr); |
| 1531 | } |
| 1532 | |
| 1533 | // Tests that watchers work with a node setup. |
| 1534 | TEST_P(AbstractEventLoopTest, NodeWatcher) { |
| 1535 | EnableNodes("me"); |
| 1536 | |
| 1537 | auto loop1 = Make(); |
| 1538 | auto loop2 = Make(); |
| 1539 | loop1->MakeWatcher("/test", [](const TestMessage &) {}); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1540 | loop2->MakeRawWatcher( |
| 1541 | configuration::GetChannel(configuration(), "/test", "aos.TestMessage", "", |
| 1542 | nullptr), |
| 1543 | [](const Context &, const void *) {}); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1544 | } |
| 1545 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1546 | // Tests that no-arg watchers work with a node setup. |
| 1547 | TEST_P(AbstractEventLoopTest, NodeNoArgWatcher) { |
| 1548 | EnableNodes("me"); |
| 1549 | |
| 1550 | auto loop1 = Make(); |
| 1551 | auto loop2 = Make(); |
| 1552 | loop1->MakeWatcher("/test", [](const TestMessage &) {}); |
| 1553 | loop2->MakeRawNoArgWatcher( |
| 1554 | configuration::GetChannel(configuration(), "/test", "aos.TestMessage", "", |
| 1555 | nullptr), |
| 1556 | [](const Context &) {}); |
| 1557 | } |
| 1558 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1559 | // Tests that fetcher work with a node setup. |
| 1560 | TEST_P(AbstractEventLoopTest, NodeFetcher) { |
| 1561 | EnableNodes("me"); |
| 1562 | auto loop1 = Make(); |
| 1563 | |
| 1564 | auto fetcher = loop1->MakeFetcher<TestMessage>("/test"); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1565 | auto raw_fetcher = loop1->MakeRawFetcher(configuration::GetChannel( |
| 1566 | configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | // Tests that sender work with a node setup. |
| 1570 | TEST_P(AbstractEventLoopTest, NodeSender) { |
| 1571 | EnableNodes("me"); |
| 1572 | auto loop1 = Make(); |
| 1573 | |
| 1574 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
| 1575 | } |
| 1576 | |
| 1577 | // Tests that watchers fail when created on the wrong node. |
| 1578 | TEST_P(AbstractEventLoopDeathTest, NodeWatcher) { |
| 1579 | EnableNodes("them"); |
| 1580 | |
| 1581 | auto loop1 = Make(); |
| 1582 | auto loop2 = Make(); |
| 1583 | EXPECT_DEATH({ loop1->MakeWatcher("/test", [](const TestMessage &) {}); }, |
| 1584 | "node"); |
| 1585 | EXPECT_DEATH( |
| 1586 | { |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1587 | loop2->MakeRawWatcher( |
| 1588 | configuration::GetChannel(configuration(), "/test", |
| 1589 | "aos.TestMessage", "", nullptr), |
| 1590 | [](const Context &, const void *) {}); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1591 | }, |
| 1592 | "node"); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 1593 | EXPECT_DEATH({ loop1->MakeNoArgWatcher<TestMessage>("/test", []() {}); }, |
| 1594 | "node"); |
| 1595 | EXPECT_DEATH( |
| 1596 | { |
| 1597 | loop2->MakeRawNoArgWatcher( |
| 1598 | configuration::GetChannel(configuration(), "/test", |
| 1599 | "aos.TestMessage", "", nullptr), |
| 1600 | [](const Context &) {}); |
| 1601 | }, |
| 1602 | "node"); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | // Tests that fetchers fail when created on the wrong node. |
| 1606 | TEST_P(AbstractEventLoopDeathTest, NodeFetcher) { |
| 1607 | EnableNodes("them"); |
| 1608 | auto loop1 = Make(); |
| 1609 | |
| 1610 | EXPECT_DEATH({ auto fetcher = loop1->MakeFetcher<TestMessage>("/test"); }, |
| 1611 | "node"); |
| 1612 | EXPECT_DEATH( |
| 1613 | { |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1614 | auto raw_fetcher = loop1->MakeRawFetcher(configuration::GetChannel( |
| 1615 | configuration(), "/test", "aos.TestMessage", "", nullptr)); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1616 | }, |
| 1617 | "node"); |
| 1618 | } |
| 1619 | |
| 1620 | // Tests that senders fail when created on the wrong node. |
| 1621 | TEST_P(AbstractEventLoopDeathTest, NodeSender) { |
| 1622 | EnableNodes("them"); |
| 1623 | auto loop1 = Make(); |
| 1624 | |
| 1625 | EXPECT_DEATH( |
| 1626 | { |
| 1627 | aos::Sender<TestMessage> sender = |
| 1628 | loop1->MakeSender<TestMessage>("/test"); |
| 1629 | }, |
| 1630 | "node"); |
| 1631 | |
| 1632 | // Note: Creating raw senders is always supported. Right now, this lets us |
| 1633 | // use them to create message_gateway. |
| 1634 | } |
| 1635 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 1636 | } // namespace testing |
| 1637 | } // namespace aos |