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()); |
| 60 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 61 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 62 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 63 | builder.add_value(200); |
| 64 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 65 | |
| 66 | EXPECT_TRUE(fetcher.Fetch()); |
| 67 | ASSERT_FALSE(fetcher.get() == nullptr); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 68 | EXPECT_EQ(fetcher.get()->value(), 200); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 71 | // Tests that watcher will receive all messages sent if they are sent after |
| 72 | // initialization and before running. |
| 73 | TEST_P(AbstractEventLoopTest, DoubleSendAtStartup) { |
| 74 | auto loop1 = Make(); |
| 75 | auto loop2 = MakePrimary(); |
| 76 | |
| 77 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 78 | |
| 79 | ::std::vector<int> values; |
| 80 | |
| 81 | loop2->MakeWatcher("/test", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 82 | values.push_back(message.value()); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 83 | if (values.size() == 2) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 84 | this->Exit(); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 85 | } |
| 86 | }); |
| 87 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 88 | // Before Run, should be ignored. |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 89 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 90 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 91 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 92 | builder.add_value(199); |
| 93 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 94 | } |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 95 | |
| 96 | loop2->OnRun([&]() { |
| 97 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 98 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 99 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 100 | builder.add_value(200); |
| 101 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 102 | } |
| 103 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 104 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 105 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 106 | builder.add_value(201); |
| 107 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 108 | } |
| 109 | }); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 110 | |
| 111 | Run(); |
| 112 | |
| 113 | EXPECT_THAT(values, ::testing::ElementsAreArray({200, 201})); |
| 114 | } |
| 115 | |
| 116 | // Tests that watcher will not receive messages sent before the watcher is |
| 117 | // created. |
| 118 | TEST_P(AbstractEventLoopTest, DoubleSendAfterStartup) { |
| 119 | auto loop1 = Make(); |
| 120 | auto loop2 = MakePrimary(); |
| 121 | |
| 122 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 123 | |
| 124 | ::std::vector<int> values; |
| 125 | |
| 126 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 127 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 128 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 129 | builder.add_value(200); |
| 130 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 131 | } |
| 132 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 133 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 134 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 135 | builder.add_value(201); |
| 136 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | loop2->MakeWatcher("/test", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 140 | values.push_back(message.value()); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 141 | }); |
| 142 | |
| 143 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 144 | auto test_timer = loop2->AddTimer([this]() { this->Exit(); }); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 145 | loop2->OnRun([&test_timer, &loop2]() { |
| 146 | test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 147 | }); |
| 148 | |
| 149 | Run(); |
| 150 | EXPECT_EQ(0, values.size()); |
| 151 | } |
| 152 | |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 153 | // Tests that FetchNext gets all the messages sent after it is constructed. |
| 154 | TEST_P(AbstractEventLoopTest, FetchNext) { |
| 155 | auto loop1 = Make(); |
| 156 | auto loop2 = MakePrimary(); |
| 157 | |
| 158 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 159 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 160 | |
| 161 | ::std::vector<int> values; |
| 162 | |
| 163 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 164 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 165 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 166 | builder.add_value(200); |
| 167 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 168 | } |
| 169 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 170 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 171 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 172 | builder.add_value(201); |
| 173 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 177 | auto test_timer = loop2->AddTimer([&fetcher, &values, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 178 | while (fetcher.FetchNext()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 179 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 180 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 181 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 182 | }); |
| 183 | |
| 184 | loop2->OnRun([&test_timer, &loop2]() { |
| 185 | test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 186 | }); |
| 187 | |
| 188 | Run(); |
| 189 | EXPECT_THAT(values, ::testing::ElementsAreArray({200, 201})); |
| 190 | } |
| 191 | |
| 192 | // Tests that FetchNext gets no messages sent before it is constructed. |
| 193 | TEST_P(AbstractEventLoopTest, FetchNextAfterSend) { |
| 194 | auto loop1 = Make(); |
| 195 | auto loop2 = MakePrimary(); |
| 196 | |
| 197 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 198 | |
| 199 | ::std::vector<int> values; |
| 200 | |
| 201 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 202 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 203 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 204 | builder.add_value(200); |
| 205 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 206 | } |
| 207 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 208 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 209 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 210 | builder.add_value(201); |
| 211 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 215 | |
| 216 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 217 | auto test_timer = loop2->AddTimer([&fetcher, &values, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 218 | while (fetcher.FetchNext()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 219 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 220 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 221 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 222 | }); |
| 223 | |
| 224 | loop2->OnRun([&test_timer, &loop2]() { |
| 225 | test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 226 | }); |
| 227 | |
| 228 | Run(); |
| 229 | EXPECT_THAT(0, values.size()); |
| 230 | } |
| 231 | |
| 232 | // Tests that Fetch returns the last message created before the loop was |
| 233 | // started. |
| 234 | TEST_P(AbstractEventLoopTest, FetchDataFromBeforeCreation) { |
| 235 | auto loop1 = Make(); |
| 236 | auto loop2 = MakePrimary(); |
| 237 | |
| 238 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 239 | |
| 240 | ::std::vector<int> values; |
| 241 | |
| 242 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 243 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 244 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 245 | builder.add_value(200); |
| 246 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 247 | } |
| 248 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 249 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 250 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 251 | builder.add_value(201); |
| 252 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 256 | |
| 257 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 258 | auto test_timer = loop2->AddTimer([&fetcher, &values, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 259 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 260 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 261 | } |
| 262 | // Do it again to make sure we don't double fetch. |
| 263 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 264 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 265 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 266 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 267 | }); |
| 268 | |
| 269 | loop2->OnRun([&test_timer, &loop2]() { |
| 270 | test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 271 | }); |
| 272 | |
| 273 | Run(); |
| 274 | EXPECT_THAT(values, ::testing::ElementsAreArray({201})); |
| 275 | } |
| 276 | |
| 277 | // Tests that Fetch and FetchNext interleave as expected. |
| 278 | TEST_P(AbstractEventLoopTest, FetchAndFetchNextTogether) { |
| 279 | auto loop1 = Make(); |
| 280 | auto loop2 = MakePrimary(); |
| 281 | |
| 282 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 283 | |
| 284 | ::std::vector<int> values; |
| 285 | |
| 286 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 287 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 288 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 289 | builder.add_value(200); |
| 290 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 291 | } |
| 292 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 293 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 294 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 295 | builder.add_value(201); |
| 296 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 300 | |
| 301 | // Add a timer to actually quit. |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 302 | auto test_timer = loop2->AddTimer([&fetcher, &values, &sender, this]() { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 303 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 304 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 305 | } |
| 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(202); |
| 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(203); |
| 317 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 318 | } |
| 319 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 320 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 321 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 322 | builder.add_value(204); |
| 323 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | if (fetcher.FetchNext()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 327 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | if (fetcher.Fetch()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 331 | values.push_back(fetcher.get()->value()); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 334 | this->Exit(); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 335 | }); |
| 336 | |
| 337 | loop2->OnRun([&test_timer, &loop2]() { |
| 338 | test_timer->Setup(loop2->monotonic_now(), ::std::chrono::milliseconds(100)); |
| 339 | }); |
| 340 | |
| 341 | Run(); |
| 342 | EXPECT_THAT(values, ::testing::ElementsAreArray({201, 202, 204})); |
| 343 | } |
| 344 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 345 | |
| 346 | // Tests that FetchNext behaves correctly when we get two messages in the queue |
| 347 | // but don't consume the first until after the second has been sent. |
| 348 | TEST_P(AbstractEventLoopTest, FetchNextTest) { |
| 349 | |
| 350 | auto send_loop = Make(); |
| 351 | auto fetch_loop = Make(); |
| 352 | auto sender = send_loop->MakeSender<TestMessage>("/test"); |
| 353 | Fetcher<TestMessage> fetcher = fetch_loop->MakeFetcher<TestMessage>("/test"); |
| 354 | |
| 355 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 356 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 357 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 358 | builder.add_value(100); |
| 359 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 363 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 364 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 365 | builder.add_value(200); |
| 366 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | ASSERT_TRUE(fetcher.FetchNext()); |
| 370 | ASSERT_NE(nullptr, fetcher.get()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 371 | EXPECT_EQ(100, fetcher.get()->value()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 372 | |
| 373 | ASSERT_TRUE(fetcher.FetchNext()); |
| 374 | ASSERT_NE(nullptr, fetcher.get()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 375 | EXPECT_EQ(200, fetcher.get()->value()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 376 | |
| 377 | // When we run off the end of the queue, expect to still have the old message: |
| 378 | ASSERT_FALSE(fetcher.FetchNext()); |
| 379 | ASSERT_NE(nullptr, fetcher.get()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 380 | EXPECT_EQ(200, fetcher.get()->value()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 383 | // Verify that making a fetcher and watcher for "/test" succeeds. |
| 384 | TEST_P(AbstractEventLoopTest, FetcherAndWatcher) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 385 | auto loop = Make(); |
| 386 | auto fetcher = loop->MakeFetcher<TestMessage>("/test"); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 387 | loop->MakeWatcher("/test", [&](const TestMessage &) {}); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 390 | // Verify that making 2 fetchers for "/test" succeeds. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 391 | TEST_P(AbstractEventLoopTest, TwoFetcher) { |
| 392 | auto loop = Make(); |
| 393 | auto fetcher = loop->MakeFetcher<TestMessage>("/test"); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 394 | auto fetcher2 = loop->MakeFetcher<TestMessage>("/test"); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 397 | // Verify that registering a watcher for an invalid channel name dies. |
| 398 | TEST_P(AbstractEventLoopDeathTest, InvalidChannelName) { |
| 399 | auto loop = Make(); |
| 400 | EXPECT_DEATH( |
| 401 | { loop->MakeWatcher("/test/invalid", [&](const TestMessage &) {}); }, |
| 402 | "/test/invalid"); |
| 403 | } |
| 404 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 405 | // Verify that registering a watcher twice for "/test" fails. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 406 | TEST_P(AbstractEventLoopDeathTest, TwoWatcher) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 407 | auto loop = Make(); |
| 408 | loop->MakeWatcher("/test", [&](const TestMessage &) {}); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 409 | EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}), |
| 410 | "/test"); |
| 411 | } |
| 412 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 413 | // Verify that SetRuntimeRealtimePriority fails while running. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 414 | TEST_P(AbstractEventLoopDeathTest, SetRuntimeRealtimePriority) { |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 415 | auto loop = MakePrimary(); |
| 416 | // Confirm that runtime priority calls work when not realtime. |
| 417 | loop->SetRuntimeRealtimePriority(5); |
| 418 | |
| 419 | loop->OnRun([&]() { loop->SetRuntimeRealtimePriority(5); }); |
| 420 | |
| 421 | EXPECT_DEATH(Run(), "realtime"); |
| 422 | } |
| 423 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 424 | // Verify that registering a watcher and a sender for "/test" fails. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 425 | TEST_P(AbstractEventLoopDeathTest, WatcherAndSender) { |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 426 | auto loop = Make(); |
| 427 | auto sender = loop->MakeSender<TestMessage>("/test"); |
| 428 | EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}), |
| 429 | "/test"); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 432 | // Verify that we can't create a sender inside OnRun. |
| 433 | TEST_P(AbstractEventLoopDeathTest, SenderInOnRun) { |
| 434 | auto loop1 = MakePrimary(); |
| 435 | |
| 436 | loop1->OnRun( |
| 437 | [&]() { auto sender = loop1->MakeSender<TestMessage>("/test2"); }); |
| 438 | |
| 439 | EXPECT_DEATH(Run(), "running"); |
| 440 | } |
| 441 | |
| 442 | // Verify that we can't create a watcher inside OnRun. |
| 443 | TEST_P(AbstractEventLoopDeathTest, WatcherInOnRun) { |
| 444 | auto loop1 = MakePrimary(); |
| 445 | |
| 446 | loop1->OnRun( |
| 447 | [&]() { loop1->MakeWatcher("/test", [&](const TestMessage &) {}); }); |
| 448 | |
| 449 | EXPECT_DEATH(Run(), "running"); |
| 450 | } |
| 451 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 452 | // Verify that Quit() works when there are multiple watchers. |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 453 | TEST_P(AbstractEventLoopTest, MultipleWatcherQuit) { |
| 454 | auto loop1 = Make(); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 455 | auto loop2 = MakePrimary(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 456 | |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 457 | loop2->MakeWatcher("/test1", [&](const TestMessage &) {}); |
| 458 | loop2->MakeWatcher("/test2", [&](const TestMessage &message) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 459 | EXPECT_EQ(message.value(), 200); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 460 | this->Exit(); |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 461 | }); |
| 462 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 463 | auto sender = loop1->MakeSender<TestMessage>("/test2"); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 464 | |
| 465 | loop2->OnRun([&]() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 466 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 467 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 468 | builder.add_value(200); |
| 469 | ASSERT_TRUE(msg.Send(builder.Finish())); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 470 | }); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 471 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 472 | Run(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 475 | // Verify that timer intervals and duration function properly. |
| 476 | TEST_P(AbstractEventLoopTest, TimerIntervalAndDuration) { |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame^] | 477 | const int kCount = 5; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 478 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame^] | 479 | auto loop = MakePrimary(); |
| 480 | ::std::vector<::aos::monotonic_clock::time_point> times; |
| 481 | ::std::vector<::aos::monotonic_clock::time_point> expected_times; |
| 482 | |
| 483 | auto test_timer = loop->AddTimer([this, ×, &expected_times, &loop]() { |
| 484 | times.push_back(loop->monotonic_now()); |
| 485 | expected_times.push_back(loop->context().monotonic_sent_time); |
| 486 | if (times.size() == kCount) { |
| 487 | this->Exit(); |
| 488 | } |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 489 | }); |
| 490 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame^] | 491 | monotonic_clock::time_point start_time = loop->monotonic_now(); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 492 | // 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^] | 493 | test_timer->Setup(start_time + chrono::seconds(1), chrono::seconds(1)); |
| 494 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 495 | Run(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 496 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame^] | 497 | // Confirm that we got both the right number of samples, and it's odd. |
| 498 | EXPECT_EQ(times.size(), static_cast<size_t>(kCount)); |
| 499 | EXPECT_EQ(times.size(), expected_times.size()); |
| 500 | EXPECT_EQ((times.size() % 2), 1); |
| 501 | |
| 502 | // Grab the middle sample. |
| 503 | ::aos::monotonic_clock::time_point average_time = times[times.size() / 2]; |
| 504 | |
| 505 | // Add up all the delays of all the times. |
| 506 | ::aos::monotonic_clock::duration sum = chrono::seconds(0); |
| 507 | for (const ::aos::monotonic_clock::time_point time : times) { |
| 508 | sum += time - average_time; |
| 509 | } |
| 510 | |
| 511 | // Average and add to the middle to find the average time. |
| 512 | sum /= times.size(); |
| 513 | average_time += sum; |
| 514 | |
| 515 | // Compute the offset from the average and the expected average. It |
| 516 | // should be pretty close to 0. |
| 517 | const ::aos::monotonic_clock::duration remainder = |
| 518 | average_time - start_time - chrono::seconds(times.size() / 2 + 1); |
| 519 | |
| 520 | const chrono::milliseconds kEpsilon(100); |
| 521 | EXPECT_LT(remainder, +kEpsilon); |
| 522 | EXPECT_GT(remainder, -kEpsilon); |
| 523 | |
| 524 | // Make sure that the average duration is close to 1 second. |
| 525 | EXPECT_NEAR(chrono::duration_cast<chrono::duration<double>>(times.back() - |
| 526 | times.front()) |
| 527 | .count() / |
| 528 | static_cast<double>(times.size() - 1), |
| 529 | 1.0, 0.1); |
| 530 | |
| 531 | // Confirm that the ideal wakeup times increment correctly. |
| 532 | for (size_t i = 1; i < expected_times.size(); ++i) { |
| 533 | EXPECT_EQ(expected_times[i], expected_times[i - 1] + chrono::seconds(1)); |
| 534 | } |
| 535 | |
| 536 | for (size_t i = 0; i < expected_times.size(); ++i) { |
| 537 | EXPECT_EQ((expected_times[i] - start_time) % chrono::seconds(1), |
| 538 | chrono::seconds(0)); |
| 539 | } |
| 540 | |
| 541 | EXPECT_LT(expected_times[expected_times.size() / 2], average_time + kEpsilon); |
| 542 | EXPECT_GT(expected_times[expected_times.size() / 2], average_time - kEpsilon); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | // Verify that we can change a timer's parameters during execution. |
| 546 | TEST_P(AbstractEventLoopTest, TimerChangeParameters) { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 547 | auto loop = MakePrimary(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 548 | ::std::vector<::aos::monotonic_clock::time_point> iteration_list; |
| 549 | |
| 550 | auto test_timer = loop->AddTimer([&iteration_list, &loop]() { |
| 551 | iteration_list.push_back(loop->monotonic_now()); |
| 552 | }); |
| 553 | |
| 554 | auto modifier_timer = loop->AddTimer([&loop, &test_timer]() { |
| 555 | test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(30)); |
| 556 | }); |
| 557 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 558 | test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(20)); |
| 559 | modifier_timer->Setup(loop->monotonic_now() + |
| 560 | ::std::chrono::milliseconds(45)); |
| 561 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(150)); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 562 | Run(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 563 | |
| 564 | EXPECT_EQ(iteration_list.size(), 7); |
| 565 | } |
| 566 | |
| 567 | // Verify that we can disable a timer during execution. |
| 568 | TEST_P(AbstractEventLoopTest, TimerDisable) { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 569 | auto loop = MakePrimary(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 570 | ::std::vector<::aos::monotonic_clock::time_point> iteration_list; |
| 571 | |
| 572 | auto test_timer = loop->AddTimer([&iteration_list, &loop]() { |
| 573 | iteration_list.push_back(loop->monotonic_now()); |
| 574 | }); |
| 575 | |
| 576 | auto ender_timer = loop->AddTimer([&test_timer]() { |
| 577 | test_timer->Disable(); |
| 578 | }); |
| 579 | |
| 580 | test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(20)); |
| 581 | ender_timer->Setup(loop->monotonic_now() + |
| 582 | ::std::chrono::milliseconds(45)); |
| 583 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(150)); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 584 | Run(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 585 | |
| 586 | EXPECT_EQ(iteration_list.size(), 3); |
| 587 | } |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 588 | |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 589 | // Verifies that the event loop implementations detect when Channel is not a |
| 590 | // pointer into confguration() |
| 591 | TEST_P(AbstractEventLoopDeathTest, InvalidChannel) { |
| 592 | auto loop = MakePrimary(); |
| 593 | |
| 594 | const Channel *channel = loop->configuration()->channels()->Get(0); |
| 595 | |
| 596 | FlatbufferDetachedBuffer<Channel> channel_copy = CopyFlatBuffer(channel); |
| 597 | |
| 598 | EXPECT_DEATH( |
| 599 | { loop->MakeRawSender(&channel_copy.message()); }, |
| 600 | "Channel pointer not found in configuration\\(\\)->channels\\(\\)"); |
| 601 | |
| 602 | EXPECT_DEATH( |
| 603 | { loop->MakeRawFetcher(&channel_copy.message()); }, |
| 604 | "Channel pointer not found in configuration\\(\\)->channels\\(\\)"); |
| 605 | |
| 606 | EXPECT_DEATH( |
| 607 | { |
| 608 | loop->MakeRawWatcher(&channel_copy.message(), |
| 609 | [](const Context, const void *) {}); |
| 610 | }, |
| 611 | "Channel pointer not found in configuration\\(\\)->channels\\(\\)"); |
| 612 | } |
| 613 | |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 614 | // Verify that the send time on a message is roughly right. |
| 615 | TEST_P(AbstractEventLoopTest, MessageSendTime) { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 616 | auto loop1 = MakePrimary(); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 617 | auto loop2 = Make(); |
| 618 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 619 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 620 | |
| 621 | auto test_timer = loop1->AddTimer([&sender]() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 622 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 623 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 624 | builder.add_value(200); |
| 625 | ASSERT_TRUE(msg.Send(builder.Finish())); |
| 626 | }); |
| 627 | |
| 628 | loop2->MakeWatcher("/test", [&loop2](const TestMessage &msg) { |
| 629 | // Confirm that the data pointer makes sense from a watcher. |
| 630 | EXPECT_GT(&msg, loop2->context().data); |
| 631 | EXPECT_LT(&msg, reinterpret_cast<void *>( |
| 632 | reinterpret_cast<char *>(loop2->context().data) + |
| 633 | loop2->context().size)); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 634 | }); |
| 635 | |
| 636 | test_timer->Setup(loop1->monotonic_now() + ::std::chrono::seconds(1)); |
| 637 | |
| 638 | EndEventLoop(loop1.get(), ::std::chrono::seconds(2)); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 639 | Run(); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 640 | |
| 641 | EXPECT_TRUE(fetcher.Fetch()); |
| 642 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 643 | monotonic_clock::duration monotonic_time_offset = |
| 644 | fetcher.context().monotonic_sent_time - |
| 645 | (loop1->monotonic_now() - ::std::chrono::seconds(1)); |
| 646 | realtime_clock::duration realtime_time_offset = |
| 647 | fetcher.context().realtime_sent_time - |
| 648 | (loop1->realtime_now() - ::std::chrono::seconds(1)); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 649 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 650 | EXPECT_TRUE(monotonic_time_offset > ::std::chrono::milliseconds(-500)) |
| 651 | << ": Got " |
| 652 | << fetcher.context().monotonic_sent_time.time_since_epoch().count() |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 653 | << " expected " << loop1->monotonic_now().time_since_epoch().count(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 654 | // Confirm that the data pointer makes sense. |
| 655 | EXPECT_GT(fetcher.get(), fetcher.context().data); |
| 656 | EXPECT_LT(fetcher.get(), |
| 657 | reinterpret_cast<void *>( |
| 658 | reinterpret_cast<char *>(fetcher.context().data) + |
| 659 | fetcher.context().size)); |
| 660 | EXPECT_TRUE(monotonic_time_offset < ::std::chrono::milliseconds(500)) |
| 661 | << ": Got " |
| 662 | << fetcher.context().monotonic_sent_time.time_since_epoch().count() |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 663 | << " expected " << loop1->monotonic_now().time_since_epoch().count(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 664 | |
| 665 | EXPECT_TRUE(realtime_time_offset > ::std::chrono::milliseconds(-500)) |
| 666 | << ": Got " |
| 667 | << fetcher.context().realtime_sent_time.time_since_epoch().count() |
| 668 | << " expected " << loop1->realtime_now().time_since_epoch().count(); |
| 669 | EXPECT_TRUE(realtime_time_offset < ::std::chrono::milliseconds(500)) |
| 670 | << ": Got " |
| 671 | << fetcher.context().realtime_sent_time.time_since_epoch().count() |
| 672 | << " expected " << loop1->realtime_now().time_since_epoch().count(); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 675 | // Tests that a couple phased loops run in a row result in the correct offset |
| 676 | // and period. |
| 677 | TEST_P(AbstractEventLoopTest, PhasedLoopTest) { |
| 678 | const chrono::milliseconds kOffset = chrono::milliseconds(400); |
| 679 | const int kCount = 5; |
| 680 | |
| 681 | auto loop1 = MakePrimary(); |
| 682 | |
| 683 | // Collect up a couple of samples. |
| 684 | ::std::vector<::aos::monotonic_clock::time_point> times; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame^] | 685 | ::std::vector<::aos::monotonic_clock::time_point> expected_times; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 686 | |
| 687 | // Run kCount iterations. |
| 688 | loop1->AddPhasedLoop( |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame^] | 689 | [×, &expected_times, &loop1, this](int count) { |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 690 | EXPECT_EQ(count, 1); |
| 691 | times.push_back(loop1->monotonic_now()); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame^] | 692 | expected_times.push_back(loop1->context().monotonic_sent_time); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 693 | if (times.size() == kCount) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 694 | this->Exit(); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 695 | } |
| 696 | }, |
| 697 | chrono::seconds(1), kOffset); |
| 698 | |
| 699 | // Add a delay to make sure that delay during startup doesn't result in a |
| 700 | // "missed cycle". |
| 701 | SleepFor(chrono::seconds(2)); |
| 702 | |
| 703 | Run(); |
| 704 | |
| 705 | // Confirm that we got both the right number of samples, and it's odd. |
| 706 | EXPECT_EQ(times.size(), static_cast<size_t>(kCount)); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame^] | 707 | EXPECT_EQ(times.size(), expected_times.size()); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 708 | EXPECT_EQ((times.size() % 2), 1); |
| 709 | |
| 710 | // Grab the middle sample. |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame^] | 711 | ::aos::monotonic_clock::time_point average_time = times[times.size() / 2]; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 712 | |
| 713 | // Add up all the delays of all the times. |
| 714 | ::aos::monotonic_clock::duration sum = chrono::seconds(0); |
| 715 | for (const ::aos::monotonic_clock::time_point time : times) { |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame^] | 716 | sum += time - average_time; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | // Average and add to the middle to find the average time. |
| 720 | sum /= times.size(); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame^] | 721 | average_time += sum; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 722 | |
| 723 | // Compute the offset from the start of the second of the average time. This |
| 724 | // should be pretty close to the offset. |
| 725 | const ::aos::monotonic_clock::duration remainder = |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame^] | 726 | average_time.time_since_epoch() - |
| 727 | chrono::duration_cast<chrono::seconds>(average_time.time_since_epoch()); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 728 | |
| 729 | const chrono::milliseconds kEpsilon(100); |
| 730 | EXPECT_LT(remainder, kOffset + kEpsilon); |
| 731 | EXPECT_GT(remainder, kOffset - kEpsilon); |
| 732 | |
| 733 | // Make sure that the average duration is close to 1 second. |
| 734 | EXPECT_NEAR(chrono::duration_cast<chrono::duration<double>>(times.back() - |
| 735 | times.front()) |
| 736 | .count() / |
| 737 | static_cast<double>(times.size() - 1), |
| 738 | 1.0, 0.1); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame^] | 739 | |
| 740 | // Confirm that the ideal wakeup times increment correctly. |
| 741 | for (size_t i = 1; i < expected_times.size(); ++i) { |
| 742 | EXPECT_EQ(expected_times[i], expected_times[i - 1] + chrono::seconds(1)); |
| 743 | } |
| 744 | |
| 745 | for (size_t i = 0; i < expected_times.size(); ++i) { |
| 746 | EXPECT_EQ(expected_times[i].time_since_epoch() % chrono::seconds(1), |
| 747 | kOffset); |
| 748 | } |
| 749 | |
| 750 | EXPECT_LT(expected_times[expected_times.size() / 2], average_time + kEpsilon); |
| 751 | EXPECT_GT(expected_times[expected_times.size() / 2], average_time - kEpsilon); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 754 | } // namespace testing |
| 755 | } // namespace aos |