Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 1 | #include <chrono> |
| 2 | #include <thread> |
| 3 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 4 | #include "absl/strings/str_cat.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 5 | #include "gtest/gtest.h" |
| 6 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 7 | #include "aos/events/ping_generated.h" |
| 8 | #include "aos/events/pong_generated.h" |
Brian Silverman | 7b266d9 | 2021-02-17 21:24:02 -0800 | [diff] [blame] | 9 | #include "aos/ipc_lib/event.h" |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 10 | #include "aos/network/message_bridge_client_lib.h" |
Austin Schuh | 89f23e3 | 2023-05-15 17:06:43 -0700 | [diff] [blame] | 11 | #include "aos/network/message_bridge_protocol.h" |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 12 | #include "aos/network/message_bridge_server_lib.h" |
Jim Ostrowski | 2192ddb | 2020-06-24 19:07:31 -0700 | [diff] [blame] | 13 | #include "aos/network/team_number.h" |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 14 | #include "aos/sha256.h" |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 15 | #include "aos/testing/path.h" |
Austin Schuh | e991fe2 | 2020-11-18 16:53:39 -0800 | [diff] [blame] | 16 | #include "aos/util/file.h" |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 17 | |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 18 | DECLARE_string(boot_uuid); |
| 19 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 20 | namespace aos { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 21 | void SetShmBase(const std::string_view base); |
| 22 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 23 | namespace message_bridge { |
| 24 | namespace testing { |
| 25 | |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 26 | using aos::testing::ArtifactPath; |
| 27 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 28 | namespace chrono = std::chrono; |
| 29 | |
Austin Schuh | e991fe2 | 2020-11-18 16:53:39 -0800 | [diff] [blame] | 30 | std::string ShmBase(const std::string_view node) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 31 | const char *tmpdir_c_str = getenv("TEST_TMPDIR"); |
| 32 | if (tmpdir_c_str != nullptr) { |
Austin Schuh | e991fe2 | 2020-11-18 16:53:39 -0800 | [diff] [blame] | 33 | return absl::StrCat(tmpdir_c_str, "/", node); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 34 | } else { |
Austin Schuh | e991fe2 | 2020-11-18 16:53:39 -0800 | [diff] [blame] | 35 | return absl::StrCat("/dev/shm/", node); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
Austin Schuh | e991fe2 | 2020-11-18 16:53:39 -0800 | [diff] [blame] | 39 | void DoSetShmBase(const std::string_view node) { |
| 40 | aos::SetShmBase(ShmBase(node)); |
| 41 | } |
| 42 | |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 43 | // Class to manage starting and stopping a thread with an event loop in it. The |
| 44 | // thread is guarenteed to be running before the constructor exits. |
| 45 | class ThreadedEventLoopRunner { |
| 46 | public: |
| 47 | ThreadedEventLoopRunner(aos::ShmEventLoop *event_loop) |
| 48 | : event_loop_(event_loop), my_thread_([this]() { |
| 49 | LOG(INFO) << "Started " << event_loop_->name(); |
| 50 | event_loop_->OnRun([this]() { event_.Set(); }); |
| 51 | event_loop_->Run(); |
| 52 | }) { |
| 53 | event_.Wait(); |
| 54 | } |
| 55 | |
| 56 | ~ThreadedEventLoopRunner() { Exit(); } |
| 57 | |
| 58 | void Exit() { |
| 59 | if (my_thread_.joinable()) { |
| 60 | event_loop_->Exit(); |
| 61 | my_thread_.join(); |
| 62 | my_thread_ = std::thread(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | aos::Event event_; |
| 68 | aos::ShmEventLoop *event_loop_; |
| 69 | std::thread my_thread_; |
| 70 | }; |
| 71 | |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 72 | // Parameters to run all the tests with. |
| 73 | struct Param { |
| 74 | // The config file to use. |
| 75 | std::string config; |
| 76 | // If true, the RemoteMessage channel should be shared between all the remote |
| 77 | // channels. If false, there will be 1 RemoteMessage channel per remote |
| 78 | // channel. |
| 79 | bool shared; |
| 80 | }; |
| 81 | |
| 82 | class MessageBridgeParameterizedTest |
| 83 | : public ::testing::TestWithParam<struct Param> { |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 84 | public: |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 85 | MessageBridgeParameterizedTest() |
| 86 | : config(aos::configuration::ReadConfig( |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 87 | ArtifactPath(absl::StrCat("aos/network/", GetParam().config)))), |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 88 | config_sha256(Sha256(config.span())), |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 89 | pi1_boot_uuid_(UUID::Random()), |
| 90 | pi2_boot_uuid_(UUID::Random()) { |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 91 | util::UnlinkRecursive(ShmBase("pi1")); |
| 92 | util::UnlinkRecursive(ShmBase("pi2")); |
| 93 | } |
Austin Schuh | e991fe2 | 2020-11-18 16:53:39 -0800 | [diff] [blame] | 94 | |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 95 | bool shared() const { return GetParam().shared; } |
| 96 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 97 | void OnPi1() { |
| 98 | DoSetShmBase("pi1"); |
| 99 | FLAGS_override_hostname = "raspberrypi"; |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 100 | FLAGS_boot_uuid = pi1_boot_uuid_.ToString(); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void OnPi2() { |
| 104 | DoSetShmBase("pi2"); |
| 105 | FLAGS_override_hostname = "raspberrypi2"; |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 106 | FLAGS_boot_uuid = pi2_boot_uuid_.ToString(); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 109 | void MakePi1Server(std::string server_config_sha256 = "") { |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 110 | OnPi1(); |
| 111 | FLAGS_application_name = "pi1_message_bridge_server"; |
| 112 | pi1_server_event_loop = |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 113 | std::make_unique<aos::ShmEventLoop>(&config.message()); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 114 | pi1_server_event_loop->SetRuntimeRealtimePriority(1); |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 115 | pi1_message_bridge_server = std::make_unique<MessageBridgeServer>( |
| 116 | pi1_server_event_loop.get(), server_config_sha256.size() == 0 |
| 117 | ? config_sha256 |
| 118 | : server_config_sha256); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void RunPi1Server(chrono::nanoseconds duration) { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 122 | // Set up a shutdown callback. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 123 | aos::TimerHandler *const quit = pi1_server_event_loop->AddTimer( |
| 124 | [this]() { pi1_server_event_loop->Exit(); }); |
| 125 | pi1_server_event_loop->OnRun([this, quit, duration]() { |
| 126 | // Stop between timestamps, not exactly on them. |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 127 | quit->Schedule(pi1_server_event_loop->monotonic_now() + duration); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 128 | }); |
| 129 | |
| 130 | pi1_server_event_loop->Run(); |
| 131 | } |
| 132 | |
| 133 | void StartPi1Server() { |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 134 | pi1_server_thread = |
| 135 | std::make_unique<ThreadedEventLoopRunner>(pi1_server_event_loop.get()); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void StopPi1Server() { |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 139 | pi1_server_thread.reset(); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 140 | pi1_message_bridge_server.reset(); |
| 141 | pi1_server_event_loop.reset(); |
| 142 | } |
| 143 | |
| 144 | void MakePi1Client() { |
| 145 | OnPi1(); |
| 146 | FLAGS_application_name = "pi1_message_bridge_client"; |
| 147 | pi1_client_event_loop = |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 148 | std::make_unique<aos::ShmEventLoop>(&config.message()); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 149 | pi1_client_event_loop->SetRuntimeRealtimePriority(1); |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 150 | pi1_message_bridge_client = std::make_unique<MessageBridgeClient>( |
| 151 | pi1_client_event_loop.get(), config_sha256); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void StartPi1Client() { |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 155 | pi1_client_thread = |
| 156 | std::make_unique<ThreadedEventLoopRunner>(pi1_client_event_loop.get()); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void StopPi1Client() { |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 160 | pi1_client_thread.reset(); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 161 | pi1_message_bridge_client.reset(); |
| 162 | pi1_client_event_loop.reset(); |
| 163 | } |
| 164 | |
| 165 | void MakePi1Test() { |
| 166 | OnPi1(); |
| 167 | FLAGS_application_name = "test1"; |
| 168 | pi1_test_event_loop = |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 169 | std::make_unique<aos::ShmEventLoop>(&config.message()); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 170 | |
| 171 | pi1_test_event_loop->MakeWatcher( |
| 172 | "/pi1/aos", [](const ServerStatistics &stats) { |
| 173 | VLOG(1) << "/pi1/aos ServerStatistics " << FlatbufferToJson(&stats); |
| 174 | }); |
| 175 | |
| 176 | pi1_test_event_loop->MakeWatcher( |
| 177 | "/pi1/aos", [](const ClientStatistics &stats) { |
| 178 | VLOG(1) << "/pi1/aos ClientStatistics " << FlatbufferToJson(&stats); |
| 179 | }); |
| 180 | |
| 181 | pi1_test_event_loop->MakeWatcher( |
| 182 | "/pi1/aos", [](const Timestamp ×tamp) { |
| 183 | VLOG(1) << "/pi1/aos Timestamp " << FlatbufferToJson(×tamp); |
| 184 | }); |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 185 | pi1_test_event_loop->MakeWatcher( |
| 186 | "/pi2/aos", [this](const Timestamp ×tamp) { |
| 187 | VLOG(1) << "/pi2/aos Timestamp " << FlatbufferToJson(×tamp); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 188 | EXPECT_EQ(pi1_test_event_loop->context().source_boot_uuid, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 189 | pi2_boot_uuid_); |
| 190 | }); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void StartPi1Test() { |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 194 | pi1_test_thread = |
| 195 | std::make_unique<ThreadedEventLoopRunner>(pi1_test_event_loop.get()); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 198 | void StopPi1Test() { pi1_test_thread.reset(); } |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 199 | |
| 200 | void MakePi2Server() { |
| 201 | OnPi2(); |
| 202 | FLAGS_application_name = "pi2_message_bridge_server"; |
| 203 | pi2_server_event_loop = |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 204 | std::make_unique<aos::ShmEventLoop>(&config.message()); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 205 | pi2_server_event_loop->SetRuntimeRealtimePriority(1); |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 206 | pi2_message_bridge_server = std::make_unique<MessageBridgeServer>( |
| 207 | pi2_server_event_loop.get(), config_sha256); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void RunPi2Server(chrono::nanoseconds duration) { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 211 | // Set up a shutdown callback. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 212 | aos::TimerHandler *const quit = pi2_server_event_loop->AddTimer( |
| 213 | [this]() { pi2_server_event_loop->Exit(); }); |
| 214 | pi2_server_event_loop->OnRun([this, quit, duration]() { |
| 215 | // Stop between timestamps, not exactly on them. |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 216 | quit->Schedule(pi2_server_event_loop->monotonic_now() + duration); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 217 | }); |
| 218 | |
| 219 | pi2_server_event_loop->Run(); |
| 220 | } |
| 221 | |
| 222 | void StartPi2Server() { |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 223 | pi2_server_thread = |
| 224 | std::make_unique<ThreadedEventLoopRunner>(pi2_server_event_loop.get()); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void StopPi2Server() { |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 228 | pi2_server_thread.reset(); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 229 | pi2_message_bridge_server.reset(); |
| 230 | pi2_server_event_loop.reset(); |
| 231 | } |
| 232 | |
| 233 | void MakePi2Client() { |
| 234 | OnPi2(); |
| 235 | FLAGS_application_name = "pi2_message_bridge_client"; |
| 236 | pi2_client_event_loop = |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 237 | std::make_unique<aos::ShmEventLoop>(&config.message()); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 238 | pi2_client_event_loop->SetRuntimeRealtimePriority(1); |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 239 | pi2_message_bridge_client = std::make_unique<MessageBridgeClient>( |
| 240 | pi2_client_event_loop.get(), config_sha256); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void RunPi2Client(chrono::nanoseconds duration) { |
| 244 | // Run for 5 seconds to make sure we have time to estimate the offset. |
| 245 | aos::TimerHandler *const quit = pi2_client_event_loop->AddTimer( |
| 246 | [this]() { pi2_client_event_loop->Exit(); }); |
| 247 | pi2_client_event_loop->OnRun([this, quit, duration]() { |
| 248 | // Stop between timestamps, not exactly on them. |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 249 | quit->Schedule(pi2_client_event_loop->monotonic_now() + duration); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 250 | }); |
| 251 | |
| 252 | // And go! |
| 253 | pi2_client_event_loop->Run(); |
| 254 | } |
| 255 | |
| 256 | void StartPi2Client() { |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 257 | pi2_client_thread = |
| 258 | std::make_unique<ThreadedEventLoopRunner>(pi2_client_event_loop.get()); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void StopPi2Client() { |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 262 | pi2_client_thread.reset(); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 263 | pi2_message_bridge_client.reset(); |
| 264 | pi2_client_event_loop.reset(); |
| 265 | } |
| 266 | |
| 267 | void MakePi2Test() { |
| 268 | OnPi2(); |
| 269 | FLAGS_application_name = "test2"; |
| 270 | pi2_test_event_loop = |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 271 | std::make_unique<aos::ShmEventLoop>(&config.message()); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 272 | |
| 273 | pi2_test_event_loop->MakeWatcher( |
| 274 | "/pi2/aos", [](const ServerStatistics &stats) { |
| 275 | VLOG(1) << "/pi2/aos ServerStatistics " << FlatbufferToJson(&stats); |
| 276 | }); |
| 277 | |
| 278 | pi2_test_event_loop->MakeWatcher( |
| 279 | "/pi2/aos", [](const ClientStatistics &stats) { |
| 280 | VLOG(1) << "/pi2/aos ClientStatistics " << FlatbufferToJson(&stats); |
| 281 | }); |
| 282 | |
| 283 | pi2_test_event_loop->MakeWatcher( |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 284 | "/pi1/aos", [this](const Timestamp ×tamp) { |
| 285 | VLOG(1) << "/pi1/aos Timestamp " << FlatbufferToJson(×tamp); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 286 | EXPECT_EQ(pi2_test_event_loop->context().source_boot_uuid, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 287 | pi1_boot_uuid_); |
| 288 | }); |
| 289 | pi2_test_event_loop->MakeWatcher( |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 290 | "/pi2/aos", [](const Timestamp ×tamp) { |
| 291 | VLOG(1) << "/pi2/aos Timestamp " << FlatbufferToJson(×tamp); |
| 292 | }); |
| 293 | } |
| 294 | |
| 295 | void StartPi2Test() { |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 296 | pi2_test_thread = |
| 297 | std::make_unique<ThreadedEventLoopRunner>(pi2_test_event_loop.get()); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 300 | void StopPi2Test() { pi2_test_thread.reset(); } |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 301 | |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 302 | aos::FlatbufferDetachedBuffer<aos::Configuration> config; |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 303 | std::string config_sha256; |
| 304 | |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 305 | const UUID pi1_boot_uuid_; |
| 306 | const UUID pi2_boot_uuid_; |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 307 | |
| 308 | std::unique_ptr<aos::ShmEventLoop> pi1_server_event_loop; |
| 309 | std::unique_ptr<MessageBridgeServer> pi1_message_bridge_server; |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 310 | std::unique_ptr<ThreadedEventLoopRunner> pi1_server_thread; |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 311 | |
| 312 | std::unique_ptr<aos::ShmEventLoop> pi1_client_event_loop; |
| 313 | std::unique_ptr<MessageBridgeClient> pi1_message_bridge_client; |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 314 | std::unique_ptr<ThreadedEventLoopRunner> pi1_client_thread; |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 315 | |
| 316 | std::unique_ptr<aos::ShmEventLoop> pi1_test_event_loop; |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 317 | std::unique_ptr<ThreadedEventLoopRunner> pi1_test_thread; |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 318 | |
| 319 | std::unique_ptr<aos::ShmEventLoop> pi2_server_event_loop; |
| 320 | std::unique_ptr<MessageBridgeServer> pi2_message_bridge_server; |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 321 | std::unique_ptr<ThreadedEventLoopRunner> pi2_server_thread; |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 322 | |
| 323 | std::unique_ptr<aos::ShmEventLoop> pi2_client_event_loop; |
| 324 | std::unique_ptr<MessageBridgeClient> pi2_message_bridge_client; |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 325 | std::unique_ptr<ThreadedEventLoopRunner> pi2_client_thread; |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 326 | |
| 327 | std::unique_ptr<aos::ShmEventLoop> pi2_test_event_loop; |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 328 | std::unique_ptr<ThreadedEventLoopRunner> pi2_test_thread; |
Austin Schuh | e991fe2 | 2020-11-18 16:53:39 -0800 | [diff] [blame] | 329 | }; |
| 330 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 331 | // Test that we can send a ping message over sctp and receive it. |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 332 | TEST_P(MessageBridgeParameterizedTest, PingPong) { |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 333 | // This is rather annoying to set up. We need to start up a client and |
| 334 | // server, on the same node, but get them to think that they are on different |
| 335 | // nodes. |
| 336 | // |
| 337 | // We then get to wait until they are connected. |
| 338 | // |
| 339 | // After they are connected, we send a Ping message. |
| 340 | // |
| 341 | // On the other end, we receive a Pong message. |
| 342 | // |
| 343 | // But, we need the client to not post directly to "/test" like it would in a |
| 344 | // real system, otherwise we will re-send the ping message... So, use an |
| 345 | // application specific map to have the client post somewhere else. |
| 346 | // |
| 347 | // To top this all off, each of these needs to be done with a ShmEventLoop, |
| 348 | // which needs to run in a separate thread... And it is really hard to get |
| 349 | // everything started up reliably. So just be super generous on timeouts and |
| 350 | // hope for the best. We can be more generous in the future if we need to. |
| 351 | // |
| 352 | // We are faking the application names by passing in --application_name=foo |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 353 | OnPi1(); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 354 | // Force ourselves to be "raspberrypi" and allocate everything. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 355 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 356 | MakePi1Server(); |
| 357 | MakePi1Client(); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 358 | |
Austin Schuh | 89e1e9c | 2023-05-15 14:38:44 -0700 | [diff] [blame] | 359 | const std::string long_data = std::string(10000, 'a'); |
| 360 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 361 | // And build the app which sends the pings. |
| 362 | FLAGS_application_name = "ping"; |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 363 | aos::ShmEventLoop ping_event_loop(&config.message()); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 364 | aos::Sender<examples::Ping> ping_sender = |
| 365 | ping_event_loop.MakeSender<examples::Ping>("/test"); |
| 366 | |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 367 | aos::ShmEventLoop pi1_test_event_loop(&config.message()); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 368 | aos::Fetcher<RemoteMessage> message_header_fetcher1 = |
| 369 | pi1_test_event_loop.MakeFetcher<RemoteMessage>( |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 370 | shared() ? "/pi1/aos/remote_timestamps/pi2" |
| 371 | : "/pi1/aos/remote_timestamps/pi2/test/aos-examples-Ping"); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 372 | |
| 373 | // Fetchers for confirming the remote timestamps made it. |
| 374 | aos::Fetcher<examples::Ping> ping_on_pi1_fetcher = |
| 375 | ping_event_loop.MakeFetcher<examples::Ping>("/test"); |
| 376 | aos::Fetcher<Timestamp> pi1_on_pi1_timestamp_fetcher = |
| 377 | ping_event_loop.MakeFetcher<Timestamp>("/aos"); |
| 378 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 379 | // Now do it for "raspberrypi2", the client. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 380 | OnPi2(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 381 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 382 | MakePi2Client(); |
| 383 | MakePi2Server(); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 384 | |
| 385 | // And build the app which sends the pongs. |
| 386 | FLAGS_application_name = "pong"; |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 387 | aos::ShmEventLoop pong_event_loop(&config.message()); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 388 | |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 389 | // And build the app for testing. |
| 390 | FLAGS_application_name = "test"; |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 391 | aos::ShmEventLoop test_event_loop(&config.message()); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 392 | |
| 393 | aos::Fetcher<ClientStatistics> client_statistics_fetcher = |
| 394 | test_event_loop.MakeFetcher<ClientStatistics>("/aos"); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 395 | aos::Fetcher<RemoteMessage> message_header_fetcher2 = |
| 396 | test_event_loop.MakeFetcher<RemoteMessage>( |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 397 | shared() ? "/pi2/aos/remote_timestamps/pi1" |
| 398 | : "/pi2/aos/remote_timestamps/pi1/pi2/aos/" |
| 399 | "aos-message_bridge-Timestamp"); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 400 | |
| 401 | // Event loop for fetching data delivered to pi2 from pi1 to match up |
| 402 | // messages. |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 403 | aos::ShmEventLoop delivered_messages_event_loop(&config.message()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 404 | aos::Fetcher<Timestamp> pi1_on_pi2_timestamp_fetcher = |
| 405 | delivered_messages_event_loop.MakeFetcher<Timestamp>("/pi1/aos"); |
| 406 | aos::Fetcher<examples::Ping> ping_on_pi2_fetcher = |
| 407 | delivered_messages_event_loop.MakeFetcher<examples::Ping>("/test"); |
| 408 | EXPECT_FALSE(ping_on_pi2_fetcher.Fetch()); |
| 409 | EXPECT_FALSE(pi1_on_pi2_timestamp_fetcher.Fetch()); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 410 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 411 | // Count the pongs. |
| 412 | int pong_count = 0; |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 413 | pong_event_loop.MakeWatcher("/test", [&pong_count, &pong_event_loop, |
| 414 | this](const examples::Ping &ping) { |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 415 | EXPECT_EQ(pong_event_loop.context().source_boot_uuid, pi1_boot_uuid_); |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 416 | ++pong_count; |
| 417 | VLOG(1) << "Got ping back " << FlatbufferToJson(&ping); |
| 418 | }); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 419 | |
| 420 | FLAGS_override_hostname = ""; |
| 421 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 422 | // Wait until we are connected, then send. |
| 423 | int ping_count = 0; |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 424 | int pi1_server_statistics_count = 0; |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 425 | ping_event_loop.MakeWatcher( |
| 426 | "/pi1/aos", |
| 427 | [this, &ping_count, &ping_sender, &pi1_server_statistics_count, |
| 428 | &long_data](const ServerStatistics &stats) { |
| 429 | VLOG(1) << "/pi1/aos ServerStatistics " << FlatbufferToJson(&stats); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 430 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 431 | ASSERT_TRUE(stats.has_connections()); |
| 432 | EXPECT_EQ(stats.connections()->size(), 1); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 433 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 434 | bool connected = false; |
| 435 | for (const ServerConnection *connection : *stats.connections()) { |
| 436 | // Confirm that we are estimating the server time offset correctly. It |
| 437 | // should be about 0 since we are on the same machine here. |
| 438 | if (connection->has_monotonic_offset()) { |
| 439 | EXPECT_LT(chrono::nanoseconds(connection->monotonic_offset()), |
| 440 | chrono::milliseconds(1)); |
| 441 | EXPECT_GT(chrono::nanoseconds(connection->monotonic_offset()), |
| 442 | chrono::milliseconds(-1)); |
| 443 | ++pi1_server_statistics_count; |
| 444 | } |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 445 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 446 | if (connection->node()->name()->string_view() == |
| 447 | pi2_client_event_loop->node()->name()->string_view()) { |
| 448 | if (connection->state() == State::CONNECTED) { |
| 449 | EXPECT_TRUE(connection->has_boot_uuid()); |
| 450 | EXPECT_EQ(connection->connection_count(), 1u); |
| 451 | EXPECT_LT(monotonic_clock::time_point(chrono::nanoseconds( |
| 452 | connection->connected_since_time())), |
| 453 | monotonic_clock::now()); |
| 454 | connected = true; |
| 455 | } else { |
| 456 | EXPECT_FALSE(connection->has_connection_count()); |
| 457 | EXPECT_FALSE(connection->has_connected_since_time()); |
| 458 | } |
| 459 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 462 | if (connected) { |
| 463 | VLOG(1) << "Connected! Sent ping."; |
| 464 | auto builder = ping_sender.MakeBuilder(); |
| 465 | builder.fbb()->CreateString(long_data); |
| 466 | examples::Ping::Builder ping_builder = |
| 467 | builder.MakeBuilder<examples::Ping>(); |
| 468 | ping_builder.add_value(ping_count + 971); |
| 469 | EXPECT_EQ(builder.Send(ping_builder.Finish()), RawSender::Error::kOk); |
| 470 | ++ping_count; |
| 471 | } |
| 472 | }); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 473 | |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 474 | // Confirm both client and server statistics messages have decent offsets in |
| 475 | // them. |
| 476 | int pi2_server_statistics_count = 0; |
Austin Schuh | 196a445 | 2020-03-15 23:12:03 -0700 | [diff] [blame] | 477 | pong_event_loop.MakeWatcher("/pi2/aos", [&pi2_server_statistics_count]( |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 478 | const ServerStatistics &stats) { |
Austin Schuh | 1ca49e9 | 2020-12-11 00:01:27 -0800 | [diff] [blame] | 479 | VLOG(1) << "/pi2/aos ServerStatistics " << FlatbufferToJson(&stats); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 480 | for (const ServerConnection *connection : *stats.connections()) { |
| 481 | if (connection->has_monotonic_offset()) { |
| 482 | ++pi2_server_statistics_count; |
| 483 | // Confirm that we are estimating the server time offset correctly. It |
| 484 | // should be about 0 since we are on the same machine here. |
| 485 | EXPECT_LT(chrono::nanoseconds(connection->monotonic_offset()), |
| 486 | chrono::milliseconds(1)); |
| 487 | EXPECT_GT(chrono::nanoseconds(connection->monotonic_offset()), |
| 488 | chrono::milliseconds(-1)); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 489 | EXPECT_TRUE(connection->has_boot_uuid()); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 490 | } |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 491 | |
| 492 | if (connection->state() == State::CONNECTED) { |
| 493 | EXPECT_EQ(connection->connection_count(), 1u); |
| 494 | EXPECT_LT(monotonic_clock::time_point( |
| 495 | chrono::nanoseconds(connection->connected_since_time())), |
| 496 | monotonic_clock::now()); |
| 497 | } else { |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 498 | // If we have been connected, we expect the connection count to stay |
| 499 | // around. |
| 500 | if (pi2_server_statistics_count > 0) { |
| 501 | EXPECT_TRUE(connection->has_connection_count()); |
| 502 | EXPECT_EQ(connection->connection_count(), 1u); |
| 503 | } else { |
| 504 | EXPECT_FALSE(connection->has_connection_count()); |
| 505 | } |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 506 | EXPECT_FALSE(connection->has_connected_since_time()); |
| 507 | } |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 508 | } |
| 509 | }); |
| 510 | |
| 511 | int pi1_client_statistics_count = 0; |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 512 | int pi1_connected_client_statistics_count = 0; |
| 513 | ping_event_loop.MakeWatcher( |
| 514 | "/pi1/aos", |
| 515 | [&pi1_client_statistics_count, |
| 516 | &pi1_connected_client_statistics_count](const ClientStatistics &stats) { |
| 517 | VLOG(1) << "/pi1/aos ClientStatistics " << FlatbufferToJson(&stats); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 518 | |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 519 | for (const ClientConnection *connection : *stats.connections()) { |
| 520 | if (connection->has_monotonic_offset()) { |
| 521 | ++pi1_client_statistics_count; |
| 522 | // It takes at least 10 microseconds to send a message between the |
| 523 | // client and server. The min (filtered) time shouldn't be over 10 |
| 524 | // milliseconds on localhost. This might have to bump up if this is |
| 525 | // proving flaky. |
| 526 | EXPECT_LT(chrono::nanoseconds(connection->monotonic_offset()), |
| 527 | chrono::milliseconds(10)) |
| 528 | << " " << connection->monotonic_offset() |
| 529 | << "ns vs 10000ns on iteration " << pi1_client_statistics_count; |
| 530 | EXPECT_GT(chrono::nanoseconds(connection->monotonic_offset()), |
| 531 | chrono::microseconds(10)) |
| 532 | << " " << connection->monotonic_offset() |
| 533 | << "ns vs 10000ns on iteration " << pi1_client_statistics_count; |
| 534 | } |
| 535 | if (connection->state() == State::CONNECTED) { |
| 536 | EXPECT_EQ(connection->connection_count(), 1u); |
| 537 | EXPECT_LT(monotonic_clock::time_point(chrono::nanoseconds( |
| 538 | connection->connected_since_time())), |
| 539 | monotonic_clock::now()); |
| 540 | // The first Connected message may not have a UUID in it since no |
| 541 | // data has flown. That's fine. |
| 542 | if (pi1_connected_client_statistics_count > 0) { |
| 543 | EXPECT_TRUE(connection->has_boot_uuid()) |
| 544 | << ": " << aos::FlatbufferToJson(connection); |
| 545 | } |
| 546 | ++pi1_connected_client_statistics_count; |
| 547 | } else { |
| 548 | EXPECT_FALSE(connection->has_connection_count()); |
| 549 | EXPECT_FALSE(connection->has_connected_since_time()); |
| 550 | } |
| 551 | } |
| 552 | }); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 553 | |
| 554 | int pi2_client_statistics_count = 0; |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 555 | int pi2_connected_client_statistics_count = 0; |
| 556 | pong_event_loop.MakeWatcher( |
| 557 | "/pi2/aos", |
| 558 | [&pi2_client_statistics_count, |
| 559 | &pi2_connected_client_statistics_count](const ClientStatistics &stats) { |
| 560 | VLOG(1) << "/pi2/aos ClientStatistics " << FlatbufferToJson(&stats); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 561 | |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 562 | for (const ClientConnection *connection : *stats.connections()) { |
| 563 | if (connection->has_monotonic_offset()) { |
| 564 | ++pi2_client_statistics_count; |
| 565 | EXPECT_LT(chrono::nanoseconds(connection->monotonic_offset()), |
| 566 | chrono::milliseconds(10)) |
| 567 | << ": got " << aos::FlatbufferToJson(connection); |
| 568 | EXPECT_GT(chrono::nanoseconds(connection->monotonic_offset()), |
| 569 | chrono::microseconds(10)) |
| 570 | << ": got " << aos::FlatbufferToJson(connection); |
| 571 | } |
| 572 | if (connection->state() == State::CONNECTED) { |
| 573 | EXPECT_EQ(connection->connection_count(), 1u); |
| 574 | EXPECT_LT(monotonic_clock::time_point(chrono::nanoseconds( |
| 575 | connection->connected_since_time())), |
| 576 | monotonic_clock::now()); |
| 577 | if (pi2_connected_client_statistics_count > 0) { |
| 578 | EXPECT_TRUE(connection->has_boot_uuid()); |
| 579 | } |
| 580 | ++pi2_connected_client_statistics_count; |
| 581 | } else { |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 582 | if (pi2_connected_client_statistics_count == 0) { |
| 583 | EXPECT_FALSE(connection->has_connection_count()) |
| 584 | << aos::FlatbufferToJson(&stats); |
| 585 | } else { |
| 586 | EXPECT_TRUE(connection->has_connection_count()) |
| 587 | << aos::FlatbufferToJson(&stats); |
| 588 | EXPECT_EQ(connection->connection_count(), 1u); |
| 589 | } |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 590 | EXPECT_FALSE(connection->has_connected_since_time()); |
| 591 | } |
| 592 | } |
| 593 | }); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 594 | |
Austin Schuh | 196a445 | 2020-03-15 23:12:03 -0700 | [diff] [blame] | 595 | ping_event_loop.MakeWatcher("/pi1/aos", [](const Timestamp ×tamp) { |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 596 | EXPECT_TRUE(timestamp.has_offsets()); |
Austin Schuh | 1ca49e9 | 2020-12-11 00:01:27 -0800 | [diff] [blame] | 597 | VLOG(1) << "/pi1/aos Timestamp " << FlatbufferToJson(×tamp); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 598 | }); |
Austin Schuh | 196a445 | 2020-03-15 23:12:03 -0700 | [diff] [blame] | 599 | pong_event_loop.MakeWatcher("/pi2/aos", [](const Timestamp ×tamp) { |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 600 | EXPECT_TRUE(timestamp.has_offsets()); |
Austin Schuh | 1ca49e9 | 2020-12-11 00:01:27 -0800 | [diff] [blame] | 601 | VLOG(1) << "/pi2/aos Timestamp " << FlatbufferToJson(×tamp); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 602 | }); |
| 603 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 604 | // Find the channel index for both the /pi1/aos Timestamp channel and Ping |
| 605 | // channel. |
| 606 | const size_t pi1_timestamp_channel = configuration::ChannelIndex( |
| 607 | pong_event_loop.configuration(), pi1_on_pi2_timestamp_fetcher.channel()); |
| 608 | const size_t ping_timestamp_channel = |
| 609 | configuration::ChannelIndex(delivered_messages_event_loop.configuration(), |
| 610 | ping_on_pi2_fetcher.channel()); |
| 611 | |
| 612 | for (const Channel *channel : *ping_event_loop.configuration()->channels()) { |
| 613 | VLOG(1) << "Channel " |
| 614 | << configuration::ChannelIndex(ping_event_loop.configuration(), |
| 615 | channel) |
| 616 | << " " << configuration::CleanedChannelToString(channel); |
| 617 | } |
| 618 | |
| 619 | // For each remote timestamp we get back, confirm that it is either a ping |
| 620 | // message, or a timestamp we sent out. Also confirm that the timestamps are |
| 621 | // correct. |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 622 | for (std::pair<int, std::string> channel : |
| 623 | shared() |
| 624 | ? std::vector<std::pair< |
| 625 | int, std::string>>{{-1, "/pi1/aos/remote_timestamps/pi2"}} |
| 626 | : std::vector<std::pair<int, std::string>>{ |
| 627 | {pi1_timestamp_channel, |
| 628 | "/pi1/aos/remote_timestamps/pi2/pi1/aos/" |
| 629 | "aos-message_bridge-Timestamp"}, |
| 630 | {ping_timestamp_channel, |
| 631 | "/pi1/aos/remote_timestamps/pi2/test/aos-examples-Ping"}}) { |
| 632 | ping_event_loop.MakeWatcher( |
| 633 | channel.second, |
| 634 | [pi1_timestamp_channel, ping_timestamp_channel, &ping_on_pi2_fetcher, |
| 635 | &ping_on_pi1_fetcher, &pi1_on_pi2_timestamp_fetcher, |
| 636 | &pi1_on_pi1_timestamp_fetcher, |
| 637 | channel_index = channel.first](const RemoteMessage &header) { |
| 638 | VLOG(1) << "/pi1/aos/remote_timestamps/pi2 RemoteMessage " |
| 639 | << aos::FlatbufferToJson(&header); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 640 | |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 641 | EXPECT_TRUE(header.has_boot_uuid()); |
| 642 | if (channel_index != -1) { |
| 643 | ASSERT_EQ(channel_index, header.channel_index()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 646 | const aos::monotonic_clock::time_point header_monotonic_sent_time( |
| 647 | chrono::nanoseconds(header.monotonic_sent_time())); |
| 648 | const aos::realtime_clock::time_point header_realtime_sent_time( |
| 649 | chrono::nanoseconds(header.realtime_sent_time())); |
| 650 | const aos::monotonic_clock::time_point header_monotonic_remote_time( |
| 651 | chrono::nanoseconds(header.monotonic_remote_time())); |
| 652 | const aos::realtime_clock::time_point header_realtime_remote_time( |
| 653 | chrono::nanoseconds(header.realtime_remote_time())); |
| 654 | |
| 655 | const Context *pi1_context = nullptr; |
| 656 | const Context *pi2_context = nullptr; |
| 657 | |
| 658 | if (header.channel_index() == pi1_timestamp_channel) { |
| 659 | // Find the forwarded message. |
| 660 | while (pi1_on_pi2_timestamp_fetcher.context().monotonic_event_time < |
| 661 | header_monotonic_sent_time) { |
| 662 | ASSERT_TRUE(pi1_on_pi2_timestamp_fetcher.FetchNext()); |
| 663 | } |
| 664 | |
| 665 | // And the source message. |
| 666 | while (pi1_on_pi1_timestamp_fetcher.context().monotonic_event_time < |
| 667 | header_monotonic_remote_time) { |
| 668 | ASSERT_TRUE(pi1_on_pi1_timestamp_fetcher.FetchNext()); |
| 669 | } |
| 670 | |
| 671 | pi1_context = &pi1_on_pi1_timestamp_fetcher.context(); |
| 672 | pi2_context = &pi1_on_pi2_timestamp_fetcher.context(); |
| 673 | } else if (header.channel_index() == ping_timestamp_channel) { |
| 674 | // Find the forwarded message. |
| 675 | while (ping_on_pi2_fetcher.context().monotonic_event_time < |
| 676 | header_monotonic_sent_time) { |
| 677 | ASSERT_TRUE(ping_on_pi2_fetcher.FetchNext()); |
| 678 | } |
| 679 | |
| 680 | // And the source message. |
| 681 | while (ping_on_pi1_fetcher.context().monotonic_event_time < |
| 682 | header_monotonic_remote_time) { |
| 683 | ASSERT_TRUE(ping_on_pi1_fetcher.FetchNext()); |
| 684 | } |
| 685 | |
| 686 | pi1_context = &ping_on_pi1_fetcher.context(); |
| 687 | pi2_context = &ping_on_pi2_fetcher.context(); |
| 688 | } else { |
| 689 | LOG(FATAL) << "Unknown channel"; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 692 | // Confirm the forwarded message has matching timestamps to the |
| 693 | // timestamps we got back. |
| 694 | EXPECT_EQ(pi2_context->queue_index, header.queue_index()); |
| 695 | EXPECT_EQ(pi2_context->monotonic_event_time, |
| 696 | header_monotonic_sent_time); |
| 697 | EXPECT_EQ(pi2_context->realtime_event_time, |
| 698 | header_realtime_sent_time); |
| 699 | EXPECT_EQ(pi2_context->realtime_remote_time, |
| 700 | header_realtime_remote_time); |
| 701 | EXPECT_EQ(pi2_context->monotonic_remote_time, |
| 702 | header_monotonic_remote_time); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 703 | |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 704 | // Confirm the forwarded message also matches the source message. |
| 705 | EXPECT_EQ(pi1_context->queue_index, header.queue_index()); |
| 706 | EXPECT_EQ(pi1_context->monotonic_event_time, |
| 707 | header_monotonic_remote_time); |
| 708 | EXPECT_EQ(pi1_context->realtime_event_time, |
| 709 | header_realtime_remote_time); |
| 710 | }); |
| 711 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 712 | |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 713 | // Start everything up. Pong is the only thing we don't know how to wait |
| 714 | // on, so start it first. |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 715 | ThreadedEventLoopRunner pong_thread(&pong_event_loop); |
| 716 | ThreadedEventLoopRunner ping_thread(&ping_event_loop); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 717 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 718 | StartPi1Server(); |
| 719 | StartPi1Client(); |
| 720 | StartPi2Client(); |
| 721 | StartPi2Server(); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 722 | |
| 723 | // And go! |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 724 | // Run for 5 seconds to make sure we have time to estimate the offset. |
| 725 | std::this_thread::sleep_for(chrono::milliseconds(5050)); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 726 | |
| 727 | // Confirm that we are estimating a monotonic offset on the client. |
| 728 | ASSERT_TRUE(client_statistics_fetcher.Fetch()); |
| 729 | |
| 730 | EXPECT_EQ(client_statistics_fetcher->connections()->size(), 1u); |
| 731 | EXPECT_EQ(client_statistics_fetcher->connections() |
| 732 | ->Get(0) |
| 733 | ->node() |
| 734 | ->name() |
| 735 | ->string_view(), |
| 736 | "pi1"); |
| 737 | |
| 738 | // Make sure the offset in one direction is less than a second. |
| 739 | EXPECT_GT( |
Austin Schuh | 2b159eb | 2021-07-31 19:42:21 -0700 | [diff] [blame] | 740 | client_statistics_fetcher->connections()->Get(0)->monotonic_offset(), 0) |
| 741 | << aos::FlatbufferToJson(client_statistics_fetcher.get()); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 742 | EXPECT_LT( |
| 743 | client_statistics_fetcher->connections()->Get(0)->monotonic_offset(), |
Austin Schuh | 2b159eb | 2021-07-31 19:42:21 -0700 | [diff] [blame] | 744 | 1000000000) |
| 745 | << aos::FlatbufferToJson(client_statistics_fetcher.get()); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 746 | |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 747 | // Shut everyone else down before confirming everything actually ran. |
| 748 | ping_thread.Exit(); |
| 749 | pong_thread.Exit(); |
| 750 | StopPi1Server(); |
| 751 | StopPi1Client(); |
| 752 | StopPi2Client(); |
| 753 | StopPi2Server(); |
| 754 | |
| 755 | // Make sure we sent something. |
| 756 | EXPECT_GE(ping_count, 1); |
| 757 | // And got something back. |
| 758 | EXPECT_GE(pong_count, 1); |
| 759 | |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 760 | EXPECT_GE(pi1_server_statistics_count, 2); |
| 761 | EXPECT_GE(pi2_server_statistics_count, 2); |
| 762 | EXPECT_GE(pi1_client_statistics_count, 2); |
| 763 | EXPECT_GE(pi2_client_statistics_count, 2); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 764 | |
| 765 | // Confirm we got timestamps back! |
| 766 | EXPECT_TRUE(message_header_fetcher1.Fetch()); |
| 767 | EXPECT_TRUE(message_header_fetcher2.Fetch()); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 768 | } |
| 769 | |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 770 | // Test that the client disconnecting triggers the server offsets on both sides |
| 771 | // to clear. |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 772 | TEST_P(MessageBridgeParameterizedTest, ClientRestart) { |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 773 | // This is rather annoying to set up. We need to start up a client and |
| 774 | // server, on the same node, but get them to think that they are on different |
| 775 | // nodes. |
| 776 | // |
| 777 | // We need the client to not post directly to "/test" like it would in a |
| 778 | // real system, otherwise we will re-send the ping message... So, use an |
| 779 | // application specific map to have the client post somewhere else. |
| 780 | // |
| 781 | // To top this all off, each of these needs to be done with a ShmEventLoop, |
| 782 | // which needs to run in a separate thread... And it is really hard to get |
| 783 | // everything started up reliably. So just be super generous on timeouts and |
| 784 | // hope for the best. We can be more generous in the future if we need to. |
| 785 | // |
| 786 | // We are faking the application names by passing in --application_name=foo |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 787 | OnPi1(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 788 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 789 | MakePi1Server(); |
| 790 | MakePi1Client(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 791 | |
| 792 | // And build the app for testing. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 793 | MakePi1Test(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 794 | aos::Fetcher<ServerStatistics> pi1_server_statistics_fetcher = |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 795 | pi1_test_event_loop->MakeFetcher<ServerStatistics>("/pi1/aos"); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 796 | |
| 797 | // Now do it for "raspberrypi2", the client. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 798 | OnPi2(); |
| 799 | MakePi2Server(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 800 | |
| 801 | // And build the app for testing. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 802 | MakePi2Test(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 803 | aos::Fetcher<ServerStatistics> pi2_server_statistics_fetcher = |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 804 | pi2_test_event_loop->MakeFetcher<ServerStatistics>("/pi2/aos"); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 805 | |
| 806 | // Wait until we are connected, then send. |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 807 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 808 | StartPi1Test(); |
| 809 | StartPi2Test(); |
| 810 | StartPi1Server(); |
| 811 | StartPi1Client(); |
| 812 | StartPi2Server(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 813 | |
| 814 | { |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 815 | MakePi2Client(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 816 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 817 | RunPi2Client(chrono::milliseconds(3050)); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 818 | |
| 819 | // Now confirm we are synchronized. |
| 820 | EXPECT_TRUE(pi1_server_statistics_fetcher.Fetch()); |
| 821 | EXPECT_TRUE(pi2_server_statistics_fetcher.Fetch()); |
| 822 | |
| 823 | const ServerConnection *const pi1_connection = |
| 824 | pi1_server_statistics_fetcher->connections()->Get(0); |
| 825 | const ServerConnection *const pi2_connection = |
| 826 | pi2_server_statistics_fetcher->connections()->Get(0); |
| 827 | |
| 828 | EXPECT_EQ(pi1_connection->state(), State::CONNECTED); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 829 | EXPECT_EQ(pi1_connection->connection_count(), 1u); |
| 830 | EXPECT_TRUE(pi1_connection->has_connected_since_time()); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 831 | EXPECT_TRUE(pi1_connection->has_monotonic_offset()); |
| 832 | EXPECT_LT(chrono::nanoseconds(pi1_connection->monotonic_offset()), |
| 833 | chrono::milliseconds(1)); |
| 834 | EXPECT_GT(chrono::nanoseconds(pi1_connection->monotonic_offset()), |
| 835 | chrono::milliseconds(-1)); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 836 | EXPECT_TRUE(pi1_connection->has_boot_uuid()); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 837 | |
| 838 | EXPECT_EQ(pi2_connection->state(), State::CONNECTED); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 839 | EXPECT_EQ(pi2_connection->connection_count(), 1u); |
| 840 | EXPECT_TRUE(pi2_connection->has_connected_since_time()); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 841 | EXPECT_TRUE(pi2_connection->has_monotonic_offset()); |
| 842 | EXPECT_LT(chrono::nanoseconds(pi2_connection->monotonic_offset()), |
| 843 | chrono::milliseconds(1)); |
| 844 | EXPECT_GT(chrono::nanoseconds(pi2_connection->monotonic_offset()), |
| 845 | chrono::milliseconds(-1)); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 846 | EXPECT_TRUE(pi2_connection->has_boot_uuid()); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 847 | |
| 848 | StopPi2Client(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 849 | } |
| 850 | |
Austin Schuh | d0d894e | 2021-10-24 17:13:11 -0700 | [diff] [blame] | 851 | std::this_thread::sleep_for(SctpClientConnection::kReconnectTimeout + |
| 852 | std::chrono::seconds(1)); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 853 | |
| 854 | { |
| 855 | // Now confirm we are un-synchronized. |
| 856 | EXPECT_TRUE(pi1_server_statistics_fetcher.Fetch()); |
| 857 | EXPECT_TRUE(pi2_server_statistics_fetcher.Fetch()); |
| 858 | const ServerConnection *const pi1_connection = |
| 859 | pi1_server_statistics_fetcher->connections()->Get(0); |
| 860 | const ServerConnection *const pi2_connection = |
| 861 | pi2_server_statistics_fetcher->connections()->Get(0); |
| 862 | |
| 863 | EXPECT_EQ(pi1_connection->state(), State::DISCONNECTED); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 864 | EXPECT_EQ(pi1_connection->connection_count(), 1u); |
| 865 | EXPECT_FALSE(pi1_connection->has_connected_since_time()); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 866 | EXPECT_FALSE(pi1_connection->has_monotonic_offset()); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 867 | EXPECT_FALSE(pi1_connection->has_boot_uuid()); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 868 | EXPECT_EQ(pi2_connection->state(), State::CONNECTED); |
| 869 | EXPECT_FALSE(pi2_connection->has_monotonic_offset()); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 870 | EXPECT_TRUE(pi2_connection->has_boot_uuid()); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 871 | EXPECT_EQ(pi2_connection->connection_count(), 1u); |
| 872 | EXPECT_TRUE(pi2_connection->has_connected_since_time()); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | { |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 876 | MakePi2Client(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 877 | // And go! |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 878 | RunPi2Client(chrono::milliseconds(3050)); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 879 | |
| 880 | EXPECT_TRUE(pi1_server_statistics_fetcher.Fetch()); |
| 881 | EXPECT_TRUE(pi2_server_statistics_fetcher.Fetch()); |
| 882 | |
| 883 | // Now confirm we are synchronized again. |
| 884 | const ServerConnection *const pi1_connection = |
| 885 | pi1_server_statistics_fetcher->connections()->Get(0); |
| 886 | const ServerConnection *const pi2_connection = |
| 887 | pi2_server_statistics_fetcher->connections()->Get(0); |
| 888 | |
| 889 | EXPECT_EQ(pi1_connection->state(), State::CONNECTED); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 890 | EXPECT_EQ(pi1_connection->connection_count(), 2u); |
| 891 | EXPECT_TRUE(pi1_connection->has_connected_since_time()); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 892 | EXPECT_TRUE(pi1_connection->has_monotonic_offset()); |
| 893 | EXPECT_LT(chrono::nanoseconds(pi1_connection->monotonic_offset()), |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 894 | chrono::milliseconds(1)) |
| 895 | << ": " << FlatbufferToJson(pi1_connection); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 896 | EXPECT_GT(chrono::nanoseconds(pi1_connection->monotonic_offset()), |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 897 | chrono::milliseconds(-1)) |
| 898 | << ": " << FlatbufferToJson(pi1_connection); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 899 | EXPECT_TRUE(pi1_connection->has_boot_uuid()); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 900 | |
| 901 | EXPECT_EQ(pi2_connection->state(), State::CONNECTED); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 902 | EXPECT_EQ(pi2_connection->connection_count(), 1u); |
| 903 | EXPECT_TRUE(pi2_connection->has_connected_since_time()); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 904 | EXPECT_TRUE(pi2_connection->has_monotonic_offset()); |
| 905 | EXPECT_LT(chrono::nanoseconds(pi2_connection->monotonic_offset()), |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 906 | chrono::milliseconds(1)) |
| 907 | << ": " << FlatbufferToJson(pi2_connection); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 908 | EXPECT_GT(chrono::nanoseconds(pi2_connection->monotonic_offset()), |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 909 | chrono::milliseconds(-1)) |
| 910 | << ": " << FlatbufferToJson(pi2_connection); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 911 | EXPECT_TRUE(pi2_connection->has_boot_uuid()); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 912 | |
| 913 | StopPi2Client(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | // Shut everyone else down |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 917 | StopPi1Server(); |
| 918 | StopPi1Client(); |
| 919 | StopPi2Server(); |
| 920 | StopPi1Test(); |
| 921 | StopPi2Test(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | // Test that the server disconnecting triggers the server offsets on the other |
| 925 | // side to clear, along with the other client. |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 926 | TEST_P(MessageBridgeParameterizedTest, ServerRestart) { |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 927 | // This is rather annoying to set up. We need to start up a client and |
| 928 | // server, on the same node, but get them to think that they are on different |
| 929 | // nodes. |
| 930 | // |
| 931 | // We need the client to not post directly to "/test" like it would in a |
| 932 | // real system, otherwise we will re-send the ping message... So, use an |
| 933 | // application specific map to have the client post somewhere else. |
| 934 | // |
| 935 | // To top this all off, each of these needs to be done with a ShmEventLoop, |
| 936 | // which needs to run in a separate thread... And it is really hard to get |
| 937 | // everything started up reliably. So just be super generous on timeouts and |
| 938 | // hope for the best. We can be more generous in the future if we need to. |
| 939 | // |
| 940 | // We are faking the application names by passing in --application_name=foo |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 941 | // Force ourselves to be "raspberrypi" and allocate everything. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 942 | OnPi1(); |
| 943 | MakePi1Server(); |
| 944 | MakePi1Client(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 945 | |
| 946 | // And build the app for testing. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 947 | MakePi1Test(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 948 | aos::Fetcher<ServerStatistics> pi1_server_statistics_fetcher = |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 949 | pi1_test_event_loop->MakeFetcher<ServerStatistics>("/pi1/aos"); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 950 | aos::Fetcher<ClientStatistics> pi1_client_statistics_fetcher = |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 951 | pi1_test_event_loop->MakeFetcher<ClientStatistics>("/pi1/aos"); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 952 | |
| 953 | // Now do it for "raspberrypi2", the client. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 954 | OnPi2(); |
| 955 | MakePi2Client(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 956 | |
| 957 | // And build the app for testing. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 958 | MakePi2Test(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 959 | aos::Fetcher<ServerStatistics> pi2_server_statistics_fetcher = |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 960 | pi2_test_event_loop->MakeFetcher<ServerStatistics>("/pi2/aos"); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 961 | |
| 962 | // Start everything up. Pong is the only thing we don't know how to wait on, |
| 963 | // so start it first. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 964 | StartPi1Test(); |
| 965 | StartPi2Test(); |
| 966 | StartPi1Server(); |
| 967 | StartPi1Client(); |
| 968 | StartPi2Client(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 969 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 970 | // Confirm both client and server statistics messages have decent offsets in |
| 971 | // them. |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 972 | |
| 973 | { |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 974 | MakePi2Server(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 975 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 976 | RunPi2Server(chrono::milliseconds(3050)); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 977 | |
| 978 | // Now confirm we are synchronized. |
| 979 | EXPECT_TRUE(pi1_server_statistics_fetcher.Fetch()); |
| 980 | EXPECT_TRUE(pi2_server_statistics_fetcher.Fetch()); |
| 981 | |
| 982 | const ServerConnection *const pi1_connection = |
| 983 | pi1_server_statistics_fetcher->connections()->Get(0); |
| 984 | const ServerConnection *const pi2_connection = |
| 985 | pi2_server_statistics_fetcher->connections()->Get(0); |
| 986 | |
| 987 | EXPECT_EQ(pi1_connection->state(), State::CONNECTED); |
| 988 | EXPECT_TRUE(pi1_connection->has_monotonic_offset()); |
| 989 | EXPECT_LT(chrono::nanoseconds(pi1_connection->monotonic_offset()), |
| 990 | chrono::milliseconds(1)); |
| 991 | EXPECT_GT(chrono::nanoseconds(pi1_connection->monotonic_offset()), |
| 992 | chrono::milliseconds(-1)); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 993 | EXPECT_TRUE(pi1_connection->has_boot_uuid()); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 994 | EXPECT_TRUE(pi1_connection->has_connected_since_time()); |
| 995 | EXPECT_EQ(pi1_connection->connection_count(), 1u); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 996 | |
| 997 | EXPECT_EQ(pi2_connection->state(), State::CONNECTED); |
| 998 | EXPECT_TRUE(pi2_connection->has_monotonic_offset()); |
| 999 | EXPECT_LT(chrono::nanoseconds(pi2_connection->monotonic_offset()), |
| 1000 | chrono::milliseconds(1)); |
| 1001 | EXPECT_GT(chrono::nanoseconds(pi2_connection->monotonic_offset()), |
| 1002 | chrono::milliseconds(-1)); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1003 | EXPECT_TRUE(pi2_connection->has_boot_uuid()); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 1004 | EXPECT_TRUE(pi2_connection->has_connected_since_time()); |
| 1005 | EXPECT_EQ(pi2_connection->connection_count(), 1u); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1006 | |
| 1007 | StopPi2Server(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | std::this_thread::sleep_for(std::chrono::seconds(2)); |
| 1011 | |
| 1012 | { |
| 1013 | // And confirm we are unsynchronized. |
| 1014 | EXPECT_TRUE(pi1_server_statistics_fetcher.Fetch()); |
| 1015 | EXPECT_TRUE(pi1_client_statistics_fetcher.Fetch()); |
| 1016 | |
| 1017 | const ServerConnection *const pi1_server_connection = |
| 1018 | pi1_server_statistics_fetcher->connections()->Get(0); |
| 1019 | const ClientConnection *const pi1_client_connection = |
| 1020 | pi1_client_statistics_fetcher->connections()->Get(0); |
| 1021 | |
| 1022 | EXPECT_EQ(pi1_server_connection->state(), State::CONNECTED); |
| 1023 | EXPECT_FALSE(pi1_server_connection->has_monotonic_offset()); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 1024 | EXPECT_TRUE(pi1_server_connection->has_connected_since_time()); |
| 1025 | EXPECT_EQ(pi1_server_connection->connection_count(), 1u); |
| 1026 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1027 | EXPECT_TRUE(pi1_server_connection->has_boot_uuid()); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 1028 | EXPECT_EQ(pi1_client_connection->state(), State::DISCONNECTED); |
| 1029 | EXPECT_FALSE(pi1_client_connection->has_monotonic_offset()); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 1030 | EXPECT_FALSE(pi1_client_connection->has_connected_since_time()); |
| 1031 | EXPECT_EQ(pi1_client_connection->connection_count(), 1u); |
| 1032 | EXPECT_FALSE(pi1_client_connection->has_boot_uuid()); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | { |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1036 | MakePi2Server(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 1037 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1038 | RunPi2Server(chrono::milliseconds(3050)); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 1039 | |
| 1040 | // And confirm we are synchronized again. |
| 1041 | EXPECT_TRUE(pi1_server_statistics_fetcher.Fetch()); |
| 1042 | EXPECT_TRUE(pi2_server_statistics_fetcher.Fetch()); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 1043 | EXPECT_TRUE(pi1_client_statistics_fetcher.Fetch()); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 1044 | |
| 1045 | const ServerConnection *const pi1_connection = |
| 1046 | pi1_server_statistics_fetcher->connections()->Get(0); |
| 1047 | const ServerConnection *const pi2_connection = |
| 1048 | pi2_server_statistics_fetcher->connections()->Get(0); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 1049 | const ClientConnection *const pi1_client_connection = |
| 1050 | pi1_client_statistics_fetcher->connections()->Get(0); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 1051 | |
| 1052 | EXPECT_EQ(pi1_connection->state(), State::CONNECTED); |
| 1053 | EXPECT_TRUE(pi1_connection->has_monotonic_offset()); |
| 1054 | EXPECT_LT(chrono::nanoseconds(pi1_connection->monotonic_offset()), |
| 1055 | chrono::milliseconds(1)); |
| 1056 | EXPECT_GT(chrono::nanoseconds(pi1_connection->monotonic_offset()), |
| 1057 | chrono::milliseconds(-1)); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1058 | EXPECT_TRUE(pi1_connection->has_boot_uuid()); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 1059 | |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 1060 | EXPECT_EQ(pi1_client_connection->state(), State::CONNECTED); |
| 1061 | EXPECT_TRUE(pi1_client_connection->has_connected_since_time()); |
| 1062 | EXPECT_EQ(pi1_client_connection->connection_count(), 2u); |
| 1063 | EXPECT_TRUE(pi1_client_connection->has_boot_uuid()); |
| 1064 | |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 1065 | EXPECT_EQ(pi2_connection->state(), State::CONNECTED); |
| 1066 | EXPECT_TRUE(pi2_connection->has_monotonic_offset()); |
| 1067 | EXPECT_LT(chrono::nanoseconds(pi2_connection->monotonic_offset()), |
| 1068 | chrono::milliseconds(1)); |
| 1069 | EXPECT_GT(chrono::nanoseconds(pi2_connection->monotonic_offset()), |
| 1070 | chrono::milliseconds(-1)); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1071 | EXPECT_TRUE(pi2_connection->has_boot_uuid()); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1072 | |
| 1073 | StopPi2Server(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | // Shut everyone else down |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1077 | StopPi1Server(); |
| 1078 | StopPi1Client(); |
| 1079 | StopPi2Client(); |
| 1080 | StopPi1Test(); |
| 1081 | StopPi2Test(); |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1084 | // TODO(austin): The above test confirms that the external state does the right |
Austin Schuh | 5344c35 | 2020-04-12 17:04:26 -0700 | [diff] [blame] | 1085 | // thing, but doesn't confirm that the internal state does. We either need to |
| 1086 | // expose a way to check the state in a thread-safe way, or need a way to jump |
| 1087 | // time for one node to do that. |
| 1088 | |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1089 | void SendPing(aos::Sender<examples::Ping> *sender, int value) { |
| 1090 | aos::Sender<examples::Ping>::Builder builder = sender->MakeBuilder(); |
| 1091 | examples::Ping::Builder ping_builder = builder.MakeBuilder<examples::Ping>(); |
| 1092 | ping_builder.add_value(value); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1093 | builder.CheckOk(builder.Send(ping_builder.Finish())); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | // Tests that when a message is sent before the bridge starts up, but is |
| 1097 | // configured as reliable, we forward it. Confirm this survives a client reset. |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 1098 | TEST_P(MessageBridgeParameterizedTest, ReliableSentBeforeClientStartup) { |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1099 | OnPi1(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1100 | |
| 1101 | FLAGS_application_name = "sender"; |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 1102 | aos::ShmEventLoop send_event_loop(&config.message()); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1103 | aos::Sender<examples::Ping> ping_sender = |
| 1104 | send_event_loop.MakeSender<examples::Ping>("/test"); |
| 1105 | SendPing(&ping_sender, 1); |
| 1106 | aos::Sender<examples::Ping> unreliable_ping_sender = |
| 1107 | send_event_loop.MakeSender<examples::Ping>("/unreliable"); |
| 1108 | SendPing(&unreliable_ping_sender, 1); |
| 1109 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1110 | MakePi1Server(); |
| 1111 | MakePi1Client(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1112 | |
| 1113 | FLAGS_application_name = "pi1_timestamp"; |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 1114 | aos::ShmEventLoop pi1_remote_timestamp_event_loop(&config.message()); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1115 | |
| 1116 | // Now do it for "raspberrypi2", the client. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1117 | OnPi2(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1118 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1119 | MakePi2Server(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1120 | |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 1121 | aos::ShmEventLoop receive_event_loop(&config.message()); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1122 | aos::Fetcher<examples::Ping> ping_fetcher = |
| 1123 | receive_event_loop.MakeFetcher<examples::Ping>("/test"); |
| 1124 | aos::Fetcher<examples::Ping> unreliable_ping_fetcher = |
| 1125 | receive_event_loop.MakeFetcher<examples::Ping>("/unreliable"); |
| 1126 | aos::Fetcher<ClientStatistics> pi2_client_statistics_fetcher = |
| 1127 | receive_event_loop.MakeFetcher<ClientStatistics>("/pi2/aos"); |
| 1128 | |
| 1129 | const size_t ping_channel_index = configuration::ChannelIndex( |
| 1130 | receive_event_loop.configuration(), ping_fetcher.channel()); |
| 1131 | |
| 1132 | std::atomic<int> ping_timestamp_count{0}; |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 1133 | const std::string channel_name = |
| 1134 | shared() ? "/pi1/aos/remote_timestamps/pi2" |
| 1135 | : "/pi1/aos/remote_timestamps/pi2/test/aos-examples-Ping"; |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1136 | pi1_remote_timestamp_event_loop.MakeWatcher( |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 1137 | channel_name, [this, channel_name, ping_channel_index, |
| 1138 | &ping_timestamp_count](const RemoteMessage &header) { |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 1139 | VLOG(1) << channel_name << " RemoteMessage " |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1140 | << aos::FlatbufferToJson(&header); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1141 | EXPECT_TRUE(header.has_boot_uuid()); |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 1142 | if (shared() && header.channel_index() != ping_channel_index) { |
| 1143 | return; |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1144 | } |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 1145 | CHECK_EQ(header.channel_index(), ping_channel_index); |
| 1146 | ++ping_timestamp_count; |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1147 | }); |
| 1148 | |
| 1149 | // Before everything starts up, confirm there is no message. |
| 1150 | EXPECT_FALSE(ping_fetcher.Fetch()); |
| 1151 | EXPECT_FALSE(unreliable_ping_fetcher.Fetch()); |
| 1152 | |
| 1153 | // Spin up the persistant pieces. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1154 | StartPi1Server(); |
| 1155 | StartPi1Client(); |
| 1156 | StartPi2Server(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1157 | |
| 1158 | // Event used to wait for the timestamp counting thread to start. |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 1159 | std::unique_ptr<ThreadedEventLoopRunner> pi1_remote_timestamp_thread = |
| 1160 | std::make_unique<ThreadedEventLoopRunner>( |
| 1161 | &pi1_remote_timestamp_event_loop); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1162 | |
| 1163 | { |
| 1164 | // Now, spin up a client for 2 seconds. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1165 | MakePi2Client(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1166 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1167 | RunPi2Client(chrono::milliseconds(2050)); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1168 | |
| 1169 | // Confirm there is no detected duplicate packet. |
| 1170 | EXPECT_TRUE(pi2_client_statistics_fetcher.Fetch()); |
| 1171 | EXPECT_EQ(pi2_client_statistics_fetcher->connections() |
| 1172 | ->Get(0) |
| 1173 | ->duplicate_packets(), |
| 1174 | 0u); |
| 1175 | |
Austin Schuh | e61d438 | 2021-03-31 21:33:02 -0700 | [diff] [blame] | 1176 | EXPECT_EQ(pi2_client_statistics_fetcher->connections() |
| 1177 | ->Get(0) |
| 1178 | ->partial_deliveries(), |
| 1179 | 0u); |
| 1180 | |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1181 | EXPECT_TRUE(ping_fetcher.Fetch()); |
| 1182 | EXPECT_FALSE(unreliable_ping_fetcher.Fetch()); |
| 1183 | EXPECT_EQ(ping_timestamp_count, 1); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1184 | |
| 1185 | StopPi2Client(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | { |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1189 | // Now, spin up a client for 2 seconds. |
| 1190 | MakePi2Client(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1191 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1192 | RunPi2Client(chrono::milliseconds(5050)); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1193 | |
| 1194 | // Confirm we detect the duplicate packet correctly. |
| 1195 | EXPECT_TRUE(pi2_client_statistics_fetcher.Fetch()); |
| 1196 | EXPECT_EQ(pi2_client_statistics_fetcher->connections() |
| 1197 | ->Get(0) |
| 1198 | ->duplicate_packets(), |
| 1199 | 1u); |
| 1200 | |
Austin Schuh | e61d438 | 2021-03-31 21:33:02 -0700 | [diff] [blame] | 1201 | EXPECT_EQ(pi2_client_statistics_fetcher->connections() |
| 1202 | ->Get(0) |
| 1203 | ->partial_deliveries(), |
| 1204 | 0u); |
| 1205 | |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1206 | EXPECT_EQ(ping_timestamp_count, 1); |
| 1207 | EXPECT_FALSE(ping_fetcher.Fetch()); |
| 1208 | EXPECT_FALSE(unreliable_ping_fetcher.Fetch()); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1209 | |
| 1210 | StopPi2Client(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | // Shut everyone else down |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1214 | StopPi1Client(); |
| 1215 | StopPi2Server(); |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 1216 | pi1_remote_timestamp_thread.reset(); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1217 | StopPi1Server(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | // Tests that when a message is sent before the bridge starts up, but is |
| 1221 | // configured as reliable, we forward it. Confirm this works across server |
| 1222 | // resets. |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 1223 | TEST_P(MessageBridgeParameterizedTest, ReliableSentBeforeServerStartup) { |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1224 | // Now do it for "raspberrypi2", the client. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1225 | OnPi2(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1226 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1227 | MakePi2Server(); |
| 1228 | MakePi2Client(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1229 | |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 1230 | aos::ShmEventLoop receive_event_loop(&config.message()); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1231 | aos::Fetcher<examples::Ping> ping_fetcher = |
| 1232 | receive_event_loop.MakeFetcher<examples::Ping>("/test"); |
| 1233 | aos::Fetcher<examples::Ping> unreliable_ping_fetcher = |
| 1234 | receive_event_loop.MakeFetcher<examples::Ping>("/unreliable"); |
| 1235 | aos::Fetcher<ClientStatistics> pi2_client_statistics_fetcher = |
| 1236 | receive_event_loop.MakeFetcher<ClientStatistics>("/pi2/aos"); |
| 1237 | |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1238 | // Force ourselves to be "raspberrypi" and allocate everything. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1239 | OnPi1(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1240 | |
| 1241 | FLAGS_application_name = "sender"; |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 1242 | aos::ShmEventLoop send_event_loop(&config.message()); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1243 | aos::Sender<examples::Ping> ping_sender = |
| 1244 | send_event_loop.MakeSender<examples::Ping>("/test"); |
| 1245 | { |
| 1246 | aos::Sender<examples::Ping>::Builder builder = ping_sender.MakeBuilder(); |
| 1247 | examples::Ping::Builder ping_builder = |
| 1248 | builder.MakeBuilder<examples::Ping>(); |
| 1249 | ping_builder.add_value(1); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1250 | builder.CheckOk(builder.Send(ping_builder.Finish())); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1251 | } |
| 1252 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1253 | MakePi1Client(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1254 | |
| 1255 | FLAGS_application_name = "pi1_timestamp"; |
Austin Schuh | f466ab5 | 2021-02-16 22:00:38 -0800 | [diff] [blame] | 1256 | aos::ShmEventLoop pi1_remote_timestamp_event_loop(&config.message()); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1257 | |
| 1258 | const size_t ping_channel_index = configuration::ChannelIndex( |
| 1259 | receive_event_loop.configuration(), ping_fetcher.channel()); |
| 1260 | |
| 1261 | std::atomic<int> ping_timestamp_count{0}; |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 1262 | const std::string channel_name = |
| 1263 | shared() ? "/pi1/aos/remote_timestamps/pi2" |
| 1264 | : "/pi1/aos/remote_timestamps/pi2/test/aos-examples-Ping"; |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1265 | pi1_remote_timestamp_event_loop.MakeWatcher( |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 1266 | channel_name, [this, channel_name, ping_channel_index, |
| 1267 | &ping_timestamp_count](const RemoteMessage &header) { |
| 1268 | VLOG(1) << channel_name << " RemoteMessage " |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1269 | << aos::FlatbufferToJson(&header); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 1270 | EXPECT_TRUE(header.has_boot_uuid()); |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 1271 | if (shared() && header.channel_index() != ping_channel_index) { |
| 1272 | return; |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1273 | } |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 1274 | CHECK_EQ(header.channel_index(), ping_channel_index); |
| 1275 | ++ping_timestamp_count; |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1276 | }); |
| 1277 | |
| 1278 | // Before everything starts up, confirm there is no message. |
| 1279 | EXPECT_FALSE(ping_fetcher.Fetch()); |
| 1280 | EXPECT_FALSE(unreliable_ping_fetcher.Fetch()); |
| 1281 | |
| 1282 | // Spin up the persistant pieces. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1283 | StartPi1Client(); |
| 1284 | StartPi2Server(); |
| 1285 | StartPi2Client(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1286 | |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 1287 | std::unique_ptr<ThreadedEventLoopRunner> pi1_remote_timestamp_thread = |
| 1288 | std::make_unique<ThreadedEventLoopRunner>( |
| 1289 | &pi1_remote_timestamp_event_loop); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1290 | |
| 1291 | { |
| 1292 | // Now, spin up a server for 2 seconds. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1293 | MakePi1Server(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1294 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1295 | RunPi1Server(chrono::milliseconds(2050)); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1296 | |
| 1297 | // Confirm there is no detected duplicate packet. |
| 1298 | EXPECT_TRUE(pi2_client_statistics_fetcher.Fetch()); |
| 1299 | EXPECT_EQ(pi2_client_statistics_fetcher->connections() |
| 1300 | ->Get(0) |
| 1301 | ->duplicate_packets(), |
| 1302 | 0u); |
| 1303 | |
Austin Schuh | e61d438 | 2021-03-31 21:33:02 -0700 | [diff] [blame] | 1304 | EXPECT_EQ(pi2_client_statistics_fetcher->connections() |
| 1305 | ->Get(0) |
| 1306 | ->partial_deliveries(), |
| 1307 | 0u); |
| 1308 | |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1309 | EXPECT_TRUE(ping_fetcher.Fetch()); |
| 1310 | EXPECT_FALSE(unreliable_ping_fetcher.Fetch()); |
| 1311 | EXPECT_EQ(ping_timestamp_count, 1); |
| 1312 | LOG(INFO) << "Shutting down first pi1 MessageBridgeServer"; |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1313 | |
| 1314 | StopPi1Server(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | { |
| 1318 | // Now, spin up a second server for 2 seconds. |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1319 | MakePi1Server(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1320 | |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1321 | RunPi1Server(chrono::milliseconds(2050)); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1322 | |
| 1323 | // Confirm we detect the duplicate packet correctly. |
| 1324 | EXPECT_TRUE(pi2_client_statistics_fetcher.Fetch()); |
| 1325 | EXPECT_EQ(pi2_client_statistics_fetcher->connections() |
| 1326 | ->Get(0) |
| 1327 | ->duplicate_packets(), |
| 1328 | 1u); |
| 1329 | |
Austin Schuh | e61d438 | 2021-03-31 21:33:02 -0700 | [diff] [blame] | 1330 | EXPECT_EQ(pi2_client_statistics_fetcher->connections() |
| 1331 | ->Get(0) |
| 1332 | ->partial_deliveries(), |
| 1333 | 0u); |
| 1334 | |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1335 | EXPECT_EQ(ping_timestamp_count, 1); |
| 1336 | EXPECT_FALSE(ping_fetcher.Fetch()); |
| 1337 | EXPECT_FALSE(unreliable_ping_fetcher.Fetch()); |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1338 | |
| 1339 | StopPi1Server(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | // Shut everyone else down |
Austin Schuh | 0a2f12f | 2021-01-08 22:48:29 -0800 | [diff] [blame] | 1343 | StopPi1Client(); |
| 1344 | StopPi2Server(); |
| 1345 | StopPi2Client(); |
Austin Schuh | a4e616a | 2023-05-15 17:59:30 -0700 | [diff] [blame] | 1346 | pi1_remote_timestamp_thread.reset(); |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 1347 | } |
| 1348 | |
Austin Schuh | 4f7cccc | 2023-05-17 09:20:33 -0700 | [diff] [blame^] | 1349 | // Test that differing config sha256's result in no connection. |
| 1350 | TEST_P(MessageBridgeParameterizedTest, MismatchedSha256) { |
| 1351 | // This is rather annoying to set up. We need to start up a client and |
| 1352 | // server, on the same node, but get them to think that they are on different |
| 1353 | // nodes. |
| 1354 | // |
| 1355 | // We need the client to not post directly to "/test" like it would in a |
| 1356 | // real system, otherwise we will re-send the ping message... So, use an |
| 1357 | // application specific map to have the client post somewhere else. |
| 1358 | // |
| 1359 | // To top this all off, each of these needs to be done with a ShmEventLoop, |
| 1360 | // which needs to run in a separate thread... And it is really hard to get |
| 1361 | // everything started up reliably. So just be super generous on timeouts and |
| 1362 | // hope for the best. We can be more generous in the future if we need to. |
| 1363 | // |
| 1364 | // We are faking the application names by passing in --application_name=foo |
| 1365 | OnPi1(); |
| 1366 | |
| 1367 | MakePi1Server( |
| 1368 | "dummy sha256 "); |
| 1369 | MakePi1Client(); |
| 1370 | |
| 1371 | // And build the app for testing. |
| 1372 | MakePi1Test(); |
| 1373 | aos::Fetcher<ServerStatistics> pi1_server_statistics_fetcher = |
| 1374 | pi1_test_event_loop->MakeFetcher<ServerStatistics>("/pi1/aos"); |
| 1375 | aos::Fetcher<ClientStatistics> pi1_client_statistics_fetcher = |
| 1376 | pi1_test_event_loop->MakeFetcher<ClientStatistics>("/pi1/aos"); |
| 1377 | |
| 1378 | // Now do it for "raspberrypi2", the client. |
| 1379 | OnPi2(); |
| 1380 | MakePi2Server(); |
| 1381 | |
| 1382 | // And build the app for testing. |
| 1383 | MakePi2Test(); |
| 1384 | aos::Fetcher<ServerStatistics> pi2_server_statistics_fetcher = |
| 1385 | pi2_test_event_loop->MakeFetcher<ServerStatistics>("/pi2/aos"); |
| 1386 | aos::Fetcher<ClientStatistics> pi2_client_statistics_fetcher = |
| 1387 | pi2_test_event_loop->MakeFetcher<ClientStatistics>("/pi2/aos"); |
| 1388 | |
| 1389 | // Wait until we are connected, then send. |
| 1390 | |
| 1391 | StartPi1Test(); |
| 1392 | StartPi2Test(); |
| 1393 | StartPi1Server(); |
| 1394 | StartPi1Client(); |
| 1395 | StartPi2Server(); |
| 1396 | |
| 1397 | { |
| 1398 | MakePi2Client(); |
| 1399 | |
| 1400 | RunPi2Client(chrono::milliseconds(3050)); |
| 1401 | |
| 1402 | // Now confirm we are synchronized. |
| 1403 | EXPECT_TRUE(pi1_server_statistics_fetcher.Fetch()); |
| 1404 | EXPECT_TRUE(pi1_client_statistics_fetcher.Fetch()); |
| 1405 | EXPECT_TRUE(pi2_server_statistics_fetcher.Fetch()); |
| 1406 | EXPECT_TRUE(pi2_client_statistics_fetcher.Fetch()); |
| 1407 | |
| 1408 | const ServerConnection *const pi1_connection = |
| 1409 | pi1_server_statistics_fetcher->connections()->Get(0); |
| 1410 | const ClientConnection *const pi1_client_connection = |
| 1411 | pi1_client_statistics_fetcher->connections()->Get(0); |
| 1412 | const ServerConnection *const pi2_connection = |
| 1413 | pi2_server_statistics_fetcher->connections()->Get(0); |
| 1414 | const ClientConnection *const pi2_client_connection = |
| 1415 | pi2_client_statistics_fetcher->connections()->Get(0); |
| 1416 | |
| 1417 | // Make sure one direction is disconnected with a bunch of connection |
| 1418 | // attempts and failures. |
| 1419 | EXPECT_EQ(pi1_connection->state(), State::DISCONNECTED); |
| 1420 | EXPECT_EQ(pi1_connection->connection_count(), 0u); |
| 1421 | EXPECT_GT(pi1_connection->invalid_connection_count(), 10u); |
| 1422 | |
| 1423 | EXPECT_EQ(pi2_client_connection->state(), State::DISCONNECTED); |
| 1424 | EXPECT_GT(pi2_client_connection->connection_count(), 10u); |
| 1425 | |
| 1426 | // And the other direction is happy. |
| 1427 | EXPECT_EQ(pi2_connection->state(), State::CONNECTED); |
| 1428 | EXPECT_EQ(pi2_connection->connection_count(), 1u); |
| 1429 | EXPECT_TRUE(pi2_connection->has_connected_since_time()); |
| 1430 | EXPECT_FALSE(pi2_connection->has_monotonic_offset()); |
| 1431 | EXPECT_TRUE(pi2_connection->has_boot_uuid()); |
| 1432 | |
| 1433 | EXPECT_EQ(pi1_client_connection->state(), State::CONNECTED); |
| 1434 | EXPECT_EQ(pi1_client_connection->connection_count(), 1u); |
| 1435 | |
| 1436 | VLOG(1) << aos::FlatbufferToJson(pi2_server_statistics_fetcher.get()); |
| 1437 | VLOG(1) << aos::FlatbufferToJson(pi1_server_statistics_fetcher.get()); |
| 1438 | VLOG(1) << aos::FlatbufferToJson(pi2_client_statistics_fetcher.get()); |
| 1439 | VLOG(1) << aos::FlatbufferToJson(pi1_client_statistics_fetcher.get()); |
| 1440 | |
| 1441 | StopPi2Client(); |
| 1442 | } |
| 1443 | |
| 1444 | // Shut everyone else down |
| 1445 | StopPi1Server(); |
| 1446 | StopPi1Client(); |
| 1447 | StopPi2Server(); |
| 1448 | StopPi1Test(); |
| 1449 | StopPi2Test(); |
| 1450 | } |
| 1451 | |
Austin Schuh | 89f23e3 | 2023-05-15 17:06:43 -0700 | [diff] [blame] | 1452 | // Test that a client which connects with too big a message gets disconnected |
| 1453 | // without crashing. |
| 1454 | TEST_P(MessageBridgeParameterizedTest, TooBigConnect) { |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 1455 | // This is rather annoying to set up. We need to start up a client and |
| 1456 | // server, on the same node, but get them to think that they are on different |
| 1457 | // nodes. |
| 1458 | // |
| 1459 | // We need the client to not post directly to "/test" like it would in a |
| 1460 | // real system, otherwise we will re-send the ping message... So, use an |
| 1461 | // application specific map to have the client post somewhere else. |
| 1462 | // |
| 1463 | // To top this all off, each of these needs to be done with a ShmEventLoop, |
| 1464 | // which needs to run in a separate thread... And it is really hard to get |
| 1465 | // everything started up reliably. So just be super generous on timeouts and |
| 1466 | // hope for the best. We can be more generous in the future if we need to. |
| 1467 | // |
| 1468 | // We are faking the application names by passing in --application_name=foo |
| 1469 | OnPi1(); |
| 1470 | |
Austin Schuh | 530f9ee | 2023-05-15 14:29:31 -0700 | [diff] [blame] | 1471 | MakePi1Server( |
| 1472 | "dummy sha256 "); |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 1473 | MakePi1Client(); |
| 1474 | |
| 1475 | // And build the app for testing. |
| 1476 | MakePi1Test(); |
| 1477 | aos::Fetcher<ServerStatistics> pi1_server_statistics_fetcher = |
| 1478 | pi1_test_event_loop->MakeFetcher<ServerStatistics>("/pi1/aos"); |
| 1479 | aos::Fetcher<ClientStatistics> pi1_client_statistics_fetcher = |
| 1480 | pi1_test_event_loop->MakeFetcher<ClientStatistics>("/pi1/aos"); |
| 1481 | |
| 1482 | // Now do it for "raspberrypi2", the client. |
| 1483 | OnPi2(); |
| 1484 | MakePi2Server(); |
| 1485 | |
| 1486 | // And build the app for testing. |
| 1487 | MakePi2Test(); |
| 1488 | aos::Fetcher<ServerStatistics> pi2_server_statistics_fetcher = |
| 1489 | pi2_test_event_loop->MakeFetcher<ServerStatistics>("/pi2/aos"); |
| 1490 | aos::Fetcher<ClientStatistics> pi2_client_statistics_fetcher = |
| 1491 | pi2_test_event_loop->MakeFetcher<ClientStatistics>("/pi2/aos"); |
| 1492 | |
| 1493 | // Wait until we are connected, then send. |
| 1494 | |
| 1495 | StartPi1Test(); |
| 1496 | StartPi2Test(); |
| 1497 | StartPi1Server(); |
| 1498 | StartPi1Client(); |
| 1499 | StartPi2Server(); |
| 1500 | |
| 1501 | { |
Austin Schuh | 89f23e3 | 2023-05-15 17:06:43 -0700 | [diff] [blame] | 1502 | // Now, spin up a SctpClient and send a massive hunk of data. This should |
| 1503 | // trigger a disconnect, but no crash. |
| 1504 | OnPi2(); |
| 1505 | FLAGS_application_name = "pi2_message_bridge_client"; |
| 1506 | pi2_client_event_loop = |
| 1507 | std::make_unique<aos::ShmEventLoop>(&config.message()); |
| 1508 | pi2_client_event_loop->SetRuntimeRealtimePriority(1); |
| 1509 | |
| 1510 | const aos::Node *const remote_node = CHECK_NOTNULL( |
| 1511 | configuration::GetNode(pi2_client_event_loop->configuration(), "pi1")); |
| 1512 | |
| 1513 | const aos::FlatbufferDetachedBuffer<aos::message_bridge::Connect> |
| 1514 | connect_message(MakeConnectMessage( |
| 1515 | pi2_client_event_loop->configuration(), |
| 1516 | pi2_client_event_loop->node(), "pi1", |
| 1517 | pi2_client_event_loop->boot_uuid(), config_sha256)); |
| 1518 | |
| 1519 | SctpClient client(remote_node->hostname()->string_view(), |
| 1520 | remote_node->port(), |
| 1521 | connect_message.message().channels_to_transfer()->size() + |
| 1522 | kControlStreams(), |
| 1523 | ""); |
| 1524 | |
| 1525 | client.SetMaxReadSize(100000); |
| 1526 | client.SetMaxWriteSize(100000); |
| 1527 | |
| 1528 | client.SetPoolSize(2u); |
| 1529 | |
| 1530 | const std::string big_data(100000, 'a'); |
| 1531 | |
| 1532 | pi2_client_event_loop->epoll()->OnReadable(client.fd(), [&]() { |
| 1533 | aos::unique_c_ptr<Message> message = client.Read(); |
| 1534 | client.FreeMessage(std::move(message)); |
| 1535 | }); |
| 1536 | |
| 1537 | aos::TimerHandler *const send_big_message = pi2_client_event_loop->AddTimer( |
| 1538 | [&]() { CHECK(client.Send(kConnectStream(), big_data, 0)); }); |
| 1539 | |
| 1540 | pi2_client_event_loop->OnRun([this, send_big_message]() { |
| 1541 | send_big_message->Schedule(pi2_client_event_loop->monotonic_now() + |
| 1542 | chrono::seconds(1)); |
| 1543 | }); |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 1544 | |
| 1545 | RunPi2Client(chrono::milliseconds(3050)); |
| 1546 | |
| 1547 | // Now confirm we are synchronized. |
| 1548 | EXPECT_TRUE(pi1_server_statistics_fetcher.Fetch()); |
| 1549 | EXPECT_TRUE(pi1_client_statistics_fetcher.Fetch()); |
| 1550 | EXPECT_TRUE(pi2_server_statistics_fetcher.Fetch()); |
Austin Schuh | 89f23e3 | 2023-05-15 17:06:43 -0700 | [diff] [blame] | 1551 | EXPECT_FALSE(pi2_client_statistics_fetcher.Fetch()); |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 1552 | |
| 1553 | const ServerConnection *const pi1_connection = |
| 1554 | pi1_server_statistics_fetcher->connections()->Get(0); |
| 1555 | const ClientConnection *const pi1_client_connection = |
| 1556 | pi1_client_statistics_fetcher->connections()->Get(0); |
| 1557 | const ServerConnection *const pi2_connection = |
| 1558 | pi2_server_statistics_fetcher->connections()->Get(0); |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 1559 | |
Austin Schuh | 4f7cccc | 2023-05-17 09:20:33 -0700 | [diff] [blame^] | 1560 | // Make sure the server we just sent a bunch of junk to is grumpy and |
| 1561 | // disconnected the bad client. |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 1562 | EXPECT_EQ(pi1_connection->state(), State::DISCONNECTED); |
| 1563 | EXPECT_EQ(pi1_connection->connection_count(), 0u); |
Austin Schuh | 89f23e3 | 2023-05-15 17:06:43 -0700 | [diff] [blame] | 1564 | EXPECT_GE(pi1_server_statistics_fetcher->invalid_connection_count(), 1u); |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 1565 | |
| 1566 | // And the other direction is happy. |
| 1567 | EXPECT_EQ(pi2_connection->state(), State::CONNECTED); |
| 1568 | EXPECT_EQ(pi2_connection->connection_count(), 1u); |
| 1569 | EXPECT_TRUE(pi2_connection->has_connected_since_time()); |
| 1570 | EXPECT_FALSE(pi2_connection->has_monotonic_offset()); |
| 1571 | EXPECT_TRUE(pi2_connection->has_boot_uuid()); |
| 1572 | |
| 1573 | EXPECT_EQ(pi1_client_connection->state(), State::CONNECTED); |
| 1574 | EXPECT_EQ(pi1_client_connection->connection_count(), 1u); |
| 1575 | |
| 1576 | VLOG(1) << aos::FlatbufferToJson(pi2_server_statistics_fetcher.get()); |
| 1577 | VLOG(1) << aos::FlatbufferToJson(pi1_server_statistics_fetcher.get()); |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 1578 | VLOG(1) << aos::FlatbufferToJson(pi1_client_statistics_fetcher.get()); |
| 1579 | |
Austin Schuh | 89f23e3 | 2023-05-15 17:06:43 -0700 | [diff] [blame] | 1580 | pi2_client_event_loop->epoll()->DeleteFd(client.fd()); |
| 1581 | |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 1582 | StopPi2Client(); |
| 1583 | } |
| 1584 | |
| 1585 | // Shut everyone else down |
| 1586 | StopPi1Server(); |
| 1587 | StopPi1Client(); |
| 1588 | StopPi2Server(); |
| 1589 | StopPi1Test(); |
| 1590 | StopPi2Test(); |
| 1591 | } |
| 1592 | |
James Kuszmaul | f4bf9fe | 2021-05-10 22:58:24 -0700 | [diff] [blame] | 1593 | INSTANTIATE_TEST_SUITE_P( |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 1594 | MessageBridgeTests, MessageBridgeParameterizedTest, |
| 1595 | ::testing::Values( |
| 1596 | Param{"message_bridge_test_combined_timestamps_common_config.json", |
| 1597 | true}, |
| 1598 | Param{"message_bridge_test_common_config.json", false})); |
| 1599 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 1600 | } // namespace testing |
| 1601 | } // namespace message_bridge |
| 1602 | } // namespace aos |