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