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