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