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