Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include "aos/events/simulated_event_loop.h" |
| 2 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 3 | #include <chrono> |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 4 | #include <functional> |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 5 | #include <string_view> |
| 6 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include "aos/events/event_loop_param_test.h" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 8 | #include "aos/events/logging/logger_generated.h" |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 9 | #include "aos/events/message_counter.h" |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 10 | #include "aos/events/ping_lib.h" |
| 11 | #include "aos/events/pong_lib.h" |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 12 | #include "aos/events/test_message_generated.h" |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 13 | #include "aos/network/message_bridge_client_generated.h" |
| 14 | #include "aos/network/message_bridge_server_generated.h" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 15 | #include "aos/network/remote_message_generated.h" |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 16 | #include "aos/network/testing_time_converter.h" |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 17 | #include "aos/network/timestamp_generated.h" |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 18 | #include "aos/testing/path.h" |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 19 | #include "gtest/gtest.h" |
| 20 | |
| 21 | namespace aos { |
| 22 | namespace testing { |
Brian Silverman | 28d1430 | 2020-09-18 15:26:17 -0700 | [diff] [blame] | 23 | namespace { |
| 24 | |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 25 | using aos::testing::ArtifactPath; |
Brian Silverman | 28d1430 | 2020-09-18 15:26:17 -0700 | [diff] [blame] | 26 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 27 | using logger::BootTimestamp; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 28 | using message_bridge::RemoteMessage; |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 29 | namespace chrono = ::std::chrono; |
| 30 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 31 | } // namespace |
| 32 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 33 | class SimulatedEventLoopTestFactory : public EventLoopTestFactory { |
| 34 | public: |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 35 | ::std::unique_ptr<EventLoop> Make(std::string_view name) override { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 36 | MaybeMake(); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 37 | return event_loop_factory_->MakeEventLoop(name, my_node()); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 38 | } |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 39 | ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name) override { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 40 | MaybeMake(); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 41 | return event_loop_factory_->MakeEventLoop(name, my_node()); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 44 | void Run() override { event_loop_factory_->Run(); } |
| 45 | void Exit() override { event_loop_factory_->Exit(); } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 46 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 47 | // TODO(austin): Implement this. It's used currently for a phased loop test. |
| 48 | // I'm not sure how much that matters. |
| 49 | void SleepFor(::std::chrono::nanoseconds /*duration*/) override {} |
| 50 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 51 | void set_send_delay(std::chrono::nanoseconds send_delay) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 52 | MaybeMake(); |
| 53 | event_loop_factory_->set_send_delay(send_delay); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 56 | private: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 57 | void MaybeMake() { |
| 58 | if (!event_loop_factory_) { |
| 59 | if (configuration()->has_nodes()) { |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 60 | event_loop_factory_ = |
| 61 | std::make_unique<SimulatedEventLoopFactory>(configuration()); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 62 | } else { |
| 63 | event_loop_factory_ = |
| 64 | std::make_unique<SimulatedEventLoopFactory>(configuration()); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | std::unique_ptr<SimulatedEventLoopFactory> event_loop_factory_; |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 71 | auto CommonParameters() { |
| 72 | return ::testing::Combine( |
| 73 | ::testing::Values([]() { return new SimulatedEventLoopTestFactory(); }), |
| 74 | ::testing::Values(ReadMethod::COPY, ReadMethod::PIN), |
| 75 | ::testing::Values(DoTimingReports::kYes, DoTimingReports::kNo)); |
| 76 | } |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 77 | |
James Kuszmaul | f4bf9fe | 2021-05-10 22:58:24 -0700 | [diff] [blame] | 78 | INSTANTIATE_TEST_SUITE_P(SimulatedEventLoopCommonTest, AbstractEventLoopTest, |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 79 | CommonParameters()); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 80 | |
James Kuszmaul | f4bf9fe | 2021-05-10 22:58:24 -0700 | [diff] [blame] | 81 | INSTANTIATE_TEST_SUITE_P(SimulatedEventLoopCommonDeathTest, |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 82 | AbstractEventLoopDeathTest, CommonParameters()); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 83 | |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 84 | // Parameters to run all the tests with. |
| 85 | struct Param { |
| 86 | // The config file to use. |
| 87 | std::string config; |
| 88 | // If true, the RemoteMessage channel should be shared between all the remote |
| 89 | // channels. If false, there will be 1 RemoteMessage channel per remote |
| 90 | // channel. |
| 91 | bool shared; |
| 92 | }; |
| 93 | |
| 94 | class RemoteMessageSimulatedEventLoopTest |
| 95 | : public ::testing::TestWithParam<struct Param> { |
| 96 | public: |
| 97 | RemoteMessageSimulatedEventLoopTest() |
| 98 | : config(aos::configuration::ReadConfig( |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 99 | ArtifactPath(absl::StrCat("aos/events/", GetParam().config)))) { |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 100 | LOG(INFO) << "Config " << GetParam().config; |
| 101 | } |
| 102 | |
| 103 | bool shared() const { return GetParam().shared; } |
| 104 | |
| 105 | std::vector<std::unique_ptr<MessageCounter<RemoteMessage>>> |
| 106 | MakePi2OnPi1MessageCounters(aos::EventLoop *event_loop) { |
| 107 | std::vector<std::unique_ptr<MessageCounter<RemoteMessage>>> counters; |
| 108 | if (shared()) { |
| 109 | counters.emplace_back(std::make_unique<MessageCounter<RemoteMessage>>( |
| 110 | event_loop, "/aos/remote_timestamps/pi2")); |
| 111 | } else { |
| 112 | counters.emplace_back(std::make_unique<MessageCounter<RemoteMessage>>( |
| 113 | event_loop, |
| 114 | "/aos/remote_timestamps/pi2/pi1/aos/aos-message_bridge-Timestamp")); |
| 115 | counters.emplace_back(std::make_unique<MessageCounter<RemoteMessage>>( |
| 116 | event_loop, "/aos/remote_timestamps/pi2/test/aos-examples-Ping")); |
| 117 | counters.emplace_back(std::make_unique<MessageCounter<RemoteMessage>>( |
| 118 | event_loop, "/aos/remote_timestamps/pi2/reliable/aos-examples-Ping")); |
| 119 | } |
| 120 | return counters; |
| 121 | } |
| 122 | |
| 123 | std::vector<std::unique_ptr<MessageCounter<RemoteMessage>>> |
| 124 | MakePi1OnPi2MessageCounters(aos::EventLoop *event_loop) { |
| 125 | std::vector<std::unique_ptr<MessageCounter<RemoteMessage>>> counters; |
| 126 | if (shared()) { |
| 127 | counters.emplace_back(std::make_unique<MessageCounter<RemoteMessage>>( |
| 128 | event_loop, "/aos/remote_timestamps/pi1")); |
| 129 | } else { |
| 130 | counters.emplace_back(std::make_unique<MessageCounter<RemoteMessage>>( |
| 131 | event_loop, "/aos/remote_timestamps/pi1/test/aos-examples-Pong")); |
| 132 | counters.emplace_back(std::make_unique<MessageCounter<RemoteMessage>>( |
| 133 | event_loop, |
| 134 | "/aos/remote_timestamps/pi1/pi2/aos/aos-message_bridge-Timestamp")); |
| 135 | } |
| 136 | return counters; |
| 137 | } |
| 138 | |
| 139 | aos::FlatbufferDetachedBuffer<aos::Configuration> config; |
| 140 | }; |
| 141 | |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 142 | class FunctionEvent : public EventScheduler::Event { |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 143 | public: |
| 144 | FunctionEvent(std::function<void()> fn) : fn_(fn) {} |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 145 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 146 | void Handle() noexcept override { fn_(); } |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 147 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 148 | private: |
| 149 | std::function<void()> fn_; |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 150 | }; |
| 151 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 152 | // Test that creating an event and running the scheduler runs the event. |
| 153 | TEST(EventSchedulerTest, ScheduleEvent) { |
| 154 | int counter = 0; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 155 | EventSchedulerScheduler scheduler_scheduler; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 156 | EventScheduler scheduler(0); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 157 | scheduler_scheduler.AddEventScheduler(&scheduler); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 158 | |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 159 | FunctionEvent e([&counter]() { counter += 1; }); |
| 160 | scheduler.Schedule(monotonic_clock::epoch() + chrono::seconds(1), &e); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 161 | scheduler_scheduler.Run(); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 162 | EXPECT_EQ(counter, 1); |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 163 | auto token = |
| 164 | scheduler.Schedule(monotonic_clock::epoch() + chrono::seconds(2), &e); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 165 | scheduler.Deschedule(token); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 166 | scheduler_scheduler.Run(); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 167 | EXPECT_EQ(counter, 1); |
| 168 | } |
| 169 | |
| 170 | // Test that descheduling an already scheduled event doesn't run the event. |
| 171 | TEST(EventSchedulerTest, DescheduleEvent) { |
| 172 | int counter = 0; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 173 | EventSchedulerScheduler scheduler_scheduler; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 174 | EventScheduler scheduler(0); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 175 | scheduler_scheduler.AddEventScheduler(&scheduler); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 176 | |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 177 | FunctionEvent e([&counter]() { counter += 1; }); |
| 178 | auto token = |
| 179 | scheduler.Schedule(monotonic_clock::epoch() + chrono::seconds(1), &e); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 180 | scheduler.Deschedule(token); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 181 | scheduler_scheduler.Run(); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 182 | EXPECT_EQ(counter, 0); |
| 183 | } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 184 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 185 | // Test that TemporarilyStopAndRun respects and preserves running. |
| 186 | TEST(EventSchedulerTest, TemporarilyStopAndRun) { |
| 187 | int counter = 0; |
| 188 | EventSchedulerScheduler scheduler_scheduler; |
| 189 | EventScheduler scheduler(0); |
| 190 | scheduler_scheduler.AddEventScheduler(&scheduler); |
| 191 | |
| 192 | scheduler_scheduler.TemporarilyStopAndRun( |
| 193 | [&]() { CHECK(!scheduler_scheduler.is_running()); }); |
| 194 | ASSERT_FALSE(scheduler_scheduler.is_running()); |
| 195 | |
| 196 | FunctionEvent e([&]() { |
| 197 | counter += 1; |
| 198 | CHECK(scheduler_scheduler.is_running()); |
| 199 | scheduler_scheduler.TemporarilyStopAndRun( |
| 200 | [&]() { CHECK(!scheduler_scheduler.is_running()); }); |
| 201 | CHECK(scheduler_scheduler.is_running()); |
| 202 | }); |
| 203 | scheduler.Schedule(monotonic_clock::epoch() + chrono::seconds(1), &e); |
| 204 | scheduler_scheduler.Run(); |
| 205 | EXPECT_EQ(counter, 1); |
| 206 | } |
| 207 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 208 | // Test that sending a message after running gets properly notified. |
| 209 | TEST(SimulatedEventLoopTest, SendAfterRunFor) { |
| 210 | SimulatedEventLoopTestFactory factory; |
| 211 | |
| 212 | SimulatedEventLoopFactory simulated_event_loop_factory( |
| 213 | factory.configuration()); |
| 214 | |
| 215 | ::std::unique_ptr<EventLoop> ping_event_loop = |
| 216 | simulated_event_loop_factory.MakeEventLoop("ping"); |
| 217 | aos::Sender<TestMessage> test_message_sender = |
| 218 | ping_event_loop->MakeSender<TestMessage>("/test"); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 219 | ASSERT_EQ(SendTestMessage(test_message_sender), RawSender::Error::kOk); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 220 | |
| 221 | std::unique_ptr<EventLoop> pong1_event_loop = |
| 222 | simulated_event_loop_factory.MakeEventLoop("pong"); |
| 223 | MessageCounter<TestMessage> test_message_counter1(pong1_event_loop.get(), |
| 224 | "/test"); |
| 225 | |
| 226 | EXPECT_FALSE(ping_event_loop->is_running()); |
| 227 | |
| 228 | // Watchers start when you start running, so there should be nothing counted. |
| 229 | simulated_event_loop_factory.RunFor(chrono::seconds(1)); |
| 230 | EXPECT_EQ(test_message_counter1.count(), 0u); |
| 231 | |
| 232 | std::unique_ptr<EventLoop> pong2_event_loop = |
| 233 | simulated_event_loop_factory.MakeEventLoop("pong"); |
| 234 | MessageCounter<TestMessage> test_message_counter2(pong2_event_loop.get(), |
| 235 | "/test"); |
| 236 | |
| 237 | // Pauses in the middle don't count though, so this should be counted. |
| 238 | // But, the fresh watcher shouldn't pick it up yet. |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 239 | ASSERT_EQ(SendTestMessage(test_message_sender), RawSender::Error::kOk); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 240 | |
| 241 | EXPECT_EQ(test_message_counter1.count(), 0u); |
| 242 | EXPECT_EQ(test_message_counter2.count(), 0u); |
| 243 | simulated_event_loop_factory.RunFor(chrono::seconds(1)); |
| 244 | |
| 245 | EXPECT_EQ(test_message_counter1.count(), 1u); |
| 246 | EXPECT_EQ(test_message_counter2.count(), 0u); |
| 247 | } |
| 248 | |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame^] | 249 | // Test that if we configure an event loop to be able to send too fast that we |
| 250 | // do allow it to do so. |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 251 | TEST(SimulatedEventLoopTest, AllowSendTooFast) { |
| 252 | SimulatedEventLoopTestFactory factory; |
| 253 | |
| 254 | SimulatedEventLoopFactory simulated_event_loop_factory( |
| 255 | factory.configuration()); |
| 256 | |
| 257 | // Create two event loops: One will be allowed to send too fast, one won't. We |
| 258 | // will then test to ensure that the one that is allowed to send too fast can |
| 259 | // indeed send too fast, but that it then makes it so that the second event |
| 260 | // loop can no longer send anything because *it* is still limited. |
| 261 | ::std::unique_ptr<EventLoop> too_fast_event_loop = |
| 262 | simulated_event_loop_factory.GetNodeEventLoopFactory(nullptr) |
| 263 | ->MakeEventLoop("too_fast_sender", |
| 264 | {NodeEventLoopFactory::CheckSentTooFast::kNo, |
| 265 | NodeEventLoopFactory::ExclusiveSenders::kNo}); |
| 266 | aos::Sender<TestMessage> too_fast_message_sender = |
| 267 | too_fast_event_loop->MakeSender<TestMessage>("/test"); |
| 268 | |
| 269 | ::std::unique_ptr<EventLoop> limited_event_loop = |
| 270 | simulated_event_loop_factory.MakeEventLoop("limited_sender"); |
| 271 | aos::Sender<TestMessage> limited_message_sender = |
| 272 | limited_event_loop->MakeSender<TestMessage>("/test"); |
| 273 | |
| 274 | const int queue_size = TestChannelQueueSize(too_fast_event_loop.get()); |
| 275 | for (int ii = 0; ii < queue_size; ++ii) { |
| 276 | ASSERT_EQ(SendTestMessage(too_fast_message_sender), RawSender::Error::kOk); |
| 277 | } |
| 278 | // And now we should start being in the sending-too-fast phase. |
| 279 | for (int ii = 0; ii < queue_size; ++ii) { |
| 280 | ASSERT_EQ(SendTestMessage(too_fast_message_sender), RawSender::Error::kOk); |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame^] | 281 | ASSERT_EQ(SendTestMessage(limited_message_sender), |
| 282 | RawSender::Error::kMessagesSentTooFast); |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | // Test that if we setup an exclusive sender that it is indeed exclusive. |
| 287 | TEST(SimulatedEventLoopDeathTest, ExclusiveSenders) { |
| 288 | SimulatedEventLoopTestFactory factory; |
| 289 | |
| 290 | SimulatedEventLoopFactory simulated_event_loop_factory( |
| 291 | factory.configuration()); |
| 292 | |
| 293 | ::std::unique_ptr<EventLoop> exclusive_event_loop = |
| 294 | simulated_event_loop_factory.GetNodeEventLoopFactory(nullptr) |
| 295 | ->MakeEventLoop("too_fast_sender", |
| 296 | {NodeEventLoopFactory::CheckSentTooFast::kYes, |
| 297 | NodeEventLoopFactory::ExclusiveSenders::kYes}); |
| 298 | exclusive_event_loop->SkipAosLog(); |
| 299 | exclusive_event_loop->SkipTimingReport(); |
| 300 | ::std::unique_ptr<EventLoop> normal_event_loop = |
| 301 | simulated_event_loop_factory.MakeEventLoop("limited_sender"); |
| 302 | // Set things up to have the exclusive sender be destroyed so we can test |
| 303 | // recovery. |
| 304 | { |
| 305 | aos::Sender<TestMessage> exclusive_sender = |
| 306 | exclusive_event_loop->MakeSender<TestMessage>("/test"); |
| 307 | |
| 308 | EXPECT_DEATH(normal_event_loop->MakeSender<TestMessage>("/test"), |
| 309 | "TestMessage"); |
| 310 | } |
| 311 | // This one should succeed now that the exclusive channel is removed. |
| 312 | aos::Sender<TestMessage> normal_sender = |
| 313 | normal_event_loop->MakeSender<TestMessage>("/test"); |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame^] | 314 | EXPECT_DEATH(exclusive_event_loop->MakeSender<TestMessage>("/test"), |
| 315 | "TestMessage"); |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 318 | void TestSentTooFastCheckEdgeCase( |
| 319 | const std::function<RawSender::Error(int, int)> expected_err, |
| 320 | const bool send_twice_at_end) { |
| 321 | SimulatedEventLoopTestFactory factory; |
| 322 | |
| 323 | auto event_loop = factory.MakePrimary("primary"); |
| 324 | |
| 325 | auto sender = event_loop->MakeSender<TestMessage>("/test"); |
| 326 | |
| 327 | const int queue_size = TestChannelQueueSize(event_loop.get()); |
| 328 | int msgs_sent = 0; |
| 329 | event_loop->AddPhasedLoop( |
| 330 | [&](int) { |
| 331 | EXPECT_EQ(SendTestMessage(sender), expected_err(msgs_sent, queue_size)); |
| 332 | msgs_sent++; |
| 333 | |
| 334 | // If send_twice_at_end, send the last two messages (message |
| 335 | // queue_size and queue_size + 1) in the same iteration, meaning that |
| 336 | // we would be sending very slightly too fast. Otherwise, we will send |
| 337 | // message queue_size + 1 in the next iteration and we will continue |
| 338 | // to be sending exactly at the channel frequency. |
| 339 | if (send_twice_at_end && (msgs_sent == queue_size)) { |
| 340 | EXPECT_EQ(SendTestMessage(sender), |
| 341 | expected_err(msgs_sent, queue_size)); |
| 342 | msgs_sent++; |
| 343 | } |
| 344 | |
| 345 | if (msgs_sent > queue_size) { |
| 346 | factory.Exit(); |
| 347 | } |
| 348 | }, |
| 349 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 350 | std::chrono::duration<double>( |
| 351 | 1.0 / TestChannelFrequency(event_loop.get())))); |
| 352 | |
| 353 | factory.Run(); |
| 354 | } |
| 355 | |
| 356 | // Tests that RawSender::Error::kMessagesSentTooFast is not returned |
| 357 | // when messages are sent at the exact frequency of the channel. |
| 358 | TEST(SimulatedEventLoopTest, SendingAtExactlyChannelFrequency) { |
| 359 | TestSentTooFastCheckEdgeCase([](int, int) { return RawSender::Error::kOk; }, |
| 360 | false); |
| 361 | } |
| 362 | |
| 363 | // Tests that RawSender::Error::kMessagesSentTooFast is returned |
| 364 | // when sending exactly one more message than allowed in a channel storage |
| 365 | // duration. |
| 366 | TEST(SimulatedEventLoopTest, SendingSlightlyTooFast) { |
| 367 | TestSentTooFastCheckEdgeCase( |
| 368 | [](const int msgs_sent, const int queue_size) { |
| 369 | return (msgs_sent == queue_size ? RawSender::Error::kMessagesSentTooFast |
| 370 | : RawSender::Error::kOk); |
| 371 | }, |
| 372 | true); |
| 373 | } |
| 374 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 375 | // Test that creating an event loop while running dies. |
| 376 | TEST(SimulatedEventLoopDeathTest, MakeEventLoopWhileRunning) { |
| 377 | SimulatedEventLoopTestFactory factory; |
| 378 | |
| 379 | SimulatedEventLoopFactory simulated_event_loop_factory( |
| 380 | factory.configuration()); |
| 381 | |
| 382 | ::std::unique_ptr<EventLoop> event_loop = |
| 383 | simulated_event_loop_factory.MakeEventLoop("ping"); |
| 384 | |
| 385 | auto timer = event_loop->AddTimer([&]() { |
| 386 | EXPECT_DEATH( |
| 387 | { |
| 388 | ::std::unique_ptr<EventLoop> event_loop2 = |
| 389 | simulated_event_loop_factory.MakeEventLoop("ping"); |
| 390 | }, |
| 391 | "event loop while running"); |
| 392 | simulated_event_loop_factory.Exit(); |
| 393 | }); |
| 394 | |
| 395 | event_loop->OnRun([&event_loop, &timer] { |
| 396 | timer->Setup(event_loop->monotonic_now() + chrono::milliseconds(50)); |
| 397 | }); |
| 398 | |
| 399 | simulated_event_loop_factory.Run(); |
| 400 | } |
| 401 | |
| 402 | // Test that creating a watcher after running dies. |
| 403 | TEST(SimulatedEventLoopDeathTest, MakeWatcherAfterRunning) { |
| 404 | SimulatedEventLoopTestFactory factory; |
| 405 | |
| 406 | SimulatedEventLoopFactory simulated_event_loop_factory( |
| 407 | factory.configuration()); |
| 408 | |
| 409 | ::std::unique_ptr<EventLoop> event_loop = |
| 410 | simulated_event_loop_factory.MakeEventLoop("ping"); |
| 411 | |
| 412 | simulated_event_loop_factory.RunFor(chrono::seconds(1)); |
| 413 | |
| 414 | EXPECT_DEATH( |
| 415 | { MessageCounter<TestMessage> counter(event_loop.get(), "/test"); }, |
| 416 | "Can't add a watcher after running"); |
| 417 | |
| 418 | ::std::unique_ptr<EventLoop> event_loop2 = |
| 419 | simulated_event_loop_factory.MakeEventLoop("ping"); |
| 420 | |
| 421 | simulated_event_loop_factory.RunFor(chrono::seconds(1)); |
| 422 | |
| 423 | EXPECT_DEATH( |
| 424 | { MessageCounter<TestMessage> counter(event_loop2.get(), "/test"); }, |
| 425 | "Can't add a watcher after running"); |
| 426 | } |
| 427 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 428 | // Test that running for a time period with no handlers causes time to progress |
| 429 | // correctly. |
| 430 | TEST(SimulatedEventLoopTest, RunForNoHandlers) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 431 | SimulatedEventLoopTestFactory factory; |
| 432 | |
| 433 | SimulatedEventLoopFactory simulated_event_loop_factory( |
| 434 | factory.configuration()); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 435 | ::std::unique_ptr<EventLoop> event_loop = |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 436 | simulated_event_loop_factory.MakeEventLoop("loop"); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 437 | |
| 438 | simulated_event_loop_factory.RunFor(chrono::seconds(1)); |
| 439 | |
| 440 | EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1), |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 441 | event_loop->monotonic_now()); |
| 442 | } |
| 443 | |
| 444 | // Test that running for a time with a periodic handler causes time to end |
| 445 | // correctly. |
| 446 | TEST(SimulatedEventLoopTest, RunForTimerHandler) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 447 | SimulatedEventLoopTestFactory factory; |
| 448 | |
| 449 | SimulatedEventLoopFactory simulated_event_loop_factory( |
| 450 | factory.configuration()); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 451 | ::std::unique_ptr<EventLoop> event_loop = |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 452 | simulated_event_loop_factory.MakeEventLoop("loop"); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 453 | |
| 454 | int counter = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 455 | auto timer = event_loop->AddTimer([&counter]() { ++counter; }); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 456 | event_loop->OnRun([&event_loop, &timer] { |
| 457 | timer->Setup(event_loop->monotonic_now() + chrono::milliseconds(50), |
| 458 | chrono::milliseconds(100)); |
| 459 | }); |
| 460 | |
| 461 | simulated_event_loop_factory.RunFor(chrono::seconds(1)); |
| 462 | |
| 463 | EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1), |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 464 | event_loop->monotonic_now()); |
| 465 | EXPECT_EQ(counter, 10); |
| 466 | } |
| 467 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 468 | // Tests that watchers have latency in simulation. |
| 469 | TEST(SimulatedEventLoopTest, WatcherTimingReport) { |
| 470 | SimulatedEventLoopTestFactory factory; |
| 471 | factory.set_send_delay(std::chrono::microseconds(50)); |
| 472 | |
| 473 | FLAGS_timing_report_ms = 1000; |
| 474 | auto loop1 = factory.MakePrimary("primary"); |
| 475 | loop1->MakeWatcher("/test", [](const TestMessage &) {}); |
| 476 | |
| 477 | auto loop2 = factory.Make("sender_loop"); |
| 478 | |
| 479 | auto loop3 = factory.Make("report_fetcher"); |
| 480 | |
| 481 | Fetcher<timing::Report> report_fetcher = |
| 482 | loop3->MakeFetcher<timing::Report>("/aos"); |
| 483 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 484 | |
| 485 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 486 | |
| 487 | // Send 10 messages in the middle of a timing report period so we get |
| 488 | // something interesting back. |
| 489 | auto test_timer = loop2->AddTimer([&sender]() { |
| 490 | for (int i = 0; i < 10; ++i) { |
| 491 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 492 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 493 | builder.add_value(200 + i); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 494 | msg.CheckOk(msg.Send(builder.Finish())); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 495 | } |
| 496 | }); |
| 497 | |
| 498 | // Quit after 1 timing report, mid way through the next cycle. |
| 499 | { |
| 500 | auto end_timer = loop1->AddTimer([&factory]() { factory.Exit(); }); |
| 501 | end_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(2500)); |
| 502 | end_timer->set_name("end"); |
| 503 | } |
| 504 | |
| 505 | loop1->OnRun([&test_timer, &loop1]() { |
| 506 | test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1500)); |
| 507 | }); |
| 508 | |
| 509 | factory.Run(); |
| 510 | |
| 511 | // And, since we are here, check that the timing report makes sense. |
| 512 | // Start by looking for our event loop's timing. |
| 513 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 514 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 515 | while (report_fetcher.FetchNext()) { |
| 516 | LOG(INFO) << "Report " << FlatbufferToJson(report_fetcher.get()); |
| 517 | if (report_fetcher->name()->string_view() == "primary") { |
| 518 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | // Check the watcher report. |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 523 | VLOG(1) << FlatbufferToJson(primary_report, {.multi_line = true}); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 524 | |
| 525 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 526 | |
| 527 | // Just the timing report timer. |
| 528 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 529 | EXPECT_EQ(primary_report.message().timers()->size(), 2); |
| 530 | |
| 531 | // No phased loops |
| 532 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 533 | |
| 534 | // And now confirm that the watcher received all 10 messages, and has latency. |
| 535 | ASSERT_NE(primary_report.message().watchers(), nullptr); |
| 536 | ASSERT_EQ(primary_report.message().watchers()->size(), 1); |
| 537 | EXPECT_EQ(primary_report.message().watchers()->Get(0)->count(), 10); |
| 538 | EXPECT_NEAR( |
| 539 | primary_report.message().watchers()->Get(0)->wakeup_latency()->average(), |
| 540 | 0.00005, 1e-9); |
| 541 | EXPECT_NEAR( |
| 542 | primary_report.message().watchers()->Get(0)->wakeup_latency()->min(), |
| 543 | 0.00005, 1e-9); |
| 544 | EXPECT_NEAR( |
| 545 | primary_report.message().watchers()->Get(0)->wakeup_latency()->max(), |
| 546 | 0.00005, 1e-9); |
| 547 | EXPECT_EQ(primary_report.message() |
| 548 | .watchers() |
| 549 | ->Get(0) |
| 550 | ->wakeup_latency() |
| 551 | ->standard_deviation(), |
| 552 | 0.0); |
| 553 | |
| 554 | EXPECT_EQ( |
| 555 | primary_report.message().watchers()->Get(0)->handler_time()->average(), |
| 556 | 0.0); |
| 557 | EXPECT_EQ(primary_report.message().watchers()->Get(0)->handler_time()->min(), |
| 558 | 0.0); |
| 559 | EXPECT_EQ(primary_report.message().watchers()->Get(0)->handler_time()->max(), |
| 560 | 0.0); |
| 561 | EXPECT_EQ(primary_report.message() |
| 562 | .watchers() |
| 563 | ->Get(0) |
| 564 | ->handler_time() |
| 565 | ->standard_deviation(), |
| 566 | 0.0); |
| 567 | } |
| 568 | |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 569 | size_t CountAll( |
| 570 | const std::vector<std::unique_ptr<MessageCounter<RemoteMessage>>> |
| 571 | &counters) { |
| 572 | size_t count = 0u; |
| 573 | for (const std::unique_ptr<MessageCounter<RemoteMessage>> &counter : |
| 574 | counters) { |
| 575 | count += counter->count(); |
| 576 | } |
| 577 | return count; |
| 578 | } |
| 579 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 580 | // Tests that ping and pong work when on 2 different nodes, and the message |
| 581 | // gateway messages are sent out as expected. |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 582 | TEST_P(RemoteMessageSimulatedEventLoopTest, MultinodePingPong) { |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 583 | const Node *pi1 = configuration::GetNode(&config.message(), "pi1"); |
| 584 | const Node *pi2 = configuration::GetNode(&config.message(), "pi2"); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 585 | const Node *pi3 = configuration::GetNode(&config.message(), "pi3"); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 586 | |
| 587 | SimulatedEventLoopFactory simulated_event_loop_factory(&config.message()); |
| 588 | |
| 589 | std::unique_ptr<EventLoop> ping_event_loop = |
| 590 | simulated_event_loop_factory.MakeEventLoop("ping", pi1); |
| 591 | Ping ping(ping_event_loop.get()); |
| 592 | |
| 593 | std::unique_ptr<EventLoop> pong_event_loop = |
| 594 | simulated_event_loop_factory.MakeEventLoop("pong", pi2); |
| 595 | Pong pong(pong_event_loop.get()); |
| 596 | |
| 597 | std::unique_ptr<EventLoop> pi2_pong_counter_event_loop = |
| 598 | simulated_event_loop_factory.MakeEventLoop("pi2_pong_counter", pi2); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 599 | MessageCounter<examples::Pong> pi2_pong_counter( |
| 600 | pi2_pong_counter_event_loop.get(), "/test"); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 601 | aos::Fetcher<message_bridge::Timestamp> pi1_on_pi2_timestamp_fetcher = |
| 602 | pi2_pong_counter_event_loop->MakeFetcher<message_bridge::Timestamp>( |
| 603 | "/pi1/aos"); |
| 604 | aos::Fetcher<examples::Ping> ping_on_pi2_fetcher = |
| 605 | pi2_pong_counter_event_loop->MakeFetcher<examples::Ping>("/test"); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 606 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 607 | std::unique_ptr<EventLoop> pi3_pong_counter_event_loop = |
| 608 | simulated_event_loop_factory.MakeEventLoop("pi3_pong_counter", pi3); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 609 | |
| 610 | std::unique_ptr<EventLoop> pi1_pong_counter_event_loop = |
| 611 | simulated_event_loop_factory.MakeEventLoop("pi1_pong_counter", pi1); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 612 | MessageCounter<examples::Pong> pi1_pong_counter( |
| 613 | pi1_pong_counter_event_loop.get(), "/test"); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 614 | aos::Fetcher<examples::Ping> ping_on_pi1_fetcher = |
| 615 | pi1_pong_counter_event_loop->MakeFetcher<examples::Ping>("/test"); |
| 616 | aos::Fetcher<message_bridge::Timestamp> pi1_on_pi1_timestamp_fetcher = |
| 617 | pi1_pong_counter_event_loop->MakeFetcher<message_bridge::Timestamp>( |
| 618 | "/aos"); |
| 619 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 620 | // Count timestamps. |
| 621 | MessageCounter<message_bridge::Timestamp> pi1_on_pi1_timestamp_counter( |
| 622 | pi1_pong_counter_event_loop.get(), "/pi1/aos"); |
| 623 | MessageCounter<message_bridge::Timestamp> pi1_on_pi2_timestamp_counter( |
| 624 | pi2_pong_counter_event_loop.get(), "/pi1/aos"); |
| 625 | MessageCounter<message_bridge::Timestamp> pi1_on_pi3_timestamp_counter( |
| 626 | pi3_pong_counter_event_loop.get(), "/pi1/aos"); |
| 627 | MessageCounter<message_bridge::Timestamp> pi2_on_pi1_timestamp_counter( |
| 628 | pi1_pong_counter_event_loop.get(), "/pi2/aos"); |
| 629 | MessageCounter<message_bridge::Timestamp> pi2_on_pi2_timestamp_counter( |
| 630 | pi2_pong_counter_event_loop.get(), "/pi2/aos"); |
| 631 | MessageCounter<message_bridge::Timestamp> pi3_on_pi1_timestamp_counter( |
| 632 | pi1_pong_counter_event_loop.get(), "/pi3/aos"); |
| 633 | MessageCounter<message_bridge::Timestamp> pi3_on_pi3_timestamp_counter( |
| 634 | pi3_pong_counter_event_loop.get(), "/pi3/aos"); |
| 635 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 636 | // Count remote timestamps |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 637 | std::vector<std::unique_ptr<MessageCounter<RemoteMessage>>> |
| 638 | remote_timestamps_pi2_on_pi1 = |
| 639 | MakePi2OnPi1MessageCounters(pi1_pong_counter_event_loop.get()); |
| 640 | std::vector<std::unique_ptr<MessageCounter<RemoteMessage>>> |
| 641 | remote_timestamps_pi1_on_pi2 = |
| 642 | MakePi1OnPi2MessageCounters(pi2_pong_counter_event_loop.get()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 643 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 644 | // Wait to let timestamp estimation start up before looking for the results. |
| 645 | simulated_event_loop_factory.RunFor(chrono::milliseconds(500)); |
| 646 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 647 | std::unique_ptr<EventLoop> pi1_statistics_counter_event_loop = |
| 648 | simulated_event_loop_factory.MakeEventLoop("pi1_statistics_counter", pi1); |
| 649 | std::unique_ptr<EventLoop> pi2_statistics_counter_event_loop = |
| 650 | simulated_event_loop_factory.MakeEventLoop("pi2_statistics_counter", pi2); |
| 651 | std::unique_ptr<EventLoop> pi3_statistics_counter_event_loop = |
| 652 | simulated_event_loop_factory.MakeEventLoop("pi3_statistics_counter", pi3); |
| 653 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 654 | int pi1_server_statistics_count = 0; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 655 | pi1_statistics_counter_event_loop->MakeWatcher( |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 656 | "/pi1/aos", [&pi1_server_statistics_count]( |
| 657 | const message_bridge::ServerStatistics &stats) { |
| 658 | VLOG(1) << "pi1 ServerStatistics " << FlatbufferToJson(&stats); |
| 659 | EXPECT_EQ(stats.connections()->size(), 2u); |
| 660 | for (const message_bridge::ServerConnection *connection : |
| 661 | *stats.connections()) { |
| 662 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 663 | EXPECT_EQ(connection->connection_count(), 1u); |
| 664 | EXPECT_EQ(connection->connected_since_time(), 0); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 665 | EXPECT_TRUE(connection->has_boot_uuid()); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 666 | if (connection->node()->name()->string_view() == "pi2") { |
| 667 | EXPECT_GT(connection->sent_packets(), 50); |
| 668 | } else if (connection->node()->name()->string_view() == "pi3") { |
| 669 | EXPECT_GE(connection->sent_packets(), 5); |
| 670 | } else { |
| 671 | LOG(FATAL) << "Unknown connection"; |
| 672 | } |
| 673 | |
| 674 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 675 | EXPECT_EQ(connection->monotonic_offset(), 0); |
| 676 | } |
| 677 | ++pi1_server_statistics_count; |
| 678 | }); |
| 679 | |
| 680 | int pi2_server_statistics_count = 0; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 681 | pi2_statistics_counter_event_loop->MakeWatcher( |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 682 | "/pi2/aos", [&pi2_server_statistics_count]( |
| 683 | const message_bridge::ServerStatistics &stats) { |
| 684 | VLOG(1) << "pi2 ServerStatistics " << FlatbufferToJson(&stats); |
| 685 | EXPECT_EQ(stats.connections()->size(), 1u); |
| 686 | |
| 687 | const message_bridge::ServerConnection *connection = |
| 688 | stats.connections()->Get(0); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 689 | EXPECT_TRUE(connection->has_boot_uuid()); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 690 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
| 691 | EXPECT_GT(connection->sent_packets(), 50); |
| 692 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 693 | EXPECT_EQ(connection->monotonic_offset(), 0); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 694 | EXPECT_EQ(connection->connection_count(), 1u); |
| 695 | EXPECT_EQ(connection->connected_since_time(), 0); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 696 | ++pi2_server_statistics_count; |
| 697 | }); |
| 698 | |
| 699 | int pi3_server_statistics_count = 0; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 700 | pi3_statistics_counter_event_loop->MakeWatcher( |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 701 | "/pi3/aos", [&pi3_server_statistics_count]( |
| 702 | const message_bridge::ServerStatistics &stats) { |
| 703 | VLOG(1) << "pi3 ServerStatistics " << FlatbufferToJson(&stats); |
| 704 | EXPECT_EQ(stats.connections()->size(), 1u); |
| 705 | |
| 706 | const message_bridge::ServerConnection *connection = |
| 707 | stats.connections()->Get(0); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 708 | EXPECT_TRUE(connection->has_boot_uuid()); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 709 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
| 710 | EXPECT_GE(connection->sent_packets(), 5); |
| 711 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 712 | EXPECT_EQ(connection->monotonic_offset(), 0); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 713 | EXPECT_EQ(connection->connection_count(), 1u); |
| 714 | EXPECT_EQ(connection->connected_since_time(), 0); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 715 | ++pi3_server_statistics_count; |
| 716 | }); |
| 717 | |
| 718 | int pi1_client_statistics_count = 0; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 719 | pi1_statistics_counter_event_loop->MakeWatcher( |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 720 | "/pi1/aos", [&pi1_client_statistics_count]( |
| 721 | const message_bridge::ClientStatistics &stats) { |
| 722 | VLOG(1) << "pi1 ClientStatistics " << FlatbufferToJson(&stats); |
| 723 | EXPECT_EQ(stats.connections()->size(), 2u); |
| 724 | |
| 725 | for (const message_bridge::ClientConnection *connection : |
| 726 | *stats.connections()) { |
| 727 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
| 728 | if (connection->node()->name()->string_view() == "pi2") { |
| 729 | EXPECT_GT(connection->received_packets(), 50); |
| 730 | } else if (connection->node()->name()->string_view() == "pi3") { |
| 731 | EXPECT_GE(connection->received_packets(), 5); |
| 732 | } else { |
| 733 | LOG(FATAL) << "Unknown connection"; |
| 734 | } |
| 735 | |
Austin Schuh | e61d438 | 2021-03-31 21:33:02 -0700 | [diff] [blame] | 736 | EXPECT_EQ(connection->partial_deliveries(), 0); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 737 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 738 | EXPECT_EQ(connection->monotonic_offset(), 150000); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 739 | EXPECT_EQ(connection->connection_count(), 1u); |
| 740 | EXPECT_EQ(connection->connected_since_time(), 0); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 741 | } |
| 742 | ++pi1_client_statistics_count; |
| 743 | }); |
| 744 | |
| 745 | int pi2_client_statistics_count = 0; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 746 | pi2_statistics_counter_event_loop->MakeWatcher( |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 747 | "/pi2/aos", [&pi2_client_statistics_count]( |
| 748 | const message_bridge::ClientStatistics &stats) { |
| 749 | VLOG(1) << "pi2 ClientStatistics " << FlatbufferToJson(&stats); |
| 750 | EXPECT_EQ(stats.connections()->size(), 1u); |
| 751 | |
| 752 | const message_bridge::ClientConnection *connection = |
| 753 | stats.connections()->Get(0); |
| 754 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
| 755 | EXPECT_GT(connection->received_packets(), 50); |
Austin Schuh | e61d438 | 2021-03-31 21:33:02 -0700 | [diff] [blame] | 756 | EXPECT_EQ(connection->partial_deliveries(), 0); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 757 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 758 | EXPECT_EQ(connection->monotonic_offset(), 150000); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 759 | EXPECT_EQ(connection->connection_count(), 1u); |
| 760 | EXPECT_EQ(connection->connected_since_time(), 0); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 761 | ++pi2_client_statistics_count; |
| 762 | }); |
| 763 | |
| 764 | int pi3_client_statistics_count = 0; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 765 | pi3_statistics_counter_event_loop->MakeWatcher( |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 766 | "/pi3/aos", [&pi3_client_statistics_count]( |
| 767 | const message_bridge::ClientStatistics &stats) { |
| 768 | VLOG(1) << "pi3 ClientStatistics " << FlatbufferToJson(&stats); |
| 769 | EXPECT_EQ(stats.connections()->size(), 1u); |
| 770 | |
| 771 | const message_bridge::ClientConnection *connection = |
| 772 | stats.connections()->Get(0); |
| 773 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
| 774 | EXPECT_GE(connection->received_packets(), 5); |
Austin Schuh | e61d438 | 2021-03-31 21:33:02 -0700 | [diff] [blame] | 775 | EXPECT_EQ(connection->partial_deliveries(), 0); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 776 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 777 | EXPECT_EQ(connection->monotonic_offset(), 150000); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 778 | EXPECT_EQ(connection->connection_count(), 1u); |
| 779 | EXPECT_EQ(connection->connected_since_time(), 0); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 780 | ++pi3_client_statistics_count; |
| 781 | }); |
| 782 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 783 | // Find the channel index for both the /pi1/aos Timestamp channel and Ping |
| 784 | // channel. |
| 785 | const size_t pi1_timestamp_channel = |
| 786 | configuration::ChannelIndex(pi1_pong_counter_event_loop->configuration(), |
| 787 | pi1_on_pi2_timestamp_fetcher.channel()); |
| 788 | const size_t ping_timestamp_channel = |
| 789 | configuration::ChannelIndex(pi1_pong_counter_event_loop->configuration(), |
| 790 | ping_on_pi2_fetcher.channel()); |
| 791 | |
| 792 | for (const Channel *channel : |
| 793 | *pi1_pong_counter_event_loop->configuration()->channels()) { |
| 794 | VLOG(1) << "Channel " |
| 795 | << configuration::ChannelIndex( |
| 796 | pi1_pong_counter_event_loop->configuration(), channel) |
| 797 | << " " << configuration::CleanedChannelToString(channel); |
| 798 | } |
| 799 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 800 | std::unique_ptr<EventLoop> pi1_remote_timestamp = |
| 801 | simulated_event_loop_factory.MakeEventLoop("pi1_remote_timestamp", pi1); |
| 802 | |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 803 | for (std::pair<int, std::string> channel : |
| 804 | shared() |
| 805 | ? std::vector<std::pair< |
| 806 | int, std::string>>{{-1, "/pi1/aos/remote_timestamps/pi2"}} |
| 807 | : std::vector<std::pair<int, std::string>>{ |
| 808 | {pi1_timestamp_channel, |
| 809 | "/pi1/aos/remote_timestamps/pi2/pi1/aos/" |
| 810 | "aos-message_bridge-Timestamp"}, |
| 811 | {ping_timestamp_channel, |
| 812 | "/pi1/aos/remote_timestamps/pi2/test/aos-examples-Ping"}}) { |
| 813 | // For each remote timestamp we get back, confirm that it is either a ping |
| 814 | // message, or a timestamp we sent out. Also confirm that the timestamps |
| 815 | // are correct. |
| 816 | pi1_remote_timestamp->MakeWatcher( |
| 817 | channel.second, |
| 818 | [pi1_timestamp_channel, ping_timestamp_channel, &ping_on_pi2_fetcher, |
| 819 | &ping_on_pi1_fetcher, &pi1_on_pi2_timestamp_fetcher, |
| 820 | &pi1_on_pi1_timestamp_fetcher, &simulated_event_loop_factory, pi2, |
| 821 | channel_index = channel.first](const RemoteMessage &header) { |
| 822 | VLOG(1) << aos::FlatbufferToJson(&header); |
| 823 | EXPECT_TRUE(header.has_boot_uuid()); |
Austin Schuh | cdd9027 | 2021-03-15 12:46:16 -0700 | [diff] [blame] | 824 | EXPECT_EQ(UUID::FromVector(header.boot_uuid()), |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 825 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi2) |
Austin Schuh | cdd9027 | 2021-03-15 12:46:16 -0700 | [diff] [blame] | 826 | ->boot_uuid()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 827 | |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 828 | const aos::monotonic_clock::time_point header_monotonic_sent_time( |
| 829 | chrono::nanoseconds(header.monotonic_sent_time())); |
| 830 | const aos::realtime_clock::time_point header_realtime_sent_time( |
| 831 | chrono::nanoseconds(header.realtime_sent_time())); |
| 832 | const aos::monotonic_clock::time_point header_monotonic_remote_time( |
| 833 | chrono::nanoseconds(header.monotonic_remote_time())); |
| 834 | const aos::realtime_clock::time_point header_realtime_remote_time( |
| 835 | chrono::nanoseconds(header.realtime_remote_time())); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 836 | |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 837 | if (channel_index != -1) { |
| 838 | ASSERT_EQ(channel_index, header.channel_index()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 841 | const Context *pi1_context = nullptr; |
| 842 | const Context *pi2_context = nullptr; |
| 843 | |
| 844 | if (header.channel_index() == pi1_timestamp_channel) { |
| 845 | // Find the forwarded message. |
| 846 | while (pi1_on_pi2_timestamp_fetcher.context().monotonic_event_time < |
| 847 | header_monotonic_sent_time) { |
| 848 | ASSERT_TRUE(pi1_on_pi2_timestamp_fetcher.FetchNext()); |
| 849 | } |
| 850 | |
| 851 | // And the source message. |
| 852 | while (pi1_on_pi1_timestamp_fetcher.context().monotonic_event_time < |
| 853 | header_monotonic_remote_time) { |
| 854 | ASSERT_TRUE(pi1_on_pi1_timestamp_fetcher.FetchNext()); |
| 855 | } |
| 856 | |
| 857 | pi1_context = &pi1_on_pi1_timestamp_fetcher.context(); |
| 858 | pi2_context = &pi1_on_pi2_timestamp_fetcher.context(); |
| 859 | } else if (header.channel_index() == ping_timestamp_channel) { |
| 860 | // Find the forwarded message. |
| 861 | while (ping_on_pi2_fetcher.context().monotonic_event_time < |
| 862 | header_monotonic_sent_time) { |
| 863 | ASSERT_TRUE(ping_on_pi2_fetcher.FetchNext()); |
| 864 | } |
| 865 | |
| 866 | // And the source message. |
| 867 | while (ping_on_pi1_fetcher.context().monotonic_event_time < |
| 868 | header_monotonic_remote_time) { |
| 869 | ASSERT_TRUE(ping_on_pi1_fetcher.FetchNext()); |
| 870 | } |
| 871 | |
| 872 | pi1_context = &ping_on_pi1_fetcher.context(); |
| 873 | pi2_context = &ping_on_pi2_fetcher.context(); |
| 874 | } else { |
| 875 | LOG(FATAL) << "Unknown channel"; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 876 | } |
| 877 | |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 878 | // Confirm the forwarded message has matching timestamps to the |
| 879 | // timestamps we got back. |
| 880 | EXPECT_EQ(pi2_context->queue_index, header.queue_index()); |
| 881 | EXPECT_EQ(pi2_context->remote_queue_index, |
| 882 | header.remote_queue_index()); |
| 883 | EXPECT_EQ(pi2_context->monotonic_event_time, |
| 884 | header_monotonic_sent_time); |
| 885 | EXPECT_EQ(pi2_context->realtime_event_time, |
| 886 | header_realtime_sent_time); |
| 887 | EXPECT_EQ(pi2_context->realtime_remote_time, |
| 888 | header_realtime_remote_time); |
| 889 | EXPECT_EQ(pi2_context->monotonic_remote_time, |
| 890 | header_monotonic_remote_time); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 891 | |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 892 | // Confirm the forwarded message also matches the source message. |
| 893 | EXPECT_EQ(pi1_context->queue_index, header.remote_queue_index()); |
| 894 | EXPECT_EQ(pi1_context->monotonic_event_time, |
| 895 | header_monotonic_remote_time); |
| 896 | EXPECT_EQ(pi1_context->realtime_event_time, |
| 897 | header_realtime_remote_time); |
| 898 | }); |
| 899 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 900 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 901 | simulated_event_loop_factory.RunFor(chrono::seconds(10) - |
| 902 | chrono::milliseconds(500) + |
| 903 | chrono::milliseconds(5)); |
| 904 | |
| 905 | EXPECT_EQ(pi1_pong_counter.count(), 1001); |
| 906 | EXPECT_EQ(pi2_pong_counter.count(), 1001); |
| 907 | |
| 908 | EXPECT_EQ(pi1_on_pi1_timestamp_counter.count(), 100); |
| 909 | EXPECT_EQ(pi1_on_pi2_timestamp_counter.count(), 100); |
| 910 | EXPECT_EQ(pi1_on_pi3_timestamp_counter.count(), 100); |
| 911 | EXPECT_EQ(pi2_on_pi1_timestamp_counter.count(), 100); |
| 912 | EXPECT_EQ(pi2_on_pi2_timestamp_counter.count(), 100); |
| 913 | EXPECT_EQ(pi3_on_pi1_timestamp_counter.count(), 100); |
| 914 | EXPECT_EQ(pi3_on_pi3_timestamp_counter.count(), 100); |
| 915 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 916 | EXPECT_EQ(pi1_server_statistics_count, 10); |
| 917 | EXPECT_EQ(pi2_server_statistics_count, 10); |
| 918 | EXPECT_EQ(pi3_server_statistics_count, 10); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 919 | |
| 920 | EXPECT_EQ(pi1_client_statistics_count, 95); |
| 921 | EXPECT_EQ(pi2_client_statistics_count, 95); |
| 922 | EXPECT_EQ(pi3_client_statistics_count, 95); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 923 | |
| 924 | // Also confirm that remote timestamps are being forwarded correctly. |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 925 | EXPECT_EQ(CountAll(remote_timestamps_pi2_on_pi1), 1101); |
| 926 | EXPECT_EQ(CountAll(remote_timestamps_pi1_on_pi2), 1101); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | // Tests that an offset between nodes can be recovered and shows up in |
| 930 | // ServerStatistics correctly. |
| 931 | TEST(SimulatedEventLoopTest, MultinodePingPongWithOffset) { |
| 932 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 933 | aos::configuration::ReadConfig(ArtifactPath( |
| 934 | "aos/events/multinode_pingpong_test_combined_config.json")); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 935 | const Node *pi1 = configuration::GetNode(&config.message(), "pi1"); |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 936 | const size_t pi1_index = configuration::GetNodeIndex(&config.message(), pi1); |
| 937 | ASSERT_EQ(pi1_index, 0u); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 938 | const Node *pi2 = configuration::GetNode(&config.message(), "pi2"); |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 939 | const size_t pi2_index = configuration::GetNodeIndex(&config.message(), pi2); |
| 940 | ASSERT_EQ(pi2_index, 1u); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 941 | const Node *pi3 = configuration::GetNode(&config.message(), "pi3"); |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 942 | const size_t pi3_index = configuration::GetNodeIndex(&config.message(), pi3); |
| 943 | ASSERT_EQ(pi3_index, 2u); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 944 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 945 | message_bridge::TestingTimeConverter time( |
| 946 | configuration::NodesCount(&config.message())); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 947 | SimulatedEventLoopFactory simulated_event_loop_factory(&config.message()); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 948 | simulated_event_loop_factory.SetTimeConverter(&time); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 949 | |
| 950 | constexpr chrono::milliseconds kOffset{1501}; |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 951 | time.AddNextTimestamp( |
| 952 | distributed_clock::epoch(), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 953 | {BootTimestamp::epoch(), BootTimestamp::epoch() + kOffset, |
| 954 | BootTimestamp::epoch()}); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 955 | |
| 956 | std::unique_ptr<EventLoop> ping_event_loop = |
| 957 | simulated_event_loop_factory.MakeEventLoop("ping", pi1); |
| 958 | Ping ping(ping_event_loop.get()); |
| 959 | |
| 960 | std::unique_ptr<EventLoop> pong_event_loop = |
| 961 | simulated_event_loop_factory.MakeEventLoop("pong", pi2); |
| 962 | Pong pong(pong_event_loop.get()); |
| 963 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 964 | // Wait to let timestamp estimation start up before looking for the results. |
| 965 | simulated_event_loop_factory.RunFor(chrono::milliseconds(500)); |
| 966 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 967 | std::unique_ptr<EventLoop> pi1_pong_counter_event_loop = |
| 968 | simulated_event_loop_factory.MakeEventLoop("pi1_pong_counter", pi1); |
| 969 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 970 | std::unique_ptr<EventLoop> pi2_pong_counter_event_loop = |
| 971 | simulated_event_loop_factory.MakeEventLoop("pi2_pong_counter", pi2); |
| 972 | |
| 973 | std::unique_ptr<EventLoop> pi3_pong_counter_event_loop = |
| 974 | simulated_event_loop_factory.MakeEventLoop("pi3_pong_counter", pi3); |
| 975 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 976 | // Confirm the offsets are being recovered correctly. |
| 977 | int pi1_server_statistics_count = 0; |
| 978 | pi1_pong_counter_event_loop->MakeWatcher( |
| 979 | "/pi1/aos", [&pi1_server_statistics_count, |
| 980 | kOffset](const message_bridge::ServerStatistics &stats) { |
| 981 | VLOG(1) << "pi1 ServerStatistics " << FlatbufferToJson(&stats); |
| 982 | EXPECT_EQ(stats.connections()->size(), 2u); |
| 983 | for (const message_bridge::ServerConnection *connection : |
| 984 | *stats.connections()) { |
| 985 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 986 | EXPECT_TRUE(connection->has_boot_uuid()); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 987 | if (connection->node()->name()->string_view() == "pi2") { |
| 988 | EXPECT_EQ(connection->monotonic_offset(), |
| 989 | chrono::nanoseconds(kOffset).count()); |
| 990 | } else if (connection->node()->name()->string_view() == "pi3") { |
| 991 | EXPECT_EQ(connection->monotonic_offset(), 0); |
| 992 | } else { |
| 993 | LOG(FATAL) << "Unknown connection"; |
| 994 | } |
| 995 | |
| 996 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 997 | } |
| 998 | ++pi1_server_statistics_count; |
| 999 | }); |
| 1000 | |
| 1001 | int pi2_server_statistics_count = 0; |
| 1002 | pi2_pong_counter_event_loop->MakeWatcher( |
| 1003 | "/pi2/aos", [&pi2_server_statistics_count, |
| 1004 | kOffset](const message_bridge::ServerStatistics &stats) { |
| 1005 | VLOG(1) << "pi2 ServerStatistics " << FlatbufferToJson(&stats); |
| 1006 | EXPECT_EQ(stats.connections()->size(), 1u); |
| 1007 | |
| 1008 | const message_bridge::ServerConnection *connection = |
| 1009 | stats.connections()->Get(0); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1010 | EXPECT_TRUE(connection->has_boot_uuid()); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1011 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
| 1012 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 1013 | EXPECT_EQ(connection->monotonic_offset(), |
| 1014 | -chrono::nanoseconds(kOffset).count()); |
| 1015 | ++pi2_server_statistics_count; |
| 1016 | }); |
| 1017 | |
| 1018 | int pi3_server_statistics_count = 0; |
| 1019 | pi3_pong_counter_event_loop->MakeWatcher( |
| 1020 | "/pi3/aos", [&pi3_server_statistics_count]( |
| 1021 | const message_bridge::ServerStatistics &stats) { |
| 1022 | VLOG(1) << "pi3 ServerStatistics " << FlatbufferToJson(&stats); |
| 1023 | EXPECT_EQ(stats.connections()->size(), 1u); |
| 1024 | |
| 1025 | const message_bridge::ServerConnection *connection = |
| 1026 | stats.connections()->Get(0); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1027 | EXPECT_TRUE(connection->has_boot_uuid()); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1028 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
| 1029 | EXPECT_TRUE(connection->has_monotonic_offset()); |
| 1030 | EXPECT_EQ(connection->monotonic_offset(), 0); |
| 1031 | ++pi3_server_statistics_count; |
| 1032 | }); |
| 1033 | |
| 1034 | simulated_event_loop_factory.RunFor(chrono::seconds(10) - |
| 1035 | chrono::milliseconds(500) + |
| 1036 | chrono::milliseconds(5)); |
| 1037 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1038 | EXPECT_EQ(pi1_server_statistics_count, 10); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1039 | EXPECT_EQ(pi2_server_statistics_count, 10); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1040 | EXPECT_EQ(pi3_server_statistics_count, 10); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | // Test that disabling statistics actually disables them. |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 1044 | TEST_P(RemoteMessageSimulatedEventLoopTest, MultinodeWithoutStatistics) { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1045 | const Node *pi1 = configuration::GetNode(&config.message(), "pi1"); |
| 1046 | const Node *pi2 = configuration::GetNode(&config.message(), "pi2"); |
| 1047 | const Node *pi3 = configuration::GetNode(&config.message(), "pi3"); |
| 1048 | |
| 1049 | SimulatedEventLoopFactory simulated_event_loop_factory(&config.message()); |
| 1050 | simulated_event_loop_factory.DisableStatistics(); |
| 1051 | |
| 1052 | std::unique_ptr<EventLoop> ping_event_loop = |
| 1053 | simulated_event_loop_factory.MakeEventLoop("ping", pi1); |
| 1054 | Ping ping(ping_event_loop.get()); |
| 1055 | |
| 1056 | std::unique_ptr<EventLoop> pong_event_loop = |
| 1057 | simulated_event_loop_factory.MakeEventLoop("pong", pi2); |
| 1058 | Pong pong(pong_event_loop.get()); |
| 1059 | |
| 1060 | std::unique_ptr<EventLoop> pi2_pong_counter_event_loop = |
| 1061 | simulated_event_loop_factory.MakeEventLoop("pi2_pong_counter", pi2); |
| 1062 | |
| 1063 | MessageCounter<examples::Pong> pi2_pong_counter( |
| 1064 | pi2_pong_counter_event_loop.get(), "/test"); |
| 1065 | |
| 1066 | std::unique_ptr<EventLoop> pi3_pong_counter_event_loop = |
| 1067 | simulated_event_loop_factory.MakeEventLoop("pi3_pong_counter", pi3); |
| 1068 | |
| 1069 | std::unique_ptr<EventLoop> pi1_pong_counter_event_loop = |
| 1070 | simulated_event_loop_factory.MakeEventLoop("pi1_pong_counter", pi1); |
| 1071 | |
| 1072 | MessageCounter<examples::Pong> pi1_pong_counter( |
| 1073 | pi1_pong_counter_event_loop.get(), "/test"); |
| 1074 | |
| 1075 | // Count timestamps. |
| 1076 | MessageCounter<message_bridge::Timestamp> pi1_on_pi1_timestamp_counter( |
| 1077 | pi1_pong_counter_event_loop.get(), "/pi1/aos"); |
| 1078 | MessageCounter<message_bridge::Timestamp> pi1_on_pi2_timestamp_counter( |
| 1079 | pi2_pong_counter_event_loop.get(), "/pi1/aos"); |
| 1080 | MessageCounter<message_bridge::Timestamp> pi1_on_pi3_timestamp_counter( |
| 1081 | pi3_pong_counter_event_loop.get(), "/pi1/aos"); |
| 1082 | MessageCounter<message_bridge::Timestamp> pi2_on_pi1_timestamp_counter( |
| 1083 | pi1_pong_counter_event_loop.get(), "/pi2/aos"); |
| 1084 | MessageCounter<message_bridge::Timestamp> pi2_on_pi2_timestamp_counter( |
| 1085 | pi2_pong_counter_event_loop.get(), "/pi2/aos"); |
| 1086 | MessageCounter<message_bridge::Timestamp> pi3_on_pi1_timestamp_counter( |
| 1087 | pi1_pong_counter_event_loop.get(), "/pi3/aos"); |
| 1088 | MessageCounter<message_bridge::Timestamp> pi3_on_pi3_timestamp_counter( |
| 1089 | pi3_pong_counter_event_loop.get(), "/pi3/aos"); |
| 1090 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1091 | // Count remote timestamps |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 1092 | std::vector<std::unique_ptr<MessageCounter<RemoteMessage>>> |
| 1093 | remote_timestamps_pi2_on_pi1 = |
| 1094 | MakePi2OnPi1MessageCounters(pi1_pong_counter_event_loop.get()); |
| 1095 | std::vector<std::unique_ptr<MessageCounter<RemoteMessage>>> |
| 1096 | remote_timestamps_pi1_on_pi2 = |
| 1097 | MakePi1OnPi2MessageCounters(pi2_pong_counter_event_loop.get()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1098 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1099 | MessageCounter<message_bridge::ServerStatistics> |
| 1100 | pi1_server_statistics_counter(pi1_pong_counter_event_loop.get(), |
| 1101 | "/pi1/aos"); |
| 1102 | MessageCounter<message_bridge::ServerStatistics> |
| 1103 | pi2_server_statistics_counter(pi2_pong_counter_event_loop.get(), |
| 1104 | "/pi2/aos"); |
| 1105 | MessageCounter<message_bridge::ServerStatistics> |
| 1106 | pi3_server_statistics_counter(pi3_pong_counter_event_loop.get(), |
| 1107 | "/pi3/aos"); |
| 1108 | |
| 1109 | MessageCounter<message_bridge::ClientStatistics> |
| 1110 | pi1_client_statistics_counter(pi1_pong_counter_event_loop.get(), |
| 1111 | "/pi1/aos"); |
| 1112 | MessageCounter<message_bridge::ClientStatistics> |
| 1113 | pi2_client_statistics_counter(pi2_pong_counter_event_loop.get(), |
| 1114 | "/pi2/aos"); |
| 1115 | MessageCounter<message_bridge::ClientStatistics> |
| 1116 | pi3_client_statistics_counter(pi3_pong_counter_event_loop.get(), |
| 1117 | "/pi3/aos"); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 1118 | |
| 1119 | simulated_event_loop_factory.RunFor(chrono::seconds(10) + |
| 1120 | chrono::milliseconds(5)); |
| 1121 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1122 | EXPECT_EQ(pi1_pong_counter.count(), 1001u); |
| 1123 | EXPECT_EQ(pi2_pong_counter.count(), 1001u); |
| 1124 | |
| 1125 | EXPECT_EQ(pi1_on_pi1_timestamp_counter.count(), 0u); |
| 1126 | EXPECT_EQ(pi1_on_pi2_timestamp_counter.count(), 0u); |
| 1127 | EXPECT_EQ(pi1_on_pi3_timestamp_counter.count(), 0u); |
| 1128 | EXPECT_EQ(pi2_on_pi1_timestamp_counter.count(), 0u); |
| 1129 | EXPECT_EQ(pi2_on_pi2_timestamp_counter.count(), 0u); |
| 1130 | EXPECT_EQ(pi3_on_pi1_timestamp_counter.count(), 0u); |
| 1131 | EXPECT_EQ(pi3_on_pi3_timestamp_counter.count(), 0u); |
| 1132 | |
| 1133 | EXPECT_EQ(pi1_server_statistics_counter.count(), 0u); |
| 1134 | EXPECT_EQ(pi2_server_statistics_counter.count(), 0u); |
| 1135 | EXPECT_EQ(pi3_server_statistics_counter.count(), 0u); |
| 1136 | |
| 1137 | EXPECT_EQ(pi1_client_statistics_counter.count(), 0u); |
| 1138 | EXPECT_EQ(pi2_client_statistics_counter.count(), 0u); |
| 1139 | EXPECT_EQ(pi3_client_statistics_counter.count(), 0u); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1140 | |
| 1141 | // Also confirm that remote timestamps are being forwarded correctly. |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 1142 | EXPECT_EQ(CountAll(remote_timestamps_pi2_on_pi1), 1001); |
| 1143 | EXPECT_EQ(CountAll(remote_timestamps_pi1_on_pi2), 1001); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 1144 | } |
| 1145 | |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1146 | bool AllConnected(const message_bridge::ServerStatistics *server_statistics) { |
| 1147 | for (const message_bridge::ServerConnection *connection : |
| 1148 | *server_statistics->connections()) { |
| 1149 | if (connection->state() != message_bridge::State::CONNECTED) { |
| 1150 | return false; |
| 1151 | } |
| 1152 | } |
| 1153 | return true; |
| 1154 | } |
| 1155 | |
| 1156 | bool AllConnectedBut(const message_bridge::ServerStatistics *server_statistics, |
| 1157 | std::string_view target) { |
| 1158 | for (const message_bridge::ServerConnection *connection : |
| 1159 | *server_statistics->connections()) { |
| 1160 | if (connection->node()->name()->string_view() == target) { |
| 1161 | if (connection->state() == message_bridge::State::CONNECTED) { |
| 1162 | return false; |
| 1163 | } |
| 1164 | } else { |
| 1165 | if (connection->state() != message_bridge::State::CONNECTED) { |
| 1166 | return false; |
| 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | return true; |
| 1171 | } |
| 1172 | |
| 1173 | bool AllConnected(const message_bridge::ClientStatistics *client_statistics) { |
| 1174 | for (const message_bridge::ClientConnection *connection : |
| 1175 | *client_statistics->connections()) { |
| 1176 | if (connection->state() != message_bridge::State::CONNECTED) { |
| 1177 | return false; |
| 1178 | } |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 1179 | EXPECT_TRUE(connection->has_boot_uuid()); |
| 1180 | EXPECT_TRUE(connection->has_connected_since_time()); |
| 1181 | EXPECT_TRUE(connection->has_connection_count()); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1182 | } |
| 1183 | return true; |
| 1184 | } |
| 1185 | |
| 1186 | bool AllConnectedBut(const message_bridge::ClientStatistics *client_statistics, |
| 1187 | std::string_view target) { |
| 1188 | for (const message_bridge::ClientConnection *connection : |
| 1189 | *client_statistics->connections()) { |
| 1190 | if (connection->node()->name()->string_view() == target) { |
| 1191 | if (connection->state() == message_bridge::State::CONNECTED) { |
| 1192 | return false; |
| 1193 | } |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 1194 | EXPECT_FALSE(connection->has_boot_uuid()); |
| 1195 | EXPECT_FALSE(connection->has_connected_since_time()); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1196 | } else { |
| 1197 | if (connection->state() != message_bridge::State::CONNECTED) { |
| 1198 | return false; |
| 1199 | } |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 1200 | EXPECT_TRUE(connection->has_boot_uuid()); |
| 1201 | EXPECT_TRUE(connection->has_connected_since_time()); |
| 1202 | EXPECT_TRUE(connection->has_connection_count()); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1203 | } |
| 1204 | } |
| 1205 | return true; |
| 1206 | } |
| 1207 | |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 1208 | int ConnectedCount(const message_bridge::ClientStatistics *client_statistics, |
| 1209 | std::string_view target) { |
| 1210 | for (const message_bridge::ClientConnection *connection : |
| 1211 | *client_statistics->connections()) { |
| 1212 | if (connection->node()->name()->string_view() == target) { |
| 1213 | return connection->connection_count(); |
| 1214 | } |
| 1215 | } |
| 1216 | return 0; |
| 1217 | } |
| 1218 | |
| 1219 | int ConnectedCount(const message_bridge::ServerStatistics *server_statistics, |
| 1220 | std::string_view target) { |
| 1221 | for (const message_bridge::ServerConnection *connection : |
| 1222 | *server_statistics->connections()) { |
| 1223 | if (connection->node()->name()->string_view() == target) { |
| 1224 | return connection->connection_count(); |
| 1225 | } |
| 1226 | } |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1230 | // Test that disconnecting nodes actually disconnects them. |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 1231 | TEST_P(RemoteMessageSimulatedEventLoopTest, MultinodeDisconnect) { |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1232 | SimulatedEventLoopFactory simulated_event_loop_factory(&config.message()); |
| 1233 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1234 | NodeEventLoopFactory *pi1 = |
| 1235 | simulated_event_loop_factory.GetNodeEventLoopFactory("pi1"); |
| 1236 | NodeEventLoopFactory *pi2 = |
| 1237 | simulated_event_loop_factory.GetNodeEventLoopFactory("pi2"); |
| 1238 | NodeEventLoopFactory *pi3 = |
| 1239 | simulated_event_loop_factory.GetNodeEventLoopFactory("pi3"); |
| 1240 | |
| 1241 | std::unique_ptr<EventLoop> ping_event_loop = pi1->MakeEventLoop("ping"); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1242 | Ping ping(ping_event_loop.get()); |
| 1243 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1244 | std::unique_ptr<EventLoop> pong_event_loop = pi2->MakeEventLoop("pong"); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1245 | Pong pong(pong_event_loop.get()); |
| 1246 | |
| 1247 | std::unique_ptr<EventLoop> pi2_pong_counter_event_loop = |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1248 | pi2->MakeEventLoop("pi2_pong_counter"); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1249 | |
| 1250 | MessageCounter<examples::Pong> pi2_pong_counter( |
| 1251 | pi2_pong_counter_event_loop.get(), "/test"); |
| 1252 | |
| 1253 | std::unique_ptr<EventLoop> pi3_pong_counter_event_loop = |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1254 | pi3->MakeEventLoop("pi3_pong_counter"); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1255 | |
| 1256 | std::unique_ptr<EventLoop> pi1_pong_counter_event_loop = |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1257 | pi1->MakeEventLoop("pi1_pong_counter"); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1258 | |
| 1259 | MessageCounter<examples::Pong> pi1_pong_counter( |
| 1260 | pi1_pong_counter_event_loop.get(), "/test"); |
| 1261 | |
| 1262 | // Count timestamps. |
| 1263 | MessageCounter<message_bridge::Timestamp> pi1_on_pi1_timestamp_counter( |
| 1264 | pi1_pong_counter_event_loop.get(), "/pi1/aos"); |
| 1265 | MessageCounter<message_bridge::Timestamp> pi1_on_pi2_timestamp_counter( |
| 1266 | pi2_pong_counter_event_loop.get(), "/pi1/aos"); |
| 1267 | MessageCounter<message_bridge::Timestamp> pi1_on_pi3_timestamp_counter( |
| 1268 | pi3_pong_counter_event_loop.get(), "/pi1/aos"); |
| 1269 | MessageCounter<message_bridge::Timestamp> pi2_on_pi1_timestamp_counter( |
| 1270 | pi1_pong_counter_event_loop.get(), "/pi2/aos"); |
| 1271 | MessageCounter<message_bridge::Timestamp> pi2_on_pi2_timestamp_counter( |
| 1272 | pi2_pong_counter_event_loop.get(), "/pi2/aos"); |
| 1273 | MessageCounter<message_bridge::Timestamp> pi3_on_pi1_timestamp_counter( |
| 1274 | pi1_pong_counter_event_loop.get(), "/pi3/aos"); |
| 1275 | MessageCounter<message_bridge::Timestamp> pi3_on_pi3_timestamp_counter( |
| 1276 | pi3_pong_counter_event_loop.get(), "/pi3/aos"); |
| 1277 | |
| 1278 | // Count remote timestamps |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 1279 | std::vector<std::unique_ptr<MessageCounter<RemoteMessage>>> |
| 1280 | remote_timestamps_pi2_on_pi1 = |
| 1281 | MakePi2OnPi1MessageCounters(pi1_pong_counter_event_loop.get()); |
| 1282 | std::vector<std::unique_ptr<MessageCounter<RemoteMessage>>> |
| 1283 | remote_timestamps_pi1_on_pi2 = |
| 1284 | MakePi1OnPi2MessageCounters(pi2_pong_counter_event_loop.get()); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1285 | |
| 1286 | MessageCounter<message_bridge::ServerStatistics> |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1287 | *pi1_server_statistics_counter; |
| 1288 | pi1->OnStartup([pi1, &pi1_server_statistics_counter]() { |
| 1289 | pi1_server_statistics_counter = |
| 1290 | pi1->AlwaysStart<MessageCounter<message_bridge::ServerStatistics>>( |
| 1291 | "pi1_server_statistics_counter", "/pi1/aos"); |
| 1292 | }); |
| 1293 | |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1294 | aos::Fetcher<message_bridge::ServerStatistics> pi1_server_statistics_fetcher = |
| 1295 | pi1_pong_counter_event_loop |
| 1296 | ->MakeFetcher<message_bridge::ServerStatistics>("/pi1/aos"); |
| 1297 | aos::Fetcher<message_bridge::ClientStatistics> pi1_client_statistics_fetcher = |
| 1298 | pi1_pong_counter_event_loop |
| 1299 | ->MakeFetcher<message_bridge::ClientStatistics>("/pi1/aos"); |
| 1300 | |
| 1301 | MessageCounter<message_bridge::ServerStatistics> |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1302 | *pi2_server_statistics_counter; |
| 1303 | pi2->OnStartup([pi2, &pi2_server_statistics_counter]() { |
| 1304 | pi2_server_statistics_counter = |
| 1305 | pi2->AlwaysStart<MessageCounter<message_bridge::ServerStatistics>>( |
| 1306 | "pi2_server_statistics_counter", "/pi2/aos"); |
| 1307 | }); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1308 | aos::Fetcher<message_bridge::ServerStatistics> pi2_server_statistics_fetcher = |
| 1309 | pi2_pong_counter_event_loop |
| 1310 | ->MakeFetcher<message_bridge::ServerStatistics>("/pi2/aos"); |
| 1311 | aos::Fetcher<message_bridge::ClientStatistics> pi2_client_statistics_fetcher = |
| 1312 | pi2_pong_counter_event_loop |
| 1313 | ->MakeFetcher<message_bridge::ClientStatistics>("/pi2/aos"); |
| 1314 | |
| 1315 | MessageCounter<message_bridge::ServerStatistics> |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1316 | *pi3_server_statistics_counter; |
| 1317 | pi3->OnStartup([pi3, &pi3_server_statistics_counter]() { |
| 1318 | pi3_server_statistics_counter = |
| 1319 | pi3->AlwaysStart<MessageCounter<message_bridge::ServerStatistics>>( |
| 1320 | "pi3_server_statistics_counter", "/pi3/aos"); |
| 1321 | }); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1322 | aos::Fetcher<message_bridge::ServerStatistics> pi3_server_statistics_fetcher = |
| 1323 | pi3_pong_counter_event_loop |
| 1324 | ->MakeFetcher<message_bridge::ServerStatistics>("/pi3/aos"); |
| 1325 | aos::Fetcher<message_bridge::ClientStatistics> pi3_client_statistics_fetcher = |
| 1326 | pi3_pong_counter_event_loop |
| 1327 | ->MakeFetcher<message_bridge::ClientStatistics>("/pi3/aos"); |
| 1328 | |
| 1329 | MessageCounter<message_bridge::ClientStatistics> |
| 1330 | pi1_client_statistics_counter(pi1_pong_counter_event_loop.get(), |
| 1331 | "/pi1/aos"); |
| 1332 | MessageCounter<message_bridge::ClientStatistics> |
| 1333 | pi2_client_statistics_counter(pi2_pong_counter_event_loop.get(), |
| 1334 | "/pi2/aos"); |
| 1335 | MessageCounter<message_bridge::ClientStatistics> |
| 1336 | pi3_client_statistics_counter(pi3_pong_counter_event_loop.get(), |
| 1337 | "/pi3/aos"); |
| 1338 | |
| 1339 | simulated_event_loop_factory.RunFor(chrono::seconds(2) + |
| 1340 | chrono::milliseconds(5)); |
| 1341 | |
| 1342 | EXPECT_EQ(pi1_pong_counter.count(), 201u); |
| 1343 | EXPECT_EQ(pi2_pong_counter.count(), 201u); |
| 1344 | |
| 1345 | EXPECT_EQ(pi1_on_pi1_timestamp_counter.count(), 20u); |
| 1346 | EXPECT_EQ(pi1_on_pi2_timestamp_counter.count(), 20u); |
| 1347 | EXPECT_EQ(pi1_on_pi3_timestamp_counter.count(), 20u); |
| 1348 | EXPECT_EQ(pi2_on_pi1_timestamp_counter.count(), 20u); |
| 1349 | EXPECT_EQ(pi2_on_pi2_timestamp_counter.count(), 20u); |
| 1350 | EXPECT_EQ(pi3_on_pi1_timestamp_counter.count(), 20u); |
| 1351 | EXPECT_EQ(pi3_on_pi3_timestamp_counter.count(), 20u); |
| 1352 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1353 | EXPECT_EQ(pi1_server_statistics_counter->count(), 2u); |
| 1354 | EXPECT_EQ(pi2_server_statistics_counter->count(), 2u); |
| 1355 | EXPECT_EQ(pi3_server_statistics_counter->count(), 2u); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1356 | |
| 1357 | EXPECT_EQ(pi1_client_statistics_counter.count(), 20u); |
| 1358 | EXPECT_EQ(pi2_client_statistics_counter.count(), 20u); |
| 1359 | EXPECT_EQ(pi3_client_statistics_counter.count(), 20u); |
| 1360 | |
| 1361 | // Also confirm that remote timestamps are being forwarded correctly. |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 1362 | EXPECT_EQ(CountAll(remote_timestamps_pi2_on_pi1), 221); |
| 1363 | EXPECT_EQ(CountAll(remote_timestamps_pi1_on_pi2), 221); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1364 | |
| 1365 | EXPECT_TRUE(pi1_server_statistics_fetcher.Fetch()); |
| 1366 | EXPECT_TRUE(AllConnected(pi1_server_statistics_fetcher.get())) |
| 1367 | << " : " << aos::FlatbufferToJson(pi1_server_statistics_fetcher.get()); |
| 1368 | EXPECT_TRUE(pi1_client_statistics_fetcher.Fetch()); |
| 1369 | EXPECT_TRUE(AllConnected(pi1_client_statistics_fetcher.get())) |
| 1370 | << " : " << aos::FlatbufferToJson(pi1_client_statistics_fetcher.get()); |
| 1371 | EXPECT_TRUE(pi2_server_statistics_fetcher.Fetch()); |
| 1372 | EXPECT_TRUE(AllConnected(pi2_server_statistics_fetcher.get())) |
| 1373 | << " : " << aos::FlatbufferToJson(pi2_server_statistics_fetcher.get()); |
| 1374 | EXPECT_TRUE(pi2_client_statistics_fetcher.Fetch()); |
| 1375 | EXPECT_TRUE(AllConnected(pi2_client_statistics_fetcher.get())) |
| 1376 | << " : " << aos::FlatbufferToJson(pi2_client_statistics_fetcher.get()); |
| 1377 | EXPECT_TRUE(pi3_server_statistics_fetcher.Fetch()); |
| 1378 | EXPECT_TRUE(AllConnected(pi3_server_statistics_fetcher.get())) |
| 1379 | << " : " << aos::FlatbufferToJson(pi3_server_statistics_fetcher.get()); |
| 1380 | EXPECT_TRUE(pi3_client_statistics_fetcher.Fetch()); |
| 1381 | EXPECT_TRUE(AllConnected(pi3_client_statistics_fetcher.get())) |
| 1382 | << " : " << aos::FlatbufferToJson(pi3_client_statistics_fetcher.get()); |
| 1383 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1384 | pi1->Disconnect(pi3->node()); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1385 | |
| 1386 | simulated_event_loop_factory.RunFor(chrono::seconds(2)); |
| 1387 | |
| 1388 | EXPECT_EQ(pi1_pong_counter.count(), 401u); |
| 1389 | EXPECT_EQ(pi2_pong_counter.count(), 401u); |
| 1390 | |
| 1391 | EXPECT_EQ(pi1_on_pi1_timestamp_counter.count(), 40u); |
| 1392 | EXPECT_EQ(pi1_on_pi2_timestamp_counter.count(), 40u); |
| 1393 | EXPECT_EQ(pi1_on_pi3_timestamp_counter.count(), 20u); |
| 1394 | EXPECT_EQ(pi2_on_pi1_timestamp_counter.count(), 40u); |
| 1395 | EXPECT_EQ(pi2_on_pi2_timestamp_counter.count(), 40u); |
| 1396 | EXPECT_EQ(pi3_on_pi1_timestamp_counter.count(), 40u); |
| 1397 | EXPECT_EQ(pi3_on_pi3_timestamp_counter.count(), 40u); |
| 1398 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1399 | EXPECT_EQ(pi1_server_statistics_counter->count(), 4u); |
| 1400 | EXPECT_EQ(pi2_server_statistics_counter->count(), 4u); |
| 1401 | EXPECT_EQ(pi3_server_statistics_counter->count(), 4u); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1402 | |
| 1403 | EXPECT_EQ(pi1_client_statistics_counter.count(), 40u); |
| 1404 | EXPECT_EQ(pi2_client_statistics_counter.count(), 40u); |
| 1405 | EXPECT_EQ(pi3_client_statistics_counter.count(), 40u); |
| 1406 | |
| 1407 | // Also confirm that remote timestamps are being forwarded correctly. |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 1408 | EXPECT_EQ(CountAll(remote_timestamps_pi2_on_pi1), 441); |
| 1409 | EXPECT_EQ(CountAll(remote_timestamps_pi1_on_pi2), 441); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1410 | |
| 1411 | EXPECT_TRUE(pi1_server_statistics_fetcher.Fetch()); |
| 1412 | EXPECT_TRUE(AllConnectedBut(pi1_server_statistics_fetcher.get(), "pi3")) |
| 1413 | << " : " << aos::FlatbufferToJson(pi1_server_statistics_fetcher.get()); |
| 1414 | EXPECT_TRUE(pi1_client_statistics_fetcher.Fetch()); |
| 1415 | EXPECT_TRUE(AllConnected(pi1_client_statistics_fetcher.get())) |
| 1416 | << " : " << aos::FlatbufferToJson(pi1_client_statistics_fetcher.get()); |
| 1417 | EXPECT_TRUE(pi2_server_statistics_fetcher.Fetch()); |
| 1418 | EXPECT_TRUE(AllConnected(pi2_server_statistics_fetcher.get())) |
| 1419 | << " : " << aos::FlatbufferToJson(pi2_server_statistics_fetcher.get()); |
| 1420 | EXPECT_TRUE(pi2_client_statistics_fetcher.Fetch()); |
| 1421 | EXPECT_TRUE(AllConnected(pi2_client_statistics_fetcher.get())) |
| 1422 | << " : " << aos::FlatbufferToJson(pi2_client_statistics_fetcher.get()); |
| 1423 | EXPECT_TRUE(pi3_server_statistics_fetcher.Fetch()); |
| 1424 | EXPECT_TRUE(AllConnected(pi3_server_statistics_fetcher.get())) |
| 1425 | << " : " << aos::FlatbufferToJson(pi3_server_statistics_fetcher.get()); |
| 1426 | EXPECT_TRUE(pi3_client_statistics_fetcher.Fetch()); |
| 1427 | EXPECT_TRUE(AllConnectedBut(pi3_client_statistics_fetcher.get(), "pi1")) |
| 1428 | << " : " << aos::FlatbufferToJson(pi3_client_statistics_fetcher.get()); |
| 1429 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1430 | pi1->Connect(pi3->node()); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1431 | |
| 1432 | simulated_event_loop_factory.RunFor(chrono::seconds(2)); |
| 1433 | |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 1434 | EXPECT_TRUE(pi1_server_statistics_fetcher.Fetch()); |
| 1435 | EXPECT_TRUE(pi1_client_statistics_fetcher.Fetch()); |
| 1436 | EXPECT_TRUE(pi2_server_statistics_fetcher.Fetch()); |
| 1437 | EXPECT_TRUE(pi2_client_statistics_fetcher.Fetch()); |
| 1438 | EXPECT_TRUE(pi3_server_statistics_fetcher.Fetch()); |
| 1439 | EXPECT_TRUE(pi3_client_statistics_fetcher.Fetch()); |
| 1440 | |
| 1441 | EXPECT_EQ(ConnectedCount(pi1_server_statistics_fetcher.get(), "pi3"), 2u) |
| 1442 | << " : " << aos::FlatbufferToJson(pi1_server_statistics_fetcher.get()); |
| 1443 | EXPECT_EQ(ConnectedCount(pi1_server_statistics_fetcher.get(), "pi2"), 1u) |
| 1444 | << " : " << aos::FlatbufferToJson(pi1_server_statistics_fetcher.get()); |
| 1445 | EXPECT_EQ(ConnectedCount(pi1_client_statistics_fetcher.get(), "pi3"), 1u) |
| 1446 | << " : " << aos::FlatbufferToJson(pi1_client_statistics_fetcher.get()); |
| 1447 | EXPECT_EQ(ConnectedCount(pi1_client_statistics_fetcher.get(), "pi2"), 1u) |
| 1448 | << " : " << aos::FlatbufferToJson(pi1_client_statistics_fetcher.get()); |
| 1449 | |
| 1450 | EXPECT_EQ(ConnectedCount(pi2_server_statistics_fetcher.get(), "pi1"), 1u) |
| 1451 | << " : " << aos::FlatbufferToJson(pi2_server_statistics_fetcher.get()); |
| 1452 | EXPECT_EQ(ConnectedCount(pi2_client_statistics_fetcher.get(), "pi1"), 1u) |
| 1453 | << " : " << aos::FlatbufferToJson(pi2_client_statistics_fetcher.get()); |
| 1454 | |
| 1455 | EXPECT_EQ(ConnectedCount(pi3_server_statistics_fetcher.get(), "pi1"), 1u) |
| 1456 | << " : " << aos::FlatbufferToJson(pi3_server_statistics_fetcher.get()); |
| 1457 | EXPECT_EQ(ConnectedCount(pi3_client_statistics_fetcher.get(), "pi1"), 2u) |
| 1458 | << " : " << aos::FlatbufferToJson(pi3_client_statistics_fetcher.get()); |
| 1459 | |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1460 | EXPECT_EQ(pi1_pong_counter.count(), 601u); |
| 1461 | EXPECT_EQ(pi2_pong_counter.count(), 601u); |
| 1462 | |
| 1463 | EXPECT_EQ(pi1_on_pi1_timestamp_counter.count(), 60u); |
| 1464 | EXPECT_EQ(pi1_on_pi2_timestamp_counter.count(), 60u); |
| 1465 | EXPECT_EQ(pi1_on_pi3_timestamp_counter.count(), 40u); |
| 1466 | EXPECT_EQ(pi2_on_pi1_timestamp_counter.count(), 60u); |
| 1467 | EXPECT_EQ(pi2_on_pi2_timestamp_counter.count(), 60u); |
| 1468 | EXPECT_EQ(pi3_on_pi1_timestamp_counter.count(), 60u); |
| 1469 | EXPECT_EQ(pi3_on_pi3_timestamp_counter.count(), 60u); |
| 1470 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1471 | EXPECT_EQ(pi1_server_statistics_counter->count(), 6u); |
| 1472 | EXPECT_EQ(pi2_server_statistics_counter->count(), 6u); |
| 1473 | EXPECT_EQ(pi3_server_statistics_counter->count(), 6u); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1474 | |
| 1475 | EXPECT_EQ(pi1_client_statistics_counter.count(), 60u); |
| 1476 | EXPECT_EQ(pi2_client_statistics_counter.count(), 60u); |
| 1477 | EXPECT_EQ(pi3_client_statistics_counter.count(), 60u); |
| 1478 | |
| 1479 | // Also confirm that remote timestamps are being forwarded correctly. |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 1480 | EXPECT_EQ(CountAll(remote_timestamps_pi2_on_pi1), 661); |
| 1481 | EXPECT_EQ(CountAll(remote_timestamps_pi1_on_pi2), 661); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1482 | |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1483 | EXPECT_TRUE(AllConnected(pi1_server_statistics_fetcher.get())) |
| 1484 | << " : " << aos::FlatbufferToJson(pi1_server_statistics_fetcher.get()); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1485 | EXPECT_TRUE(AllConnected(pi1_client_statistics_fetcher.get())) |
| 1486 | << " : " << aos::FlatbufferToJson(pi1_client_statistics_fetcher.get()); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1487 | EXPECT_TRUE(AllConnected(pi2_server_statistics_fetcher.get())) |
| 1488 | << " : " << aos::FlatbufferToJson(pi2_server_statistics_fetcher.get()); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1489 | EXPECT_TRUE(AllConnected(pi2_client_statistics_fetcher.get())) |
| 1490 | << " : " << aos::FlatbufferToJson(pi2_client_statistics_fetcher.get()); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1491 | EXPECT_TRUE(AllConnected(pi3_server_statistics_fetcher.get())) |
| 1492 | << " : " << aos::FlatbufferToJson(pi3_server_statistics_fetcher.get()); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1493 | EXPECT_TRUE(AllConnected(pi3_client_statistics_fetcher.get())) |
| 1494 | << " : " << aos::FlatbufferToJson(pi3_client_statistics_fetcher.get()); |
| 1495 | } |
| 1496 | |
Austin Schuh | 2febf0d | 2020-09-21 22:24:30 -0700 | [diff] [blame] | 1497 | // Tests that the time offset having a slope doesn't break the world. |
| 1498 | // SimulatedMessageBridge has enough self consistency CHECK statements to |
| 1499 | // confirm, and we can can also check a message in each direction to make sure |
| 1500 | // it gets delivered as expected. |
| 1501 | TEST(SimulatedEventLoopTest, MultinodePingPongWithOffsetAndSlope) { |
| 1502 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 1503 | aos::configuration::ReadConfig(ArtifactPath( |
| 1504 | "aos/events/multinode_pingpong_test_combined_config.json")); |
Austin Schuh | 2febf0d | 2020-09-21 22:24:30 -0700 | [diff] [blame] | 1505 | const Node *pi1 = configuration::GetNode(&config.message(), "pi1"); |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1506 | const size_t pi1_index = configuration::GetNodeIndex(&config.message(), pi1); |
| 1507 | ASSERT_EQ(pi1_index, 0u); |
Austin Schuh | 2febf0d | 2020-09-21 22:24:30 -0700 | [diff] [blame] | 1508 | const Node *pi2 = configuration::GetNode(&config.message(), "pi2"); |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1509 | const size_t pi2_index = configuration::GetNodeIndex(&config.message(), pi2); |
| 1510 | ASSERT_EQ(pi2_index, 1u); |
| 1511 | const Node *pi3 = configuration::GetNode(&config.message(), "pi3"); |
| 1512 | const size_t pi3_index = configuration::GetNodeIndex(&config.message(), pi3); |
| 1513 | ASSERT_EQ(pi3_index, 2u); |
Austin Schuh | 2febf0d | 2020-09-21 22:24:30 -0700 | [diff] [blame] | 1514 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1515 | message_bridge::TestingTimeConverter time( |
| 1516 | configuration::NodesCount(&config.message())); |
Austin Schuh | 2febf0d | 2020-09-21 22:24:30 -0700 | [diff] [blame] | 1517 | SimulatedEventLoopFactory simulated_event_loop_factory(&config.message()); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1518 | simulated_event_loop_factory.SetTimeConverter(&time); |
Austin Schuh | 2febf0d | 2020-09-21 22:24:30 -0700 | [diff] [blame] | 1519 | |
Austin Schuh | 2febf0d | 2020-09-21 22:24:30 -0700 | [diff] [blame] | 1520 | constexpr chrono::milliseconds kOffset{150100}; |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1521 | time.AddNextTimestamp( |
| 1522 | distributed_clock::epoch(), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1523 | {BootTimestamp::epoch(), BootTimestamp::epoch() + kOffset, |
| 1524 | BootTimestamp::epoch()}); |
| 1525 | time.AddNextTimestamp(distributed_clock::epoch() + chrono::seconds(10), |
| 1526 | {BootTimestamp::epoch() + chrono::milliseconds(9999), |
| 1527 | BootTimestamp::epoch() + kOffset + chrono::seconds(10), |
| 1528 | BootTimestamp::epoch() + chrono::milliseconds(9999)}); |
Austin Schuh | 2febf0d | 2020-09-21 22:24:30 -0700 | [diff] [blame] | 1529 | |
| 1530 | std::unique_ptr<EventLoop> ping_event_loop = |
| 1531 | simulated_event_loop_factory.MakeEventLoop("ping", pi1); |
| 1532 | Ping ping(ping_event_loop.get()); |
| 1533 | |
| 1534 | std::unique_ptr<EventLoop> pong_event_loop = |
| 1535 | simulated_event_loop_factory.MakeEventLoop("pong", pi2); |
| 1536 | Pong pong(pong_event_loop.get()); |
| 1537 | |
| 1538 | std::unique_ptr<EventLoop> pi1_counter_event_loop = |
| 1539 | simulated_event_loop_factory.MakeEventLoop("pi1_counter", pi1); |
| 1540 | std::unique_ptr<EventLoop> pi2_counter_event_loop = |
| 1541 | simulated_event_loop_factory.MakeEventLoop("pi2_counter", pi2); |
| 1542 | |
| 1543 | aos::Fetcher<examples::Ping> ping_on_pi1_fetcher = |
| 1544 | pi1_counter_event_loop->MakeFetcher<examples::Ping>("/test"); |
| 1545 | aos::Fetcher<examples::Ping> ping_on_pi2_fetcher = |
| 1546 | pi2_counter_event_loop->MakeFetcher<examples::Ping>("/test"); |
| 1547 | |
| 1548 | aos::Fetcher<examples::Pong> pong_on_pi2_fetcher = |
| 1549 | pi2_counter_event_loop->MakeFetcher<examples::Pong>("/test"); |
| 1550 | aos::Fetcher<examples::Pong> pong_on_pi1_fetcher = |
| 1551 | pi1_counter_event_loop->MakeFetcher<examples::Pong>("/test"); |
| 1552 | |
| 1553 | // End after a pong message comes back. This will leave the latest messages |
| 1554 | // on all channels so we can look at timestamps easily and check they make |
| 1555 | // sense. |
| 1556 | std::unique_ptr<EventLoop> pi1_pong_ender = |
| 1557 | simulated_event_loop_factory.MakeEventLoop("pi2_counter", pi1); |
| 1558 | int count = 0; |
| 1559 | pi1_pong_ender->MakeWatcher( |
| 1560 | "/test", [&simulated_event_loop_factory, &count](const examples::Pong &) { |
| 1561 | if (++count == 100) { |
| 1562 | simulated_event_loop_factory.Exit(); |
| 1563 | } |
| 1564 | }); |
| 1565 | |
| 1566 | // Run enough that messages should be delivered. |
| 1567 | simulated_event_loop_factory.Run(); |
| 1568 | |
| 1569 | // Grab the latest messages. |
| 1570 | EXPECT_TRUE(ping_on_pi1_fetcher.Fetch()); |
| 1571 | EXPECT_TRUE(ping_on_pi2_fetcher.Fetch()); |
| 1572 | EXPECT_TRUE(pong_on_pi1_fetcher.Fetch()); |
| 1573 | EXPECT_TRUE(pong_on_pi2_fetcher.Fetch()); |
| 1574 | |
| 1575 | // Compute their time on the global distributed clock so we can compute |
| 1576 | // distance betwen them. |
| 1577 | const distributed_clock::time_point pi1_ping_time = |
| 1578 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi1) |
| 1579 | ->ToDistributedClock( |
| 1580 | ping_on_pi1_fetcher.context().monotonic_event_time); |
| 1581 | const distributed_clock::time_point pi2_ping_time = |
| 1582 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi2) |
| 1583 | ->ToDistributedClock( |
| 1584 | ping_on_pi2_fetcher.context().monotonic_event_time); |
| 1585 | const distributed_clock::time_point pi1_pong_time = |
| 1586 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi1) |
| 1587 | ->ToDistributedClock( |
| 1588 | pong_on_pi1_fetcher.context().monotonic_event_time); |
| 1589 | const distributed_clock::time_point pi2_pong_time = |
| 1590 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi2) |
| 1591 | ->ToDistributedClock( |
| 1592 | pong_on_pi2_fetcher.context().monotonic_event_time); |
| 1593 | |
| 1594 | // And confirm the delivery delay is just about exactly 150 uS for both |
| 1595 | // directions like expected. There will be a couple ns of rounding errors in |
| 1596 | // the conversion functions that aren't worth accounting for right now. This |
| 1597 | // will either be really close, or really far. |
| 1598 | EXPECT_GE(pi2_ping_time, chrono::microseconds(150) - chrono::nanoseconds(10) + |
| 1599 | pi1_ping_time); |
| 1600 | EXPECT_LE(pi2_ping_time, chrono::microseconds(150) + chrono::nanoseconds(10) + |
| 1601 | pi1_ping_time); |
| 1602 | |
| 1603 | EXPECT_GE(pi1_pong_time, chrono::microseconds(150) - chrono::nanoseconds(10) + |
| 1604 | pi2_pong_time); |
| 1605 | EXPECT_LE(pi1_pong_time, chrono::microseconds(150) + chrono::nanoseconds(10) + |
| 1606 | pi2_pong_time); |
| 1607 | } |
| 1608 | |
Austin Schuh | 4c570ea | 2020-11-19 23:13:24 -0800 | [diff] [blame] | 1609 | void SendPing(aos::Sender<examples::Ping> *sender, int value) { |
| 1610 | aos::Sender<examples::Ping>::Builder builder = sender->MakeBuilder(); |
| 1611 | examples::Ping::Builder ping_builder = builder.MakeBuilder<examples::Ping>(); |
| 1612 | ping_builder.add_value(value); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1613 | builder.CheckOk(builder.Send(ping_builder.Finish())); |
Austin Schuh | 4c570ea | 2020-11-19 23:13:24 -0800 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | // Tests that reliable (and unreliable) ping messages get forwarded as expected. |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 1617 | TEST_P(RemoteMessageSimulatedEventLoopTest, MultinodeStartupTesting) { |
Austin Schuh | 4c570ea | 2020-11-19 23:13:24 -0800 | [diff] [blame] | 1618 | const Node *pi1 = configuration::GetNode(&config.message(), "pi1"); |
| 1619 | const Node *pi2 = configuration::GetNode(&config.message(), "pi2"); |
| 1620 | |
| 1621 | SimulatedEventLoopFactory simulated_event_loop_factory(&config.message()); |
| 1622 | |
| 1623 | std::unique_ptr<EventLoop> ping_event_loop = |
| 1624 | simulated_event_loop_factory.MakeEventLoop("ping", pi1); |
| 1625 | aos::Sender<examples::Ping> pi1_reliable_sender = |
| 1626 | ping_event_loop->MakeSender<examples::Ping>("/reliable"); |
| 1627 | aos::Sender<examples::Ping> pi1_unreliable_sender = |
| 1628 | ping_event_loop->MakeSender<examples::Ping>("/unreliable"); |
| 1629 | SendPing(&pi1_reliable_sender, 1); |
| 1630 | SendPing(&pi1_unreliable_sender, 1); |
| 1631 | |
| 1632 | std::unique_ptr<EventLoop> pi2_pong_event_loop = |
| 1633 | simulated_event_loop_factory.MakeEventLoop("pong", pi2); |
| 1634 | MessageCounter<examples::Ping> pi2_reliable_counter(pi2_pong_event_loop.get(), |
| 1635 | "/reliable"); |
| 1636 | MessageCounter<examples::Ping> pi2_unreliable_counter( |
| 1637 | pi2_pong_event_loop.get(), "/unreliable"); |
| 1638 | aos::Fetcher<examples::Ping> reliable_on_pi2_fetcher = |
| 1639 | pi2_pong_event_loop->MakeFetcher<examples::Ping>("/reliable"); |
| 1640 | aos::Fetcher<examples::Ping> unreliable_on_pi2_fetcher = |
| 1641 | pi2_pong_event_loop->MakeFetcher<examples::Ping>("/unreliable"); |
| 1642 | |
| 1643 | const size_t reliable_channel_index = configuration::ChannelIndex( |
| 1644 | pi2_pong_event_loop->configuration(), reliable_on_pi2_fetcher.channel()); |
| 1645 | |
| 1646 | std::unique_ptr<EventLoop> pi1_remote_timestamp = |
| 1647 | simulated_event_loop_factory.MakeEventLoop("pi1_remote_timestamp", pi1); |
| 1648 | |
Austin Schuh | eeaa202 | 2021-01-02 21:52:03 -0800 | [diff] [blame] | 1649 | const chrono::nanoseconds network_delay = |
| 1650 | simulated_event_loop_factory.network_delay(); |
| 1651 | |
Austin Schuh | 4c570ea | 2020-11-19 23:13:24 -0800 | [diff] [blame] | 1652 | int reliable_timestamp_count = 0; |
| 1653 | pi1_remote_timestamp->MakeWatcher( |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 1654 | shared() ? "/pi1/aos/remote_timestamps/pi2" |
| 1655 | : "/pi1/aos/remote_timestamps/pi2/reliable/aos-examples-Ping", |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1656 | [reliable_channel_index, &reliable_timestamp_count, |
Austin Schuh | eeaa202 | 2021-01-02 21:52:03 -0800 | [diff] [blame] | 1657 | &simulated_event_loop_factory, pi2, network_delay, &pi2_pong_event_loop, |
| 1658 | &pi1_remote_timestamp](const RemoteMessage &header) { |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1659 | EXPECT_TRUE(header.has_boot_uuid()); |
Austin Schuh | cdd9027 | 2021-03-15 12:46:16 -0700 | [diff] [blame] | 1660 | EXPECT_EQ(UUID::FromVector(header.boot_uuid()), |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1661 | simulated_event_loop_factory.GetNodeEventLoopFactory(pi2) |
Austin Schuh | cdd9027 | 2021-03-15 12:46:16 -0700 | [diff] [blame] | 1662 | ->boot_uuid()); |
Austin Schuh | 4c570ea | 2020-11-19 23:13:24 -0800 | [diff] [blame] | 1663 | VLOG(1) << aos::FlatbufferToJson(&header); |
| 1664 | if (header.channel_index() == reliable_channel_index) { |
| 1665 | ++reliable_timestamp_count; |
| 1666 | } |
Austin Schuh | eeaa202 | 2021-01-02 21:52:03 -0800 | [diff] [blame] | 1667 | |
| 1668 | const aos::monotonic_clock::time_point header_monotonic_sent_time( |
| 1669 | chrono::nanoseconds(header.monotonic_sent_time())); |
| 1670 | |
| 1671 | EXPECT_EQ(pi1_remote_timestamp->context().monotonic_event_time, |
| 1672 | header_monotonic_sent_time + network_delay + |
| 1673 | (pi1_remote_timestamp->monotonic_now() - |
| 1674 | pi2_pong_event_loop->monotonic_now())); |
Austin Schuh | 4c570ea | 2020-11-19 23:13:24 -0800 | [diff] [blame] | 1675 | }); |
| 1676 | |
| 1677 | // Wait to let timestamp estimation start up before looking for the results. |
| 1678 | simulated_event_loop_factory.RunFor(chrono::milliseconds(500)); |
| 1679 | |
| 1680 | EXPECT_EQ(pi2_reliable_counter.count(), 1u); |
| 1681 | // This one isn't reliable, but was sent before the start. It should *not* be |
| 1682 | // delivered. |
| 1683 | EXPECT_EQ(pi2_unreliable_counter.count(), 0u); |
| 1684 | // Confirm we got a timestamp logged for the message that was forwarded. |
| 1685 | EXPECT_EQ(reliable_timestamp_count, 1u); |
| 1686 | |
| 1687 | SendPing(&pi1_reliable_sender, 2); |
| 1688 | SendPing(&pi1_unreliable_sender, 2); |
| 1689 | simulated_event_loop_factory.RunFor(chrono::milliseconds(500)); |
| 1690 | EXPECT_EQ(pi2_reliable_counter.count(), 2u); |
| 1691 | EXPECT_EQ(pi2_unreliable_counter.count(), 1u); |
| 1692 | |
| 1693 | EXPECT_EQ(reliable_timestamp_count, 2u); |
| 1694 | } |
| 1695 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1696 | // Tests that rebooting a node changes the ServerStatistics message and the |
| 1697 | // RemoteTimestamp message. |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 1698 | TEST_P(RemoteMessageSimulatedEventLoopTest, BootUUIDTest) { |
Austin Schuh | 72e6568 | 2021-09-02 11:37:05 -0700 | [diff] [blame] | 1699 | const UUID pi1_boot0 = UUID::Random(); |
| 1700 | const UUID pi2_boot0 = UUID::Random(); |
| 1701 | const UUID pi2_boot1 = UUID::Random(); |
| 1702 | const UUID pi3_boot0 = UUID::Random(); |
| 1703 | UUID expected_boot_uuid = pi2_boot0; |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1704 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1705 | message_bridge::TestingTimeConverter time( |
| 1706 | configuration::NodesCount(&config.message())); |
| 1707 | SimulatedEventLoopFactory factory(&config.message()); |
| 1708 | factory.SetTimeConverter(&time); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1709 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1710 | const size_t pi1_index = |
| 1711 | configuration::GetNodeIndex(&config.message(), "pi1"); |
| 1712 | const size_t pi2_index = |
| 1713 | configuration::GetNodeIndex(&config.message(), "pi2"); |
| 1714 | const size_t pi3_index = |
| 1715 | configuration::GetNodeIndex(&config.message(), "pi3"); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1716 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1717 | { |
| 1718 | time.AddNextTimestamp(distributed_clock::epoch(), |
| 1719 | {BootTimestamp::epoch(), BootTimestamp::epoch(), |
| 1720 | BootTimestamp::epoch()}); |
| 1721 | |
| 1722 | const chrono::nanoseconds dt = chrono::milliseconds(2001); |
| 1723 | |
| 1724 | time.AddNextTimestamp( |
| 1725 | distributed_clock::epoch() + dt, |
| 1726 | {BootTimestamp::epoch() + dt, |
| 1727 | BootTimestamp{.boot = 1, .time = monotonic_clock::epoch()}, |
| 1728 | BootTimestamp::epoch() + dt}); |
| 1729 | |
| 1730 | time.set_boot_uuid(pi1_index, 0, pi1_boot0); |
| 1731 | time.set_boot_uuid(pi2_index, 0, pi2_boot0); |
| 1732 | time.set_boot_uuid(pi2_index, 1, pi2_boot1); |
| 1733 | time.set_boot_uuid(pi3_index, 0, pi3_boot0); |
| 1734 | } |
| 1735 | |
| 1736 | NodeEventLoopFactory *pi1 = factory.GetNodeEventLoopFactory("pi1"); |
| 1737 | NodeEventLoopFactory *pi2 = factory.GetNodeEventLoopFactory("pi2"); |
| 1738 | |
| 1739 | pi1->OnStartup([pi1]() { pi1->AlwaysStart<Ping>("ping"); }); |
| 1740 | pi2->OnStartup([pi2]() { pi2->AlwaysStart<Pong>("pong"); }); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1741 | |
| 1742 | std::unique_ptr<EventLoop> pi1_remote_timestamp = |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1743 | pi1->MakeEventLoop("pi1_remote_timestamp"); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1744 | |
| 1745 | int timestamp_count = 0; |
| 1746 | pi1_remote_timestamp->MakeWatcher( |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 1747 | "/pi2/aos", [&expected_boot_uuid, |
| 1748 | &pi1_remote_timestamp](const message_bridge::Timestamp &) { |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1749 | EXPECT_EQ(pi1_remote_timestamp->context().source_boot_uuid, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 1750 | expected_boot_uuid); |
| 1751 | }); |
| 1752 | pi1_remote_timestamp->MakeWatcher( |
| 1753 | "/test", |
| 1754 | [&expected_boot_uuid, &pi1_remote_timestamp](const examples::Pong &) { |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1755 | EXPECT_EQ(pi1_remote_timestamp->context().source_boot_uuid, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 1756 | expected_boot_uuid); |
| 1757 | }); |
| 1758 | pi1_remote_timestamp->MakeWatcher( |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 1759 | shared() ? "/pi1/aos/remote_timestamps/pi2" |
| 1760 | : "/pi1/aos/remote_timestamps/pi2/test/aos-examples-Ping", |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1761 | [×tamp_count, &expected_boot_uuid](const RemoteMessage &header) { |
| 1762 | EXPECT_TRUE(header.has_boot_uuid()); |
Austin Schuh | cdd9027 | 2021-03-15 12:46:16 -0700 | [diff] [blame] | 1763 | EXPECT_EQ(UUID::FromVector(header.boot_uuid()), expected_boot_uuid); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1764 | VLOG(1) << aos::FlatbufferToJson(&header); |
| 1765 | ++timestamp_count; |
| 1766 | }); |
| 1767 | |
| 1768 | int pi1_server_statistics_count = 0; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1769 | bool first_pi1_server_statistics = true; |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 1770 | int boot_number = 0; |
| 1771 | monotonic_clock::time_point expected_connection_time = pi1->monotonic_now(); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1772 | pi1_remote_timestamp->MakeWatcher( |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 1773 | "/pi1/aos", |
| 1774 | [&pi1_server_statistics_count, &expected_boot_uuid, |
| 1775 | &expected_connection_time, &first_pi1_server_statistics, |
| 1776 | &boot_number](const message_bridge::ServerStatistics &stats) { |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1777 | VLOG(1) << "pi1 ServerStatistics " << FlatbufferToJson(&stats); |
| 1778 | for (const message_bridge::ServerConnection *connection : |
| 1779 | *stats.connections()) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1780 | if (connection->state() == message_bridge::State::CONNECTED) { |
| 1781 | ASSERT_TRUE(connection->has_boot_uuid()); |
| 1782 | } |
| 1783 | if (!first_pi1_server_statistics) { |
| 1784 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
| 1785 | } |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1786 | if (connection->node()->name()->string_view() == "pi2") { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1787 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
| 1788 | ASSERT_TRUE(connection->has_boot_uuid()); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1789 | EXPECT_EQ(expected_boot_uuid, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 1790 | UUID::FromString(connection->boot_uuid())) |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1791 | << " : Got " << aos::FlatbufferToJson(&stats); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 1792 | EXPECT_EQ(monotonic_clock::time_point(chrono::nanoseconds( |
| 1793 | connection->connected_since_time())), |
| 1794 | expected_connection_time); |
| 1795 | EXPECT_EQ(boot_number + 1, connection->connection_count()); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1796 | ++pi1_server_statistics_count; |
| 1797 | } |
| 1798 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1799 | first_pi1_server_statistics = false; |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1800 | }); |
| 1801 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1802 | int pi1_client_statistics_count = 0; |
| 1803 | pi1_remote_timestamp->MakeWatcher( |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 1804 | "/pi1/aos", [&pi1_client_statistics_count, &expected_boot_uuid, |
| 1805 | &expected_connection_time, &boot_number]( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1806 | const message_bridge::ClientStatistics &stats) { |
| 1807 | VLOG(1) << "pi1 ClientStatistics " << FlatbufferToJson(&stats); |
| 1808 | for (const message_bridge::ClientConnection *connection : |
| 1809 | *stats.connections()) { |
| 1810 | EXPECT_EQ(connection->state(), message_bridge::State::CONNECTED); |
| 1811 | if (connection->node()->name()->string_view() == "pi2") { |
| 1812 | ++pi1_client_statistics_count; |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 1813 | EXPECT_EQ(expected_boot_uuid, |
| 1814 | UUID::FromString(connection->boot_uuid())) |
| 1815 | << " : Got " << aos::FlatbufferToJson(&stats); |
| 1816 | EXPECT_EQ(monotonic_clock::time_point(chrono::nanoseconds( |
| 1817 | connection->connected_since_time())), |
| 1818 | expected_connection_time); |
| 1819 | EXPECT_EQ(boot_number + 1, connection->connection_count()); |
| 1820 | } else { |
| 1821 | EXPECT_EQ(connection->connected_since_time(), 0); |
| 1822 | EXPECT_EQ(1, connection->connection_count()); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | }); |
| 1826 | |
| 1827 | // Confirm that reboot changes the UUID. |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 1828 | pi2->OnShutdown([&expected_boot_uuid, &boot_number, &expected_connection_time, |
| 1829 | pi1, pi2, pi2_boot1]() { |
| 1830 | expected_boot_uuid = pi2_boot1; |
| 1831 | ++boot_number; |
| 1832 | LOG(INFO) << "OnShutdown triggered for pi2"; |
| 1833 | pi2->OnStartup( |
| 1834 | [&expected_boot_uuid, &expected_connection_time, pi1, pi2]() { |
| 1835 | EXPECT_EQ(expected_boot_uuid, pi2->boot_uuid()); |
| 1836 | expected_connection_time = pi1->monotonic_now(); |
| 1837 | }); |
| 1838 | }); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1839 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1840 | // Let a couple of ServerStatistics messages show up before rebooting. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1841 | factory.RunFor(chrono::milliseconds(2002)); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1842 | |
| 1843 | EXPECT_GT(timestamp_count, 100); |
| 1844 | EXPECT_GE(pi1_server_statistics_count, 1u); |
| 1845 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1846 | timestamp_count = 0; |
| 1847 | pi1_server_statistics_count = 0; |
| 1848 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1849 | factory.RunFor(chrono::milliseconds(2000)); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1850 | EXPECT_GT(timestamp_count, 100); |
| 1851 | EXPECT_GE(pi1_server_statistics_count, 1u); |
| 1852 | } |
| 1853 | |
James Kuszmaul | f4bf9fe | 2021-05-10 22:58:24 -0700 | [diff] [blame] | 1854 | INSTANTIATE_TEST_SUITE_P( |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 1855 | All, RemoteMessageSimulatedEventLoopTest, |
| 1856 | ::testing::Values( |
| 1857 | Param{"multinode_pingpong_test_combined_config.json", true}, |
| 1858 | Param{"multinode_pingpong_test_split_config.json", false})); |
| 1859 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1860 | // Tests that Startup and Shutdown do reasonable things. |
| 1861 | TEST(SimulatedEventLoopTest, MultinodePingPongStartup) { |
| 1862 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 1863 | aos::configuration::ReadConfig( |
| 1864 | ArtifactPath("aos/events/multinode_pingpong_test_split_config.json")); |
| 1865 | |
Austin Schuh | 72e6568 | 2021-09-02 11:37:05 -0700 | [diff] [blame] | 1866 | size_t pi1_shutdown_counter = 0; |
| 1867 | size_t pi2_shutdown_counter = 0; |
| 1868 | MessageCounter<examples::Pong> *pi1_pong_counter = nullptr; |
| 1869 | MessageCounter<examples::Ping> *pi2_ping_counter = nullptr; |
| 1870 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1871 | message_bridge::TestingTimeConverter time( |
| 1872 | configuration::NodesCount(&config.message())); |
| 1873 | SimulatedEventLoopFactory factory(&config.message()); |
| 1874 | factory.SetTimeConverter(&time); |
| 1875 | time.AddNextTimestamp( |
| 1876 | distributed_clock::epoch(), |
| 1877 | {BootTimestamp::epoch(), BootTimestamp::epoch(), BootTimestamp::epoch()}); |
| 1878 | |
| 1879 | const chrono::nanoseconds dt = chrono::seconds(10) + chrono::milliseconds(6); |
| 1880 | |
| 1881 | time.AddNextTimestamp( |
| 1882 | distributed_clock::epoch() + dt, |
| 1883 | {BootTimestamp{.boot = 1, .time = monotonic_clock::epoch()}, |
| 1884 | BootTimestamp{.boot = 1, .time = monotonic_clock::epoch()}, |
| 1885 | BootTimestamp::epoch() + dt}); |
| 1886 | |
| 1887 | NodeEventLoopFactory *pi1 = factory.GetNodeEventLoopFactory("pi1"); |
| 1888 | NodeEventLoopFactory *pi2 = factory.GetNodeEventLoopFactory("pi2"); |
| 1889 | |
| 1890 | // Configure startup to start Ping and Pong, and count. |
| 1891 | size_t pi1_startup_counter = 0; |
| 1892 | size_t pi2_startup_counter = 0; |
| 1893 | pi1->OnStartup([pi1]() { |
| 1894 | LOG(INFO) << "Made ping"; |
| 1895 | pi1->AlwaysStart<Ping>("ping"); |
| 1896 | }); |
| 1897 | pi1->OnStartup([&pi1_startup_counter]() { ++pi1_startup_counter; }); |
| 1898 | pi2->OnStartup([pi2]() { |
| 1899 | LOG(INFO) << "Made pong"; |
| 1900 | pi2->AlwaysStart<Pong>("pong"); |
| 1901 | }); |
| 1902 | pi2->OnStartup([&pi2_startup_counter]() { ++pi2_startup_counter; }); |
| 1903 | |
| 1904 | // Shutdown just counts. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1905 | pi1->OnShutdown([&pi1_shutdown_counter]() { ++pi1_shutdown_counter; }); |
| 1906 | pi2->OnShutdown([&pi2_shutdown_counter]() { ++pi2_shutdown_counter; }); |
| 1907 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1908 | // Automatically make counters on startup. |
| 1909 | pi1->OnStartup([&pi1_pong_counter, pi1]() { |
| 1910 | pi1_pong_counter = pi1->AlwaysStart<MessageCounter<examples::Pong>>( |
| 1911 | "pi1_pong_counter", "/test"); |
| 1912 | }); |
| 1913 | pi1->OnShutdown([&pi1_pong_counter]() { pi1_pong_counter = nullptr; }); |
| 1914 | pi2->OnStartup([&pi2_ping_counter, pi2]() { |
| 1915 | pi2_ping_counter = pi2->AlwaysStart<MessageCounter<examples::Ping>>( |
| 1916 | "pi2_ping_counter", "/test"); |
| 1917 | }); |
| 1918 | pi2->OnShutdown([&pi2_ping_counter]() { pi2_ping_counter = nullptr; }); |
| 1919 | |
| 1920 | EXPECT_EQ(pi2_ping_counter, nullptr); |
| 1921 | EXPECT_EQ(pi1_pong_counter, nullptr); |
| 1922 | |
| 1923 | EXPECT_EQ(pi1_startup_counter, 0u); |
| 1924 | EXPECT_EQ(pi2_startup_counter, 0u); |
| 1925 | EXPECT_EQ(pi1_shutdown_counter, 0u); |
| 1926 | EXPECT_EQ(pi2_shutdown_counter, 0u); |
| 1927 | |
| 1928 | factory.RunFor(chrono::seconds(10) + chrono::milliseconds(5)); |
| 1929 | EXPECT_EQ(pi1_startup_counter, 1u); |
| 1930 | EXPECT_EQ(pi2_startup_counter, 1u); |
| 1931 | EXPECT_EQ(pi1_shutdown_counter, 0u); |
| 1932 | EXPECT_EQ(pi2_shutdown_counter, 0u); |
| 1933 | EXPECT_EQ(pi2_ping_counter->count(), 1001); |
| 1934 | EXPECT_EQ(pi1_pong_counter->count(), 1001); |
| 1935 | |
| 1936 | LOG(INFO) << pi1->monotonic_now(); |
| 1937 | LOG(INFO) << pi2->monotonic_now(); |
| 1938 | |
| 1939 | factory.RunFor(chrono::seconds(5) + chrono::milliseconds(5)); |
| 1940 | |
| 1941 | EXPECT_EQ(pi1_startup_counter, 2u); |
| 1942 | EXPECT_EQ(pi2_startup_counter, 2u); |
| 1943 | EXPECT_EQ(pi1_shutdown_counter, 1u); |
| 1944 | EXPECT_EQ(pi2_shutdown_counter, 1u); |
| 1945 | EXPECT_EQ(pi2_ping_counter->count(), 501); |
| 1946 | EXPECT_EQ(pi1_pong_counter->count(), 501); |
| 1947 | } |
| 1948 | |
| 1949 | // Tests that OnStartup handlers can be added after running and get called, and |
| 1950 | // can't be called when running. |
| 1951 | TEST(SimulatedEventLoopDeathTest, OnStartupWhileRunning) { |
| 1952 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 1953 | aos::configuration::ReadConfig( |
| 1954 | ArtifactPath("aos/events/multinode_pingpong_test_split_config.json")); |
| 1955 | |
| 1956 | // Test that we can add startup handlers as long as we aren't running, and |
| 1957 | // they get run when Run gets called again. |
| 1958 | // Test that adding a startup handler when running fails. |
| 1959 | // |
| 1960 | // Test shutdown handlers get called on destruction. |
| 1961 | SimulatedEventLoopFactory factory(&config.message()); |
| 1962 | |
| 1963 | NodeEventLoopFactory *pi1 = factory.GetNodeEventLoopFactory("pi1"); |
| 1964 | |
| 1965 | int startup_count0 = 0; |
| 1966 | int startup_count1 = 0; |
| 1967 | |
| 1968 | pi1->OnStartup([&]() { ++startup_count0; }); |
| 1969 | EXPECT_EQ(startup_count0, 0); |
| 1970 | EXPECT_EQ(startup_count1, 0); |
| 1971 | |
| 1972 | factory.RunFor(chrono::nanoseconds(1)); |
| 1973 | EXPECT_EQ(startup_count0, 1); |
| 1974 | EXPECT_EQ(startup_count1, 0); |
| 1975 | |
| 1976 | pi1->OnStartup([&]() { ++startup_count1; }); |
| 1977 | EXPECT_EQ(startup_count0, 1); |
| 1978 | EXPECT_EQ(startup_count1, 0); |
| 1979 | |
| 1980 | factory.RunFor(chrono::nanoseconds(1)); |
| 1981 | EXPECT_EQ(startup_count0, 1); |
| 1982 | EXPECT_EQ(startup_count1, 1); |
| 1983 | |
| 1984 | std::unique_ptr<EventLoop> loop = pi1->MakeEventLoop("foo"); |
| 1985 | loop->OnRun([&]() { pi1->OnStartup([]() {}); }); |
| 1986 | |
| 1987 | EXPECT_DEATH({ factory.RunFor(chrono::nanoseconds(1)); }, |
| 1988 | "Can only register OnStartup handlers when not running."); |
| 1989 | } |
| 1990 | |
| 1991 | // Tests that OnStartup handlers can be added after running and get called, and |
| 1992 | // all the handlers get called on reboot. Shutdown handlers are tested the same |
| 1993 | // way. |
| 1994 | TEST(SimulatedEventLoopTest, OnStartupShutdownAllRestarts) { |
| 1995 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 1996 | aos::configuration::ReadConfig( |
| 1997 | ArtifactPath("aos/events/multinode_pingpong_test_split_config.json")); |
| 1998 | |
Austin Schuh | 72e6568 | 2021-09-02 11:37:05 -0700 | [diff] [blame] | 1999 | int startup_count0 = 0; |
| 2000 | int shutdown_count0 = 0; |
| 2001 | int startup_count1 = 0; |
| 2002 | int shutdown_count1 = 0; |
| 2003 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2004 | message_bridge::TestingTimeConverter time( |
| 2005 | configuration::NodesCount(&config.message())); |
| 2006 | SimulatedEventLoopFactory factory(&config.message()); |
| 2007 | factory.SetTimeConverter(&time); |
| 2008 | time.StartEqual(); |
| 2009 | |
| 2010 | const chrono::nanoseconds dt = chrono::seconds(10); |
| 2011 | time.RebootAt(0, distributed_clock::epoch() + dt); |
| 2012 | time.RebootAt(0, distributed_clock::epoch() + 2 * dt); |
| 2013 | |
| 2014 | NodeEventLoopFactory *pi1 = factory.GetNodeEventLoopFactory("pi1"); |
| 2015 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 2016 | pi1->OnStartup([&]() { ++startup_count0; }); |
| 2017 | pi1->OnShutdown([&]() { ++shutdown_count0; }); |
| 2018 | EXPECT_EQ(startup_count0, 0); |
| 2019 | EXPECT_EQ(startup_count1, 0); |
| 2020 | EXPECT_EQ(shutdown_count0, 0); |
| 2021 | EXPECT_EQ(shutdown_count1, 0); |
| 2022 | |
| 2023 | factory.RunFor(chrono::nanoseconds(1)); |
| 2024 | EXPECT_EQ(startup_count0, 1); |
| 2025 | EXPECT_EQ(startup_count1, 0); |
| 2026 | EXPECT_EQ(shutdown_count0, 0); |
| 2027 | EXPECT_EQ(shutdown_count1, 0); |
| 2028 | |
| 2029 | pi1->OnStartup([&]() { ++startup_count1; }); |
| 2030 | EXPECT_EQ(startup_count0, 1); |
| 2031 | EXPECT_EQ(startup_count1, 0); |
| 2032 | EXPECT_EQ(shutdown_count0, 0); |
| 2033 | EXPECT_EQ(shutdown_count1, 0); |
| 2034 | |
| 2035 | factory.RunFor(chrono::nanoseconds(1)); |
| 2036 | EXPECT_EQ(startup_count0, 1); |
| 2037 | EXPECT_EQ(startup_count1, 1); |
| 2038 | EXPECT_EQ(shutdown_count0, 0); |
| 2039 | EXPECT_EQ(shutdown_count1, 0); |
| 2040 | |
| 2041 | factory.RunFor(chrono::seconds(15)); |
| 2042 | |
| 2043 | EXPECT_EQ(startup_count0, 2); |
| 2044 | EXPECT_EQ(startup_count1, 2); |
| 2045 | EXPECT_EQ(shutdown_count0, 1); |
| 2046 | EXPECT_EQ(shutdown_count1, 0); |
| 2047 | |
| 2048 | pi1->OnShutdown([&]() { ++shutdown_count1; }); |
| 2049 | factory.RunFor(chrono::seconds(10)); |
| 2050 | |
| 2051 | EXPECT_EQ(startup_count0, 3); |
| 2052 | EXPECT_EQ(startup_count1, 3); |
| 2053 | EXPECT_EQ(shutdown_count0, 2); |
| 2054 | EXPECT_EQ(shutdown_count1, 1); |
| 2055 | } |
| 2056 | |
| 2057 | // Tests that event loops which outlive shutdown crash. |
| 2058 | TEST(SimulatedEventLoopDeathTest, EventLoopOutlivesReboot) { |
| 2059 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 2060 | aos::configuration::ReadConfig( |
| 2061 | ArtifactPath("aos/events/multinode_pingpong_test_split_config.json")); |
| 2062 | |
| 2063 | message_bridge::TestingTimeConverter time( |
| 2064 | configuration::NodesCount(&config.message())); |
| 2065 | SimulatedEventLoopFactory factory(&config.message()); |
| 2066 | factory.SetTimeConverter(&time); |
| 2067 | time.StartEqual(); |
| 2068 | |
| 2069 | const chrono::nanoseconds dt = chrono::seconds(10); |
| 2070 | time.RebootAt(0, distributed_clock::epoch() + dt); |
| 2071 | |
| 2072 | NodeEventLoopFactory *pi1 = factory.GetNodeEventLoopFactory("pi1"); |
| 2073 | |
| 2074 | std::unique_ptr<EventLoop> loop = pi1->MakeEventLoop("foo"); |
| 2075 | |
| 2076 | EXPECT_DEATH({ factory.RunFor(dt * 2); }, "Event loop"); |
| 2077 | } |
| 2078 | |
| 2079 | // Tests that messages don't survive a reboot of a node. |
| 2080 | TEST(SimulatedEventLoopTest, ChannelClearedOnReboot) { |
| 2081 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 2082 | aos::configuration::ReadConfig( |
| 2083 | ArtifactPath("aos/events/multinode_pingpong_test_split_config.json")); |
| 2084 | |
| 2085 | message_bridge::TestingTimeConverter time( |
| 2086 | configuration::NodesCount(&config.message())); |
| 2087 | SimulatedEventLoopFactory factory(&config.message()); |
| 2088 | factory.SetTimeConverter(&time); |
| 2089 | time.StartEqual(); |
| 2090 | |
| 2091 | const chrono::nanoseconds dt = chrono::seconds(10); |
| 2092 | time.RebootAt(0, distributed_clock::epoch() + dt); |
| 2093 | |
| 2094 | NodeEventLoopFactory *pi1 = factory.GetNodeEventLoopFactory("pi1"); |
| 2095 | |
| 2096 | const UUID boot_uuid = pi1->boot_uuid(); |
| 2097 | EXPECT_NE(boot_uuid, UUID::Zero()); |
| 2098 | |
| 2099 | { |
| 2100 | ::std::unique_ptr<EventLoop> ping_event_loop = pi1->MakeEventLoop("ping"); |
| 2101 | aos::Sender<examples::Ping> test_message_sender = |
| 2102 | ping_event_loop->MakeSender<examples::Ping>("/reliable"); |
| 2103 | SendPing(&test_message_sender, 1); |
| 2104 | } |
| 2105 | |
| 2106 | factory.RunFor(chrono::seconds(5)); |
| 2107 | |
| 2108 | { |
| 2109 | ::std::unique_ptr<EventLoop> ping_event_loop = pi1->MakeEventLoop("ping"); |
| 2110 | aos::Fetcher<examples::Ping> fetcher = |
| 2111 | ping_event_loop->MakeFetcher<examples::Ping>("/reliable"); |
| 2112 | EXPECT_TRUE(fetcher.Fetch()); |
| 2113 | } |
| 2114 | |
| 2115 | factory.RunFor(chrono::seconds(10)); |
| 2116 | |
| 2117 | { |
| 2118 | ::std::unique_ptr<EventLoop> ping_event_loop = pi1->MakeEventLoop("ping"); |
| 2119 | aos::Fetcher<examples::Ping> fetcher = |
| 2120 | ping_event_loop->MakeFetcher<examples::Ping>("/reliable"); |
| 2121 | EXPECT_FALSE(fetcher.Fetch()); |
| 2122 | } |
| 2123 | EXPECT_NE(boot_uuid, pi1->boot_uuid()); |
| 2124 | } |
| 2125 | |
| 2126 | // Tests that reliable messages get resent on reboot. |
| 2127 | TEST(SimulatedEventLoopTest, ReliableMessageResentOnReboot) { |
| 2128 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 2129 | aos::configuration::ReadConfig( |
| 2130 | ArtifactPath("aos/events/multinode_pingpong_test_split_config.json")); |
| 2131 | |
| 2132 | message_bridge::TestingTimeConverter time( |
| 2133 | configuration::NodesCount(&config.message())); |
| 2134 | SimulatedEventLoopFactory factory(&config.message()); |
| 2135 | factory.SetTimeConverter(&time); |
| 2136 | time.StartEqual(); |
| 2137 | |
| 2138 | const chrono::nanoseconds dt = chrono::seconds(1); |
| 2139 | time.RebootAt(1, distributed_clock::epoch() + dt); |
| 2140 | |
| 2141 | NodeEventLoopFactory *pi1 = factory.GetNodeEventLoopFactory("pi1"); |
| 2142 | NodeEventLoopFactory *pi2 = factory.GetNodeEventLoopFactory("pi2"); |
| 2143 | |
| 2144 | const UUID pi1_boot_uuid = pi1->boot_uuid(); |
| 2145 | const UUID pi2_boot_uuid = pi2->boot_uuid(); |
| 2146 | EXPECT_NE(pi1_boot_uuid, UUID::Zero()); |
| 2147 | EXPECT_NE(pi2_boot_uuid, UUID::Zero()); |
| 2148 | |
| 2149 | { |
| 2150 | ::std::unique_ptr<EventLoop> ping_event_loop = pi1->MakeEventLoop("ping"); |
| 2151 | aos::Sender<examples::Ping> test_message_sender = |
| 2152 | ping_event_loop->MakeSender<examples::Ping>("/reliable"); |
| 2153 | SendPing(&test_message_sender, 1); |
| 2154 | } |
| 2155 | |
| 2156 | factory.RunFor(chrono::milliseconds(500)); |
| 2157 | |
| 2158 | { |
| 2159 | ::std::unique_ptr<EventLoop> ping_event_loop = pi2->MakeEventLoop("pong"); |
| 2160 | aos::Fetcher<examples::Ping> fetcher = |
| 2161 | ping_event_loop->MakeFetcher<examples::Ping>("/reliable"); |
| 2162 | EXPECT_TRUE(fetcher.Fetch()); |
| 2163 | } |
| 2164 | |
| 2165 | factory.RunFor(chrono::seconds(1)); |
| 2166 | |
| 2167 | { |
| 2168 | ::std::unique_ptr<EventLoop> ping_event_loop = pi2->MakeEventLoop("pong"); |
| 2169 | aos::Fetcher<examples::Ping> fetcher = |
| 2170 | ping_event_loop->MakeFetcher<examples::Ping>("/reliable"); |
| 2171 | EXPECT_TRUE(fetcher.Fetch()); |
| 2172 | } |
| 2173 | EXPECT_NE(pi2_boot_uuid, pi2->boot_uuid()); |
| 2174 | } |
| 2175 | |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 2176 | class SimulatedEventLoopDisconnectTest : public ::testing::Test { |
| 2177 | public: |
| 2178 | SimulatedEventLoopDisconnectTest() |
| 2179 | : config(aos::configuration::ReadConfig(ArtifactPath( |
| 2180 | "aos/events/multinode_pingpong_test_split_config.json"))), |
| 2181 | time(configuration::NodesCount(&config.message())), |
| 2182 | factory(&config.message()) { |
| 2183 | factory.SetTimeConverter(&time); |
| 2184 | } |
| 2185 | |
| 2186 | void VerifyChannels(std::set<const aos::Channel *> statistics_channels, |
| 2187 | const monotonic_clock::time_point allowable_message_time, |
| 2188 | std::set<const aos::Node *> empty_nodes) { |
| 2189 | NodeEventLoopFactory *pi1 = factory.GetNodeEventLoopFactory("pi1"); |
| 2190 | NodeEventLoopFactory *pi2 = factory.GetNodeEventLoopFactory("pi2"); |
| 2191 | std::unique_ptr<aos::EventLoop> pi1_event_loop = |
| 2192 | pi1->MakeEventLoop("fetcher"); |
| 2193 | std::unique_ptr<aos::EventLoop> pi2_event_loop = |
| 2194 | pi2->MakeEventLoop("fetcher"); |
| 2195 | for (const aos::Channel *channel : *factory.configuration()->channels()) { |
| 2196 | if (configuration::ChannelIsReadableOnNode(channel, |
| 2197 | pi1_event_loop->node())) { |
| 2198 | std::unique_ptr<aos::RawFetcher> fetcher = |
| 2199 | pi1_event_loop->MakeRawFetcher(channel); |
| 2200 | if (statistics_channels.find(channel) == statistics_channels.end() || |
| 2201 | empty_nodes.find(pi1_event_loop->node()) != empty_nodes.end()) { |
| 2202 | EXPECT_FALSE(fetcher->Fetch() && |
| 2203 | fetcher->context().monotonic_event_time > |
| 2204 | allowable_message_time) |
| 2205 | << ": Found recent message on channel " |
| 2206 | << configuration::CleanedChannelToString(channel) << " and time " |
| 2207 | << fetcher->context().monotonic_event_time << " > " |
| 2208 | << allowable_message_time << " on pi1"; |
| 2209 | } else { |
| 2210 | EXPECT_TRUE(fetcher->Fetch() && |
| 2211 | fetcher->context().monotonic_event_time >= |
| 2212 | allowable_message_time) |
| 2213 | << ": Didn't find recent message on channel " |
| 2214 | << configuration::CleanedChannelToString(channel) << " on pi1"; |
| 2215 | } |
| 2216 | } |
| 2217 | if (configuration::ChannelIsReadableOnNode(channel, |
| 2218 | pi2_event_loop->node())) { |
| 2219 | std::unique_ptr<aos::RawFetcher> fetcher = |
| 2220 | pi2_event_loop->MakeRawFetcher(channel); |
| 2221 | if (statistics_channels.find(channel) == statistics_channels.end() || |
| 2222 | empty_nodes.find(pi2_event_loop->node()) != empty_nodes.end()) { |
| 2223 | EXPECT_FALSE(fetcher->Fetch() && |
| 2224 | fetcher->context().monotonic_event_time > |
| 2225 | allowable_message_time) |
| 2226 | << ": Found message on channel " |
| 2227 | << configuration::CleanedChannelToString(channel) << " and time " |
| 2228 | << fetcher->context().monotonic_event_time << " > " |
| 2229 | << allowable_message_time << " on pi2"; |
| 2230 | } else { |
| 2231 | EXPECT_TRUE(fetcher->Fetch() && |
| 2232 | fetcher->context().monotonic_event_time >= |
| 2233 | allowable_message_time) |
| 2234 | << ": Didn't find message on channel " |
| 2235 | << configuration::CleanedChannelToString(channel) << " on pi2"; |
| 2236 | } |
| 2237 | } |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | aos::FlatbufferDetachedBuffer<aos::Configuration> config; |
| 2242 | |
| 2243 | message_bridge::TestingTimeConverter time; |
| 2244 | SimulatedEventLoopFactory factory; |
| 2245 | }; |
| 2246 | |
| 2247 | // Tests that if we have message bridge client/server disabled, and timing |
| 2248 | // reports disabled, no messages are sent. Also tests that we can disconnect a |
| 2249 | // node and disable statistics on it and it actually fully disconnects. |
| 2250 | TEST_F(SimulatedEventLoopDisconnectTest, NoMessagesWhenDisabled) { |
| 2251 | time.StartEqual(); |
| 2252 | factory.SkipTimingReport(); |
| 2253 | factory.DisableStatistics(); |
| 2254 | |
| 2255 | NodeEventLoopFactory *pi1 = factory.GetNodeEventLoopFactory("pi1"); |
| 2256 | NodeEventLoopFactory *pi2 = factory.GetNodeEventLoopFactory("pi2"); |
| 2257 | |
| 2258 | std::unique_ptr<aos::EventLoop> pi1_event_loop = |
| 2259 | pi1->MakeEventLoop("fetcher"); |
| 2260 | std::unique_ptr<aos::EventLoop> pi2_event_loop = |
| 2261 | pi2->MakeEventLoop("fetcher"); |
| 2262 | |
| 2263 | factory.RunFor(chrono::milliseconds(100000)); |
| 2264 | |
| 2265 | // Confirm no messages are sent if we've configured them all off. |
| 2266 | VerifyChannels({}, monotonic_clock::min_time, {}); |
| 2267 | |
| 2268 | // Now, confirm that all the message_bridge channels come back when we |
| 2269 | // re-enable. |
| 2270 | factory.EnableStatistics(); |
| 2271 | |
| 2272 | factory.RunFor(chrono::milliseconds(10050)); |
| 2273 | |
| 2274 | // Build up the list of all the messages we expect when we come back. |
| 2275 | { |
| 2276 | std::set<const aos::Channel *> statistics_channels; |
Brian Silverman | 18c8aac | 2022-01-02 00:03:37 -0800 | [diff] [blame] | 2277 | for (const std::pair<std::string_view, const Node *> &pi : |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 2278 | std::vector<std::pair<std::string_view, const Node *>>{ |
| 2279 | {"/pi1/aos", pi1->node()}, |
| 2280 | {"/pi2/aos", pi1->node()}, |
| 2281 | {"/pi3/aos", pi1->node()}}) { |
| 2282 | statistics_channels.insert(configuration::GetChannel( |
| 2283 | factory.configuration(), pi.first, "aos.message_bridge.Timestamp", "", |
| 2284 | pi.second)); |
| 2285 | statistics_channels.insert(configuration::GetChannel( |
| 2286 | factory.configuration(), pi.first, |
| 2287 | "aos.message_bridge.ServerStatistics", "", pi.second)); |
| 2288 | statistics_channels.insert(configuration::GetChannel( |
| 2289 | factory.configuration(), pi.first, |
| 2290 | "aos.message_bridge.ClientStatistics", "", pi.second)); |
| 2291 | } |
| 2292 | |
| 2293 | statistics_channels.insert(configuration::GetChannel( |
| 2294 | factory.configuration(), |
| 2295 | "/pi1/aos/remote_timestamps/pi2/pi1/aos/aos-message_bridge-Timestamp", |
| 2296 | "aos.message_bridge.RemoteMessage", "", pi1->node())); |
| 2297 | statistics_channels.insert(configuration::GetChannel( |
| 2298 | factory.configuration(), |
| 2299 | "/pi2/aos/remote_timestamps/pi1/pi2/aos/aos-message_bridge-Timestamp", |
| 2300 | "aos.message_bridge.RemoteMessage", "", pi2->node())); |
| 2301 | VerifyChannels(statistics_channels, monotonic_clock::min_time, {}); |
| 2302 | } |
| 2303 | |
| 2304 | // Now test that we can disable the messages for a single node |
| 2305 | pi2->DisableStatistics(); |
| 2306 | const aos::monotonic_clock::time_point statistics_disable_time = |
| 2307 | pi2->monotonic_now(); |
| 2308 | factory.RunFor(chrono::milliseconds(10000)); |
| 2309 | |
| 2310 | // We should see a much smaller set of messages, but should still see messages |
| 2311 | // forwarded, mainly the timestamp message. |
| 2312 | { |
| 2313 | std::set<const aos::Channel *> statistics_channels; |
Brian Silverman | 18c8aac | 2022-01-02 00:03:37 -0800 | [diff] [blame] | 2314 | for (const std::pair<std::string_view, const Node *> &pi : |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 2315 | std::vector<std::pair<std::string_view, const Node *>>{ |
| 2316 | {"/pi1/aos", pi1->node()}, {"/pi3/aos", pi1->node()}}) { |
| 2317 | statistics_channels.insert(configuration::GetChannel( |
| 2318 | factory.configuration(), pi.first, "aos.message_bridge.Timestamp", "", |
| 2319 | pi.second)); |
| 2320 | statistics_channels.insert(configuration::GetChannel( |
| 2321 | factory.configuration(), pi.first, |
| 2322 | "aos.message_bridge.ServerStatistics", "", pi.second)); |
| 2323 | statistics_channels.insert(configuration::GetChannel( |
| 2324 | factory.configuration(), pi.first, |
| 2325 | "aos.message_bridge.ClientStatistics", "", pi.second)); |
| 2326 | } |
| 2327 | |
| 2328 | statistics_channels.insert(configuration::GetChannel( |
| 2329 | factory.configuration(), |
| 2330 | "/pi1/aos/remote_timestamps/pi2/pi1/aos/aos-message_bridge-Timestamp", |
| 2331 | "aos.message_bridge.RemoteMessage", "", pi1->node())); |
| 2332 | VerifyChannels(statistics_channels, statistics_disable_time, {}); |
| 2333 | } |
| 2334 | |
| 2335 | // Now, fully disconnect the node. This will completely quiet down pi2. |
| 2336 | pi1->Disconnect(pi2->node()); |
| 2337 | pi2->Disconnect(pi1->node()); |
| 2338 | |
| 2339 | const aos::monotonic_clock::time_point disconnect_disable_time = |
| 2340 | pi2->monotonic_now(); |
| 2341 | factory.RunFor(chrono::milliseconds(10000)); |
| 2342 | |
| 2343 | { |
| 2344 | std::set<const aos::Channel *> statistics_channels; |
Brian Silverman | 18c8aac | 2022-01-02 00:03:37 -0800 | [diff] [blame] | 2345 | for (const std::pair<std::string_view, const Node *> &pi : |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 2346 | std::vector<std::pair<std::string_view, const Node *>>{ |
| 2347 | {"/pi1/aos", pi1->node()}, {"/pi3/aos", pi1->node()}}) { |
| 2348 | statistics_channels.insert(configuration::GetChannel( |
| 2349 | factory.configuration(), pi.first, "aos.message_bridge.Timestamp", "", |
| 2350 | pi.second)); |
| 2351 | statistics_channels.insert(configuration::GetChannel( |
| 2352 | factory.configuration(), pi.first, |
| 2353 | "aos.message_bridge.ServerStatistics", "", pi.second)); |
| 2354 | statistics_channels.insert(configuration::GetChannel( |
| 2355 | factory.configuration(), pi.first, |
| 2356 | "aos.message_bridge.ClientStatistics", "", pi.second)); |
| 2357 | } |
| 2358 | |
| 2359 | VerifyChannels(statistics_channels, disconnect_disable_time, {pi2->node()}); |
| 2360 | } |
| 2361 | } |
| 2362 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 2363 | } // namespace testing |
| 2364 | } // namespace aos |