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