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