blob: 74d22c207c3661844a914bf66fe3008d67a2d59b [file] [log] [blame]
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001#include <chrono>
2#include <thread>
3
Austin Schuh2f8fd752020-09-01 22:38:28 -07004#include "absl/strings/str_cat.h"
Austin Schuhe84c3ed2019-12-14 15:29:48 -08005#include "aos/events/ping_generated.h"
6#include "aos/events/pong_generated.h"
Brian Silverman7b266d92021-02-17 21:24:02 -08007#include "aos/ipc_lib/event.h"
Austin Schuhe84c3ed2019-12-14 15:29:48 -08008#include "aos/network/message_bridge_client_lib.h"
9#include "aos/network/message_bridge_server_lib.h"
Jim Ostrowski2192ddb2020-06-24 19:07:31 -070010#include "aos/network/team_number.h"
Austin Schuh373f1762021-06-02 21:07:09 -070011#include "aos/testing/path.h"
Austin Schuhe991fe22020-11-18 16:53:39 -080012#include "aos/util/file.h"
Brian Silverman7b266d92021-02-17 21:24:02 -080013#include "gtest/gtest.h"
Austin Schuhe84c3ed2019-12-14 15:29:48 -080014
Austin Schuh8902fa52021-03-14 22:39:24 -070015DECLARE_string(boot_uuid);
16
Austin Schuhe84c3ed2019-12-14 15:29:48 -080017namespace aos {
Austin Schuh2f8fd752020-09-01 22:38:28 -070018void SetShmBase(const std::string_view base);
19
Austin Schuhe84c3ed2019-12-14 15:29:48 -080020namespace message_bridge {
21namespace testing {
22
Austin Schuh373f1762021-06-02 21:07:09 -070023using aos::testing::ArtifactPath;
24
Austin Schuhe84c3ed2019-12-14 15:29:48 -080025namespace chrono = std::chrono;
26
Austin Schuhe991fe22020-11-18 16:53:39 -080027std::string ShmBase(const std::string_view node) {
Austin Schuh2f8fd752020-09-01 22:38:28 -070028 const char *tmpdir_c_str = getenv("TEST_TMPDIR");
29 if (tmpdir_c_str != nullptr) {
Austin Schuhe991fe22020-11-18 16:53:39 -080030 return absl::StrCat(tmpdir_c_str, "/", node);
Austin Schuh2f8fd752020-09-01 22:38:28 -070031 } else {
Austin Schuhe991fe22020-11-18 16:53:39 -080032 return absl::StrCat("/dev/shm/", node);
Austin Schuh2f8fd752020-09-01 22:38:28 -070033 }
34}
35
Austin Schuhe991fe22020-11-18 16:53:39 -080036void DoSetShmBase(const std::string_view node) {
37 aos::SetShmBase(ShmBase(node));
38}
39
Austin Schuh36a2c3e2021-02-18 22:28:38 -080040// Parameters to run all the tests with.
41struct Param {
42 // The config file to use.
43 std::string config;
44 // If true, the RemoteMessage channel should be shared between all the remote
45 // channels. If false, there will be 1 RemoteMessage channel per remote
46 // channel.
47 bool shared;
48};
49
50class MessageBridgeParameterizedTest
51 : public ::testing::TestWithParam<struct Param> {
Austin Schuh0de30f32020-12-06 12:44:28 -080052 public:
Austin Schuh36a2c3e2021-02-18 22:28:38 -080053 MessageBridgeParameterizedTest()
54 : config(aos::configuration::ReadConfig(
Austin Schuh373f1762021-06-02 21:07:09 -070055 ArtifactPath(absl::StrCat("aos/network/", GetParam().config)))),
Austin Schuh8902fa52021-03-14 22:39:24 -070056 pi1_boot_uuid_(UUID::Random()),
57 pi2_boot_uuid_(UUID::Random()) {
Austin Schuh0de30f32020-12-06 12:44:28 -080058 util::UnlinkRecursive(ShmBase("pi1"));
59 util::UnlinkRecursive(ShmBase("pi2"));
60 }
Austin Schuhe991fe22020-11-18 16:53:39 -080061
Austin Schuh36a2c3e2021-02-18 22:28:38 -080062 bool shared() const { return GetParam().shared; }
63
Austin Schuh0a2f12f2021-01-08 22:48:29 -080064 void OnPi1() {
65 DoSetShmBase("pi1");
66 FLAGS_override_hostname = "raspberrypi";
Austin Schuh8902fa52021-03-14 22:39:24 -070067 FLAGS_boot_uuid = pi1_boot_uuid_.ToString();
Austin Schuh0a2f12f2021-01-08 22:48:29 -080068 }
69
70 void OnPi2() {
71 DoSetShmBase("pi2");
72 FLAGS_override_hostname = "raspberrypi2";
Austin Schuh8902fa52021-03-14 22:39:24 -070073 FLAGS_boot_uuid = pi2_boot_uuid_.ToString();
Austin Schuh0a2f12f2021-01-08 22:48:29 -080074 }
75
76 void MakePi1Server() {
77 OnPi1();
78 FLAGS_application_name = "pi1_message_bridge_server";
79 pi1_server_event_loop =
Austin Schuhf466ab52021-02-16 22:00:38 -080080 std::make_unique<aos::ShmEventLoop>(&config.message());
Austin Schuh0a2f12f2021-01-08 22:48:29 -080081 pi1_server_event_loop->SetRuntimeRealtimePriority(1);
82 pi1_message_bridge_server =
83 std::make_unique<MessageBridgeServer>(pi1_server_event_loop.get());
84 }
85
86 void RunPi1Server(chrono::nanoseconds duration) {
87 // Setup a shutdown callback.
88 aos::TimerHandler *const quit = pi1_server_event_loop->AddTimer(
89 [this]() { pi1_server_event_loop->Exit(); });
90 pi1_server_event_loop->OnRun([this, quit, duration]() {
91 // Stop between timestamps, not exactly on them.
92 quit->Setup(pi1_server_event_loop->monotonic_now() + duration);
93 });
94
95 pi1_server_event_loop->Run();
96 }
97
98 void StartPi1Server() {
99 pi1_server_thread = std::thread([this]() {
100 LOG(INFO) << "Started pi1_message_bridge_server";
101 pi1_server_event_loop->Run();
102 });
103 }
104
105 void StopPi1Server() {
106 if (pi1_server_thread.joinable()) {
107 pi1_server_event_loop->Exit();
108 pi1_server_thread.join();
109 pi1_server_thread = std::thread();
110 }
111 pi1_message_bridge_server.reset();
112 pi1_server_event_loop.reset();
113 }
114
115 void MakePi1Client() {
116 OnPi1();
117 FLAGS_application_name = "pi1_message_bridge_client";
118 pi1_client_event_loop =
Austin Schuhf466ab52021-02-16 22:00:38 -0800119 std::make_unique<aos::ShmEventLoop>(&config.message());
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800120 pi1_client_event_loop->SetRuntimeRealtimePriority(1);
121 pi1_message_bridge_client =
122 std::make_unique<MessageBridgeClient>(pi1_client_event_loop.get());
123 }
124
125 void StartPi1Client() {
126 pi1_client_thread = std::thread([this]() {
127 LOG(INFO) << "Started pi1_message_bridge_client";
128 pi1_client_event_loop->Run();
129 });
130 }
131
132 void StopPi1Client() {
133 pi1_client_event_loop->Exit();
134 pi1_client_thread.join();
135 pi1_client_thread = std::thread();
136 pi1_message_bridge_client.reset();
137 pi1_client_event_loop.reset();
138 }
139
140 void MakePi1Test() {
141 OnPi1();
142 FLAGS_application_name = "test1";
143 pi1_test_event_loop =
Austin Schuhf466ab52021-02-16 22:00:38 -0800144 std::make_unique<aos::ShmEventLoop>(&config.message());
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800145
146 pi1_test_event_loop->MakeWatcher(
147 "/pi1/aos", [](const ServerStatistics &stats) {
148 VLOG(1) << "/pi1/aos ServerStatistics " << FlatbufferToJson(&stats);
149 });
150
151 pi1_test_event_loop->MakeWatcher(
152 "/pi1/aos", [](const ClientStatistics &stats) {
153 VLOG(1) << "/pi1/aos ClientStatistics " << FlatbufferToJson(&stats);
154 });
155
156 pi1_test_event_loop->MakeWatcher(
157 "/pi1/aos", [](const Timestamp &timestamp) {
158 VLOG(1) << "/pi1/aos Timestamp " << FlatbufferToJson(&timestamp);
159 });
Austin Schuh8902fa52021-03-14 22:39:24 -0700160 pi1_test_event_loop->MakeWatcher(
161 "/pi2/aos", [this](const Timestamp &timestamp) {
162 VLOG(1) << "/pi2/aos Timestamp " << FlatbufferToJson(&timestamp);
Austin Schuha9012be2021-07-21 15:19:11 -0700163 EXPECT_EQ(pi1_test_event_loop->context().source_boot_uuid,
Austin Schuh8902fa52021-03-14 22:39:24 -0700164 pi2_boot_uuid_);
165 });
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800166 }
167
168 void StartPi1Test() {
169 pi1_test_thread = std::thread([this]() {
170 LOG(INFO) << "Started pi1_test";
171 pi1_test_event_loop->Run();
172 });
173 }
174
175 void StopPi1Test() {
176 pi1_test_event_loop->Exit();
177 pi1_test_thread.join();
178 }
179
180 void MakePi2Server() {
181 OnPi2();
182 FLAGS_application_name = "pi2_message_bridge_server";
183 pi2_server_event_loop =
Austin Schuhf466ab52021-02-16 22:00:38 -0800184 std::make_unique<aos::ShmEventLoop>(&config.message());
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800185 pi2_server_event_loop->SetRuntimeRealtimePriority(1);
186 pi2_message_bridge_server =
187 std::make_unique<MessageBridgeServer>(pi2_server_event_loop.get());
188 }
189
190 void RunPi2Server(chrono::nanoseconds duration) {
191 // Setup a shutdown callback.
192 aos::TimerHandler *const quit = pi2_server_event_loop->AddTimer(
193 [this]() { pi2_server_event_loop->Exit(); });
194 pi2_server_event_loop->OnRun([this, quit, duration]() {
195 // Stop between timestamps, not exactly on them.
196 quit->Setup(pi2_server_event_loop->monotonic_now() + duration);
197 });
198
199 pi2_server_event_loop->Run();
200 }
201
202 void StartPi2Server() {
203 pi2_server_thread = std::thread([this]() {
204 LOG(INFO) << "Started pi2_message_bridge_server";
205 pi2_server_event_loop->Run();
206 });
207 }
208
209 void StopPi2Server() {
210 if (pi2_server_thread.joinable()) {
211 pi2_server_event_loop->Exit();
212 pi2_server_thread.join();
213 pi2_server_thread = std::thread();
214 }
215 pi2_message_bridge_server.reset();
216 pi2_server_event_loop.reset();
217 }
218
219 void MakePi2Client() {
220 OnPi2();
221 FLAGS_application_name = "pi2_message_bridge_client";
222 pi2_client_event_loop =
Austin Schuhf466ab52021-02-16 22:00:38 -0800223 std::make_unique<aos::ShmEventLoop>(&config.message());
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800224 pi2_client_event_loop->SetRuntimeRealtimePriority(1);
225 pi2_message_bridge_client =
226 std::make_unique<MessageBridgeClient>(pi2_client_event_loop.get());
227 }
228
229 void RunPi2Client(chrono::nanoseconds duration) {
230 // Run for 5 seconds to make sure we have time to estimate the offset.
231 aos::TimerHandler *const quit = pi2_client_event_loop->AddTimer(
232 [this]() { pi2_client_event_loop->Exit(); });
233 pi2_client_event_loop->OnRun([this, quit, duration]() {
234 // Stop between timestamps, not exactly on them.
235 quit->Setup(pi2_client_event_loop->monotonic_now() + duration);
236 });
237
238 // And go!
239 pi2_client_event_loop->Run();
240 }
241
242 void StartPi2Client() {
243 pi2_client_thread = std::thread([this]() {
244 LOG(INFO) << "Started pi2_message_bridge_client";
245 pi2_client_event_loop->Run();
246 });
247 }
248
249 void StopPi2Client() {
250 if (pi2_client_thread.joinable()) {
251 pi2_client_event_loop->Exit();
252 pi2_client_thread.join();
253 pi2_client_thread = std::thread();
254 }
255 pi2_message_bridge_client.reset();
256 pi2_client_event_loop.reset();
257 }
258
259 void MakePi2Test() {
260 OnPi2();
261 FLAGS_application_name = "test2";
262 pi2_test_event_loop =
Austin Schuhf466ab52021-02-16 22:00:38 -0800263 std::make_unique<aos::ShmEventLoop>(&config.message());
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800264
265 pi2_test_event_loop->MakeWatcher(
266 "/pi2/aos", [](const ServerStatistics &stats) {
267 VLOG(1) << "/pi2/aos ServerStatistics " << FlatbufferToJson(&stats);
268 });
269
270 pi2_test_event_loop->MakeWatcher(
271 "/pi2/aos", [](const ClientStatistics &stats) {
272 VLOG(1) << "/pi2/aos ClientStatistics " << FlatbufferToJson(&stats);
273 });
274
275 pi2_test_event_loop->MakeWatcher(
Austin Schuh8902fa52021-03-14 22:39:24 -0700276 "/pi1/aos", [this](const Timestamp &timestamp) {
277 VLOG(1) << "/pi1/aos Timestamp " << FlatbufferToJson(&timestamp);
Austin Schuha9012be2021-07-21 15:19:11 -0700278 EXPECT_EQ(pi2_test_event_loop->context().source_boot_uuid,
Austin Schuh8902fa52021-03-14 22:39:24 -0700279 pi1_boot_uuid_);
280 });
281 pi2_test_event_loop->MakeWatcher(
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800282 "/pi2/aos", [](const Timestamp &timestamp) {
283 VLOG(1) << "/pi2/aos Timestamp " << FlatbufferToJson(&timestamp);
284 });
285 }
286
287 void StartPi2Test() {
288 pi2_test_thread = std::thread([this]() {
289 LOG(INFO) << "Started pi2_message_bridge_test";
290 pi2_test_event_loop->Run();
291 });
292 }
293
294 void StopPi2Test() {
295 pi2_test_event_loop->Exit();
296 pi2_test_thread.join();
297 }
298
Austin Schuhf466ab52021-02-16 22:00:38 -0800299 aos::FlatbufferDetachedBuffer<aos::Configuration> config;
Austin Schuh8902fa52021-03-14 22:39:24 -0700300 const UUID pi1_boot_uuid_;
301 const UUID pi2_boot_uuid_;
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800302
303 std::unique_ptr<aos::ShmEventLoop> pi1_server_event_loop;
304 std::unique_ptr<MessageBridgeServer> pi1_message_bridge_server;
305 std::thread pi1_server_thread;
306
307 std::unique_ptr<aos::ShmEventLoop> pi1_client_event_loop;
308 std::unique_ptr<MessageBridgeClient> pi1_message_bridge_client;
309 std::thread pi1_client_thread;
310
311 std::unique_ptr<aos::ShmEventLoop> pi1_test_event_loop;
312 std::thread pi1_test_thread;
313
314 std::unique_ptr<aos::ShmEventLoop> pi2_server_event_loop;
315 std::unique_ptr<MessageBridgeServer> pi2_message_bridge_server;
316 std::thread pi2_server_thread;
317
318 std::unique_ptr<aos::ShmEventLoop> pi2_client_event_loop;
319 std::unique_ptr<MessageBridgeClient> pi2_message_bridge_client;
320 std::thread pi2_client_thread;
321
322 std::unique_ptr<aos::ShmEventLoop> pi2_test_event_loop;
323 std::thread pi2_test_thread;
Austin Schuhe991fe22020-11-18 16:53:39 -0800324};
325
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800326// Test that we can send a ping message over sctp and receive it.
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800327TEST_P(MessageBridgeParameterizedTest, PingPong) {
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800328 // This is rather annoying to set up. We need to start up a client and
329 // server, on the same node, but get them to think that they are on different
330 // nodes.
331 //
332 // We then get to wait until they are connected.
333 //
334 // After they are connected, we send a Ping message.
335 //
336 // On the other end, we receive a Pong message.
337 //
338 // But, we need the client to not post directly to "/test" like it would in a
339 // real system, otherwise we will re-send the ping message... So, use an
340 // application specific map to have the client post somewhere else.
341 //
342 // To top this all off, each of these needs to be done with a ShmEventLoop,
343 // which needs to run in a separate thread... And it is really hard to get
344 // everything started up reliably. So just be super generous on timeouts and
345 // hope for the best. We can be more generous in the future if we need to.
346 //
347 // We are faking the application names by passing in --application_name=foo
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800348 OnPi1();
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800349 // Force ourselves to be "raspberrypi" and allocate everything.
Austin Schuh2f8fd752020-09-01 22:38:28 -0700350
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800351 MakePi1Server();
352 MakePi1Client();
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800353
354 // And build the app which sends the pings.
355 FLAGS_application_name = "ping";
Austin Schuhf466ab52021-02-16 22:00:38 -0800356 aos::ShmEventLoop ping_event_loop(&config.message());
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800357 aos::Sender<examples::Ping> ping_sender =
358 ping_event_loop.MakeSender<examples::Ping>("/test");
359
Austin Schuhf466ab52021-02-16 22:00:38 -0800360 aos::ShmEventLoop pi1_test_event_loop(&config.message());
Austin Schuh0de30f32020-12-06 12:44:28 -0800361 aos::Fetcher<RemoteMessage> message_header_fetcher1 =
362 pi1_test_event_loop.MakeFetcher<RemoteMessage>(
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800363 shared() ? "/pi1/aos/remote_timestamps/pi2"
364 : "/pi1/aos/remote_timestamps/pi2/test/aos-examples-Ping");
Austin Schuh2f8fd752020-09-01 22:38:28 -0700365
366 // Fetchers for confirming the remote timestamps made it.
367 aos::Fetcher<examples::Ping> ping_on_pi1_fetcher =
368 ping_event_loop.MakeFetcher<examples::Ping>("/test");
369 aos::Fetcher<Timestamp> pi1_on_pi1_timestamp_fetcher =
370 ping_event_loop.MakeFetcher<Timestamp>("/aos");
371
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800372 // Now do it for "raspberrypi2", the client.
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800373 OnPi2();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700374
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800375 MakePi2Client();
376 MakePi2Server();
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800377
378 // And build the app which sends the pongs.
379 FLAGS_application_name = "pong";
Austin Schuhf466ab52021-02-16 22:00:38 -0800380 aos::ShmEventLoop pong_event_loop(&config.message());
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800381
Austin Schuh7bc59052020-02-16 23:48:33 -0800382 // And build the app for testing.
383 FLAGS_application_name = "test";
Austin Schuhf466ab52021-02-16 22:00:38 -0800384 aos::ShmEventLoop test_event_loop(&config.message());
Austin Schuh7bc59052020-02-16 23:48:33 -0800385
386 aos::Fetcher<ClientStatistics> client_statistics_fetcher =
387 test_event_loop.MakeFetcher<ClientStatistics>("/aos");
Austin Schuh0de30f32020-12-06 12:44:28 -0800388 aos::Fetcher<RemoteMessage> message_header_fetcher2 =
389 test_event_loop.MakeFetcher<RemoteMessage>(
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800390 shared() ? "/pi2/aos/remote_timestamps/pi1"
391 : "/pi2/aos/remote_timestamps/pi1/pi2/aos/"
392 "aos-message_bridge-Timestamp");
Austin Schuh2f8fd752020-09-01 22:38:28 -0700393
394 // Event loop for fetching data delivered to pi2 from pi1 to match up
395 // messages.
Austin Schuhf466ab52021-02-16 22:00:38 -0800396 aos::ShmEventLoop delivered_messages_event_loop(&config.message());
Austin Schuh2f8fd752020-09-01 22:38:28 -0700397 aos::Fetcher<Timestamp> pi1_on_pi2_timestamp_fetcher =
398 delivered_messages_event_loop.MakeFetcher<Timestamp>("/pi1/aos");
399 aos::Fetcher<examples::Ping> ping_on_pi2_fetcher =
400 delivered_messages_event_loop.MakeFetcher<examples::Ping>("/test");
401 EXPECT_FALSE(ping_on_pi2_fetcher.Fetch());
402 EXPECT_FALSE(pi1_on_pi2_timestamp_fetcher.Fetch());
Austin Schuh7bc59052020-02-16 23:48:33 -0800403
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800404 // Count the pongs.
405 int pong_count = 0;
Austin Schuh8902fa52021-03-14 22:39:24 -0700406 pong_event_loop.MakeWatcher("/test", [&pong_count, &pong_event_loop,
407 this](const examples::Ping &ping) {
Austin Schuha9012be2021-07-21 15:19:11 -0700408 EXPECT_EQ(pong_event_loop.context().source_boot_uuid, pi1_boot_uuid_);
Austin Schuh8902fa52021-03-14 22:39:24 -0700409 ++pong_count;
410 VLOG(1) << "Got ping back " << FlatbufferToJson(&ping);
411 });
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800412
413 FLAGS_override_hostname = "";
414
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800415 // Wait until we are connected, then send.
416 int ping_count = 0;
Austin Schuh7bc59052020-02-16 23:48:33 -0800417 int pi1_server_statistics_count = 0;
Austin Schuh61e973f2021-02-21 21:43:56 -0800418 ping_event_loop.MakeWatcher("/pi1/aos", [this, &ping_count, &ping_sender,
419 &pi1_server_statistics_count](
420 const ServerStatistics &stats) {
421 VLOG(1) << "/pi1/aos ServerStatistics " << FlatbufferToJson(&stats);
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800422
Austin Schuh61e973f2021-02-21 21:43:56 -0800423 ASSERT_TRUE(stats.has_connections());
424 EXPECT_EQ(stats.connections()->size(), 1);
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800425
Austin Schuh61e973f2021-02-21 21:43:56 -0800426 bool connected = false;
427 for (const ServerConnection *connection : *stats.connections()) {
428 // Confirm that we are estimating the server time offset correctly. It
429 // should be about 0 since we are on the same machine here.
430 if (connection->has_monotonic_offset()) {
431 EXPECT_LT(chrono::nanoseconds(connection->monotonic_offset()),
432 chrono::milliseconds(1));
433 EXPECT_GT(chrono::nanoseconds(connection->monotonic_offset()),
434 chrono::milliseconds(-1));
435 ++pi1_server_statistics_count;
436 }
Austin Schuh7bc59052020-02-16 23:48:33 -0800437
Austin Schuh61e973f2021-02-21 21:43:56 -0800438 if (connection->node()->name()->string_view() ==
439 pi2_client_event_loop->node()->name()->string_view()) {
440 if (connection->state() == State::CONNECTED) {
441 EXPECT_TRUE(connection->has_boot_uuid());
442 connected = true;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800443 }
Austin Schuh61e973f2021-02-21 21:43:56 -0800444 }
445 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800446
Austin Schuh61e973f2021-02-21 21:43:56 -0800447 if (connected) {
448 VLOG(1) << "Connected! Sent ping.";
449 auto builder = ping_sender.MakeBuilder();
450 examples::Ping::Builder ping_builder =
451 builder.MakeBuilder<examples::Ping>();
452 ping_builder.add_value(ping_count + 971);
453 builder.Send(ping_builder.Finish());
454 ++ping_count;
455 }
456 });
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800457
Austin Schuh7bc59052020-02-16 23:48:33 -0800458 // Confirm both client and server statistics messages have decent offsets in
459 // them.
460 int pi2_server_statistics_count = 0;
Austin Schuh196a4452020-03-15 23:12:03 -0700461 pong_event_loop.MakeWatcher("/pi2/aos", [&pi2_server_statistics_count](
Austin Schuh7bc59052020-02-16 23:48:33 -0800462 const ServerStatistics &stats) {
Austin Schuh1ca49e92020-12-11 00:01:27 -0800463 VLOG(1) << "/pi2/aos ServerStatistics " << FlatbufferToJson(&stats);
Austin Schuh7bc59052020-02-16 23:48:33 -0800464 for (const ServerConnection *connection : *stats.connections()) {
465 if (connection->has_monotonic_offset()) {
466 ++pi2_server_statistics_count;
467 // Confirm that we are estimating the server time offset correctly. It
468 // should be about 0 since we are on the same machine here.
469 EXPECT_LT(chrono::nanoseconds(connection->monotonic_offset()),
470 chrono::milliseconds(1));
471 EXPECT_GT(chrono::nanoseconds(connection->monotonic_offset()),
472 chrono::milliseconds(-1));
Austin Schuh20ac95d2020-12-05 17:24:19 -0800473 EXPECT_TRUE(connection->has_boot_uuid());
Austin Schuh7bc59052020-02-16 23:48:33 -0800474 }
475 }
476 });
477
478 int pi1_client_statistics_count = 0;
Austin Schuh5344c352020-04-12 17:04:26 -0700479 ping_event_loop.MakeWatcher("/pi1/aos", [&pi1_client_statistics_count](
480 const ClientStatistics &stats) {
Austin Schuh1ca49e92020-12-11 00:01:27 -0800481 VLOG(1) << "/pi1/aos ClientStatistics " << FlatbufferToJson(&stats);
Austin Schuh7bc59052020-02-16 23:48:33 -0800482
Austin Schuh5344c352020-04-12 17:04:26 -0700483 for (const ClientConnection *connection : *stats.connections()) {
484 if (connection->has_monotonic_offset()) {
485 ++pi1_client_statistics_count;
486 // It takes at least 10 microseconds to send a message between the
487 // client and server. The min (filtered) time shouldn't be over 10
488 // milliseconds on localhost. This might have to bump up if this is
489 // proving flaky.
490 EXPECT_LT(chrono::nanoseconds(connection->monotonic_offset()),
Austin Schuh3edddcc2020-12-29 13:32:02 -0800491 chrono::milliseconds(10))
492 << " " << connection->monotonic_offset()
493 << "ns vs 10000ns on iteration " << pi1_client_statistics_count;
Austin Schuh5344c352020-04-12 17:04:26 -0700494 EXPECT_GT(chrono::nanoseconds(connection->monotonic_offset()),
Austin Schuh3edddcc2020-12-29 13:32:02 -0800495 chrono::microseconds(10))
496 << " " << connection->monotonic_offset()
497 << "ns vs 10000ns on iteration " << pi1_client_statistics_count;
Austin Schuh5344c352020-04-12 17:04:26 -0700498 }
499 }
500 });
Austin Schuh7bc59052020-02-16 23:48:33 -0800501
502 int pi2_client_statistics_count = 0;
Austin Schuh196a4452020-03-15 23:12:03 -0700503 pong_event_loop.MakeWatcher("/pi2/aos", [&pi2_client_statistics_count](
Austin Schuh7bc59052020-02-16 23:48:33 -0800504 const ClientStatistics &stats) {
Austin Schuh1ca49e92020-12-11 00:01:27 -0800505 VLOG(1) << "/pi2/aos ClientStatistics " << FlatbufferToJson(&stats);
Austin Schuh7bc59052020-02-16 23:48:33 -0800506
507 for (const ClientConnection *connection : *stats.connections()) {
508 if (connection->has_monotonic_offset()) {
509 ++pi2_client_statistics_count;
510 EXPECT_LT(chrono::nanoseconds(connection->monotonic_offset()),
511 chrono::milliseconds(10));
512 EXPECT_GT(chrono::nanoseconds(connection->monotonic_offset()),
513 chrono::microseconds(10));
514 }
515 }
516 });
517
Austin Schuh196a4452020-03-15 23:12:03 -0700518 ping_event_loop.MakeWatcher("/pi1/aos", [](const Timestamp &timestamp) {
Austin Schuh7bc59052020-02-16 23:48:33 -0800519 EXPECT_TRUE(timestamp.has_offsets());
Austin Schuh1ca49e92020-12-11 00:01:27 -0800520 VLOG(1) << "/pi1/aos Timestamp " << FlatbufferToJson(&timestamp);
Austin Schuh7bc59052020-02-16 23:48:33 -0800521 });
Austin Schuh196a4452020-03-15 23:12:03 -0700522 pong_event_loop.MakeWatcher("/pi2/aos", [](const Timestamp &timestamp) {
Austin Schuh7bc59052020-02-16 23:48:33 -0800523 EXPECT_TRUE(timestamp.has_offsets());
Austin Schuh1ca49e92020-12-11 00:01:27 -0800524 VLOG(1) << "/pi2/aos Timestamp " << FlatbufferToJson(&timestamp);
Austin Schuh7bc59052020-02-16 23:48:33 -0800525 });
526
527 // Run for 5 seconds to make sure we have time to estimate the offset.
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800528 aos::TimerHandler *quit = ping_event_loop.AddTimer(
529 [&ping_event_loop]() { ping_event_loop.Exit(); });
530 ping_event_loop.OnRun([quit, &ping_event_loop]() {
Austin Schuh7bc59052020-02-16 23:48:33 -0800531 // Stop between timestamps, not exactly on them.
532 quit->Setup(ping_event_loop.monotonic_now() + chrono::milliseconds(5050));
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800533 });
534
Austin Schuh2f8fd752020-09-01 22:38:28 -0700535 // Find the channel index for both the /pi1/aos Timestamp channel and Ping
536 // channel.
537 const size_t pi1_timestamp_channel = configuration::ChannelIndex(
538 pong_event_loop.configuration(), pi1_on_pi2_timestamp_fetcher.channel());
539 const size_t ping_timestamp_channel =
540 configuration::ChannelIndex(delivered_messages_event_loop.configuration(),
541 ping_on_pi2_fetcher.channel());
542
543 for (const Channel *channel : *ping_event_loop.configuration()->channels()) {
544 VLOG(1) << "Channel "
545 << configuration::ChannelIndex(ping_event_loop.configuration(),
546 channel)
547 << " " << configuration::CleanedChannelToString(channel);
548 }
549
550 // For each remote timestamp we get back, confirm that it is either a ping
551 // message, or a timestamp we sent out. Also confirm that the timestamps are
552 // correct.
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800553 for (std::pair<int, std::string> channel :
554 shared()
555 ? std::vector<std::pair<
556 int, std::string>>{{-1, "/pi1/aos/remote_timestamps/pi2"}}
557 : std::vector<std::pair<int, std::string>>{
558 {pi1_timestamp_channel,
559 "/pi1/aos/remote_timestamps/pi2/pi1/aos/"
560 "aos-message_bridge-Timestamp"},
561 {ping_timestamp_channel,
562 "/pi1/aos/remote_timestamps/pi2/test/aos-examples-Ping"}}) {
563 ping_event_loop.MakeWatcher(
564 channel.second,
565 [pi1_timestamp_channel, ping_timestamp_channel, &ping_on_pi2_fetcher,
566 &ping_on_pi1_fetcher, &pi1_on_pi2_timestamp_fetcher,
567 &pi1_on_pi1_timestamp_fetcher,
568 channel_index = channel.first](const RemoteMessage &header) {
569 VLOG(1) << "/pi1/aos/remote_timestamps/pi2 RemoteMessage "
570 << aos::FlatbufferToJson(&header);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700571
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800572 EXPECT_TRUE(header.has_boot_uuid());
573 if (channel_index != -1) {
574 ASSERT_EQ(channel_index, header.channel_index());
Austin Schuh2f8fd752020-09-01 22:38:28 -0700575 }
576
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800577 const aos::monotonic_clock::time_point header_monotonic_sent_time(
578 chrono::nanoseconds(header.monotonic_sent_time()));
579 const aos::realtime_clock::time_point header_realtime_sent_time(
580 chrono::nanoseconds(header.realtime_sent_time()));
581 const aos::monotonic_clock::time_point header_monotonic_remote_time(
582 chrono::nanoseconds(header.monotonic_remote_time()));
583 const aos::realtime_clock::time_point header_realtime_remote_time(
584 chrono::nanoseconds(header.realtime_remote_time()));
585
586 const Context *pi1_context = nullptr;
587 const Context *pi2_context = nullptr;
588
589 if (header.channel_index() == pi1_timestamp_channel) {
590 // Find the forwarded message.
591 while (pi1_on_pi2_timestamp_fetcher.context().monotonic_event_time <
592 header_monotonic_sent_time) {
593 ASSERT_TRUE(pi1_on_pi2_timestamp_fetcher.FetchNext());
594 }
595
596 // And the source message.
597 while (pi1_on_pi1_timestamp_fetcher.context().monotonic_event_time <
598 header_monotonic_remote_time) {
599 ASSERT_TRUE(pi1_on_pi1_timestamp_fetcher.FetchNext());
600 }
601
602 pi1_context = &pi1_on_pi1_timestamp_fetcher.context();
603 pi2_context = &pi1_on_pi2_timestamp_fetcher.context();
604 } else if (header.channel_index() == ping_timestamp_channel) {
605 // Find the forwarded message.
606 while (ping_on_pi2_fetcher.context().monotonic_event_time <
607 header_monotonic_sent_time) {
608 ASSERT_TRUE(ping_on_pi2_fetcher.FetchNext());
609 }
610
611 // And the source message.
612 while (ping_on_pi1_fetcher.context().monotonic_event_time <
613 header_monotonic_remote_time) {
614 ASSERT_TRUE(ping_on_pi1_fetcher.FetchNext());
615 }
616
617 pi1_context = &ping_on_pi1_fetcher.context();
618 pi2_context = &ping_on_pi2_fetcher.context();
619 } else {
620 LOG(FATAL) << "Unknown channel";
Austin Schuh2f8fd752020-09-01 22:38:28 -0700621 }
622
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800623 // Confirm the forwarded message has matching timestamps to the
624 // timestamps we got back.
625 EXPECT_EQ(pi2_context->queue_index, header.queue_index());
626 EXPECT_EQ(pi2_context->monotonic_event_time,
627 header_monotonic_sent_time);
628 EXPECT_EQ(pi2_context->realtime_event_time,
629 header_realtime_sent_time);
630 EXPECT_EQ(pi2_context->realtime_remote_time,
631 header_realtime_remote_time);
632 EXPECT_EQ(pi2_context->monotonic_remote_time,
633 header_monotonic_remote_time);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700634
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800635 // Confirm the forwarded message also matches the source message.
636 EXPECT_EQ(pi1_context->queue_index, header.queue_index());
637 EXPECT_EQ(pi1_context->monotonic_event_time,
638 header_monotonic_remote_time);
639 EXPECT_EQ(pi1_context->realtime_event_time,
640 header_realtime_remote_time);
641 });
642 }
Austin Schuh2f8fd752020-09-01 22:38:28 -0700643
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800644 // Start everything up. Pong is the only thing we don't know how to wait
645 // on, so start it first.
Austin Schuh7bc59052020-02-16 23:48:33 -0800646 std::thread pong_thread([&pong_event_loop]() { pong_event_loop.Run(); });
647
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800648 StartPi1Server();
649 StartPi1Client();
650 StartPi2Client();
651 StartPi2Server();
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800652
653 // And go!
654 ping_event_loop.Run();
655
656 // Shut everyone else down
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800657 StopPi1Server();
658 StopPi1Client();
659 StopPi2Client();
660 StopPi2Server();
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800661 pong_event_loop.Exit();
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800662 pong_thread.join();
663
664 // Make sure we sent something.
665 EXPECT_GE(ping_count, 1);
666 // And got something back.
667 EXPECT_GE(pong_count, 1);
Austin Schuh7bc59052020-02-16 23:48:33 -0800668
669 // Confirm that we are estimating a monotonic offset on the client.
670 ASSERT_TRUE(client_statistics_fetcher.Fetch());
671
672 EXPECT_EQ(client_statistics_fetcher->connections()->size(), 1u);
673 EXPECT_EQ(client_statistics_fetcher->connections()
674 ->Get(0)
675 ->node()
676 ->name()
677 ->string_view(),
678 "pi1");
679
680 // Make sure the offset in one direction is less than a second.
681 EXPECT_GT(
Austin Schuh2b159eb2021-07-31 19:42:21 -0700682 client_statistics_fetcher->connections()->Get(0)->monotonic_offset(), 0)
683 << aos::FlatbufferToJson(client_statistics_fetcher.get());
Austin Schuh7bc59052020-02-16 23:48:33 -0800684 EXPECT_LT(
685 client_statistics_fetcher->connections()->Get(0)->monotonic_offset(),
Austin Schuh2b159eb2021-07-31 19:42:21 -0700686 1000000000)
687 << aos::FlatbufferToJson(client_statistics_fetcher.get());
Austin Schuh7bc59052020-02-16 23:48:33 -0800688
689 EXPECT_GE(pi1_server_statistics_count, 2);
690 EXPECT_GE(pi2_server_statistics_count, 2);
691 EXPECT_GE(pi1_client_statistics_count, 2);
692 EXPECT_GE(pi2_client_statistics_count, 2);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700693
694 // Confirm we got timestamps back!
695 EXPECT_TRUE(message_header_fetcher1.Fetch());
696 EXPECT_TRUE(message_header_fetcher2.Fetch());
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800697}
698
Austin Schuh5344c352020-04-12 17:04:26 -0700699// Test that the client disconnecting triggers the server offsets on both sides
700// to clear.
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800701TEST_P(MessageBridgeParameterizedTest, ClientRestart) {
Austin Schuh5344c352020-04-12 17:04:26 -0700702 // This is rather annoying to set up. We need to start up a client and
703 // server, on the same node, but get them to think that they are on different
704 // nodes.
705 //
706 // We need the client to not post directly to "/test" like it would in a
707 // real system, otherwise we will re-send the ping message... So, use an
708 // application specific map to have the client post somewhere else.
709 //
710 // To top this all off, each of these needs to be done with a ShmEventLoop,
711 // which needs to run in a separate thread... And it is really hard to get
712 // everything started up reliably. So just be super generous on timeouts and
713 // hope for the best. We can be more generous in the future if we need to.
714 //
715 // We are faking the application names by passing in --application_name=foo
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800716 OnPi1();
Austin Schuh5344c352020-04-12 17:04:26 -0700717
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800718 MakePi1Server();
719 MakePi1Client();
Austin Schuh5344c352020-04-12 17:04:26 -0700720
721 // And build the app for testing.
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800722 MakePi1Test();
Austin Schuh5344c352020-04-12 17:04:26 -0700723 aos::Fetcher<ServerStatistics> pi1_server_statistics_fetcher =
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800724 pi1_test_event_loop->MakeFetcher<ServerStatistics>("/pi1/aos");
Austin Schuh5344c352020-04-12 17:04:26 -0700725
726 // Now do it for "raspberrypi2", the client.
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800727 OnPi2();
728 MakePi2Server();
Austin Schuh5344c352020-04-12 17:04:26 -0700729
730 // And build the app for testing.
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800731 MakePi2Test();
Austin Schuh5344c352020-04-12 17:04:26 -0700732 aos::Fetcher<ServerStatistics> pi2_server_statistics_fetcher =
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800733 pi2_test_event_loop->MakeFetcher<ServerStatistics>("/pi2/aos");
Austin Schuh5344c352020-04-12 17:04:26 -0700734
735 // Wait until we are connected, then send.
Austin Schuh5344c352020-04-12 17:04:26 -0700736
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800737 StartPi1Test();
738 StartPi2Test();
739 StartPi1Server();
740 StartPi1Client();
741 StartPi2Server();
Austin Schuh5344c352020-04-12 17:04:26 -0700742
743 {
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800744 MakePi2Client();
Austin Schuh5344c352020-04-12 17:04:26 -0700745
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800746 RunPi2Client(chrono::milliseconds(3050));
Austin Schuh5344c352020-04-12 17:04:26 -0700747
748 // Now confirm we are synchronized.
749 EXPECT_TRUE(pi1_server_statistics_fetcher.Fetch());
750 EXPECT_TRUE(pi2_server_statistics_fetcher.Fetch());
751
752 const ServerConnection *const pi1_connection =
753 pi1_server_statistics_fetcher->connections()->Get(0);
754 const ServerConnection *const pi2_connection =
755 pi2_server_statistics_fetcher->connections()->Get(0);
756
757 EXPECT_EQ(pi1_connection->state(), State::CONNECTED);
758 EXPECT_TRUE(pi1_connection->has_monotonic_offset());
759 EXPECT_LT(chrono::nanoseconds(pi1_connection->monotonic_offset()),
760 chrono::milliseconds(1));
761 EXPECT_GT(chrono::nanoseconds(pi1_connection->monotonic_offset()),
762 chrono::milliseconds(-1));
Austin Schuh20ac95d2020-12-05 17:24:19 -0800763 EXPECT_TRUE(pi1_connection->has_boot_uuid());
Austin Schuh5344c352020-04-12 17:04:26 -0700764
765 EXPECT_EQ(pi2_connection->state(), State::CONNECTED);
766 EXPECT_TRUE(pi2_connection->has_monotonic_offset());
767 EXPECT_LT(chrono::nanoseconds(pi2_connection->monotonic_offset()),
768 chrono::milliseconds(1));
769 EXPECT_GT(chrono::nanoseconds(pi2_connection->monotonic_offset()),
770 chrono::milliseconds(-1));
Austin Schuh20ac95d2020-12-05 17:24:19 -0800771 EXPECT_TRUE(pi2_connection->has_boot_uuid());
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800772
773 StopPi2Client();
Austin Schuh5344c352020-04-12 17:04:26 -0700774 }
775
776 std::this_thread::sleep_for(std::chrono::seconds(2));
777
778 {
779 // Now confirm we are un-synchronized.
780 EXPECT_TRUE(pi1_server_statistics_fetcher.Fetch());
781 EXPECT_TRUE(pi2_server_statistics_fetcher.Fetch());
782 const ServerConnection *const pi1_connection =
783 pi1_server_statistics_fetcher->connections()->Get(0);
784 const ServerConnection *const pi2_connection =
785 pi2_server_statistics_fetcher->connections()->Get(0);
786
787 EXPECT_EQ(pi1_connection->state(), State::DISCONNECTED);
788 EXPECT_FALSE(pi1_connection->has_monotonic_offset());
Austin Schuh20ac95d2020-12-05 17:24:19 -0800789 EXPECT_FALSE(pi1_connection->has_boot_uuid());
Austin Schuh5344c352020-04-12 17:04:26 -0700790 EXPECT_EQ(pi2_connection->state(), State::CONNECTED);
791 EXPECT_FALSE(pi2_connection->has_monotonic_offset());
Austin Schuh20ac95d2020-12-05 17:24:19 -0800792 EXPECT_TRUE(pi2_connection->has_boot_uuid());
Austin Schuh5344c352020-04-12 17:04:26 -0700793 }
794
795 {
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800796 MakePi2Client();
Austin Schuh5344c352020-04-12 17:04:26 -0700797 // And go!
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800798 RunPi2Client(chrono::milliseconds(3050));
Austin Schuh5344c352020-04-12 17:04:26 -0700799
800 EXPECT_TRUE(pi1_server_statistics_fetcher.Fetch());
801 EXPECT_TRUE(pi2_server_statistics_fetcher.Fetch());
802
803 // Now confirm we are synchronized again.
804 const ServerConnection *const pi1_connection =
805 pi1_server_statistics_fetcher->connections()->Get(0);
806 const ServerConnection *const pi2_connection =
807 pi2_server_statistics_fetcher->connections()->Get(0);
808
809 EXPECT_EQ(pi1_connection->state(), State::CONNECTED);
810 EXPECT_TRUE(pi1_connection->has_monotonic_offset());
811 EXPECT_LT(chrono::nanoseconds(pi1_connection->monotonic_offset()),
812 chrono::milliseconds(1));
813 EXPECT_GT(chrono::nanoseconds(pi1_connection->monotonic_offset()),
814 chrono::milliseconds(-1));
Austin Schuh20ac95d2020-12-05 17:24:19 -0800815 EXPECT_TRUE(pi1_connection->has_boot_uuid());
Austin Schuh5344c352020-04-12 17:04:26 -0700816
817 EXPECT_EQ(pi2_connection->state(), State::CONNECTED);
818 EXPECT_TRUE(pi2_connection->has_monotonic_offset());
819 EXPECT_LT(chrono::nanoseconds(pi2_connection->monotonic_offset()),
820 chrono::milliseconds(1));
821 EXPECT_GT(chrono::nanoseconds(pi2_connection->monotonic_offset()),
822 chrono::milliseconds(-1));
Austin Schuh20ac95d2020-12-05 17:24:19 -0800823 EXPECT_TRUE(pi2_connection->has_boot_uuid());
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800824
825 StopPi2Client();
Austin Schuh5344c352020-04-12 17:04:26 -0700826 }
827
828 // Shut everyone else down
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800829 StopPi1Server();
830 StopPi1Client();
831 StopPi2Server();
832 StopPi1Test();
833 StopPi2Test();
Austin Schuh5344c352020-04-12 17:04:26 -0700834}
835
836// Test that the server disconnecting triggers the server offsets on the other
837// side to clear, along with the other client.
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800838TEST_P(MessageBridgeParameterizedTest, ServerRestart) {
Austin Schuh5344c352020-04-12 17:04:26 -0700839 // This is rather annoying to set up. We need to start up a client and
840 // server, on the same node, but get them to think that they are on different
841 // nodes.
842 //
843 // We need the client to not post directly to "/test" like it would in a
844 // real system, otherwise we will re-send the ping message... So, use an
845 // application specific map to have the client post somewhere else.
846 //
847 // To top this all off, each of these needs to be done with a ShmEventLoop,
848 // which needs to run in a separate thread... And it is really hard to get
849 // everything started up reliably. So just be super generous on timeouts and
850 // hope for the best. We can be more generous in the future if we need to.
851 //
852 // We are faking the application names by passing in --application_name=foo
Austin Schuh5344c352020-04-12 17:04:26 -0700853 // Force ourselves to be "raspberrypi" and allocate everything.
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800854 OnPi1();
855 MakePi1Server();
856 MakePi1Client();
Austin Schuh5344c352020-04-12 17:04:26 -0700857
858 // And build the app for testing.
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800859 MakePi1Test();
Austin Schuh5344c352020-04-12 17:04:26 -0700860 aos::Fetcher<ServerStatistics> pi1_server_statistics_fetcher =
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800861 pi1_test_event_loop->MakeFetcher<ServerStatistics>("/pi1/aos");
Austin Schuh5344c352020-04-12 17:04:26 -0700862 aos::Fetcher<ClientStatistics> pi1_client_statistics_fetcher =
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800863 pi1_test_event_loop->MakeFetcher<ClientStatistics>("/pi1/aos");
Austin Schuh5344c352020-04-12 17:04:26 -0700864
865 // Now do it for "raspberrypi2", the client.
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800866 OnPi2();
867 MakePi2Client();
Austin Schuh5344c352020-04-12 17:04:26 -0700868
869 // And build the app for testing.
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800870 MakePi2Test();
Austin Schuh5344c352020-04-12 17:04:26 -0700871 aos::Fetcher<ServerStatistics> pi2_server_statistics_fetcher =
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800872 pi2_test_event_loop->MakeFetcher<ServerStatistics>("/pi2/aos");
Austin Schuh5344c352020-04-12 17:04:26 -0700873
874 // Start everything up. Pong is the only thing we don't know how to wait on,
875 // so start it first.
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800876 StartPi1Test();
877 StartPi2Test();
878 StartPi1Server();
879 StartPi1Client();
880 StartPi2Client();
Austin Schuh5344c352020-04-12 17:04:26 -0700881
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800882 // Confirm both client and server statistics messages have decent offsets in
883 // them.
Austin Schuh5344c352020-04-12 17:04:26 -0700884
885 {
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800886 MakePi2Server();
Austin Schuh5344c352020-04-12 17:04:26 -0700887
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800888 RunPi2Server(chrono::milliseconds(3050));
Austin Schuh5344c352020-04-12 17:04:26 -0700889
890 // Now confirm we are synchronized.
891 EXPECT_TRUE(pi1_server_statistics_fetcher.Fetch());
892 EXPECT_TRUE(pi2_server_statistics_fetcher.Fetch());
893
894 const ServerConnection *const pi1_connection =
895 pi1_server_statistics_fetcher->connections()->Get(0);
896 const ServerConnection *const pi2_connection =
897 pi2_server_statistics_fetcher->connections()->Get(0);
898
899 EXPECT_EQ(pi1_connection->state(), State::CONNECTED);
900 EXPECT_TRUE(pi1_connection->has_monotonic_offset());
901 EXPECT_LT(chrono::nanoseconds(pi1_connection->monotonic_offset()),
902 chrono::milliseconds(1));
903 EXPECT_GT(chrono::nanoseconds(pi1_connection->monotonic_offset()),
904 chrono::milliseconds(-1));
Austin Schuh20ac95d2020-12-05 17:24:19 -0800905 EXPECT_TRUE(pi1_connection->has_boot_uuid());
Austin Schuh5344c352020-04-12 17:04:26 -0700906
907 EXPECT_EQ(pi2_connection->state(), State::CONNECTED);
908 EXPECT_TRUE(pi2_connection->has_monotonic_offset());
909 EXPECT_LT(chrono::nanoseconds(pi2_connection->monotonic_offset()),
910 chrono::milliseconds(1));
911 EXPECT_GT(chrono::nanoseconds(pi2_connection->monotonic_offset()),
912 chrono::milliseconds(-1));
Austin Schuh20ac95d2020-12-05 17:24:19 -0800913 EXPECT_TRUE(pi2_connection->has_boot_uuid());
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800914
915 StopPi2Server();
Austin Schuh5344c352020-04-12 17:04:26 -0700916 }
917
918 std::this_thread::sleep_for(std::chrono::seconds(2));
919
920 {
921 // And confirm we are unsynchronized.
922 EXPECT_TRUE(pi1_server_statistics_fetcher.Fetch());
923 EXPECT_TRUE(pi1_client_statistics_fetcher.Fetch());
924
925 const ServerConnection *const pi1_server_connection =
926 pi1_server_statistics_fetcher->connections()->Get(0);
927 const ClientConnection *const pi1_client_connection =
928 pi1_client_statistics_fetcher->connections()->Get(0);
929
930 EXPECT_EQ(pi1_server_connection->state(), State::CONNECTED);
931 EXPECT_FALSE(pi1_server_connection->has_monotonic_offset());
Austin Schuh20ac95d2020-12-05 17:24:19 -0800932 EXPECT_TRUE(pi1_server_connection->has_boot_uuid());
Austin Schuh5344c352020-04-12 17:04:26 -0700933 EXPECT_EQ(pi1_client_connection->state(), State::DISCONNECTED);
934 EXPECT_FALSE(pi1_client_connection->has_monotonic_offset());
935 }
936
937 {
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800938 MakePi2Server();
Austin Schuh5344c352020-04-12 17:04:26 -0700939
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800940 RunPi2Server(chrono::milliseconds(3050));
Austin Schuh5344c352020-04-12 17:04:26 -0700941
942 // And confirm we are synchronized again.
943 EXPECT_TRUE(pi1_server_statistics_fetcher.Fetch());
944 EXPECT_TRUE(pi2_server_statistics_fetcher.Fetch());
945
946 const ServerConnection *const pi1_connection =
947 pi1_server_statistics_fetcher->connections()->Get(0);
948 const ServerConnection *const pi2_connection =
949 pi2_server_statistics_fetcher->connections()->Get(0);
950
951 EXPECT_EQ(pi1_connection->state(), State::CONNECTED);
952 EXPECT_TRUE(pi1_connection->has_monotonic_offset());
953 EXPECT_LT(chrono::nanoseconds(pi1_connection->monotonic_offset()),
954 chrono::milliseconds(1));
955 EXPECT_GT(chrono::nanoseconds(pi1_connection->monotonic_offset()),
956 chrono::milliseconds(-1));
Austin Schuh20ac95d2020-12-05 17:24:19 -0800957 EXPECT_TRUE(pi1_connection->has_boot_uuid());
Austin Schuh5344c352020-04-12 17:04:26 -0700958
959 EXPECT_EQ(pi2_connection->state(), State::CONNECTED);
960 EXPECT_TRUE(pi2_connection->has_monotonic_offset());
961 EXPECT_LT(chrono::nanoseconds(pi2_connection->monotonic_offset()),
962 chrono::milliseconds(1));
963 EXPECT_GT(chrono::nanoseconds(pi2_connection->monotonic_offset()),
964 chrono::milliseconds(-1));
Austin Schuh20ac95d2020-12-05 17:24:19 -0800965 EXPECT_TRUE(pi2_connection->has_boot_uuid());
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800966
967 StopPi2Server();
Austin Schuh5344c352020-04-12 17:04:26 -0700968 }
969
970 // Shut everyone else down
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800971 StopPi1Server();
972 StopPi1Client();
973 StopPi2Client();
974 StopPi1Test();
975 StopPi2Test();
Austin Schuh5344c352020-04-12 17:04:26 -0700976}
977
Austin Schuh4889b182020-11-18 19:11:56 -0800978// TODO(austin): The above test confirms that the external state does the right
Austin Schuh5344c352020-04-12 17:04:26 -0700979// thing, but doesn't confirm that the internal state does. We either need to
980// expose a way to check the state in a thread-safe way, or need a way to jump
981// time for one node to do that.
982
Austin Schuh4889b182020-11-18 19:11:56 -0800983void SendPing(aos::Sender<examples::Ping> *sender, int value) {
984 aos::Sender<examples::Ping>::Builder builder = sender->MakeBuilder();
985 examples::Ping::Builder ping_builder = builder.MakeBuilder<examples::Ping>();
986 ping_builder.add_value(value);
987 builder.Send(ping_builder.Finish());
988}
989
990// Tests that when a message is sent before the bridge starts up, but is
991// configured as reliable, we forward it. Confirm this survives a client reset.
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800992TEST_P(MessageBridgeParameterizedTest, ReliableSentBeforeClientStartup) {
Austin Schuh0a2f12f2021-01-08 22:48:29 -0800993 OnPi1();
Austin Schuh4889b182020-11-18 19:11:56 -0800994
995 FLAGS_application_name = "sender";
Austin Schuhf466ab52021-02-16 22:00:38 -0800996 aos::ShmEventLoop send_event_loop(&config.message());
Austin Schuh4889b182020-11-18 19:11:56 -0800997 aos::Sender<examples::Ping> ping_sender =
998 send_event_loop.MakeSender<examples::Ping>("/test");
999 SendPing(&ping_sender, 1);
1000 aos::Sender<examples::Ping> unreliable_ping_sender =
1001 send_event_loop.MakeSender<examples::Ping>("/unreliable");
1002 SendPing(&unreliable_ping_sender, 1);
1003
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001004 MakePi1Server();
1005 MakePi1Client();
Austin Schuh4889b182020-11-18 19:11:56 -08001006
1007 FLAGS_application_name = "pi1_timestamp";
Austin Schuhf466ab52021-02-16 22:00:38 -08001008 aos::ShmEventLoop pi1_remote_timestamp_event_loop(&config.message());
Austin Schuh4889b182020-11-18 19:11:56 -08001009
1010 // Now do it for "raspberrypi2", the client.
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001011 OnPi2();
Austin Schuh4889b182020-11-18 19:11:56 -08001012
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001013 MakePi2Server();
Austin Schuh4889b182020-11-18 19:11:56 -08001014
Austin Schuhf466ab52021-02-16 22:00:38 -08001015 aos::ShmEventLoop receive_event_loop(&config.message());
Austin Schuh4889b182020-11-18 19:11:56 -08001016 aos::Fetcher<examples::Ping> ping_fetcher =
1017 receive_event_loop.MakeFetcher<examples::Ping>("/test");
1018 aos::Fetcher<examples::Ping> unreliable_ping_fetcher =
1019 receive_event_loop.MakeFetcher<examples::Ping>("/unreliable");
1020 aos::Fetcher<ClientStatistics> pi2_client_statistics_fetcher =
1021 receive_event_loop.MakeFetcher<ClientStatistics>("/pi2/aos");
1022
1023 const size_t ping_channel_index = configuration::ChannelIndex(
1024 receive_event_loop.configuration(), ping_fetcher.channel());
1025
1026 std::atomic<int> ping_timestamp_count{0};
Austin Schuh36a2c3e2021-02-18 22:28:38 -08001027 const std::string channel_name =
1028 shared() ? "/pi1/aos/remote_timestamps/pi2"
1029 : "/pi1/aos/remote_timestamps/pi2/test/aos-examples-Ping";
Austin Schuh4889b182020-11-18 19:11:56 -08001030 pi1_remote_timestamp_event_loop.MakeWatcher(
Austin Schuh36a2c3e2021-02-18 22:28:38 -08001031 channel_name, [this, channel_name, ping_channel_index,
1032 &ping_timestamp_count](const RemoteMessage &header) {
Austin Schuh61e973f2021-02-21 21:43:56 -08001033 VLOG(1) << channel_name << " RemoteMessage "
Austin Schuh0de30f32020-12-06 12:44:28 -08001034 << aos::FlatbufferToJson(&header);
Austin Schuh20ac95d2020-12-05 17:24:19 -08001035 EXPECT_TRUE(header.has_boot_uuid());
Austin Schuh36a2c3e2021-02-18 22:28:38 -08001036 if (shared() && header.channel_index() != ping_channel_index) {
1037 return;
Austin Schuh4889b182020-11-18 19:11:56 -08001038 }
Austin Schuh36a2c3e2021-02-18 22:28:38 -08001039 CHECK_EQ(header.channel_index(), ping_channel_index);
1040 ++ping_timestamp_count;
Austin Schuh4889b182020-11-18 19:11:56 -08001041 });
1042
1043 // Before everything starts up, confirm there is no message.
1044 EXPECT_FALSE(ping_fetcher.Fetch());
1045 EXPECT_FALSE(unreliable_ping_fetcher.Fetch());
1046
1047 // Spin up the persistant pieces.
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001048 StartPi1Server();
1049 StartPi1Client();
1050 StartPi2Server();
Austin Schuh4889b182020-11-18 19:11:56 -08001051
1052 // Event used to wait for the timestamp counting thread to start.
1053 aos::Event event;
1054 std::thread pi1_remote_timestamp_thread(
1055 [&pi1_remote_timestamp_event_loop, &event]() {
1056 pi1_remote_timestamp_event_loop.OnRun([&event]() { event.Set(); });
1057 pi1_remote_timestamp_event_loop.Run();
1058 });
1059
1060 event.Wait();
1061
1062 {
1063 // Now, spin up a client for 2 seconds.
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001064 MakePi2Client();
Austin Schuh4889b182020-11-18 19:11:56 -08001065
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001066 RunPi2Client(chrono::milliseconds(2050));
Austin Schuh4889b182020-11-18 19:11:56 -08001067
1068 // Confirm there is no detected duplicate packet.
1069 EXPECT_TRUE(pi2_client_statistics_fetcher.Fetch());
1070 EXPECT_EQ(pi2_client_statistics_fetcher->connections()
1071 ->Get(0)
1072 ->duplicate_packets(),
1073 0u);
1074
Austin Schuhe61d4382021-03-31 21:33:02 -07001075 EXPECT_EQ(pi2_client_statistics_fetcher->connections()
1076 ->Get(0)
1077 ->partial_deliveries(),
1078 0u);
1079
Austin Schuh4889b182020-11-18 19:11:56 -08001080 EXPECT_TRUE(ping_fetcher.Fetch());
1081 EXPECT_FALSE(unreliable_ping_fetcher.Fetch());
1082 EXPECT_EQ(ping_timestamp_count, 1);
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001083
1084 StopPi2Client();
Austin Schuh4889b182020-11-18 19:11:56 -08001085 }
1086
1087 {
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001088 // Now, spin up a client for 2 seconds.
1089 MakePi2Client();
Austin Schuh4889b182020-11-18 19:11:56 -08001090
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001091 RunPi2Client(chrono::milliseconds(5050));
Austin Schuh4889b182020-11-18 19:11:56 -08001092
1093 // Confirm we detect the duplicate packet correctly.
1094 EXPECT_TRUE(pi2_client_statistics_fetcher.Fetch());
1095 EXPECT_EQ(pi2_client_statistics_fetcher->connections()
1096 ->Get(0)
1097 ->duplicate_packets(),
1098 1u);
1099
Austin Schuhe61d4382021-03-31 21:33:02 -07001100 EXPECT_EQ(pi2_client_statistics_fetcher->connections()
1101 ->Get(0)
1102 ->partial_deliveries(),
1103 0u);
1104
Austin Schuh4889b182020-11-18 19:11:56 -08001105 EXPECT_EQ(ping_timestamp_count, 1);
1106 EXPECT_FALSE(ping_fetcher.Fetch());
1107 EXPECT_FALSE(unreliable_ping_fetcher.Fetch());
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001108
1109 StopPi2Client();
Austin Schuh4889b182020-11-18 19:11:56 -08001110 }
1111
1112 // Shut everyone else down
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001113 StopPi1Client();
1114 StopPi2Server();
Austin Schuh4889b182020-11-18 19:11:56 -08001115 pi1_remote_timestamp_event_loop.Exit();
1116 pi1_remote_timestamp_thread.join();
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001117 StopPi1Server();
Austin Schuh4889b182020-11-18 19:11:56 -08001118}
1119
1120// Tests that when a message is sent before the bridge starts up, but is
1121// configured as reliable, we forward it. Confirm this works across server
1122// resets.
Austin Schuh36a2c3e2021-02-18 22:28:38 -08001123TEST_P(MessageBridgeParameterizedTest, ReliableSentBeforeServerStartup) {
Austin Schuh4889b182020-11-18 19:11:56 -08001124 // Now do it for "raspberrypi2", the client.
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001125 OnPi2();
Austin Schuh4889b182020-11-18 19:11:56 -08001126
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001127 MakePi2Server();
1128 MakePi2Client();
Austin Schuh4889b182020-11-18 19:11:56 -08001129
Austin Schuhf466ab52021-02-16 22:00:38 -08001130 aos::ShmEventLoop receive_event_loop(&config.message());
Austin Schuh4889b182020-11-18 19:11:56 -08001131 aos::Fetcher<examples::Ping> ping_fetcher =
1132 receive_event_loop.MakeFetcher<examples::Ping>("/test");
1133 aos::Fetcher<examples::Ping> unreliable_ping_fetcher =
1134 receive_event_loop.MakeFetcher<examples::Ping>("/unreliable");
1135 aos::Fetcher<ClientStatistics> pi2_client_statistics_fetcher =
1136 receive_event_loop.MakeFetcher<ClientStatistics>("/pi2/aos");
1137
Austin Schuh4889b182020-11-18 19:11:56 -08001138 // Force ourselves to be "raspberrypi" and allocate everything.
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001139 OnPi1();
Austin Schuh4889b182020-11-18 19:11:56 -08001140
1141 FLAGS_application_name = "sender";
Austin Schuhf466ab52021-02-16 22:00:38 -08001142 aos::ShmEventLoop send_event_loop(&config.message());
Austin Schuh4889b182020-11-18 19:11:56 -08001143 aos::Sender<examples::Ping> ping_sender =
1144 send_event_loop.MakeSender<examples::Ping>("/test");
1145 {
1146 aos::Sender<examples::Ping>::Builder builder = ping_sender.MakeBuilder();
1147 examples::Ping::Builder ping_builder =
1148 builder.MakeBuilder<examples::Ping>();
1149 ping_builder.add_value(1);
1150 builder.Send(ping_builder.Finish());
1151 }
1152
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001153 MakePi1Client();
Austin Schuh4889b182020-11-18 19:11:56 -08001154
1155 FLAGS_application_name = "pi1_timestamp";
Austin Schuhf466ab52021-02-16 22:00:38 -08001156 aos::ShmEventLoop pi1_remote_timestamp_event_loop(&config.message());
Austin Schuh4889b182020-11-18 19:11:56 -08001157
1158 const size_t ping_channel_index = configuration::ChannelIndex(
1159 receive_event_loop.configuration(), ping_fetcher.channel());
1160
1161 std::atomic<int> ping_timestamp_count{0};
Austin Schuh36a2c3e2021-02-18 22:28:38 -08001162 const std::string channel_name =
1163 shared() ? "/pi1/aos/remote_timestamps/pi2"
1164 : "/pi1/aos/remote_timestamps/pi2/test/aos-examples-Ping";
Austin Schuh4889b182020-11-18 19:11:56 -08001165 pi1_remote_timestamp_event_loop.MakeWatcher(
Austin Schuh36a2c3e2021-02-18 22:28:38 -08001166 channel_name, [this, channel_name, ping_channel_index,
1167 &ping_timestamp_count](const RemoteMessage &header) {
1168 VLOG(1) << channel_name << " RemoteMessage "
Austin Schuh0de30f32020-12-06 12:44:28 -08001169 << aos::FlatbufferToJson(&header);
Austin Schuh20ac95d2020-12-05 17:24:19 -08001170 EXPECT_TRUE(header.has_boot_uuid());
Austin Schuh36a2c3e2021-02-18 22:28:38 -08001171 if (shared() && header.channel_index() != ping_channel_index) {
1172 return;
Austin Schuh4889b182020-11-18 19:11:56 -08001173 }
Austin Schuh36a2c3e2021-02-18 22:28:38 -08001174 CHECK_EQ(header.channel_index(), ping_channel_index);
1175 ++ping_timestamp_count;
Austin Schuh4889b182020-11-18 19:11:56 -08001176 });
1177
1178 // Before everything starts up, confirm there is no message.
1179 EXPECT_FALSE(ping_fetcher.Fetch());
1180 EXPECT_FALSE(unreliable_ping_fetcher.Fetch());
1181
1182 // Spin up the persistant pieces.
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001183 StartPi1Client();
1184 StartPi2Server();
1185 StartPi2Client();
Austin Schuh4889b182020-11-18 19:11:56 -08001186
1187 // Event used to wait for the timestamp counting thread to start.
1188 aos::Event event;
1189 std::thread pi1_remote_timestamp_thread(
1190 [&pi1_remote_timestamp_event_loop, &event]() {
1191 pi1_remote_timestamp_event_loop.OnRun([&event]() { event.Set(); });
1192 pi1_remote_timestamp_event_loop.Run();
1193 });
1194
1195 event.Wait();
1196
1197 {
1198 // Now, spin up a server for 2 seconds.
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001199 MakePi1Server();
Austin Schuh4889b182020-11-18 19:11:56 -08001200
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001201 RunPi1Server(chrono::milliseconds(2050));
Austin Schuh4889b182020-11-18 19:11:56 -08001202
1203 // Confirm there is no detected duplicate packet.
1204 EXPECT_TRUE(pi2_client_statistics_fetcher.Fetch());
1205 EXPECT_EQ(pi2_client_statistics_fetcher->connections()
1206 ->Get(0)
1207 ->duplicate_packets(),
1208 0u);
1209
Austin Schuhe61d4382021-03-31 21:33:02 -07001210 EXPECT_EQ(pi2_client_statistics_fetcher->connections()
1211 ->Get(0)
1212 ->partial_deliveries(),
1213 0u);
1214
Austin Schuh4889b182020-11-18 19:11:56 -08001215 EXPECT_TRUE(ping_fetcher.Fetch());
1216 EXPECT_FALSE(unreliable_ping_fetcher.Fetch());
1217 EXPECT_EQ(ping_timestamp_count, 1);
1218 LOG(INFO) << "Shutting down first pi1 MessageBridgeServer";
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001219
1220 StopPi1Server();
Austin Schuh4889b182020-11-18 19:11:56 -08001221 }
1222
1223 {
1224 // Now, spin up a second server for 2 seconds.
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001225 MakePi1Server();
Austin Schuh4889b182020-11-18 19:11:56 -08001226
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001227 RunPi1Server(chrono::milliseconds(2050));
Austin Schuh4889b182020-11-18 19:11:56 -08001228
1229 // Confirm we detect the duplicate packet correctly.
1230 EXPECT_TRUE(pi2_client_statistics_fetcher.Fetch());
1231 EXPECT_EQ(pi2_client_statistics_fetcher->connections()
1232 ->Get(0)
1233 ->duplicate_packets(),
1234 1u);
1235
Austin Schuhe61d4382021-03-31 21:33:02 -07001236
1237 EXPECT_EQ(pi2_client_statistics_fetcher->connections()
1238 ->Get(0)
1239 ->partial_deliveries(),
1240 0u);
1241
Austin Schuh4889b182020-11-18 19:11:56 -08001242 EXPECT_EQ(ping_timestamp_count, 1);
1243 EXPECT_FALSE(ping_fetcher.Fetch());
1244 EXPECT_FALSE(unreliable_ping_fetcher.Fetch());
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001245
1246 StopPi1Server();
Austin Schuh4889b182020-11-18 19:11:56 -08001247 }
1248
1249 // Shut everyone else down
Austin Schuh0a2f12f2021-01-08 22:48:29 -08001250 StopPi1Client();
1251 StopPi2Server();
1252 StopPi2Client();
Austin Schuh4889b182020-11-18 19:11:56 -08001253 pi1_remote_timestamp_event_loop.Exit();
1254 pi1_remote_timestamp_thread.join();
Austin Schuh4889b182020-11-18 19:11:56 -08001255}
1256
James Kuszmaulf4bf9fe2021-05-10 22:58:24 -07001257INSTANTIATE_TEST_SUITE_P(
Austin Schuh36a2c3e2021-02-18 22:28:38 -08001258 MessageBridgeTests, MessageBridgeParameterizedTest,
1259 ::testing::Values(
1260 Param{"message_bridge_test_combined_timestamps_common_config.json",
1261 true},
1262 Param{"message_bridge_test_common_config.json", false}));
1263
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001264} // namespace testing
1265} // namespace message_bridge
1266} // namespace aos