Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include "aos/events/simulated_event_loop.h" |
| 2 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 3 | #include <string_view> |
| 4 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 5 | #include "aos/events/event_loop_param_test.h" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 6 | #include "aos/events/logging/logger_generated.h" |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 7 | #include "aos/events/message_counter.h" |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 8 | #include "aos/events/ping_lib.h" |
| 9 | #include "aos/events/pong_lib.h" |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 10 | #include "aos/events/test_message_generated.h" |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 11 | #include "aos/network/message_bridge_client_generated.h" |
| 12 | #include "aos/network/message_bridge_server_generated.h" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 13 | #include "aos/network/remote_message_generated.h" |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 14 | #include "aos/network/timestamp_generated.h" |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 15 | #include "gtest/gtest.h" |
| 16 | |
| 17 | namespace aos { |
| 18 | namespace testing { |
Brian Silverman | 28d1430 | 2020-09-18 15:26:17 -0700 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | std::string ConfigPrefix() { return "aos/"; } |
| 22 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 23 | using message_bridge::RemoteMessage; |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 24 | namespace chrono = ::std::chrono; |
| 25 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 26 | } // namespace |
| 27 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 28 | class SimulatedEventLoopTestFactory : public EventLoopTestFactory { |
| 29 | public: |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 30 | ::std::unique_ptr<EventLoop> Make(std::string_view name) override { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 31 | MaybeMake(); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 32 | return event_loop_factory_->MakeEventLoop(name, my_node()); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 33 | } |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 34 | ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name) override { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 35 | MaybeMake(); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 36 | return event_loop_factory_->MakeEventLoop(name, my_node()); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 39 | void Run() override { event_loop_factory_->Run(); } |
| 40 | void Exit() override { event_loop_factory_->Exit(); } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 41 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 42 | // TODO(austin): Implement this. It's used currently for a phased loop test. |
| 43 | // I'm not sure how much that matters. |
| 44 | void SleepFor(::std::chrono::nanoseconds /*duration*/) override {} |
| 45 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 46 | void set_send_delay(std::chrono::nanoseconds send_delay) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 47 | MaybeMake(); |
| 48 | event_loop_factory_->set_send_delay(send_delay); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 51 | private: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 52 | void MaybeMake() { |
| 53 | if (!event_loop_factory_) { |
| 54 | if (configuration()->has_nodes()) { |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 55 | event_loop_factory_ = |
| 56 | std::make_unique<SimulatedEventLoopFactory>(configuration()); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 57 | } else { |
| 58 | event_loop_factory_ = |
| 59 | std::make_unique<SimulatedEventLoopFactory>(configuration()); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | std::unique_ptr<SimulatedEventLoopFactory> event_loop_factory_; |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 66 | INSTANTIATE_TEST_CASE_P(SimulatedEventLoopCopyTest, AbstractEventLoopTest, |
| 67 | ::testing::Values(std::make_tuple( |
| 68 | []() { |
| 69 | return new SimulatedEventLoopTestFactory(); |
| 70 | }, |
| 71 | ReadMethod::COPY))); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 72 | |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 73 | INSTANTIATE_TEST_CASE_P( |
| 74 | SimulatedEventLoopCopyDeathTest, AbstractEventLoopDeathTest, |
| 75 | ::testing::Values( |
| 76 | std::make_tuple([]() { return new SimulatedEventLoopTestFactory(); }, |
| 77 | ReadMethod::COPY))); |
| 78 | |
| 79 | INSTANTIATE_TEST_CASE_P(SimulatedEventLoopPinTest, AbstractEventLoopTest, |
| 80 | ::testing::Values(std::make_tuple( |
| 81 | []() { |
| 82 | return new SimulatedEventLoopTestFactory(); |
| 83 | }, |
| 84 | ReadMethod::PIN))); |
| 85 | |
| 86 | INSTANTIATE_TEST_CASE_P( |
| 87 | SimulatedEventLoopPinDeathTest, AbstractEventLoopDeathTest, |
| 88 | ::testing::Values( |
| 89 | std::make_tuple([]() { return new SimulatedEventLoopTestFactory(); }, |
| 90 | ReadMethod::PIN))); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 91 | |
| 92 | // Test that creating an event and running the scheduler runs the event. |
| 93 | TEST(EventSchedulerTest, ScheduleEvent) { |
| 94 | int counter = 0; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 95 | EventSchedulerScheduler scheduler_scheduler; |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 96 | EventScheduler scheduler; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 97 | scheduler_scheduler.AddEventScheduler(&scheduler); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 98 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 99 | scheduler.Schedule(monotonic_clock::epoch() + chrono::seconds(1), |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 100 | [&counter]() { counter += 1; }); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 101 | scheduler_scheduler.Run(); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 102 | EXPECT_EQ(counter, 1); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 103 | auto token = scheduler.Schedule(monotonic_clock::epoch() + chrono::seconds(2), |
| 104 | [&counter]() { counter += 1; }); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 105 | scheduler.Deschedule(token); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 106 | scheduler_scheduler.Run(); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 107 | EXPECT_EQ(counter, 1); |
| 108 | } |
| 109 | |
| 110 | // Test that descheduling an already scheduled event doesn't run the event. |
| 111 | TEST(EventSchedulerTest, DescheduleEvent) { |
| 112 | int counter = 0; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 113 | EventSchedulerScheduler scheduler_scheduler; |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 114 | EventScheduler scheduler; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 115 | scheduler_scheduler.AddEventScheduler(&scheduler); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 116 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 117 | auto token = scheduler.Schedule(monotonic_clock::epoch() + chrono::seconds(1), |
| 118 | [&counter]() { counter += 1; }); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 119 | scheduler.Deschedule(token); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 120 | scheduler_scheduler.Run(); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 121 | EXPECT_EQ(counter, 0); |
| 122 | } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 123 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 124 | void SendTestMessage(aos::Sender<TestMessage> *sender, int value) { |
| 125 | aos::Sender<TestMessage>::Builder builder = sender->MakeBuilder(); |
| 126 | TestMessage::Builder test_message_builder = |
| 127 | builder.MakeBuilder<TestMessage>(); |
| 128 | test_message_builder.add_value(value); |
| 129 | builder.Send(test_message_builder.Finish()); |
| 130 | } |
| 131 | |
| 132 | // Test that sending a message after running gets properly notified. |
| 133 | TEST(SimulatedEventLoopTest, SendAfterRunFor) { |
| 134 | SimulatedEventLoopTestFactory factory; |
| 135 | |
| 136 | SimulatedEventLoopFactory simulated_event_loop_factory( |
| 137 | factory.configuration()); |
| 138 | |
| 139 | ::std::unique_ptr<EventLoop> ping_event_loop = |
| 140 | simulated_event_loop_factory.MakeEventLoop("ping"); |
| 141 | aos::Sender<TestMessage> test_message_sender = |
| 142 | ping_event_loop->MakeSender<TestMessage>("/test"); |
| 143 | SendTestMessage(&test_message_sender, 1); |
| 144 | |
| 145 | std::unique_ptr<EventLoop> pong1_event_loop = |
| 146 | simulated_event_loop_factory.MakeEventLoop("pong"); |
| 147 | MessageCounter<TestMessage> test_message_counter1(pong1_event_loop.get(), |
| 148 | "/test"); |
| 149 | |
| 150 | EXPECT_FALSE(ping_event_loop->is_running()); |
| 151 | |
| 152 | // Watchers start when you start running, so there should be nothing counted. |
| 153 | simulated_event_loop_factory.RunFor(chrono::seconds(1)); |
| 154 | EXPECT_EQ(test_message_counter1.count(), 0u); |
| 155 | |
| 156 | std::unique_ptr<EventLoop> pong2_event_loop = |
| 157 | simulated_event_loop_factory.MakeEventLoop("pong"); |
| 158 | MessageCounter<TestMessage> test_message_counter2(pong2_event_loop.get(), |
| 159 | "/test"); |
| 160 | |
| 161 | // Pauses in the middle don't count though, so this should be counted. |
| 162 | // But, the fresh watcher shouldn't pick it up yet. |
| 163 | SendTestMessage(&test_message_sender, 2); |
| 164 | |
| 165 | EXPECT_EQ(test_message_counter1.count(), 0u); |
| 166 | EXPECT_EQ(test_message_counter2.count(), 0u); |
| 167 | simulated_event_loop_factory.RunFor(chrono::seconds(1)); |
| 168 | |
| 169 | EXPECT_EQ(test_message_counter1.count(), 1u); |
| 170 | EXPECT_EQ(test_message_counter2.count(), 0u); |
| 171 | } |
| 172 | |
| 173 | // Test that creating an event loop while running dies. |
| 174 | TEST(SimulatedEventLoopDeathTest, MakeEventLoopWhileRunning) { |
| 175 | SimulatedEventLoopTestFactory factory; |
| 176 | |
| 177 | SimulatedEventLoopFactory simulated_event_loop_factory( |
| 178 | factory.configuration()); |
| 179 | |
| 180 | ::std::unique_ptr<EventLoop> event_loop = |
| 181 | simulated_event_loop_factory.MakeEventLoop("ping"); |
| 182 | |
| 183 | auto timer = event_loop->AddTimer([&]() { |
| 184 | EXPECT_DEATH( |
| 185 | { |
| 186 | ::std::unique_ptr<EventLoop> event_loop2 = |
| 187 | simulated_event_loop_factory.MakeEventLoop("ping"); |
| 188 | }, |
| 189 | "event loop while running"); |
| 190 | simulated_event_loop_factory.Exit(); |
| 191 | }); |
| 192 | |
| 193 | event_loop->OnRun([&event_loop, &timer] { |
| 194 | timer->Setup(event_loop->monotonic_now() + chrono::milliseconds(50)); |
| 195 | }); |
| 196 | |
| 197 | simulated_event_loop_factory.Run(); |
| 198 | } |
| 199 | |
| 200 | // Test that creating a watcher after running dies. |
| 201 | TEST(SimulatedEventLoopDeathTest, MakeWatcherAfterRunning) { |
| 202 | SimulatedEventLoopTestFactory factory; |
| 203 | |
| 204 | SimulatedEventLoopFactory simulated_event_loop_factory( |
| 205 | factory.configuration()); |
| 206 | |
| 207 | ::std::unique_ptr<EventLoop> event_loop = |
| 208 | simulated_event_loop_factory.MakeEventLoop("ping"); |
| 209 | |
| 210 | simulated_event_loop_factory.RunFor(chrono::seconds(1)); |
| 211 | |
| 212 | EXPECT_DEATH( |
| 213 | { MessageCounter<TestMessage> counter(event_loop.get(), "/test"); }, |
| 214 | "Can't add a watcher after running"); |
| 215 | |
| 216 | ::std::unique_ptr<EventLoop> event_loop2 = |
| 217 | simulated_event_loop_factory.MakeEventLoop("ping"); |
| 218 | |
| 219 | simulated_event_loop_factory.RunFor(chrono::seconds(1)); |
| 220 | |
| 221 | EXPECT_DEATH( |
| 222 | { MessageCounter<TestMessage> counter(event_loop2.get(), "/test"); }, |
| 223 | "Can't add a watcher after running"); |
| 224 | } |
| 225 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 226 | // Test that running for a time period with no handlers causes time to progress |
| 227 | // correctly. |
| 228 | TEST(SimulatedEventLoopTest, RunForNoHandlers) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 229 | SimulatedEventLoopTestFactory factory; |
| 230 | |
| 231 | SimulatedEventLoopFactory simulated_event_loop_factory( |
| 232 | factory.configuration()); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 233 | ::std::unique_ptr<EventLoop> event_loop = |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 234 | simulated_event_loop_factory.MakeEventLoop("loop"); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 235 | |
| 236 | simulated_event_loop_factory.RunFor(chrono::seconds(1)); |
| 237 | |
| 238 | EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1), |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 239 | event_loop->monotonic_now()); |
| 240 | } |
| 241 | |
| 242 | // Test that running for a time with a periodic handler causes time to end |
| 243 | // correctly. |
| 244 | TEST(SimulatedEventLoopTest, RunForTimerHandler) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 245 | SimulatedEventLoopTestFactory factory; |
| 246 | |
| 247 | SimulatedEventLoopFactory simulated_event_loop_factory( |
| 248 | factory.configuration()); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 249 | ::std::unique_ptr<EventLoop> event_loop = |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 250 | simulated_event_loop_factory.MakeEventLoop("loop"); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 251 | |
| 252 | int counter = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 253 | auto timer = event_loop->AddTimer([&counter]() { ++counter; }); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 254 | event_loop->OnRun([&event_loop, &timer] { |
| 255 | timer->Setup(event_loop->monotonic_now() + chrono::milliseconds(50), |
| 256 | chrono::milliseconds(100)); |
| 257 | }); |
| 258 | |
| 259 | simulated_event_loop_factory.RunFor(chrono::seconds(1)); |
| 260 | |
| 261 | EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1), |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 262 | event_loop->monotonic_now()); |
| 263 | EXPECT_EQ(counter, 10); |
| 264 | } |
| 265 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 266 | // Tests that watchers have latency in simulation. |
| 267 | TEST(SimulatedEventLoopTest, WatcherTimingReport) { |
| 268 | SimulatedEventLoopTestFactory factory; |
| 269 | factory.set_send_delay(std::chrono::microseconds(50)); |
| 270 | |
| 271 | FLAGS_timing_report_ms = 1000; |
| 272 | auto loop1 = factory.MakePrimary("primary"); |
| 273 | loop1->MakeWatcher("/test", [](const TestMessage &) {}); |
| 274 | |
| 275 | auto loop2 = factory.Make("sender_loop"); |
| 276 | |
| 277 | auto loop3 = factory.Make("report_fetcher"); |
| 278 | |
| 279 | Fetcher<timing::Report> report_fetcher = |
| 280 | loop3->MakeFetcher<timing::Report>("/aos"); |
| 281 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 282 | |
| 283 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 284 | |
| 285 | // Send 10 messages in the middle of a timing report period so we get |
| 286 | // something interesting back. |
| 287 | auto test_timer = loop2->AddTimer([&sender]() { |
| 288 | for (int i = 0; i < 10; ++i) { |
| 289 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 290 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 291 | builder.add_value(200 + i); |
| 292 | ASSERT_TRUE(msg.Send(builder.Finish())); |
| 293 | } |
| 294 | }); |
| 295 | |
| 296 | // Quit after 1 timing report, mid way through the next cycle. |
| 297 | { |
| 298 | auto end_timer = loop1->AddTimer([&factory]() { factory.Exit(); }); |
| 299 | end_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(2500)); |
| 300 | end_timer->set_name("end"); |
| 301 | } |
| 302 | |
| 303 | loop1->OnRun([&test_timer, &loop1]() { |
| 304 | test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1500)); |
| 305 | }); |
| 306 | |
| 307 | factory.Run(); |
| 308 | |
| 309 | // And, since we are here, check that the timing report makes sense. |
| 310 | // Start by looking for our event loop's timing. |
| 311 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 312 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 313 | while (report_fetcher.FetchNext()) { |
| 314 | LOG(INFO) << "Report " << FlatbufferToJson(report_fetcher.get()); |
| 315 | if (report_fetcher->name()->string_view() == "primary") { |
| 316 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | // Check the watcher report. |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 321 | VLOG(1) << FlatbufferToJson(primary_report, {.multi_line = true}); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 322 | |
| 323 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 324 | |
| 325 | // Just the timing report timer. |
| 326 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 327 | EXPECT_EQ(primary_report.message().timers()->size(), 2); |
| 328 | |
| 329 | // No phased loops |
| 330 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 331 | |
| 332 | // And now confirm that the watcher received all 10 messages, and has latency. |
| 333 | ASSERT_NE(primary_report.message().watchers(), nullptr); |
| 334 | ASSERT_EQ(primary_report.message().watchers()->size(), 1); |
| 335 | EXPECT_EQ(primary_report.message().watchers()->Get(0)->count(), 10); |
| 336 | EXPECT_NEAR( |
| 337 | primary_report.message().watchers()->Get(0)->wakeup_latency()->average(), |
| 338 | 0.00005, 1e-9); |
| 339 | EXPECT_NEAR( |
| 340 | primary_report.message().watchers()->Get(0)->wakeup_latency()->min(), |
| 341 | 0.00005, 1e-9); |
| 342 | EXPECT_NEAR( |
| 343 | primary_report.message().watchers()->Get(0)->wakeup_latency()->max(), |
| 344 | 0.00005, 1e-9); |
| 345 | EXPECT_EQ(primary_report.message() |
| 346 | .watchers() |
| 347 | ->Get(0) |
| 348 | ->wakeup_latency() |
| 349 | ->standard_deviation(), |
| 350 | 0.0); |
| 351 | |
| 352 | EXPECT_EQ( |
| 353 | primary_report.message().watchers()->Get(0)->handler_time()->average(), |
| 354 | 0.0); |
| 355 | EXPECT_EQ(primary_report.message().watchers()->Get(0)->handler_time()->min(), |
| 356 | 0.0); |
| 357 | EXPECT_EQ(primary_report.message().watchers()->Get(0)->handler_time()->max(), |
| 358 | 0.0); |
| 359 | EXPECT_EQ(primary_report.message() |
| 360 | .watchers() |
| 361 | ->Get(0) |
| 362 | ->handler_time() |
| 363 | ->standard_deviation(), |
| 364 | 0.0); |
| 365 | } |
| 366 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 367 | // Tests that ping and pong work when on 2 different nodes, and the message |
| 368 | // gateway messages are sent out as expected. |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 369 | TEST(SimulatedEventLoopTest, MultinodePingPong) { |
| 370 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Brian Silverman | 28d1430 | 2020-09-18 15:26:17 -0700 | [diff] [blame] | 371 | aos::configuration::ReadConfig(ConfigPrefix() + |
| 372 | "events/multinode_pingpong_config.json"); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 373 | const Node *pi1 = configuration::GetNode(&config.message(), "pi1"); |
| 374 | const Node *pi2 = configuration::GetNode(&config.message(), "pi2"); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 375 | const Node *pi3 = configuration::GetNode(&config.message(), "pi3"); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 376 | |
| 377 | SimulatedEventLoopFactory simulated_event_loop_factory(&config.message()); |
| 378 | |
| 379 | std::unique_ptr<EventLoop> ping_event_loop = |
| 380 | simulated_event_loop_factory.MakeEventLoop("ping", pi1); |
| 381 | Ping ping(ping_event_loop.get()); |
| 382 | |
| 383 | std::unique_ptr<EventLoop> pong_event_loop = |
| 384 | simulated_event_loop_factory.MakeEventLoop("pong", pi2); |
| 385 | Pong pong(pong_event_loop.get()); |
| 386 | |
| 387 | std::unique_ptr<EventLoop> pi2_pong_counter_event_loop = |
| 388 | simulated_event_loop_factory.MakeEventLoop("pi2_pong_counter", pi2); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 389 | MessageCounter<examples::Pong> pi2_pong_counter( |
| 390 | pi2_pong_counter_event_loop.get(), "/test"); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 391 | aos::Fetcher<message_bridge::Timestamp> pi1_on_pi2_timestamp_fetcher = |
| 392 | pi2_pong_counter_event_loop->MakeFetcher<message_bridge::Timestamp>( |
| 393 | "/pi1/aos"); |
| 394 | aos::Fetcher<examples::Ping> ping_on_pi2_fetcher = |
| 395 | pi2_pong_counter_event_loop->MakeFetcher<examples::Ping>("/test"); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 396 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 397 | std::unique_ptr<EventLoop> pi3_pong_counter_event_loop = |
| 398 | simulated_event_loop_factory.MakeEventLoop("pi3_pong_counter", pi3); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 399 | |
| 400 | std::unique_ptr<EventLoop> pi1_pong_counter_event_loop = |
| 401 | simulated_event_loop_factory.MakeEventLoop("pi1_pong_counter", pi1); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 402 | MessageCounter<examples::Pong> pi1_pong_counter( |
| 403 | pi1_pong_counter_event_loop.get(), "/test"); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 404 | aos::Fetcher<examples::Ping> ping_on_pi1_fetcher = |
| 405 | pi1_pong_counter_event_loop->MakeFetcher<examples::Ping>("/test"); |
| 406 | aos::Fetcher<message_bridge::Timestamp> pi1_on_pi1_timestamp_fetcher = |
| 407 | pi1_pong_counter_event_loop->MakeFetcher<message_bridge::Timestamp>( |
| 408 | "/aos"); |
| 409 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 410 | // Count timestamps. |
| 411 | MessageCounter<message_bridge::Timestamp> pi1_on_pi1_timestamp_counter( |
| 412 | pi1_pong_counter_event_loop.get(), "/pi1/aos"); |
| 413 | MessageCounter<message_bridge::Timestamp> pi1_on_pi2_timestamp_counter( |
| 414 | pi2_pong_counter_event_loop.get(), "/pi1/aos"); |
| 415 | MessageCounter<message_bridge::Timestamp> pi1_on_pi3_timestamp_counter( |
| 416 | pi3_pong_counter_event_loop.get(), "/pi1/aos"); |
| 417 | MessageCounter<message_bridge::Timestamp> pi2_on_pi1_timestamp_counter( |
| 418 | pi1_pong_counter_event_loop.get(), "/pi2/aos"); |
| 419 | MessageCounter<message_bridge::Timestamp> pi2_on_pi2_timestamp_counter( |
| 420 | pi2_pong_counter_event_loop.get(), "/pi2/aos"); |
| 421 | MessageCounter<message_bridge::Timestamp> pi3_on_pi1_timestamp_counter( |
| 422 | pi1_pong_counter_event_loop.get(), "/pi3/aos"); |
| 423 | MessageCounter<message_bridge::Timestamp> pi3_on_pi3_timestamp_counter( |
| 424 | pi3_pong_counter_event_loop.get(), "/pi3/aos"); |
| 425 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 426 | // Count remote timestamps |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 427 | MessageCounter<RemoteMessage> remote_timestamps_pi2_on_pi1( |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 428 | pi1_pong_counter_event_loop.get(), "/aos/remote_timestamps/pi2"); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 429 | MessageCounter<RemoteMessage> remote_timestamps_pi1_on_pi2( |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 430 | pi2_pong_counter_event_loop.get(), "/aos/remote_timestamps/pi1"); |
| 431 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 432 | // Wait to let timestamp estimation start up before looking for the results. |
| 433 | simulated_event_loop_factory.RunFor(chrono::milliseconds(500)); |
| 434 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 435 | std::unique_ptr<EventLoop> pi1_statistics_counter_event_loop = |
| 436 | simulated_event_loop_factory.MakeEventLoop("pi1_statistics_counter", pi1); |
| 437 | std::unique_ptr<EventLoop> pi2_statistics_counter_event_loop = |
| 438 | simulated_event_loop_factory.MakeEventLoop("pi2_statistics_counter", pi2); |
| 439 | std::unique_ptr<EventLoop> pi3_statistics_counter_event_loop = |
| 440 | simulated_event_loop_factory.MakeEventLoop("pi3_statistics_counter", pi3); |
| 441 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 442 | int pi1_server_statistics_count = 0; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 443 | pi1_statistics_counter_event_loop->MakeWatcher( |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 444 | "/pi1/aos", [&pi1_server_statistics_count]( |
| 445 | const message_bridge::ServerStatistics &stats) { |
| 446 | VLOG(1) << "pi1 ServerStatistics " << FlatbufferToJson(&stats); |
| 447 | EXPECT_EQ(stats.connections()->size(), 2u); |
| 448 | for (const message_bridge::ServerConnection *connection : |
| 449 | *stats.connections()) { |
| 450 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 451 | EXPECT_TRUE(connection->has_boot_uuid()); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 452 | if (connection->node()->name()->string_view() == "pi2") { |
| 453 | EXPECT_GT(connection->sent_packets(), 50); |
| 454 | } else if (connection->node()->name()->string_view() == "pi3") { |
| 455 | EXPECT_GE(connection->sent_packets(), 5); |
| 456 | } else { |
| 457 | LOG(FATAL) << "Unknown connection"; |
| 458 | } |
| 459 | |
| 460 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 461 | EXPECT_EQ(connection->monotonic_offset(), 0); |
| 462 | } |
| 463 | ++pi1_server_statistics_count; |
| 464 | }); |
| 465 | |
| 466 | int pi2_server_statistics_count = 0; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 467 | pi2_statistics_counter_event_loop->MakeWatcher( |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 468 | "/pi2/aos", [&pi2_server_statistics_count]( |
| 469 | const message_bridge::ServerStatistics &stats) { |
| 470 | VLOG(1) << "pi2 ServerStatistics " << FlatbufferToJson(&stats); |
| 471 | EXPECT_EQ(stats.connections()->size(), 1u); |
| 472 | |
| 473 | const message_bridge::ServerConnection *connection = |
| 474 | stats.connections()->Get(0); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 475 | EXPECT_TRUE(connection->has_boot_uuid()); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 476 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
| 477 | EXPECT_GT(connection->sent_packets(), 50); |
| 478 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 479 | EXPECT_EQ(connection->monotonic_offset(), 0); |
| 480 | ++pi2_server_statistics_count; |
| 481 | }); |
| 482 | |
| 483 | int pi3_server_statistics_count = 0; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 484 | pi3_statistics_counter_event_loop->MakeWatcher( |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 485 | "/pi3/aos", [&pi3_server_statistics_count]( |
| 486 | const message_bridge::ServerStatistics &stats) { |
| 487 | VLOG(1) << "pi3 ServerStatistics " << FlatbufferToJson(&stats); |
| 488 | EXPECT_EQ(stats.connections()->size(), 1u); |
| 489 | |
| 490 | const message_bridge::ServerConnection *connection = |
| 491 | stats.connections()->Get(0); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 492 | EXPECT_TRUE(connection->has_boot_uuid()); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 493 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
| 494 | EXPECT_GE(connection->sent_packets(), 5); |
| 495 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 496 | EXPECT_EQ(connection->monotonic_offset(), 0); |
| 497 | ++pi3_server_statistics_count; |
| 498 | }); |
| 499 | |
| 500 | int pi1_client_statistics_count = 0; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 501 | pi1_statistics_counter_event_loop->MakeWatcher( |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 502 | "/pi1/aos", [&pi1_client_statistics_count]( |
| 503 | const message_bridge::ClientStatistics &stats) { |
| 504 | VLOG(1) << "pi1 ClientStatistics " << FlatbufferToJson(&stats); |
| 505 | EXPECT_EQ(stats.connections()->size(), 2u); |
| 506 | |
| 507 | for (const message_bridge::ClientConnection *connection : |
| 508 | *stats.connections()) { |
| 509 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
| 510 | if (connection->node()->name()->string_view() == "pi2") { |
| 511 | EXPECT_GT(connection->received_packets(), 50); |
| 512 | } else if (connection->node()->name()->string_view() == "pi3") { |
| 513 | EXPECT_GE(connection->received_packets(), 5); |
| 514 | } else { |
| 515 | LOG(FATAL) << "Unknown connection"; |
| 516 | } |
| 517 | |
| 518 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 519 | EXPECT_EQ(connection->monotonic_offset(), 150000); |
| 520 | } |
| 521 | ++pi1_client_statistics_count; |
| 522 | }); |
| 523 | |
| 524 | int pi2_client_statistics_count = 0; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 525 | pi2_statistics_counter_event_loop->MakeWatcher( |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 526 | "/pi2/aos", [&pi2_client_statistics_count]( |
| 527 | const message_bridge::ClientStatistics &stats) { |
| 528 | VLOG(1) << "pi2 ClientStatistics " << FlatbufferToJson(&stats); |
| 529 | EXPECT_EQ(stats.connections()->size(), 1u); |
| 530 | |
| 531 | const message_bridge::ClientConnection *connection = |
| 532 | stats.connections()->Get(0); |
| 533 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
| 534 | EXPECT_GT(connection->received_packets(), 50); |
| 535 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 536 | EXPECT_EQ(connection->monotonic_offset(), 150000); |
| 537 | ++pi2_client_statistics_count; |
| 538 | }); |
| 539 | |
| 540 | int pi3_client_statistics_count = 0; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 541 | pi3_statistics_counter_event_loop->MakeWatcher( |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 542 | "/pi3/aos", [&pi3_client_statistics_count]( |
| 543 | const message_bridge::ClientStatistics &stats) { |
| 544 | VLOG(1) << "pi3 ClientStatistics " << FlatbufferToJson(&stats); |
| 545 | EXPECT_EQ(stats.connections()->size(), 1u); |
| 546 | |
| 547 | const message_bridge::ClientConnection *connection = |
| 548 | stats.connections()->Get(0); |
| 549 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
| 550 | EXPECT_GE(connection->received_packets(), 5); |
| 551 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 552 | EXPECT_EQ(connection->monotonic_offset(), 150000); |
| 553 | ++pi3_client_statistics_count; |
| 554 | }); |
| 555 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 556 | // Find the channel index for both the /pi1/aos Timestamp channel and Ping |
| 557 | // channel. |
| 558 | const size_t pi1_timestamp_channel = |
| 559 | configuration::ChannelIndex(pi1_pong_counter_event_loop->configuration(), |
| 560 | pi1_on_pi2_timestamp_fetcher.channel()); |
| 561 | const size_t ping_timestamp_channel = |
| 562 | configuration::ChannelIndex(pi1_pong_counter_event_loop->configuration(), |
| 563 | ping_on_pi2_fetcher.channel()); |
| 564 | |
| 565 | for (const Channel *channel : |
| 566 | *pi1_pong_counter_event_loop->configuration()->channels()) { |
| 567 | VLOG(1) << "Channel " |
| 568 | << configuration::ChannelIndex( |
| 569 | pi1_pong_counter_event_loop->configuration(), channel) |
| 570 | << " " << configuration::CleanedChannelToString(channel); |
| 571 | } |
| 572 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 573 | std::unique_ptr<EventLoop> pi1_remote_timestamp = |
| 574 | simulated_event_loop_factory.MakeEventLoop("pi1_remote_timestamp", pi1); |
| 575 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 576 | // For each remote timestamp we get back, confirm that it is either a ping |
| 577 | // message, or a timestamp we sent out. Also confirm that the timestamps are |
| 578 | // correct. |
| 579 | pi1_remote_timestamp->MakeWatcher( |
| 580 | "/pi1/aos/remote_timestamps/pi2", |
| 581 | [pi1_timestamp_channel, ping_timestamp_channel, &ping_on_pi2_fetcher, |
| 582 | &ping_on_pi1_fetcher, &pi1_on_pi2_timestamp_fetcher, |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 583 | &pi1_on_pi1_timestamp_fetcher, &simulated_event_loop_factory, |
| 584 | pi2](const RemoteMessage &header) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 585 | VLOG(1) << aos::FlatbufferToJson(&header); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 586 | EXPECT_TRUE(header.has_boot_uuid()); |
| 587 | EXPECT_EQ(header.boot_uuid()->string_view(), |
| 588 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi2) |
| 589 | ->boot_uuid() |
| 590 | .string_view()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 591 | |
| 592 | const aos::monotonic_clock::time_point header_monotonic_sent_time( |
| 593 | chrono::nanoseconds(header.monotonic_sent_time())); |
| 594 | const aos::realtime_clock::time_point header_realtime_sent_time( |
| 595 | chrono::nanoseconds(header.realtime_sent_time())); |
| 596 | const aos::monotonic_clock::time_point header_monotonic_remote_time( |
| 597 | chrono::nanoseconds(header.monotonic_remote_time())); |
| 598 | const aos::realtime_clock::time_point header_realtime_remote_time( |
| 599 | chrono::nanoseconds(header.realtime_remote_time())); |
| 600 | |
| 601 | const Context *pi1_context = nullptr; |
| 602 | const Context *pi2_context = nullptr; |
| 603 | |
| 604 | if (header.channel_index() == pi1_timestamp_channel) { |
| 605 | // Find the forwarded message. |
| 606 | while (pi1_on_pi2_timestamp_fetcher.context().monotonic_event_time < |
| 607 | header_monotonic_sent_time) { |
| 608 | ASSERT_TRUE(pi1_on_pi2_timestamp_fetcher.FetchNext()); |
| 609 | } |
| 610 | |
| 611 | // And the source message. |
| 612 | while (pi1_on_pi1_timestamp_fetcher.context().monotonic_event_time < |
| 613 | header_monotonic_remote_time) { |
| 614 | ASSERT_TRUE(pi1_on_pi1_timestamp_fetcher.FetchNext()); |
| 615 | } |
| 616 | |
| 617 | pi1_context = &pi1_on_pi1_timestamp_fetcher.context(); |
| 618 | pi2_context = &pi1_on_pi2_timestamp_fetcher.context(); |
| 619 | } else if (header.channel_index() == ping_timestamp_channel) { |
| 620 | // Find the forwarded message. |
| 621 | while (ping_on_pi2_fetcher.context().monotonic_event_time < |
| 622 | header_monotonic_sent_time) { |
| 623 | ASSERT_TRUE(ping_on_pi2_fetcher.FetchNext()); |
| 624 | } |
| 625 | |
| 626 | // And the source message. |
| 627 | while (ping_on_pi1_fetcher.context().monotonic_event_time < |
| 628 | header_monotonic_remote_time) { |
| 629 | ASSERT_TRUE(ping_on_pi1_fetcher.FetchNext()); |
| 630 | } |
| 631 | |
| 632 | pi1_context = &ping_on_pi1_fetcher.context(); |
| 633 | pi2_context = &ping_on_pi2_fetcher.context(); |
| 634 | } else { |
| 635 | LOG(FATAL) << "Unknown channel"; |
| 636 | } |
| 637 | |
| 638 | // Confirm the forwarded message has matching timestamps to the |
| 639 | // timestamps we got back. |
| 640 | EXPECT_EQ(pi2_context->queue_index, header.queue_index()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 641 | EXPECT_EQ(pi2_context->remote_queue_index, header.remote_queue_index()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 642 | EXPECT_EQ(pi2_context->monotonic_event_time, |
| 643 | header_monotonic_sent_time); |
| 644 | EXPECT_EQ(pi2_context->realtime_event_time, header_realtime_sent_time); |
| 645 | EXPECT_EQ(pi2_context->realtime_remote_time, |
| 646 | header_realtime_remote_time); |
| 647 | EXPECT_EQ(pi2_context->monotonic_remote_time, |
| 648 | header_monotonic_remote_time); |
| 649 | |
| 650 | // Confirm the forwarded message also matches the source message. |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 651 | EXPECT_EQ(pi1_context->queue_index, header.remote_queue_index()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 652 | EXPECT_EQ(pi1_context->monotonic_event_time, |
| 653 | header_monotonic_remote_time); |
| 654 | EXPECT_EQ(pi1_context->realtime_event_time, |
| 655 | header_realtime_remote_time); |
| 656 | }); |
| 657 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 658 | simulated_event_loop_factory.RunFor(chrono::seconds(10) - |
| 659 | chrono::milliseconds(500) + |
| 660 | chrono::milliseconds(5)); |
| 661 | |
| 662 | EXPECT_EQ(pi1_pong_counter.count(), 1001); |
| 663 | EXPECT_EQ(pi2_pong_counter.count(), 1001); |
| 664 | |
| 665 | EXPECT_EQ(pi1_on_pi1_timestamp_counter.count(), 100); |
| 666 | EXPECT_EQ(pi1_on_pi2_timestamp_counter.count(), 100); |
| 667 | EXPECT_EQ(pi1_on_pi3_timestamp_counter.count(), 100); |
| 668 | EXPECT_EQ(pi2_on_pi1_timestamp_counter.count(), 100); |
| 669 | EXPECT_EQ(pi2_on_pi2_timestamp_counter.count(), 100); |
| 670 | EXPECT_EQ(pi3_on_pi1_timestamp_counter.count(), 100); |
| 671 | EXPECT_EQ(pi3_on_pi3_timestamp_counter.count(), 100); |
| 672 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 673 | EXPECT_EQ(pi1_server_statistics_count, 10); |
| 674 | EXPECT_EQ(pi2_server_statistics_count, 10); |
| 675 | EXPECT_EQ(pi3_server_statistics_count, 10); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 676 | |
| 677 | EXPECT_EQ(pi1_client_statistics_count, 95); |
| 678 | EXPECT_EQ(pi2_client_statistics_count, 95); |
| 679 | EXPECT_EQ(pi3_client_statistics_count, 95); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 680 | |
| 681 | // Also confirm that remote timestamps are being forwarded correctly. |
| 682 | EXPECT_EQ(remote_timestamps_pi2_on_pi1.count(), 1101); |
| 683 | EXPECT_EQ(remote_timestamps_pi1_on_pi2.count(), 1101); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | // Tests that an offset between nodes can be recovered and shows up in |
| 687 | // ServerStatistics correctly. |
| 688 | TEST(SimulatedEventLoopTest, MultinodePingPongWithOffset) { |
| 689 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Brian Silverman | 28d1430 | 2020-09-18 15:26:17 -0700 | [diff] [blame] | 690 | aos::configuration::ReadConfig(ConfigPrefix() + |
| 691 | "events/multinode_pingpong_config.json"); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 692 | const Node *pi1 = configuration::GetNode(&config.message(), "pi1"); |
| 693 | const Node *pi2 = configuration::GetNode(&config.message(), "pi2"); |
| 694 | const Node *pi3 = configuration::GetNode(&config.message(), "pi3"); |
| 695 | |
| 696 | SimulatedEventLoopFactory simulated_event_loop_factory(&config.message()); |
| 697 | NodeEventLoopFactory *pi2_factory = |
| 698 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi2); |
| 699 | |
| 700 | constexpr chrono::milliseconds kOffset{1501}; |
| 701 | pi2_factory->SetDistributedOffset(kOffset, 1.0); |
| 702 | |
| 703 | std::unique_ptr<EventLoop> ping_event_loop = |
| 704 | simulated_event_loop_factory.MakeEventLoop("ping", pi1); |
| 705 | Ping ping(ping_event_loop.get()); |
| 706 | |
| 707 | std::unique_ptr<EventLoop> pong_event_loop = |
| 708 | simulated_event_loop_factory.MakeEventLoop("pong", pi2); |
| 709 | Pong pong(pong_event_loop.get()); |
| 710 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 711 | // Wait to let timestamp estimation start up before looking for the results. |
| 712 | simulated_event_loop_factory.RunFor(chrono::milliseconds(500)); |
| 713 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 714 | std::unique_ptr<EventLoop> pi2_pong_counter_event_loop = |
| 715 | simulated_event_loop_factory.MakeEventLoop("pi2_pong_counter", pi2); |
| 716 | |
| 717 | std::unique_ptr<EventLoop> pi3_pong_counter_event_loop = |
| 718 | simulated_event_loop_factory.MakeEventLoop("pi3_pong_counter", pi3); |
| 719 | |
| 720 | std::unique_ptr<EventLoop> pi1_pong_counter_event_loop = |
| 721 | simulated_event_loop_factory.MakeEventLoop("pi1_pong_counter", pi1); |
| 722 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 723 | // Confirm the offsets are being recovered correctly. |
| 724 | int pi1_server_statistics_count = 0; |
| 725 | pi1_pong_counter_event_loop->MakeWatcher( |
| 726 | "/pi1/aos", [&pi1_server_statistics_count, |
| 727 | kOffset](const message_bridge::ServerStatistics &stats) { |
| 728 | VLOG(1) << "pi1 ServerStatistics " << FlatbufferToJson(&stats); |
| 729 | EXPECT_EQ(stats.connections()->size(), 2u); |
| 730 | for (const message_bridge::ServerConnection *connection : |
| 731 | *stats.connections()) { |
| 732 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 733 | EXPECT_TRUE(connection->has_boot_uuid()); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 734 | if (connection->node()->name()->string_view() == "pi2") { |
| 735 | EXPECT_EQ(connection->monotonic_offset(), |
| 736 | chrono::nanoseconds(kOffset).count()); |
| 737 | } else if (connection->node()->name()->string_view() == "pi3") { |
| 738 | EXPECT_EQ(connection->monotonic_offset(), 0); |
| 739 | } else { |
| 740 | LOG(FATAL) << "Unknown connection"; |
| 741 | } |
| 742 | |
| 743 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 744 | } |
| 745 | ++pi1_server_statistics_count; |
| 746 | }); |
| 747 | |
| 748 | int pi2_server_statistics_count = 0; |
| 749 | pi2_pong_counter_event_loop->MakeWatcher( |
| 750 | "/pi2/aos", [&pi2_server_statistics_count, |
| 751 | kOffset](const message_bridge::ServerStatistics &stats) { |
| 752 | VLOG(1) << "pi2 ServerStatistics " << FlatbufferToJson(&stats); |
| 753 | EXPECT_EQ(stats.connections()->size(), 1u); |
| 754 | |
| 755 | const message_bridge::ServerConnection *connection = |
| 756 | stats.connections()->Get(0); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 757 | EXPECT_TRUE(connection->has_boot_uuid()); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 758 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
| 759 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 760 | EXPECT_EQ(connection->monotonic_offset(), |
| 761 | -chrono::nanoseconds(kOffset).count()); |
| 762 | ++pi2_server_statistics_count; |
| 763 | }); |
| 764 | |
| 765 | int pi3_server_statistics_count = 0; |
| 766 | pi3_pong_counter_event_loop->MakeWatcher( |
| 767 | "/pi3/aos", [&pi3_server_statistics_count]( |
| 768 | const message_bridge::ServerStatistics &stats) { |
| 769 | VLOG(1) << "pi3 ServerStatistics " << FlatbufferToJson(&stats); |
| 770 | EXPECT_EQ(stats.connections()->size(), 1u); |
| 771 | |
| 772 | const message_bridge::ServerConnection *connection = |
| 773 | stats.connections()->Get(0); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 774 | EXPECT_TRUE(connection->has_boot_uuid()); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 775 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
| 776 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 777 | EXPECT_EQ(connection->monotonic_offset(), 0); |
| 778 | ++pi3_server_statistics_count; |
| 779 | }); |
| 780 | |
| 781 | simulated_event_loop_factory.RunFor(chrono::seconds(10) - |
| 782 | chrono::milliseconds(500) + |
| 783 | chrono::milliseconds(5)); |
| 784 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 785 | EXPECT_EQ(pi1_server_statistics_count, 10); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 786 | EXPECT_EQ(pi2_server_statistics_count, 9); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 787 | EXPECT_EQ(pi3_server_statistics_count, 10); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | // Test that disabling statistics actually disables them. |
| 791 | TEST(SimulatedEventLoopTest, MultinodeWithoutStatistics) { |
| 792 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Brian Silverman | 28d1430 | 2020-09-18 15:26:17 -0700 | [diff] [blame] | 793 | aos::configuration::ReadConfig(ConfigPrefix() + |
| 794 | "events/multinode_pingpong_config.json"); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 795 | const Node *pi1 = configuration::GetNode(&config.message(), "pi1"); |
| 796 | const Node *pi2 = configuration::GetNode(&config.message(), "pi2"); |
| 797 | const Node *pi3 = configuration::GetNode(&config.message(), "pi3"); |
| 798 | |
| 799 | SimulatedEventLoopFactory simulated_event_loop_factory(&config.message()); |
| 800 | simulated_event_loop_factory.DisableStatistics(); |
| 801 | |
| 802 | std::unique_ptr<EventLoop> ping_event_loop = |
| 803 | simulated_event_loop_factory.MakeEventLoop("ping", pi1); |
| 804 | Ping ping(ping_event_loop.get()); |
| 805 | |
| 806 | std::unique_ptr<EventLoop> pong_event_loop = |
| 807 | simulated_event_loop_factory.MakeEventLoop("pong", pi2); |
| 808 | Pong pong(pong_event_loop.get()); |
| 809 | |
| 810 | std::unique_ptr<EventLoop> pi2_pong_counter_event_loop = |
| 811 | simulated_event_loop_factory.MakeEventLoop("pi2_pong_counter", pi2); |
| 812 | |
| 813 | MessageCounter<examples::Pong> pi2_pong_counter( |
| 814 | pi2_pong_counter_event_loop.get(), "/test"); |
| 815 | |
| 816 | std::unique_ptr<EventLoop> pi3_pong_counter_event_loop = |
| 817 | simulated_event_loop_factory.MakeEventLoop("pi3_pong_counter", pi3); |
| 818 | |
| 819 | std::unique_ptr<EventLoop> pi1_pong_counter_event_loop = |
| 820 | simulated_event_loop_factory.MakeEventLoop("pi1_pong_counter", pi1); |
| 821 | |
| 822 | MessageCounter<examples::Pong> pi1_pong_counter( |
| 823 | pi1_pong_counter_event_loop.get(), "/test"); |
| 824 | |
| 825 | // Count timestamps. |
| 826 | MessageCounter<message_bridge::Timestamp> pi1_on_pi1_timestamp_counter( |
| 827 | pi1_pong_counter_event_loop.get(), "/pi1/aos"); |
| 828 | MessageCounter<message_bridge::Timestamp> pi1_on_pi2_timestamp_counter( |
| 829 | pi2_pong_counter_event_loop.get(), "/pi1/aos"); |
| 830 | MessageCounter<message_bridge::Timestamp> pi1_on_pi3_timestamp_counter( |
| 831 | pi3_pong_counter_event_loop.get(), "/pi1/aos"); |
| 832 | MessageCounter<message_bridge::Timestamp> pi2_on_pi1_timestamp_counter( |
| 833 | pi1_pong_counter_event_loop.get(), "/pi2/aos"); |
| 834 | MessageCounter<message_bridge::Timestamp> pi2_on_pi2_timestamp_counter( |
| 835 | pi2_pong_counter_event_loop.get(), "/pi2/aos"); |
| 836 | MessageCounter<message_bridge::Timestamp> pi3_on_pi1_timestamp_counter( |
| 837 | pi1_pong_counter_event_loop.get(), "/pi3/aos"); |
| 838 | MessageCounter<message_bridge::Timestamp> pi3_on_pi3_timestamp_counter( |
| 839 | pi3_pong_counter_event_loop.get(), "/pi3/aos"); |
| 840 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 841 | // Count remote timestamps |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 842 | MessageCounter<RemoteMessage> remote_timestamps_pi2_on_pi1( |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 843 | pi1_pong_counter_event_loop.get(), "/aos/remote_timestamps/pi2"); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 844 | MessageCounter<RemoteMessage> remote_timestamps_pi1_on_pi2( |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 845 | pi2_pong_counter_event_loop.get(), "/aos/remote_timestamps/pi1"); |
| 846 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 847 | MessageCounter<message_bridge::ServerStatistics> |
| 848 | pi1_server_statistics_counter(pi1_pong_counter_event_loop.get(), |
| 849 | "/pi1/aos"); |
| 850 | MessageCounter<message_bridge::ServerStatistics> |
| 851 | pi2_server_statistics_counter(pi2_pong_counter_event_loop.get(), |
| 852 | "/pi2/aos"); |
| 853 | MessageCounter<message_bridge::ServerStatistics> |
| 854 | pi3_server_statistics_counter(pi3_pong_counter_event_loop.get(), |
| 855 | "/pi3/aos"); |
| 856 | |
| 857 | MessageCounter<message_bridge::ClientStatistics> |
| 858 | pi1_client_statistics_counter(pi1_pong_counter_event_loop.get(), |
| 859 | "/pi1/aos"); |
| 860 | MessageCounter<message_bridge::ClientStatistics> |
| 861 | pi2_client_statistics_counter(pi2_pong_counter_event_loop.get(), |
| 862 | "/pi2/aos"); |
| 863 | MessageCounter<message_bridge::ClientStatistics> |
| 864 | pi3_client_statistics_counter(pi3_pong_counter_event_loop.get(), |
| 865 | "/pi3/aos"); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 866 | |
| 867 | simulated_event_loop_factory.RunFor(chrono::seconds(10) + |
| 868 | chrono::milliseconds(5)); |
| 869 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 870 | EXPECT_EQ(pi1_pong_counter.count(), 1001u); |
| 871 | EXPECT_EQ(pi2_pong_counter.count(), 1001u); |
| 872 | |
| 873 | EXPECT_EQ(pi1_on_pi1_timestamp_counter.count(), 0u); |
| 874 | EXPECT_EQ(pi1_on_pi2_timestamp_counter.count(), 0u); |
| 875 | EXPECT_EQ(pi1_on_pi3_timestamp_counter.count(), 0u); |
| 876 | EXPECT_EQ(pi2_on_pi1_timestamp_counter.count(), 0u); |
| 877 | EXPECT_EQ(pi2_on_pi2_timestamp_counter.count(), 0u); |
| 878 | EXPECT_EQ(pi3_on_pi1_timestamp_counter.count(), 0u); |
| 879 | EXPECT_EQ(pi3_on_pi3_timestamp_counter.count(), 0u); |
| 880 | |
| 881 | EXPECT_EQ(pi1_server_statistics_counter.count(), 0u); |
| 882 | EXPECT_EQ(pi2_server_statistics_counter.count(), 0u); |
| 883 | EXPECT_EQ(pi3_server_statistics_counter.count(), 0u); |
| 884 | |
| 885 | EXPECT_EQ(pi1_client_statistics_counter.count(), 0u); |
| 886 | EXPECT_EQ(pi2_client_statistics_counter.count(), 0u); |
| 887 | EXPECT_EQ(pi3_client_statistics_counter.count(), 0u); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 888 | |
| 889 | // Also confirm that remote timestamps are being forwarded correctly. |
| 890 | EXPECT_EQ(remote_timestamps_pi2_on_pi1.count(), 1001); |
| 891 | EXPECT_EQ(remote_timestamps_pi1_on_pi2.count(), 1001); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 892 | } |
| 893 | |
Austin Schuh | 2febf0d | 2020-09-21 22:24:30 -0700 | [diff] [blame] | 894 | // Tests that the time offset having a slope doesn't break the world. |
| 895 | // SimulatedMessageBridge has enough self consistency CHECK statements to |
| 896 | // confirm, and we can can also check a message in each direction to make sure |
| 897 | // it gets delivered as expected. |
| 898 | TEST(SimulatedEventLoopTest, MultinodePingPongWithOffsetAndSlope) { |
| 899 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 900 | aos::configuration::ReadConfig(ConfigPrefix() + |
| 901 | "events/multinode_pingpong_config.json"); |
| 902 | const Node *pi1 = configuration::GetNode(&config.message(), "pi1"); |
| 903 | const Node *pi2 = configuration::GetNode(&config.message(), "pi2"); |
| 904 | |
| 905 | SimulatedEventLoopFactory simulated_event_loop_factory(&config.message()); |
| 906 | NodeEventLoopFactory *pi2_factory = |
| 907 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi2); |
| 908 | |
| 909 | // Move the pi far into the future so the slope is significant. And set it to |
| 910 | // something reasonable. |
| 911 | constexpr chrono::milliseconds kOffset{150100}; |
| 912 | pi2_factory->SetDistributedOffset(kOffset, 1.0001); |
| 913 | |
| 914 | std::unique_ptr<EventLoop> ping_event_loop = |
| 915 | simulated_event_loop_factory.MakeEventLoop("ping", pi1); |
| 916 | Ping ping(ping_event_loop.get()); |
| 917 | |
| 918 | std::unique_ptr<EventLoop> pong_event_loop = |
| 919 | simulated_event_loop_factory.MakeEventLoop("pong", pi2); |
| 920 | Pong pong(pong_event_loop.get()); |
| 921 | |
| 922 | std::unique_ptr<EventLoop> pi1_counter_event_loop = |
| 923 | simulated_event_loop_factory.MakeEventLoop("pi1_counter", pi1); |
| 924 | std::unique_ptr<EventLoop> pi2_counter_event_loop = |
| 925 | simulated_event_loop_factory.MakeEventLoop("pi2_counter", pi2); |
| 926 | |
| 927 | aos::Fetcher<examples::Ping> ping_on_pi1_fetcher = |
| 928 | pi1_counter_event_loop->MakeFetcher<examples::Ping>("/test"); |
| 929 | aos::Fetcher<examples::Ping> ping_on_pi2_fetcher = |
| 930 | pi2_counter_event_loop->MakeFetcher<examples::Ping>("/test"); |
| 931 | |
| 932 | aos::Fetcher<examples::Pong> pong_on_pi2_fetcher = |
| 933 | pi2_counter_event_loop->MakeFetcher<examples::Pong>("/test"); |
| 934 | aos::Fetcher<examples::Pong> pong_on_pi1_fetcher = |
| 935 | pi1_counter_event_loop->MakeFetcher<examples::Pong>("/test"); |
| 936 | |
| 937 | // End after a pong message comes back. This will leave the latest messages |
| 938 | // on all channels so we can look at timestamps easily and check they make |
| 939 | // sense. |
| 940 | std::unique_ptr<EventLoop> pi1_pong_ender = |
| 941 | simulated_event_loop_factory.MakeEventLoop("pi2_counter", pi1); |
| 942 | int count = 0; |
| 943 | pi1_pong_ender->MakeWatcher( |
| 944 | "/test", [&simulated_event_loop_factory, &count](const examples::Pong &) { |
| 945 | if (++count == 100) { |
| 946 | simulated_event_loop_factory.Exit(); |
| 947 | } |
| 948 | }); |
| 949 | |
| 950 | // Run enough that messages should be delivered. |
| 951 | simulated_event_loop_factory.Run(); |
| 952 | |
| 953 | // Grab the latest messages. |
| 954 | EXPECT_TRUE(ping_on_pi1_fetcher.Fetch()); |
| 955 | EXPECT_TRUE(ping_on_pi2_fetcher.Fetch()); |
| 956 | EXPECT_TRUE(pong_on_pi1_fetcher.Fetch()); |
| 957 | EXPECT_TRUE(pong_on_pi2_fetcher.Fetch()); |
| 958 | |
| 959 | // Compute their time on the global distributed clock so we can compute |
| 960 | // distance betwen them. |
| 961 | const distributed_clock::time_point pi1_ping_time = |
| 962 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi1) |
| 963 | ->ToDistributedClock( |
| 964 | ping_on_pi1_fetcher.context().monotonic_event_time); |
| 965 | const distributed_clock::time_point pi2_ping_time = |
| 966 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi2) |
| 967 | ->ToDistributedClock( |
| 968 | ping_on_pi2_fetcher.context().monotonic_event_time); |
| 969 | const distributed_clock::time_point pi1_pong_time = |
| 970 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi1) |
| 971 | ->ToDistributedClock( |
| 972 | pong_on_pi1_fetcher.context().monotonic_event_time); |
| 973 | const distributed_clock::time_point pi2_pong_time = |
| 974 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi2) |
| 975 | ->ToDistributedClock( |
| 976 | pong_on_pi2_fetcher.context().monotonic_event_time); |
| 977 | |
| 978 | // And confirm the delivery delay is just about exactly 150 uS for both |
| 979 | // directions like expected. There will be a couple ns of rounding errors in |
| 980 | // the conversion functions that aren't worth accounting for right now. This |
| 981 | // will either be really close, or really far. |
| 982 | EXPECT_GE(pi2_ping_time, chrono::microseconds(150) - chrono::nanoseconds(10) + |
| 983 | pi1_ping_time); |
| 984 | EXPECT_LE(pi2_ping_time, chrono::microseconds(150) + chrono::nanoseconds(10) + |
| 985 | pi1_ping_time); |
| 986 | |
| 987 | EXPECT_GE(pi1_pong_time, chrono::microseconds(150) - chrono::nanoseconds(10) + |
| 988 | pi2_pong_time); |
| 989 | EXPECT_LE(pi1_pong_time, chrono::microseconds(150) + chrono::nanoseconds(10) + |
| 990 | pi2_pong_time); |
| 991 | } |
| 992 | |
Austin Schuh | 4c570ea | 2020-11-19 23:13:24 -0800 | [diff] [blame] | 993 | void SendPing(aos::Sender<examples::Ping> *sender, int value) { |
| 994 | aos::Sender<examples::Ping>::Builder builder = sender->MakeBuilder(); |
| 995 | examples::Ping::Builder ping_builder = builder.MakeBuilder<examples::Ping>(); |
| 996 | ping_builder.add_value(value); |
| 997 | builder.Send(ping_builder.Finish()); |
| 998 | } |
| 999 | |
| 1000 | // Tests that reliable (and unreliable) ping messages get forwarded as expected. |
| 1001 | TEST(SimulatedEventLoopTest, MultinodeStartupTesting) { |
| 1002 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 1003 | aos::configuration::ReadConfig(ConfigPrefix() + |
| 1004 | "events/multinode_pingpong_config.json"); |
| 1005 | const Node *pi1 = configuration::GetNode(&config.message(), "pi1"); |
| 1006 | const Node *pi2 = configuration::GetNode(&config.message(), "pi2"); |
| 1007 | |
| 1008 | SimulatedEventLoopFactory simulated_event_loop_factory(&config.message()); |
| 1009 | |
| 1010 | std::unique_ptr<EventLoop> ping_event_loop = |
| 1011 | simulated_event_loop_factory.MakeEventLoop("ping", pi1); |
| 1012 | aos::Sender<examples::Ping> pi1_reliable_sender = |
| 1013 | ping_event_loop->MakeSender<examples::Ping>("/reliable"); |
| 1014 | aos::Sender<examples::Ping> pi1_unreliable_sender = |
| 1015 | ping_event_loop->MakeSender<examples::Ping>("/unreliable"); |
| 1016 | SendPing(&pi1_reliable_sender, 1); |
| 1017 | SendPing(&pi1_unreliable_sender, 1); |
| 1018 | |
| 1019 | std::unique_ptr<EventLoop> pi2_pong_event_loop = |
| 1020 | simulated_event_loop_factory.MakeEventLoop("pong", pi2); |
| 1021 | MessageCounter<examples::Ping> pi2_reliable_counter(pi2_pong_event_loop.get(), |
| 1022 | "/reliable"); |
| 1023 | MessageCounter<examples::Ping> pi2_unreliable_counter( |
| 1024 | pi2_pong_event_loop.get(), "/unreliable"); |
| 1025 | aos::Fetcher<examples::Ping> reliable_on_pi2_fetcher = |
| 1026 | pi2_pong_event_loop->MakeFetcher<examples::Ping>("/reliable"); |
| 1027 | aos::Fetcher<examples::Ping> unreliable_on_pi2_fetcher = |
| 1028 | pi2_pong_event_loop->MakeFetcher<examples::Ping>("/unreliable"); |
| 1029 | |
| 1030 | const size_t reliable_channel_index = configuration::ChannelIndex( |
| 1031 | pi2_pong_event_loop->configuration(), reliable_on_pi2_fetcher.channel()); |
| 1032 | |
| 1033 | std::unique_ptr<EventLoop> pi1_remote_timestamp = |
| 1034 | simulated_event_loop_factory.MakeEventLoop("pi1_remote_timestamp", pi1); |
| 1035 | |
| 1036 | int reliable_timestamp_count = 0; |
| 1037 | pi1_remote_timestamp->MakeWatcher( |
| 1038 | "/pi1/aos/remote_timestamps/pi2", |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 1039 | [reliable_channel_index, &reliable_timestamp_count, |
| 1040 | &simulated_event_loop_factory, pi2](const RemoteMessage &header) { |
| 1041 | EXPECT_TRUE(header.has_boot_uuid()); |
| 1042 | EXPECT_EQ(header.boot_uuid()->string_view(), |
| 1043 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi2) |
| 1044 | ->boot_uuid() |
| 1045 | .string_view()); |
Austin Schuh | 4c570ea | 2020-11-19 23:13:24 -0800 | [diff] [blame] | 1046 | VLOG(1) << aos::FlatbufferToJson(&header); |
| 1047 | if (header.channel_index() == reliable_channel_index) { |
| 1048 | ++reliable_timestamp_count; |
| 1049 | } |
| 1050 | }); |
| 1051 | |
| 1052 | // Wait to let timestamp estimation start up before looking for the results. |
| 1053 | simulated_event_loop_factory.RunFor(chrono::milliseconds(500)); |
| 1054 | |
| 1055 | EXPECT_EQ(pi2_reliable_counter.count(), 1u); |
| 1056 | // This one isn't reliable, but was sent before the start. It should *not* be |
| 1057 | // delivered. |
| 1058 | EXPECT_EQ(pi2_unreliable_counter.count(), 0u); |
| 1059 | // Confirm we got a timestamp logged for the message that was forwarded. |
| 1060 | EXPECT_EQ(reliable_timestamp_count, 1u); |
| 1061 | |
| 1062 | SendPing(&pi1_reliable_sender, 2); |
| 1063 | SendPing(&pi1_unreliable_sender, 2); |
| 1064 | simulated_event_loop_factory.RunFor(chrono::milliseconds(500)); |
| 1065 | EXPECT_EQ(pi2_reliable_counter.count(), 2u); |
| 1066 | EXPECT_EQ(pi2_unreliable_counter.count(), 1u); |
| 1067 | |
| 1068 | EXPECT_EQ(reliable_timestamp_count, 2u); |
| 1069 | } |
| 1070 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 1071 | // Tests that rebooting a node changes the ServerStatistics message and the |
| 1072 | // RemoteTimestamp message. |
| 1073 | TEST(SimulatedEventLoopTest, BootUUIDTest) { |
| 1074 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 1075 | aos::configuration::ReadConfig(ConfigPrefix() + |
| 1076 | "events/multinode_pingpong_config.json"); |
| 1077 | const Node *pi1 = configuration::GetNode(&config.message(), "pi1"); |
| 1078 | const Node *pi2 = configuration::GetNode(&config.message(), "pi2"); |
| 1079 | |
| 1080 | SimulatedEventLoopFactory simulated_event_loop_factory(&config.message()); |
| 1081 | |
| 1082 | std::unique_ptr<EventLoop> ping_event_loop = |
| 1083 | simulated_event_loop_factory.MakeEventLoop("ping", pi1); |
| 1084 | Ping ping(ping_event_loop.get()); |
| 1085 | |
| 1086 | std::unique_ptr<EventLoop> pong_event_loop = |
| 1087 | simulated_event_loop_factory.MakeEventLoop("pong", pi2); |
| 1088 | Pong pong(pong_event_loop.get()); |
| 1089 | |
| 1090 | std::unique_ptr<EventLoop> pi1_remote_timestamp = |
| 1091 | simulated_event_loop_factory.MakeEventLoop("pi1_remote_timestamp", pi1); |
| 1092 | std::string expected_boot_uuid( |
| 1093 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi2) |
| 1094 | ->boot_uuid() |
| 1095 | .string_view()); |
| 1096 | |
| 1097 | int timestamp_count = 0; |
| 1098 | pi1_remote_timestamp->MakeWatcher( |
| 1099 | "/pi1/aos/remote_timestamps/pi2", |
| 1100 | [×tamp_count, &expected_boot_uuid](const RemoteMessage &header) { |
| 1101 | EXPECT_TRUE(header.has_boot_uuid()); |
| 1102 | EXPECT_EQ(header.boot_uuid()->string_view(), expected_boot_uuid); |
| 1103 | VLOG(1) << aos::FlatbufferToJson(&header); |
| 1104 | ++timestamp_count; |
| 1105 | }); |
| 1106 | |
| 1107 | int pi1_server_statistics_count = 0; |
| 1108 | pi1_remote_timestamp->MakeWatcher( |
| 1109 | "/pi1/aos", [&pi1_server_statistics_count, &expected_boot_uuid]( |
| 1110 | const message_bridge::ServerStatistics &stats) { |
| 1111 | VLOG(1) << "pi1 ServerStatistics " << FlatbufferToJson(&stats); |
| 1112 | for (const message_bridge::ServerConnection *connection : |
| 1113 | *stats.connections()) { |
| 1114 | EXPECT_TRUE(connection->has_boot_uuid()); |
| 1115 | if (connection->node()->name()->string_view() == "pi2") { |
| 1116 | EXPECT_EQ(expected_boot_uuid, |
| 1117 | connection->boot_uuid()->string_view()) |
| 1118 | << " : Got " << aos::FlatbufferToJson(&stats); |
| 1119 | ++pi1_server_statistics_count; |
| 1120 | } |
| 1121 | } |
| 1122 | }); |
| 1123 | |
| 1124 | // Let a couple of ServerStatistics messages show up before rebooting. |
| 1125 | simulated_event_loop_factory.RunFor(chrono::milliseconds(2001)); |
| 1126 | |
| 1127 | EXPECT_GT(timestamp_count, 100); |
| 1128 | EXPECT_GE(pi1_server_statistics_count, 1u); |
| 1129 | |
| 1130 | // Confirm that reboot changes the UUID. |
| 1131 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi2)->Reboot(); |
| 1132 | |
| 1133 | EXPECT_NE(expected_boot_uuid, |
| 1134 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi2) |
| 1135 | ->boot_uuid() |
| 1136 | .string_view()); |
| 1137 | |
| 1138 | expected_boot_uuid = |
| 1139 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi2) |
| 1140 | ->boot_uuid() |
| 1141 | .string_view(); |
| 1142 | timestamp_count = 0; |
| 1143 | pi1_server_statistics_count = 0; |
| 1144 | |
| 1145 | simulated_event_loop_factory.RunFor(chrono::milliseconds(2000)); |
| 1146 | EXPECT_GT(timestamp_count, 100); |
| 1147 | EXPECT_GE(pi1_server_statistics_count, 1u); |
| 1148 | } |
| 1149 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 1150 | } // namespace testing |
| 1151 | } // namespace aos |