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 "glog/logging.h" |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 6 | #include "gmock/gmock.h" |
| 7 | #include "gtest/gtest.h" |
| 8 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 9 | #include "aos/events/test_message_generated.h" |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 10 | #include "aos/flatbuffer_merge.h" |
| 11 | #include "glog/logging.h" |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 12 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 13 | namespace aos { |
| 14 | namespace testing { |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 15 | namespace { |
| 16 | namespace chrono = ::std::chrono; |
| 17 | } // namespace |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 18 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 19 | // Tests that watcher can receive messages from a sender. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 20 | // Also tests that OnRun() works. |
| 21 | TEST_P(AbstractEventLoopTest, Basic) { |
| 22 | auto loop1 = Make(); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 23 | auto loop2 = MakePrimary(); |
| 24 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | aos::Sender<TestMessage> sender = loop1->MakeSender<TestMessage>("/test"); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 26 | |
| 27 | bool happened = false; |
| 28 | |
| 29 | loop2->OnRun([&]() { |
| 30 | happened = true; |
| 31 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 32 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 33 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 34 | builder.add_value(200); |
| 35 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 36 | }); |
| 37 | |
| 38 | loop2->MakeWatcher("/test", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 39 | EXPECT_EQ(message.value(), 200); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 40 | this->Exit(); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 41 | }); |
| 42 | |
| 43 | EXPECT_FALSE(happened); |
| 44 | Run(); |
| 45 | EXPECT_TRUE(happened); |
| 46 | } |
| 47 | |
| 48 | // Tests that a fetcher can fetch from a sender. |
| 49 | // Also tests that OnRun() works. |
| 50 | TEST_P(AbstractEventLoopTest, FetchWithoutRun) { |
| 51 | auto loop1 = Make(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 52 | auto loop2 = Make(); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 53 | auto loop3 = MakePrimary(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 54 | |
| 55 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 56 | |
| 57 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 58 | |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 59 | EXPECT_FALSE(fetcher.Fetch()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 60 | EXPECT_EQ(fetcher.get(), nullptr); |
| 61 | |
| 62 | EXPECT_EQ(fetcher.context().monotonic_sent_time, monotonic_clock::min_time); |
| 63 | EXPECT_EQ(fetcher.context().realtime_sent_time, realtime_clock::min_time); |
| 64 | EXPECT_EQ(fetcher.context().queue_index, 0xffffffffu); |
| 65 | EXPECT_EQ(fetcher.context().size, 0u); |
| 66 | EXPECT_EQ(fetcher.context().data, nullptr); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 67 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 68 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 69 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 70 | builder.add_value(200); |
| 71 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 72 | |
| 73 | EXPECT_TRUE(fetcher.Fetch()); |
| 74 | ASSERT_FALSE(fetcher.get() == nullptr); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 75 | EXPECT_EQ(fetcher.get()->value(), 200); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 76 | |
| 77 | const chrono::milliseconds kEpsilon(100); |
| 78 | |
| 79 | EXPECT_GE(fetcher.context().monotonic_sent_time, |
| 80 | loop2->monotonic_now() - kEpsilon); |
| 81 | EXPECT_LE(fetcher.context().monotonic_sent_time, |
| 82 | loop2->monotonic_now() + kEpsilon); |
| 83 | EXPECT_GE(fetcher.context().realtime_sent_time, |
| 84 | loop2->realtime_now() - kEpsilon); |
| 85 | EXPECT_LE(fetcher.context().realtime_sent_time, |
| 86 | loop2->realtime_now() + kEpsilon); |
| 87 | EXPECT_EQ(fetcher.context().queue_index, 0x0u); |
| 88 | EXPECT_EQ(fetcher.context().size, 20u); |
| 89 | EXPECT_NE(fetcher.context().data, nullptr); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 92 | // Tests that watcher will receive all messages sent if they are sent after |
| 93 | // initialization and before running. |
| 94 | TEST_P(AbstractEventLoopTest, DoubleSendAtStartup) { |
| 95 | auto loop1 = Make(); |
| 96 | auto loop2 = MakePrimary(); |
| 97 | |
| 98 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 99 | |
| 100 | ::std::vector<int> values; |
| 101 | |
| 102 | loop2->MakeWatcher("/test", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 103 | values.push_back(message.value()); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 104 | if (values.size() == 2) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 105 | this->Exit(); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 106 | } |
| 107 | }); |
| 108 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 109 | // Before Run, should be ignored. |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 110 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 111 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 112 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 113 | builder.add_value(199); |
| 114 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 115 | } |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 116 | |
| 117 | loop2->OnRun([&]() { |
| 118 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 119 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 120 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 121 | builder.add_value(200); |
| 122 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 123 | } |
| 124 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 125 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 126 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 127 | builder.add_value(201); |
| 128 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 129 | } |
| 130 | }); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 131 | |
| 132 | Run(); |
| 133 | |
| 134 | EXPECT_THAT(values, ::testing::ElementsAreArray({200, 201})); |
| 135 | } |
| 136 | |
| 137 | // Tests that watcher will not receive messages sent before the watcher is |
| 138 | // created. |
| 139 | TEST_P(AbstractEventLoopTest, DoubleSendAfterStartup) { |
| 140 | auto loop1 = Make(); |
| 141 | auto loop2 = MakePrimary(); |
| 142 | |
| 143 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 144 | |
| 145 | ::std::vector<int> values; |
| 146 | |
| 147 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 148 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 149 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 150 | builder.add_value(200); |
| 151 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 152 | } |
| 153 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 154 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 155 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 156 | builder.add_value(201); |
| 157 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | loop2->MakeWatcher("/test", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 161 | values.push_back(message.value()); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 162 | }); |
| 163 | |
| 164 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 165 | auto test_timer = loop2->AddTimer([this]() { this->Exit(); }); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 166 | loop2->OnRun([&test_timer, &loop2]() { |
| 167 | test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 168 | }); |
| 169 | |
| 170 | Run(); |
| 171 | EXPECT_EQ(0, values.size()); |
| 172 | } |
| 173 | |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 174 | // Tests that FetchNext gets all the messages sent after it is constructed. |
| 175 | TEST_P(AbstractEventLoopTest, FetchNext) { |
| 176 | auto loop1 = Make(); |
| 177 | auto loop2 = MakePrimary(); |
| 178 | |
| 179 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 180 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 181 | |
| 182 | ::std::vector<int> values; |
| 183 | |
| 184 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 185 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 186 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 187 | builder.add_value(200); |
| 188 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 189 | } |
| 190 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 191 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 192 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 193 | builder.add_value(201); |
| 194 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 198 | auto test_timer = loop2->AddTimer([&fetcher, &values, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 199 | while (fetcher.FetchNext()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 200 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 201 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 202 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 203 | }); |
| 204 | |
| 205 | loop2->OnRun([&test_timer, &loop2]() { |
| 206 | test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 207 | }); |
| 208 | |
| 209 | Run(); |
| 210 | EXPECT_THAT(values, ::testing::ElementsAreArray({200, 201})); |
| 211 | } |
| 212 | |
| 213 | // Tests that FetchNext gets no messages sent before it is constructed. |
| 214 | TEST_P(AbstractEventLoopTest, FetchNextAfterSend) { |
| 215 | auto loop1 = Make(); |
| 216 | auto loop2 = MakePrimary(); |
| 217 | |
| 218 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 219 | |
| 220 | ::std::vector<int> values; |
| 221 | |
| 222 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 223 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 224 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 225 | builder.add_value(200); |
| 226 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 227 | } |
| 228 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 229 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 230 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 231 | builder.add_value(201); |
| 232 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 236 | |
| 237 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 238 | auto test_timer = loop2->AddTimer([&fetcher, &values, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 239 | while (fetcher.FetchNext()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 240 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 241 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 242 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 243 | }); |
| 244 | |
| 245 | loop2->OnRun([&test_timer, &loop2]() { |
| 246 | test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 247 | }); |
| 248 | |
| 249 | Run(); |
| 250 | EXPECT_THAT(0, values.size()); |
| 251 | } |
| 252 | |
| 253 | // Tests that Fetch returns the last message created before the loop was |
| 254 | // started. |
| 255 | TEST_P(AbstractEventLoopTest, FetchDataFromBeforeCreation) { |
| 256 | auto loop1 = Make(); |
| 257 | auto loop2 = MakePrimary(); |
| 258 | |
| 259 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 260 | |
| 261 | ::std::vector<int> values; |
| 262 | |
| 263 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 264 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 265 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 266 | builder.add_value(200); |
| 267 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 268 | } |
| 269 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 270 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 271 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 272 | builder.add_value(201); |
| 273 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 277 | |
| 278 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 279 | auto test_timer = loop2->AddTimer([&fetcher, &values, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 280 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 281 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 282 | } |
| 283 | // Do it again to make sure we don't double fetch. |
| 284 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 285 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 286 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 287 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 288 | }); |
| 289 | |
| 290 | loop2->OnRun([&test_timer, &loop2]() { |
| 291 | test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 292 | }); |
| 293 | |
| 294 | Run(); |
| 295 | EXPECT_THAT(values, ::testing::ElementsAreArray({201})); |
| 296 | } |
| 297 | |
| 298 | // Tests that Fetch and FetchNext interleave as expected. |
| 299 | TEST_P(AbstractEventLoopTest, FetchAndFetchNextTogether) { |
| 300 | auto loop1 = Make(); |
| 301 | auto loop2 = MakePrimary(); |
| 302 | |
| 303 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 304 | |
| 305 | ::std::vector<int> values; |
| 306 | |
| 307 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 308 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 309 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 310 | builder.add_value(200); |
| 311 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 312 | } |
| 313 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 314 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 315 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 316 | builder.add_value(201); |
| 317 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 321 | |
| 322 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 323 | auto test_timer = loop2->AddTimer([&fetcher, &values, &sender, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 324 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 325 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 329 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 330 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 331 | builder.add_value(202); |
| 332 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 333 | } |
| 334 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 335 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 336 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 337 | builder.add_value(203); |
| 338 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 339 | } |
| 340 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 341 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 342 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 343 | builder.add_value(204); |
| 344 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | if (fetcher.FetchNext()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 348 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 352 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 355 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 356 | }); |
| 357 | |
| 358 | loop2->OnRun([&test_timer, &loop2]() { |
| 359 | test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 360 | }); |
| 361 | |
| 362 | Run(); |
| 363 | EXPECT_THAT(values, ::testing::ElementsAreArray({201, 202, 204})); |
| 364 | } |
| 365 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 366 | |
| 367 | // Tests that FetchNext behaves correctly when we get two messages in the queue |
| 368 | // but don't consume the first until after the second has been sent. |
| 369 | TEST_P(AbstractEventLoopTest, FetchNextTest) { |
| 370 | |
| 371 | auto send_loop = Make(); |
| 372 | auto fetch_loop = Make(); |
| 373 | auto sender = send_loop->MakeSender<TestMessage>("/test"); |
| 374 | Fetcher<TestMessage> fetcher = fetch_loop->MakeFetcher<TestMessage>("/test"); |
| 375 | |
| 376 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 377 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 378 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 379 | builder.add_value(100); |
| 380 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 384 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 385 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 386 | builder.add_value(200); |
| 387 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | ASSERT_TRUE(fetcher.FetchNext()); |
| 391 | ASSERT_NE(nullptr, fetcher.get()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 392 | EXPECT_EQ(100, fetcher.get()->value()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 393 | |
| 394 | ASSERT_TRUE(fetcher.FetchNext()); |
| 395 | ASSERT_NE(nullptr, fetcher.get()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 396 | EXPECT_EQ(200, fetcher.get()->value()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 397 | |
| 398 | // When we run off the end of the queue, expect to still have the old message: |
| 399 | ASSERT_FALSE(fetcher.FetchNext()); |
| 400 | ASSERT_NE(nullptr, fetcher.get()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 401 | EXPECT_EQ(200, fetcher.get()->value()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 404 | // Verify that making a fetcher and watcher for "/test" succeeds. |
| 405 | TEST_P(AbstractEventLoopTest, FetcherAndWatcher) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 406 | auto loop = Make(); |
| 407 | auto fetcher = loop->MakeFetcher<TestMessage>("/test"); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 408 | loop->MakeWatcher("/test", [&](const TestMessage &) {}); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 409 | } |
| 410 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 411 | // Verify that making 2 fetchers for "/test" succeeds. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 412 | TEST_P(AbstractEventLoopTest, TwoFetcher) { |
| 413 | auto loop = Make(); |
| 414 | auto fetcher = loop->MakeFetcher<TestMessage>("/test"); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 415 | auto fetcher2 = loop->MakeFetcher<TestMessage>("/test"); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 418 | // Verify that registering a watcher for an invalid channel name dies. |
| 419 | TEST_P(AbstractEventLoopDeathTest, InvalidChannelName) { |
| 420 | auto loop = Make(); |
| 421 | EXPECT_DEATH( |
| 422 | { loop->MakeWatcher("/test/invalid", [&](const TestMessage &) {}); }, |
| 423 | "/test/invalid"); |
| 424 | } |
| 425 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 426 | // Verify that registering a watcher twice for "/test" fails. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 427 | TEST_P(AbstractEventLoopDeathTest, TwoWatcher) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 428 | auto loop = Make(); |
| 429 | loop->MakeWatcher("/test", [&](const TestMessage &) {}); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 430 | EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}), |
| 431 | "/test"); |
| 432 | } |
| 433 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 434 | // Verify that SetRuntimeRealtimePriority fails while running. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 435 | TEST_P(AbstractEventLoopDeathTest, SetRuntimeRealtimePriority) { |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 436 | auto loop = MakePrimary(); |
| 437 | // Confirm that runtime priority calls work when not realtime. |
| 438 | loop->SetRuntimeRealtimePriority(5); |
| 439 | |
| 440 | loop->OnRun([&]() { loop->SetRuntimeRealtimePriority(5); }); |
| 441 | |
| 442 | EXPECT_DEATH(Run(), "realtime"); |
| 443 | } |
| 444 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 445 | // Verify that registering a watcher and a sender for "/test" fails. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 446 | TEST_P(AbstractEventLoopDeathTest, WatcherAndSender) { |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 447 | auto loop = Make(); |
| 448 | auto sender = loop->MakeSender<TestMessage>("/test"); |
| 449 | EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}), |
| 450 | "/test"); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 453 | // Verify that we can't create a sender inside OnRun. |
| 454 | TEST_P(AbstractEventLoopDeathTest, SenderInOnRun) { |
| 455 | auto loop1 = MakePrimary(); |
| 456 | |
| 457 | loop1->OnRun( |
| 458 | [&]() { auto sender = loop1->MakeSender<TestMessage>("/test2"); }); |
| 459 | |
| 460 | EXPECT_DEATH(Run(), "running"); |
| 461 | } |
| 462 | |
| 463 | // Verify that we can't create a watcher inside OnRun. |
| 464 | TEST_P(AbstractEventLoopDeathTest, WatcherInOnRun) { |
| 465 | auto loop1 = MakePrimary(); |
| 466 | |
| 467 | loop1->OnRun( |
| 468 | [&]() { loop1->MakeWatcher("/test", [&](const TestMessage &) {}); }); |
| 469 | |
| 470 | EXPECT_DEATH(Run(), "running"); |
| 471 | } |
| 472 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 473 | // Verify that Quit() works when there are multiple watchers. |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 474 | TEST_P(AbstractEventLoopTest, MultipleWatcherQuit) { |
| 475 | auto loop1 = Make(); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 476 | auto loop2 = MakePrimary(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 477 | |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 478 | loop2->MakeWatcher("/test1", [&](const TestMessage &) {}); |
| 479 | loop2->MakeWatcher("/test2", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 480 | EXPECT_EQ(message.value(), 200); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 481 | this->Exit(); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 482 | }); |
| 483 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 484 | auto sender = loop1->MakeSender<TestMessage>("/test2"); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 485 | |
| 486 | loop2->OnRun([&]() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 487 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 488 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 489 | builder.add_value(200); |
| 490 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 491 | }); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 492 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 493 | Run(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 496 | // Verify that timer intervals and duration function properly. |
| 497 | TEST_P(AbstractEventLoopTest, TimerIntervalAndDuration) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 498 | // Force a slower rate so we are guarenteed to have reports for our timer. |
| 499 | FLAGS_timing_report_ms = 2000; |
| 500 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 501 | const int kCount = 5; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 502 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 503 | auto loop = MakePrimary(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 504 | auto loop2 = Make(); |
| 505 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 506 | ::std::vector<::aos::monotonic_clock::time_point> times; |
| 507 | ::std::vector<::aos::monotonic_clock::time_point> expected_times; |
| 508 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 509 | Fetcher<timing::Report> report_fetcher = |
| 510 | loop2->MakeFetcher<timing::Report>("/aos"); |
| 511 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 512 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 513 | auto test_timer = loop->AddTimer([this, ×, &expected_times, &loop]() { |
| 514 | times.push_back(loop->monotonic_now()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 515 | EXPECT_EQ(loop->context().realtime_sent_time, realtime_clock::min_time); |
| 516 | EXPECT_EQ(loop->context().queue_index, 0xffffffffu); |
| 517 | EXPECT_EQ(loop->context().size, 0u); |
| 518 | EXPECT_EQ(loop->context().data, nullptr); |
| 519 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 520 | expected_times.push_back(loop->context().monotonic_sent_time); |
| 521 | if (times.size() == kCount) { |
| 522 | this->Exit(); |
| 523 | } |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 524 | }); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 525 | test_timer->set_name("Test loop"); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 526 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 527 | const monotonic_clock::time_point start_time = loop->monotonic_now(); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 528 | // 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] | 529 | test_timer->Setup(start_time + chrono::seconds(1), chrono::seconds(1)); |
| 530 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 531 | Run(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 532 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 533 | // Confirm that we got both the right number of samples, and it's odd. |
| 534 | EXPECT_EQ(times.size(), static_cast<size_t>(kCount)); |
| 535 | EXPECT_EQ(times.size(), expected_times.size()); |
| 536 | EXPECT_EQ((times.size() % 2), 1); |
| 537 | |
| 538 | // Grab the middle sample. |
| 539 | ::aos::monotonic_clock::time_point average_time = times[times.size() / 2]; |
| 540 | |
| 541 | // Add up all the delays of all the times. |
| 542 | ::aos::monotonic_clock::duration sum = chrono::seconds(0); |
| 543 | for (const ::aos::monotonic_clock::time_point time : times) { |
| 544 | sum += time - average_time; |
| 545 | } |
| 546 | |
| 547 | // Average and add to the middle to find the average time. |
| 548 | sum /= times.size(); |
| 549 | average_time += sum; |
| 550 | |
| 551 | // Compute the offset from the average and the expected average. It |
| 552 | // should be pretty close to 0. |
| 553 | const ::aos::monotonic_clock::duration remainder = |
| 554 | average_time - start_time - chrono::seconds(times.size() / 2 + 1); |
| 555 | |
| 556 | const chrono::milliseconds kEpsilon(100); |
| 557 | EXPECT_LT(remainder, +kEpsilon); |
| 558 | EXPECT_GT(remainder, -kEpsilon); |
| 559 | |
| 560 | // Make sure that the average duration is close to 1 second. |
| 561 | EXPECT_NEAR(chrono::duration_cast<chrono::duration<double>>(times.back() - |
| 562 | times.front()) |
| 563 | .count() / |
| 564 | static_cast<double>(times.size() - 1), |
| 565 | 1.0, 0.1); |
| 566 | |
| 567 | // Confirm that the ideal wakeup times increment correctly. |
| 568 | for (size_t i = 1; i < expected_times.size(); ++i) { |
| 569 | EXPECT_EQ(expected_times[i], expected_times[i - 1] + chrono::seconds(1)); |
| 570 | } |
| 571 | |
| 572 | for (size_t i = 0; i < expected_times.size(); ++i) { |
| 573 | EXPECT_EQ((expected_times[i] - start_time) % chrono::seconds(1), |
| 574 | chrono::seconds(0)); |
| 575 | } |
| 576 | |
| 577 | EXPECT_LT(expected_times[expected_times.size() / 2], average_time + kEpsilon); |
| 578 | EXPECT_GT(expected_times[expected_times.size() / 2], average_time - kEpsilon); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 579 | |
| 580 | // And, since we are here, check that the timing report makes sense. |
| 581 | // Start by looking for our event loop's timing. |
| 582 | FlatbufferDetachedBuffer<timing::Report> report = |
| 583 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 584 | while (report_fetcher.FetchNext()) { |
| 585 | if (report_fetcher->name()->string_view() == "primary") { |
| 586 | report = CopyFlatBuffer(report_fetcher.get()); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | // Confirm that we have the right number of reports, and the contents are |
| 591 | // sane. |
| 592 | VLOG(1) << FlatbufferToJson(report, true); |
| 593 | |
| 594 | EXPECT_EQ(report.message().name()->string_view(), "primary"); |
| 595 | |
| 596 | ASSERT_NE(report.message().senders(), nullptr); |
| 597 | EXPECT_EQ(report.message().senders()->size(), 1); |
| 598 | |
| 599 | ASSERT_NE(report.message().timers(), nullptr); |
| 600 | EXPECT_EQ(report.message().timers()->size(), 2); |
| 601 | |
| 602 | EXPECT_EQ(report.message().timers()->Get(0)->name()->string_view(), |
| 603 | "Test loop"); |
| 604 | EXPECT_GE(report.message().timers()->Get(0)->count(), 1); |
| 605 | |
| 606 | EXPECT_EQ(report.message().timers()->Get(1)->name()->string_view(), |
| 607 | "timing_reports"); |
| 608 | EXPECT_EQ(report.message().timers()->Get(1)->count(), 1); |
| 609 | |
| 610 | // Make sure there is a single phased loop report with our report in it. |
| 611 | ASSERT_EQ(report.message().phased_loops(), nullptr); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | // Verify that we can change a timer's parameters during execution. |
| 615 | TEST_P(AbstractEventLoopTest, TimerChangeParameters) { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 616 | auto loop = MakePrimary(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 617 | ::std::vector<::aos::monotonic_clock::time_point> iteration_list; |
| 618 | |
| 619 | auto test_timer = loop->AddTimer([&iteration_list, &loop]() { |
| 620 | iteration_list.push_back(loop->monotonic_now()); |
| 621 | }); |
| 622 | |
| 623 | auto modifier_timer = loop->AddTimer([&loop, &test_timer]() { |
| 624 | test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(30)); |
| 625 | }); |
| 626 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 627 | test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(20)); |
| 628 | modifier_timer->Setup(loop->monotonic_now() + |
| 629 | ::std::chrono::milliseconds(45)); |
| 630 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(150)); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 631 | Run(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 632 | |
| 633 | EXPECT_EQ(iteration_list.size(), 7); |
| 634 | } |
| 635 | |
| 636 | // Verify that we can disable a timer during execution. |
| 637 | TEST_P(AbstractEventLoopTest, TimerDisable) { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 638 | auto loop = MakePrimary(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 639 | ::std::vector<::aos::monotonic_clock::time_point> iteration_list; |
| 640 | |
| 641 | auto test_timer = loop->AddTimer([&iteration_list, &loop]() { |
| 642 | iteration_list.push_back(loop->monotonic_now()); |
| 643 | }); |
| 644 | |
| 645 | auto ender_timer = loop->AddTimer([&test_timer]() { |
| 646 | test_timer->Disable(); |
| 647 | }); |
| 648 | |
| 649 | test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(20)); |
| 650 | ender_timer->Setup(loop->monotonic_now() + |
| 651 | ::std::chrono::milliseconds(45)); |
| 652 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(150)); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 653 | Run(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 654 | |
| 655 | EXPECT_EQ(iteration_list.size(), 3); |
| 656 | } |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 657 | |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 658 | // Verifies that the event loop implementations detect when Channel is not a |
| 659 | // pointer into confguration() |
| 660 | TEST_P(AbstractEventLoopDeathTest, InvalidChannel) { |
| 661 | auto loop = MakePrimary(); |
| 662 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 663 | const Channel *channel = loop->configuration()->channels()->Get(1); |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 664 | |
| 665 | FlatbufferDetachedBuffer<Channel> channel_copy = CopyFlatBuffer(channel); |
| 666 | |
| 667 | EXPECT_DEATH( |
| 668 | { loop->MakeRawSender(&channel_copy.message()); }, |
| 669 | "Channel pointer not found in configuration\\(\\)->channels\\(\\)"); |
| 670 | |
| 671 | EXPECT_DEATH( |
| 672 | { loop->MakeRawFetcher(&channel_copy.message()); }, |
| 673 | "Channel pointer not found in configuration\\(\\)->channels\\(\\)"); |
| 674 | |
| 675 | EXPECT_DEATH( |
| 676 | { |
| 677 | loop->MakeRawWatcher(&channel_copy.message(), |
| 678 | [](const Context, const void *) {}); |
| 679 | }, |
| 680 | "Channel pointer not found in configuration\\(\\)->channels\\(\\)"); |
| 681 | } |
| 682 | |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 683 | // Verify that the send time on a message is roughly right. |
| 684 | TEST_P(AbstractEventLoopTest, MessageSendTime) { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 685 | auto loop1 = MakePrimary(); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 686 | auto loop2 = Make(); |
| 687 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 688 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 689 | |
| 690 | auto test_timer = loop1->AddTimer([&sender]() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 691 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 692 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 693 | builder.add_value(200); |
| 694 | ASSERT_TRUE(msg.Send(builder.Finish())); |
| 695 | }); |
| 696 | |
| 697 | loop2->MakeWatcher("/test", [&loop2](const TestMessage &msg) { |
| 698 | // Confirm that the data pointer makes sense from a watcher. |
| 699 | EXPECT_GT(&msg, loop2->context().data); |
| 700 | EXPECT_LT(&msg, reinterpret_cast<void *>( |
| 701 | reinterpret_cast<char *>(loop2->context().data) + |
| 702 | loop2->context().size)); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 703 | }); |
| 704 | |
| 705 | test_timer->Setup(loop1->monotonic_now() + ::std::chrono::seconds(1)); |
| 706 | |
| 707 | EndEventLoop(loop1.get(), ::std::chrono::seconds(2)); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 708 | Run(); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 709 | |
| 710 | EXPECT_TRUE(fetcher.Fetch()); |
| 711 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 712 | monotonic_clock::duration monotonic_time_offset = |
| 713 | fetcher.context().monotonic_sent_time - |
| 714 | (loop1->monotonic_now() - ::std::chrono::seconds(1)); |
| 715 | realtime_clock::duration realtime_time_offset = |
| 716 | fetcher.context().realtime_sent_time - |
| 717 | (loop1->realtime_now() - ::std::chrono::seconds(1)); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 718 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 719 | EXPECT_TRUE(monotonic_time_offset > ::std::chrono::milliseconds(-500)) |
| 720 | << ": Got " |
| 721 | << fetcher.context().monotonic_sent_time.time_since_epoch().count() |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 722 | << " expected " << loop1->monotonic_now().time_since_epoch().count(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 723 | // Confirm that the data pointer makes sense. |
| 724 | EXPECT_GT(fetcher.get(), fetcher.context().data); |
| 725 | EXPECT_LT(fetcher.get(), |
| 726 | reinterpret_cast<void *>( |
| 727 | reinterpret_cast<char *>(fetcher.context().data) + |
| 728 | fetcher.context().size)); |
| 729 | EXPECT_TRUE(monotonic_time_offset < ::std::chrono::milliseconds(500)) |
| 730 | << ": Got " |
| 731 | << fetcher.context().monotonic_sent_time.time_since_epoch().count() |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 732 | << " expected " << loop1->monotonic_now().time_since_epoch().count(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 733 | |
| 734 | EXPECT_TRUE(realtime_time_offset > ::std::chrono::milliseconds(-500)) |
| 735 | << ": Got " |
| 736 | << fetcher.context().realtime_sent_time.time_since_epoch().count() |
| 737 | << " expected " << loop1->realtime_now().time_since_epoch().count(); |
| 738 | EXPECT_TRUE(realtime_time_offset < ::std::chrono::milliseconds(500)) |
| 739 | << ": Got " |
| 740 | << fetcher.context().realtime_sent_time.time_since_epoch().count() |
| 741 | << " expected " << loop1->realtime_now().time_since_epoch().count(); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 744 | // Tests that a couple phased loops run in a row result in the correct offset |
| 745 | // and period. |
| 746 | TEST_P(AbstractEventLoopTest, PhasedLoopTest) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 747 | // Force a slower rate so we are guarenteed to have reports for our phased |
| 748 | // loop. |
| 749 | FLAGS_timing_report_ms = 2000; |
| 750 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 751 | const chrono::milliseconds kOffset = chrono::milliseconds(400); |
| 752 | const int kCount = 5; |
| 753 | |
| 754 | auto loop1 = MakePrimary(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 755 | auto loop2 = Make(); |
| 756 | |
| 757 | Fetcher<timing::Report> report_fetcher = |
| 758 | loop2->MakeFetcher<timing::Report>("/aos"); |
| 759 | EXPECT_FALSE(report_fetcher.Fetch()); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 760 | |
| 761 | // Collect up a couple of samples. |
| 762 | ::std::vector<::aos::monotonic_clock::time_point> times; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 763 | ::std::vector<::aos::monotonic_clock::time_point> expected_times; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 764 | |
| 765 | // Run kCount iterations. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 766 | loop1 |
| 767 | ->AddPhasedLoop( |
| 768 | [×, &expected_times, &loop1, this](int count) { |
| 769 | EXPECT_EQ(count, 1); |
| 770 | times.push_back(loop1->monotonic_now()); |
| 771 | expected_times.push_back(loop1->context().monotonic_sent_time); |
| 772 | |
| 773 | EXPECT_EQ(loop1->context().realtime_sent_time, |
| 774 | realtime_clock::min_time); |
| 775 | EXPECT_EQ(loop1->context().queue_index, 0xffffffffu); |
| 776 | EXPECT_EQ(loop1->context().size, 0u); |
| 777 | EXPECT_EQ(loop1->context().data, nullptr); |
| 778 | |
| 779 | if (times.size() == kCount) { |
| 780 | LOG(INFO) << "Exiting"; |
| 781 | this->Exit(); |
| 782 | } |
| 783 | }, |
| 784 | chrono::seconds(1), kOffset) |
| 785 | ->set_name("Test loop"); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 786 | |
| 787 | // Add a delay to make sure that delay during startup doesn't result in a |
| 788 | // "missed cycle". |
| 789 | SleepFor(chrono::seconds(2)); |
| 790 | |
| 791 | Run(); |
| 792 | |
| 793 | // Confirm that we got both the right number of samples, and it's odd. |
| 794 | EXPECT_EQ(times.size(), static_cast<size_t>(kCount)); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 795 | EXPECT_EQ(times.size(), expected_times.size()); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 796 | EXPECT_EQ((times.size() % 2), 1); |
| 797 | |
| 798 | // Grab the middle sample. |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 799 | ::aos::monotonic_clock::time_point average_time = times[times.size() / 2]; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 800 | |
| 801 | // Add up all the delays of all the times. |
| 802 | ::aos::monotonic_clock::duration sum = chrono::seconds(0); |
| 803 | for (const ::aos::monotonic_clock::time_point time : times) { |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 804 | sum += time - average_time; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | // Average and add to the middle to find the average time. |
| 808 | sum /= times.size(); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 809 | average_time += sum; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 810 | |
| 811 | // Compute the offset from the start of the second of the average time. This |
| 812 | // should be pretty close to the offset. |
| 813 | const ::aos::monotonic_clock::duration remainder = |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 814 | average_time.time_since_epoch() - |
| 815 | chrono::duration_cast<chrono::seconds>(average_time.time_since_epoch()); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 816 | |
| 817 | const chrono::milliseconds kEpsilon(100); |
| 818 | EXPECT_LT(remainder, kOffset + kEpsilon); |
| 819 | EXPECT_GT(remainder, kOffset - kEpsilon); |
| 820 | |
| 821 | // Make sure that the average duration is close to 1 second. |
| 822 | EXPECT_NEAR(chrono::duration_cast<chrono::duration<double>>(times.back() - |
| 823 | times.front()) |
| 824 | .count() / |
| 825 | static_cast<double>(times.size() - 1), |
| 826 | 1.0, 0.1); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 827 | |
| 828 | // Confirm that the ideal wakeup times increment correctly. |
| 829 | for (size_t i = 1; i < expected_times.size(); ++i) { |
| 830 | EXPECT_EQ(expected_times[i], expected_times[i - 1] + chrono::seconds(1)); |
| 831 | } |
| 832 | |
| 833 | for (size_t i = 0; i < expected_times.size(); ++i) { |
| 834 | EXPECT_EQ(expected_times[i].time_since_epoch() % chrono::seconds(1), |
| 835 | kOffset); |
| 836 | } |
| 837 | |
| 838 | EXPECT_LT(expected_times[expected_times.size() / 2], average_time + kEpsilon); |
| 839 | EXPECT_GT(expected_times[expected_times.size() / 2], average_time - kEpsilon); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 840 | |
| 841 | // And, since we are here, check that the timing report makes sense. |
| 842 | // Start by looking for our event loop's timing. |
| 843 | FlatbufferDetachedBuffer<timing::Report> report = |
| 844 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 845 | while (report_fetcher.FetchNext()) { |
| 846 | if (report_fetcher->name()->string_view() == "primary") { |
| 847 | report = CopyFlatBuffer(report_fetcher.get()); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | VLOG(1) << FlatbufferToJson(report, true); |
| 852 | |
| 853 | EXPECT_EQ(report.message().name()->string_view(), "primary"); |
| 854 | |
| 855 | ASSERT_NE(report.message().senders(), nullptr); |
| 856 | EXPECT_EQ(report.message().senders()->size(), 1); |
| 857 | |
| 858 | ASSERT_NE(report.message().timers(), nullptr); |
| 859 | EXPECT_EQ(report.message().timers()->size(), 1); |
| 860 | |
| 861 | // Make sure there is a single phased loop report with our report in it. |
| 862 | ASSERT_NE(report.message().phased_loops(), nullptr); |
| 863 | ASSERT_EQ(report.message().phased_loops()->size(), 1); |
| 864 | EXPECT_EQ(report.message().phased_loops()->Get(0)->name()->string_view(), |
| 865 | "Test loop"); |
| 866 | EXPECT_GE(report.message().phased_loops()->Get(0)->count(), 1); |
| 867 | } |
| 868 | |
| 869 | // Tests that senders count correctly in the timing report. |
| 870 | TEST_P(AbstractEventLoopTest, SenderTimingReport) { |
| 871 | FLAGS_timing_report_ms = 1000; |
| 872 | auto loop1 = MakePrimary(); |
| 873 | |
| 874 | auto loop2 = Make("watcher_loop"); |
| 875 | loop2->MakeWatcher("/test", [](const TestMessage &) {}); |
| 876 | |
| 877 | auto loop3 = Make(); |
| 878 | |
| 879 | Fetcher<timing::Report> report_fetcher = |
| 880 | loop3->MakeFetcher<timing::Report>("/aos"); |
| 881 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 882 | |
| 883 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 884 | |
| 885 | // Add a timer to actually quit. |
| 886 | auto test_timer = loop1->AddTimer([&sender]() { |
| 887 | for (int i = 0; i < 10; ++i) { |
| 888 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 889 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 890 | builder.add_value(200 + i); |
| 891 | ASSERT_TRUE(msg.Send(builder.Finish())); |
| 892 | } |
| 893 | }); |
| 894 | |
| 895 | // Quit after 1 timing report, mid way through the next cycle. |
| 896 | EndEventLoop(loop1.get(), chrono::milliseconds(2500)); |
| 897 | |
| 898 | loop1->OnRun([&test_timer, &loop1]() { |
| 899 | test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1500)); |
| 900 | }); |
| 901 | |
| 902 | Run(); |
| 903 | |
| 904 | // And, since we are here, check that the timing report makes sense. |
| 905 | // Start by looking for our event loop's timing. |
| 906 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 907 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 908 | while (report_fetcher.FetchNext()) { |
| 909 | LOG(INFO) << "Report " << FlatbufferToJson(report_fetcher.get()); |
| 910 | if (report_fetcher->name()->string_view() == "primary") { |
| 911 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | LOG(INFO) << FlatbufferToJson(primary_report, true); |
| 916 | |
| 917 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 918 | |
| 919 | ASSERT_NE(primary_report.message().senders(), nullptr); |
| 920 | EXPECT_EQ(primary_report.message().senders()->size(), 2); |
| 921 | |
| 922 | // Confirm that the sender looks sane. |
| 923 | EXPECT_EQ( |
| 924 | loop1->configuration() |
| 925 | ->channels() |
| 926 | ->Get(primary_report.message().senders()->Get(0)->channel_index()) |
| 927 | ->name() |
| 928 | ->string_view(), |
| 929 | "/test"); |
| 930 | EXPECT_EQ(primary_report.message().senders()->Get(0)->count(), 10); |
| 931 | |
| 932 | // Confirm that the timing primary_report sender looks sane. |
| 933 | EXPECT_EQ( |
| 934 | loop1->configuration() |
| 935 | ->channels() |
| 936 | ->Get(primary_report.message().senders()->Get(1)->channel_index()) |
| 937 | ->name() |
| 938 | ->string_view(), |
| 939 | "/aos"); |
| 940 | EXPECT_EQ(primary_report.message().senders()->Get(1)->count(), 1); |
| 941 | |
| 942 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 943 | EXPECT_EQ(primary_report.message().timers()->size(), 3); |
| 944 | |
| 945 | // Make sure there are no phased loops or watchers. |
| 946 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 947 | ASSERT_EQ(primary_report.message().watchers(), nullptr); |
| 948 | } |
| 949 | |
| 950 | // Tests that senders count correctly in the timing report. |
| 951 | TEST_P(AbstractEventLoopTest, WatcherTimingReport) { |
| 952 | FLAGS_timing_report_ms = 1000; |
| 953 | auto loop1 = MakePrimary(); |
| 954 | loop1->MakeWatcher("/test", [](const TestMessage &) {}); |
| 955 | |
| 956 | auto loop2 = Make("sender_loop"); |
| 957 | |
| 958 | auto loop3 = Make(); |
| 959 | |
| 960 | Fetcher<timing::Report> report_fetcher = |
| 961 | loop3->MakeFetcher<timing::Report>("/aos"); |
| 962 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 963 | |
| 964 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 965 | |
| 966 | // Add a timer to actually quit. |
| 967 | auto test_timer = loop1->AddTimer([&sender]() { |
| 968 | for (int i = 0; i < 10; ++i) { |
| 969 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 970 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 971 | builder.add_value(200 + i); |
| 972 | ASSERT_TRUE(msg.Send(builder.Finish())); |
| 973 | } |
| 974 | }); |
| 975 | |
| 976 | // Quit after 1 timing report, mid way through the next cycle. |
| 977 | EndEventLoop(loop1.get(), chrono::milliseconds(2500)); |
| 978 | |
| 979 | loop1->OnRun([&test_timer, &loop1]() { |
| 980 | test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1500)); |
| 981 | }); |
| 982 | |
| 983 | Run(); |
| 984 | |
| 985 | // And, since we are here, check that the timing report makes sense. |
| 986 | // Start by looking for our event loop's timing. |
| 987 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 988 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 989 | while (report_fetcher.FetchNext()) { |
| 990 | LOG(INFO) << "Report " << FlatbufferToJson(report_fetcher.get()); |
| 991 | if (report_fetcher->name()->string_view() == "primary") { |
| 992 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | // Check the watcher report. |
| 997 | VLOG(1) << FlatbufferToJson(primary_report, true); |
| 998 | |
| 999 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 1000 | |
| 1001 | // Just the timing report timer. |
| 1002 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 1003 | EXPECT_EQ(primary_report.message().timers()->size(), 3); |
| 1004 | |
| 1005 | // No phased loops |
| 1006 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 1007 | |
| 1008 | ASSERT_NE(primary_report.message().watchers(), nullptr); |
| 1009 | ASSERT_EQ(primary_report.message().watchers()->size(), 1); |
| 1010 | EXPECT_EQ(primary_report.message().watchers()->Get(0)->count(), 10); |
| 1011 | } |
| 1012 | |
| 1013 | // Tests that fetchers count correctly in the timing report. |
| 1014 | TEST_P(AbstractEventLoopTest, FetcherTimingReport) { |
| 1015 | FLAGS_timing_report_ms = 1000; |
| 1016 | auto loop1 = MakePrimary(); |
| 1017 | auto loop2 = Make("sender_loop"); |
| 1018 | |
| 1019 | auto loop3 = Make(); |
| 1020 | |
| 1021 | Fetcher<timing::Report> report_fetcher = |
| 1022 | loop3->MakeFetcher<timing::Report>("/aos"); |
| 1023 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 1024 | |
| 1025 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 1026 | auto fetcher1 = loop1->MakeFetcher<TestMessage>("/test"); |
| 1027 | auto fetcher2 = loop1->MakeFetcher<TestMessage>("/test"); |
| 1028 | fetcher1.Fetch(); |
| 1029 | fetcher2.Fetch(); |
| 1030 | |
| 1031 | // Add a timer to actually quit. |
| 1032 | auto test_timer = loop1->AddTimer([&sender]() { |
| 1033 | for (int i = 0; i < 10; ++i) { |
| 1034 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 1035 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 1036 | builder.add_value(200 + i); |
| 1037 | ASSERT_TRUE(msg.Send(builder.Finish())); |
| 1038 | } |
| 1039 | }); |
| 1040 | |
| 1041 | auto test_timer2 = loop1->AddTimer([&fetcher1, &fetcher2]() { |
| 1042 | fetcher1.Fetch(); |
| 1043 | while (fetcher2.FetchNext()) { |
| 1044 | } |
| 1045 | }); |
| 1046 | |
| 1047 | // Quit after 1 timing report, mid way through the next cycle. |
| 1048 | EndEventLoop(loop1.get(), chrono::milliseconds(2500)); |
| 1049 | |
| 1050 | loop1->OnRun([test_timer, test_timer2, &loop1]() { |
| 1051 | test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1400)); |
| 1052 | test_timer2->Setup(loop1->monotonic_now() + chrono::milliseconds(1600)); |
| 1053 | }); |
| 1054 | |
| 1055 | Run(); |
| 1056 | |
| 1057 | // And, since we are here, check that the timing report makes sense. |
| 1058 | // Start by looking for our event loop's timing. |
| 1059 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 1060 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 1061 | while (report_fetcher.FetchNext()) { |
| 1062 | if (report_fetcher->name()->string_view() == "primary") { |
| 1063 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | VLOG(1) << FlatbufferToJson(primary_report, true); |
| 1068 | |
| 1069 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 1070 | |
| 1071 | ASSERT_NE(primary_report.message().senders(), nullptr); |
| 1072 | EXPECT_EQ(primary_report.message().senders()->size(), 1); |
| 1073 | |
| 1074 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 1075 | EXPECT_EQ(primary_report.message().timers()->size(), 4); |
| 1076 | |
| 1077 | // Make sure there are no phased loops or watchers. |
| 1078 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 1079 | ASSERT_EQ(primary_report.message().watchers(), nullptr); |
| 1080 | |
| 1081 | // Now look at the fetchrs. |
| 1082 | ASSERT_NE(primary_report.message().fetchers(), nullptr); |
| 1083 | ASSERT_EQ(primary_report.message().fetchers()->size(), 2); |
| 1084 | |
| 1085 | EXPECT_EQ(primary_report.message().fetchers()->Get(0)->count(), 1); |
| 1086 | EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->average(), |
| 1087 | 0.1); |
| 1088 | EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->min(), |
| 1089 | 0.1); |
| 1090 | EXPECT_GE(primary_report.message().fetchers()->Get(0)->latency()->max(), |
| 1091 | 0.1); |
| 1092 | EXPECT_EQ(primary_report.message() |
| 1093 | .fetchers() |
| 1094 | ->Get(0) |
| 1095 | ->latency() |
| 1096 | ->standard_deviation(), |
| 1097 | 0.0); |
| 1098 | |
| 1099 | EXPECT_EQ(primary_report.message().fetchers()->Get(1)->count(), 10); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 1102 | // Tests that a raw watcher and raw fetcher can receive messages from a raw |
| 1103 | // sender without messing up offsets. |
| 1104 | TEST_P(AbstractEventLoopTest, RawBasic) { |
| 1105 | auto loop1 = Make(); |
| 1106 | auto loop2 = MakePrimary(); |
| 1107 | auto loop3 = Make(); |
| 1108 | |
| 1109 | const std::string kData("971 is the best"); |
| 1110 | |
| 1111 | std::unique_ptr<aos::RawSender> sender = |
| 1112 | loop1->MakeRawSender(loop1->configuration()->channels()->Get(1)); |
| 1113 | |
| 1114 | std::unique_ptr<aos::RawFetcher> fetcher = |
| 1115 | loop3->MakeRawFetcher(loop3->configuration()->channels()->Get(1)); |
| 1116 | |
| 1117 | loop2->OnRun( |
| 1118 | [&]() { EXPECT_TRUE(sender->Send(kData.data(), kData.size())); }); |
| 1119 | |
| 1120 | bool happened = false; |
| 1121 | loop2->MakeRawWatcher( |
| 1122 | loop2->configuration()->channels()->Get(1), |
| 1123 | [this, &kData, &fetcher, &happened](const Context &context, |
| 1124 | const void *message) { |
| 1125 | happened = true; |
| 1126 | EXPECT_EQ(std::string_view(kData), |
| 1127 | std::string_view(reinterpret_cast<const char *>(message), |
| 1128 | context.size)); |
| 1129 | EXPECT_EQ(std::string_view(kData), |
| 1130 | std::string_view(reinterpret_cast<const char *>(context.data), |
| 1131 | context.size)); |
| 1132 | |
| 1133 | ASSERT_TRUE(fetcher->Fetch()); |
| 1134 | |
| 1135 | EXPECT_EQ(std::string_view(kData), |
| 1136 | std::string_view( |
| 1137 | reinterpret_cast<const char *>(fetcher->context().data), |
| 1138 | fetcher->context().size)); |
| 1139 | |
| 1140 | this->Exit(); |
| 1141 | }); |
| 1142 | |
| 1143 | EXPECT_FALSE(happened); |
| 1144 | Run(); |
| 1145 | EXPECT_TRUE(happened); |
| 1146 | } |
| 1147 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 1148 | } // namespace testing |
| 1149 | } // namespace aos |