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