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