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