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