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