blob: 315a272900a01174aa6944d903cb4443527d193a [file] [log] [blame]
James Kuszmaul38735e82019-12-07 16:42:06 -08001#include "aos/events/logging/logger.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -08002
Austin Schuh315b96b2020-12-11 21:21:12 -08003#include "absl/strings/str_format.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -08004#include "aos/events/event_loop.h"
Austin Schuh01b4c352020-09-21 23:09:39 -07005#include "aos/events/message_counter.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -08006#include "aos/events/ping_lib.h"
7#include "aos/events/pong_lib.h"
8#include "aos/events/simulated_event_loop.h"
Austin Schuh0de30f32020-12-06 12:44:28 -08009#include "aos/network/remote_message_generated.h"
Austin Schuh8d7e0bb2020-10-02 17:57:00 -070010#include "aos/network/timestamp_generated.h"
Austin Schuhc243b422020-10-11 15:35:08 -070011#include "aos/testing/tmpdir.h"
Austin Schuh2f8fd752020-09-01 22:38:28 -070012#include "aos/util/file.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080013#include "glog/logging.h"
Austin Schuh6f3babe2020-01-26 20:34:50 -080014#include "gmock/gmock.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080015#include "gtest/gtest.h"
16
Austin Schuh3bd4c402020-11-06 18:19:06 -080017#ifdef LZMA
18#include "aos/events/logging/lzma_encoder.h"
19#endif
20
Austin Schuhe309d2a2019-11-29 13:25:21 -080021namespace aos {
22namespace logger {
23namespace testing {
24
25namespace chrono = std::chrono;
Austin Schuh0de30f32020-12-06 12:44:28 -080026using aos::message_bridge::RemoteMessage;
Austin Schuh01b4c352020-09-21 23:09:39 -070027using aos::testing::MessageCounter;
Austin Schuhe309d2a2019-11-29 13:25:21 -080028
29class LoggerTest : public ::testing::Test {
30 public:
31 LoggerTest()
32 : config_(
33 aos::configuration::ReadConfig("aos/events/pingpong_config.json")),
34 event_loop_factory_(&config_.message()),
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080035 ping_event_loop_(event_loop_factory_.MakeEventLoop("ping")),
Austin Schuhe309d2a2019-11-29 13:25:21 -080036 ping_(ping_event_loop_.get()),
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080037 pong_event_loop_(event_loop_factory_.MakeEventLoop("pong")),
Austin Schuhe309d2a2019-11-29 13:25:21 -080038 pong_(pong_event_loop_.get()) {}
39
40 // Config and factory.
41 aos::FlatbufferDetachedBuffer<aos::Configuration> config_;
42 SimulatedEventLoopFactory event_loop_factory_;
43
44 // Event loop and app for Ping
45 std::unique_ptr<EventLoop> ping_event_loop_;
46 Ping ping_;
47
48 // Event loop and app for Pong
49 std::unique_ptr<EventLoop> pong_event_loop_;
50 Pong pong_;
51};
52
Brian Silverman1f345222020-09-24 21:14:48 -070053using LoggerDeathTest = LoggerTest;
54
Austin Schuhe309d2a2019-11-29 13:25:21 -080055// Tests that we can startup at all. This confirms that the channels are all in
56// the config.
57TEST_F(LoggerTest, Starts) {
Austin Schuhc243b422020-10-11 15:35:08 -070058 const ::std::string tmpdir = aos::testing::TestTmpDir();
Austin Schuh2f8fd752020-09-01 22:38:28 -070059 const ::std::string base_name = tmpdir + "/logfile";
60 const ::std::string logfile = base_name + ".part0.bfbs";
Austin Schuhe309d2a2019-11-29 13:25:21 -080061 // Remove it.
62 unlink(logfile.c_str());
63
64 LOG(INFO) << "Logging data to " << logfile;
65
66 {
Austin Schuhe309d2a2019-11-29 13:25:21 -080067 std::unique_ptr<EventLoop> logger_event_loop =
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080068 event_loop_factory_.MakeEventLoop("logger");
Austin Schuhe309d2a2019-11-29 13:25:21 -080069
70 event_loop_factory_.RunFor(chrono::milliseconds(95));
71
Brian Silverman1f345222020-09-24 21:14:48 -070072 Logger logger(logger_event_loop.get());
73 logger.set_polling_period(std::chrono::milliseconds(100));
74 logger.StartLoggingLocalNamerOnRun(base_name);
Austin Schuhe309d2a2019-11-29 13:25:21 -080075 event_loop_factory_.RunFor(chrono::milliseconds(20000));
76 }
77
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080078 // Even though it doesn't make any difference here, exercise the logic for
79 // passing in a separate config.
80 LogReader reader(logfile, &config_.message());
Austin Schuhe309d2a2019-11-29 13:25:21 -080081
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080082 // Confirm that we can remap logged channels to point to new buses.
83 reader.RemapLoggedChannel<aos::examples::Ping>("/test", "/original");
Austin Schuhe309d2a2019-11-29 13:25:21 -080084
Austin Schuh15649d62019-12-28 16:36:38 -080085 // This sends out the fetched messages and advances time to the start of the
86 // log file.
James Kuszmaul84ff3e52020-01-03 19:48:53 -080087 reader.Register();
Austin Schuhe309d2a2019-11-29 13:25:21 -080088
Austin Schuh6f3babe2020-01-26 20:34:50 -080089 EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(nullptr));
James Kuszmaul84ff3e52020-01-03 19:48:53 -080090
Austin Schuhe309d2a2019-11-29 13:25:21 -080091 std::unique_ptr<EventLoop> test_event_loop =
James Kuszmaul84ff3e52020-01-03 19:48:53 -080092 reader.event_loop_factory()->MakeEventLoop("log_reader");
Austin Schuhe309d2a2019-11-29 13:25:21 -080093
94 int ping_count = 10;
95 int pong_count = 10;
96
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080097 // Confirm that the ping value matches in the remapped channel location.
98 test_event_loop->MakeWatcher("/original/test",
Austin Schuhe309d2a2019-11-29 13:25:21 -080099 [&ping_count](const examples::Ping &ping) {
100 EXPECT_EQ(ping.value(), ping_count + 1);
101 ++ping_count;
102 });
103 // Confirm that the ping and pong counts both match, and the value also
104 // matches.
105 test_event_loop->MakeWatcher(
106 "/test", [&pong_count, &ping_count](const examples::Pong &pong) {
107 EXPECT_EQ(pong.value(), pong_count + 1);
108 ++pong_count;
109 EXPECT_EQ(ping_count, pong_count);
110 });
111
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800112 reader.event_loop_factory()->RunFor(std::chrono::seconds(100));
Austin Schuhe309d2a2019-11-29 13:25:21 -0800113 EXPECT_EQ(ping_count, 2010);
Austin Schuhe309d2a2019-11-29 13:25:21 -0800114}
115
Brian Silverman1f345222020-09-24 21:14:48 -0700116// Tests calling StartLogging twice.
117TEST_F(LoggerDeathTest, ExtraStart) {
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800118 const ::std::string tmpdir = aos::testing::TestTmpDir();
Brian Silverman1f345222020-09-24 21:14:48 -0700119 const ::std::string base_name1 = tmpdir + "/logfile1";
120 const ::std::string logfile1 = base_name1 + ".part0.bfbs";
121 const ::std::string base_name2 = tmpdir + "/logfile2";
122 const ::std::string logfile2 = base_name2 + ".part0.bfbs";
123 unlink(logfile1.c_str());
124 unlink(logfile2.c_str());
125
126 LOG(INFO) << "Logging data to " << logfile1 << " then " << logfile2;
127
128 {
129 std::unique_ptr<EventLoop> logger_event_loop =
130 event_loop_factory_.MakeEventLoop("logger");
131
132 event_loop_factory_.RunFor(chrono::milliseconds(95));
133
134 Logger logger(logger_event_loop.get());
135 logger.set_polling_period(std::chrono::milliseconds(100));
136 logger_event_loop->OnRun(
137 [base_name1, base_name2, &logger_event_loop, &logger]() {
138 logger.StartLogging(std::make_unique<LocalLogNamer>(
139 base_name1, logger_event_loop->node()));
140 EXPECT_DEATH(logger.StartLogging(std::make_unique<LocalLogNamer>(
141 base_name2, logger_event_loop->node())),
142 "Already logging");
143 });
144 event_loop_factory_.RunFor(chrono::milliseconds(20000));
145 }
146}
147
148// Tests calling StopLogging twice.
149TEST_F(LoggerDeathTest, ExtraStop) {
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800150 const ::std::string tmpdir = aos::testing::TestTmpDir();
Brian Silverman1f345222020-09-24 21:14:48 -0700151 const ::std::string base_name = tmpdir + "/logfile";
152 const ::std::string logfile = base_name + ".part0.bfbs";
153 // Remove it.
154 unlink(logfile.c_str());
155
156 LOG(INFO) << "Logging data to " << logfile;
157
158 {
159 std::unique_ptr<EventLoop> logger_event_loop =
160 event_loop_factory_.MakeEventLoop("logger");
161
162 event_loop_factory_.RunFor(chrono::milliseconds(95));
163
164 Logger logger(logger_event_loop.get());
165 logger.set_polling_period(std::chrono::milliseconds(100));
166 logger_event_loop->OnRun([base_name, &logger_event_loop, &logger]() {
167 logger.StartLogging(std::make_unique<LocalLogNamer>(
168 base_name, logger_event_loop->node()));
169 logger.StopLogging(aos::monotonic_clock::min_time);
170 EXPECT_DEATH(logger.StopLogging(aos::monotonic_clock::min_time),
171 "Not logging right now");
172 });
173 event_loop_factory_.RunFor(chrono::milliseconds(20000));
174 }
175}
176
177// Tests that we can startup twice.
178TEST_F(LoggerTest, StartsTwice) {
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800179 const ::std::string tmpdir = aos::testing::TestTmpDir();
Brian Silverman1f345222020-09-24 21:14:48 -0700180 const ::std::string base_name1 = tmpdir + "/logfile1";
181 const ::std::string logfile1 = base_name1 + ".part0.bfbs";
182 const ::std::string base_name2 = tmpdir + "/logfile2";
183 const ::std::string logfile2 = base_name2 + ".part0.bfbs";
184 unlink(logfile1.c_str());
185 unlink(logfile2.c_str());
186
187 LOG(INFO) << "Logging data to " << logfile1 << " then " << logfile2;
188
189 {
190 std::unique_ptr<EventLoop> logger_event_loop =
191 event_loop_factory_.MakeEventLoop("logger");
192
193 event_loop_factory_.RunFor(chrono::milliseconds(95));
194
195 Logger logger(logger_event_loop.get());
196 logger.set_polling_period(std::chrono::milliseconds(100));
197 logger.StartLogging(
198 std::make_unique<LocalLogNamer>(base_name1, logger_event_loop->node()));
199 event_loop_factory_.RunFor(chrono::milliseconds(10000));
200 logger.StopLogging(logger_event_loop->monotonic_now());
201 event_loop_factory_.RunFor(chrono::milliseconds(10000));
202 logger.StartLogging(
203 std::make_unique<LocalLogNamer>(base_name2, logger_event_loop->node()));
204 event_loop_factory_.RunFor(chrono::milliseconds(10000));
205 }
206
207 for (const auto &logfile :
208 {std::make_tuple(logfile1, 10), std::make_tuple(logfile2, 2010)}) {
209 SCOPED_TRACE(std::get<0>(logfile));
210 LogReader reader(std::get<0>(logfile));
211 reader.Register();
212
213 EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(nullptr));
214
215 std::unique_ptr<EventLoop> test_event_loop =
216 reader.event_loop_factory()->MakeEventLoop("log_reader");
217
218 int ping_count = std::get<1>(logfile);
219 int pong_count = std::get<1>(logfile);
220
221 // Confirm that the ping and pong counts both match, and the value also
222 // matches.
223 test_event_loop->MakeWatcher("/test",
224 [&ping_count](const examples::Ping &ping) {
225 EXPECT_EQ(ping.value(), ping_count + 1);
226 ++ping_count;
227 });
228 test_event_loop->MakeWatcher(
229 "/test", [&pong_count, &ping_count](const examples::Pong &pong) {
230 EXPECT_EQ(pong.value(), pong_count + 1);
231 ++pong_count;
232 EXPECT_EQ(ping_count, pong_count);
233 });
234
235 reader.event_loop_factory()->RunFor(std::chrono::seconds(100));
236 EXPECT_EQ(ping_count, std::get<1>(logfile) + 1000);
237 }
238}
239
Austin Schuhfa895892020-01-07 20:07:41 -0800240// Tests that we can read and write rotated log files.
241TEST_F(LoggerTest, RotatedLogFile) {
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800242 const ::std::string tmpdir = aos::testing::TestTmpDir();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700243 const ::std::string base_name = tmpdir + "/logfile";
244 const ::std::string logfile0 = base_name + ".part0.bfbs";
245 const ::std::string logfile1 = base_name + ".part1.bfbs";
Austin Schuhfa895892020-01-07 20:07:41 -0800246 // Remove it.
247 unlink(logfile0.c_str());
248 unlink(logfile1.c_str());
249
250 LOG(INFO) << "Logging data to " << logfile0 << " and " << logfile1;
251
252 {
Austin Schuhfa895892020-01-07 20:07:41 -0800253 std::unique_ptr<EventLoop> logger_event_loop =
254 event_loop_factory_.MakeEventLoop("logger");
255
256 event_loop_factory_.RunFor(chrono::milliseconds(95));
257
Brian Silverman1f345222020-09-24 21:14:48 -0700258 Logger logger(logger_event_loop.get());
259 logger.set_polling_period(std::chrono::milliseconds(100));
260 logger.StartLoggingLocalNamerOnRun(base_name);
Austin Schuhfa895892020-01-07 20:07:41 -0800261 event_loop_factory_.RunFor(chrono::milliseconds(10000));
Austin Schuh2f8fd752020-09-01 22:38:28 -0700262 logger.Rotate();
Austin Schuhfa895892020-01-07 20:07:41 -0800263 event_loop_factory_.RunFor(chrono::milliseconds(10000));
264 }
265
Austin Schuh64fab802020-09-09 22:47:47 -0700266 {
267 // Confirm that the UUIDs match for both the parts and the logger, and the
268 // parts_index increments.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800269 std::vector<SizePrefixedFlatbufferVector<LogFileHeader>> log_header;
Austin Schuh64fab802020-09-09 22:47:47 -0700270 for (std::string_view f : {logfile0, logfile1}) {
Austin Schuh3bd4c402020-11-06 18:19:06 -0800271 log_header.emplace_back(ReadHeader(f).value());
Austin Schuh64fab802020-09-09 22:47:47 -0700272 }
273
Brian Silvermanae7c0332020-09-30 16:58:23 -0700274 EXPECT_EQ(log_header[0].message().log_event_uuid()->string_view(),
275 log_header[1].message().log_event_uuid()->string_view());
Austin Schuh64fab802020-09-09 22:47:47 -0700276 EXPECT_EQ(log_header[0].message().parts_uuid()->string_view(),
277 log_header[1].message().parts_uuid()->string_view());
278
279 EXPECT_EQ(log_header[0].message().parts_index(), 0);
280 EXPECT_EQ(log_header[1].message().parts_index(), 1);
281 }
282
Austin Schuhfa895892020-01-07 20:07:41 -0800283 // Even though it doesn't make any difference here, exercise the logic for
284 // passing in a separate config.
Austin Schuh287d43d2020-12-04 20:19:33 -0800285 LogReader reader(SortParts({logfile0, logfile1}), &config_.message());
Austin Schuhfa895892020-01-07 20:07:41 -0800286
287 // Confirm that we can remap logged channels to point to new buses.
288 reader.RemapLoggedChannel<aos::examples::Ping>("/test", "/original");
289
290 // This sends out the fetched messages and advances time to the start of the
291 // log file.
292 reader.Register();
293
Austin Schuh6f3babe2020-01-26 20:34:50 -0800294 EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(nullptr));
Austin Schuhfa895892020-01-07 20:07:41 -0800295
296 std::unique_ptr<EventLoop> test_event_loop =
297 reader.event_loop_factory()->MakeEventLoop("log_reader");
298
299 int ping_count = 10;
300 int pong_count = 10;
301
302 // Confirm that the ping value matches in the remapped channel location.
303 test_event_loop->MakeWatcher("/original/test",
304 [&ping_count](const examples::Ping &ping) {
305 EXPECT_EQ(ping.value(), ping_count + 1);
306 ++ping_count;
307 });
308 // Confirm that the ping and pong counts both match, and the value also
309 // matches.
310 test_event_loop->MakeWatcher(
311 "/test", [&pong_count, &ping_count](const examples::Pong &pong) {
312 EXPECT_EQ(pong.value(), pong_count + 1);
313 ++pong_count;
314 EXPECT_EQ(ping_count, pong_count);
315 });
316
317 reader.event_loop_factory()->RunFor(std::chrono::seconds(100));
318 EXPECT_EQ(ping_count, 2010);
319}
320
Austin Schuh4c4e0092019-12-22 16:18:03 -0800321// Tests that a large number of messages per second doesn't overwhelm writev.
322TEST_F(LoggerTest, ManyMessages) {
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800323 const ::std::string tmpdir = aos::testing::TestTmpDir();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700324 const ::std::string base_name = tmpdir + "/logfile";
325 const ::std::string logfile = base_name + ".part0.bfbs";
Austin Schuh4c4e0092019-12-22 16:18:03 -0800326 // Remove the log file.
327 unlink(logfile.c_str());
328
329 LOG(INFO) << "Logging data to " << logfile;
Austin Schuh4c3b9702020-08-30 11:34:55 -0700330 ping_.set_quiet(true);
Austin Schuh4c4e0092019-12-22 16:18:03 -0800331
332 {
Austin Schuh4c4e0092019-12-22 16:18:03 -0800333 std::unique_ptr<EventLoop> logger_event_loop =
334 event_loop_factory_.MakeEventLoop("logger");
335
336 std::unique_ptr<EventLoop> ping_spammer_event_loop =
337 event_loop_factory_.MakeEventLoop("ping_spammer");
338 aos::Sender<examples::Ping> ping_sender =
339 ping_spammer_event_loop->MakeSender<examples::Ping>("/test");
340
341 aos::TimerHandler *timer_handler =
342 ping_spammer_event_loop->AddTimer([&ping_sender]() {
343 aos::Sender<examples::Ping>::Builder builder =
344 ping_sender.MakeBuilder();
345 examples::Ping::Builder ping_builder =
346 builder.MakeBuilder<examples::Ping>();
347 CHECK(builder.Send(ping_builder.Finish()));
348 });
349
350 // 100 ms / 0.05 ms -> 2000 messages. Should be enough to crash it.
351 ping_spammer_event_loop->OnRun([&ping_spammer_event_loop, timer_handler]() {
352 timer_handler->Setup(ping_spammer_event_loop->monotonic_now(),
353 chrono::microseconds(50));
354 });
355
Brian Silverman1f345222020-09-24 21:14:48 -0700356 Logger logger(logger_event_loop.get());
357 logger.set_polling_period(std::chrono::milliseconds(100));
358 logger.StartLoggingLocalNamerOnRun(base_name);
Austin Schuh4c4e0092019-12-22 16:18:03 -0800359
360 event_loop_factory_.RunFor(chrono::milliseconds(1000));
361 }
362}
363
Austin Schuh315b96b2020-12-11 21:21:12 -0800364std::vector<std::string> MakeLogFiles(std::string logfile_base) {
365 return std::vector<std::string>(
366 {logfile_base + "_pi1_data.part0.bfbs",
367 logfile_base + "_pi2_data/test/aos.examples.Pong.part0.bfbs",
368 logfile_base + "_pi2_data/test/aos.examples.Pong.part1.bfbs",
369 logfile_base + "_pi2_data.part0.bfbs",
370 logfile_base + "_timestamps/pi1/aos/remote_timestamps/pi2/"
371 "aos.message_bridge.RemoteMessage.part0.bfbs",
372 logfile_base + "_timestamps/pi1/aos/remote_timestamps/pi2/"
373 "aos.message_bridge.RemoteMessage.part1.bfbs",
374 logfile_base + "_timestamps/pi2/aos/remote_timestamps/pi1/"
375 "aos.message_bridge.RemoteMessage.part0.bfbs",
376 logfile_base + "_timestamps/pi2/aos/remote_timestamps/pi1/"
377 "aos.message_bridge.RemoteMessage.part1.bfbs",
378 logfile_base +
379 "_pi1_data/pi1/aos/aos.message_bridge.Timestamp.part0.bfbs",
380 logfile_base +
381 "_pi1_data/pi1/aos/aos.message_bridge.Timestamp.part1.bfbs",
382 logfile_base +
383 "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part0.bfbs",
384 logfile_base +
385 "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part1.bfbs"});
386}
387
Austin Schuh15649d62019-12-28 16:36:38 -0800388class MultinodeLoggerTest : public ::testing::Test {
389 public:
390 MultinodeLoggerTest()
391 : config_(aos::configuration::ReadConfig(
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800392 "aos/events/logging/multinode_pingpong_config.json")),
Austin Schuhac0771c2020-01-07 18:36:30 -0800393 event_loop_factory_(&config_.message()),
Austin Schuhcde938c2020-02-02 17:30:07 -0800394 pi1_(
395 configuration::GetNode(event_loop_factory_.configuration(), "pi1")),
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700396 pi2_(
397 configuration::GetNode(event_loop_factory_.configuration(), "pi2")),
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800398 tmp_dir_(aos::testing::TestTmpDir()),
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700399 logfile_base_(tmp_dir_ + "/multi_logfile"),
Austin Schuh315b96b2020-12-11 21:21:12 -0800400 pi1_reboot_logfiles_(
Austin Schuh2f8fd752020-09-01 22:38:28 -0700401 {logfile_base_ + "_pi1_data.part0.bfbs",
402 logfile_base_ + "_pi2_data/test/aos.examples.Pong.part0.bfbs",
403 logfile_base_ + "_pi2_data/test/aos.examples.Pong.part1.bfbs",
Austin Schuh315b96b2020-12-11 21:21:12 -0800404 logfile_base_ + "_pi2_data/test/aos.examples.Pong.part2.bfbs",
Austin Schuh2f8fd752020-09-01 22:38:28 -0700405 logfile_base_ + "_timestamps/pi1/aos/remote_timestamps/pi2/"
Austin Schuh0de30f32020-12-06 12:44:28 -0800406 "aos.message_bridge.RemoteMessage.part0.bfbs",
Austin Schuh2f8fd752020-09-01 22:38:28 -0700407 logfile_base_ + "_timestamps/pi1/aos/remote_timestamps/pi2/"
Austin Schuh0de30f32020-12-06 12:44:28 -0800408 "aos.message_bridge.RemoteMessage.part1.bfbs",
Austin Schuh315b96b2020-12-11 21:21:12 -0800409 logfile_base_ + "_timestamps/pi1/aos/remote_timestamps/pi2/"
410 "aos.message_bridge.RemoteMessage.part2.bfbs",
Austin Schuh2f8fd752020-09-01 22:38:28 -0700411 logfile_base_ +
412 "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part0.bfbs",
413 logfile_base_ +
Austin Schuh315b96b2020-12-11 21:21:12 -0800414 "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part1.bfbs",
415 logfile_base_ +
416 "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part2.bfbs"}),
417 logfiles_(MakeLogFiles(logfile_base_)),
Austin Schuhc9049732020-12-21 22:27:15 -0800418 pi1_single_direction_logfiles_(
419 {logfile_base_ + "_pi1_data.part0.bfbs",
420 logfile_base_ + "_pi2_data/test/aos.examples.Pong.part0.bfbs",
421 logfile_base_ + "_timestamps/pi1/aos/remote_timestamps/pi2/"
422 "aos.message_bridge.RemoteMessage.part0.bfbs",
423 logfile_base_ +
424 "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part0.bfbs"}),
Austin Schuh2f8fd752020-09-01 22:38:28 -0700425 structured_logfiles_{
426 std::vector<std::string>{logfiles_[0]},
427 std::vector<std::string>{logfiles_[1], logfiles_[2]},
428 std::vector<std::string>{logfiles_[3]},
429 std::vector<std::string>{logfiles_[4], logfiles_[5]},
430 std::vector<std::string>{logfiles_[6], logfiles_[7]},
431 std::vector<std::string>{logfiles_[8], logfiles_[9]},
432 std::vector<std::string>{logfiles_[10], logfiles_[11]}},
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700433 ping_event_loop_(event_loop_factory_.MakeEventLoop("ping", pi1_)),
434 ping_(ping_event_loop_.get()),
435 pong_event_loop_(event_loop_factory_.MakeEventLoop("pong", pi2_)),
436 pong_(pong_event_loop_.get()) {
437 // Go through and remove the logfiles if they already exist.
438 for (const auto file : logfiles_) {
439 unlink(file.c_str());
Austin Schuhc6f8f1b2020-12-02 23:23:39 -0800440 unlink((file + ".xz").c_str());
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700441 }
442
Austin Schuh315b96b2020-12-11 21:21:12 -0800443 for (const auto file : MakeLogFiles("relogged")) {
444 unlink(file.c_str());
445 }
446
447 for (const auto file : pi1_reboot_logfiles_) {
448 unlink(file.c_str());
449 }
450
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700451 LOG(INFO) << "Logging data to " << logfiles_[0] << ", " << logfiles_[1]
452 << " and " << logfiles_[2];
453 }
454
455 struct LoggerState {
456 std::unique_ptr<EventLoop> event_loop;
457 std::unique_ptr<Logger> logger;
458 };
459
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700460 LoggerState MakeLogger(const Node *node,
461 SimulatedEventLoopFactory *factory = nullptr) {
462 if (factory == nullptr) {
463 factory = &event_loop_factory_;
464 }
465 return {factory->MakeEventLoop("logger", node), {}};
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700466 }
467
Austin Schuh3bd4c402020-11-06 18:19:06 -0800468 void StartLogger(LoggerState *logger, std::string logfile_base = "",
469 bool compress = false) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700470 if (logfile_base.empty()) {
471 logfile_base = logfile_base_;
472 }
473
Brian Silverman1f345222020-09-24 21:14:48 -0700474 logger->logger = std::make_unique<Logger>(logger->event_loop.get());
475 logger->logger->set_polling_period(std::chrono::milliseconds(100));
Austin Schuh3bd4c402020-11-06 18:19:06 -0800476 logger->event_loop->OnRun([logger, logfile_base, compress]() {
477 std::unique_ptr<MultiNodeLogNamer> namer =
478 std::make_unique<MultiNodeLogNamer>(
479 logfile_base, logger->event_loop->configuration(),
480 logger->event_loop->node());
481 if (compress) {
482#ifdef LZMA
483 namer->set_extension(".xz");
484 namer->set_encoder_factory(
485 []() { return std::make_unique<aos::logger::LzmaEncoder>(3); });
486#else
487 LOG(FATAL) << "Compression unsupported";
488#endif
489 }
490
491 logger->logger->StartLogging(std::move(namer));
Brian Silverman1f345222020-09-24 21:14:48 -0700492 });
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700493 }
Austin Schuh15649d62019-12-28 16:36:38 -0800494
Austin Schuh3bd4c402020-11-06 18:19:06 -0800495 void VerifyParts(const std::vector<LogFile> &sorted_parts,
496 const std::vector<std::string> &corrupted_parts = {}) {
497 EXPECT_EQ(sorted_parts.size(), 2u);
498
499 // Count up the number of UUIDs and make sure they are what we expect as a
500 // sanity check.
501 std::set<std::string> log_event_uuids;
502 std::set<std::string> parts_uuids;
503 std::set<std::string> both_uuids;
504
505 size_t missing_rt_count = 0;
506
507 std::vector<std::string> logger_nodes;
508 for (const LogFile &log_file : sorted_parts) {
509 EXPECT_FALSE(log_file.log_event_uuid.empty());
510 log_event_uuids.insert(log_file.log_event_uuid);
511 logger_nodes.emplace_back(log_file.logger_node);
512 both_uuids.insert(log_file.log_event_uuid);
513
514 for (const LogParts &part : log_file.parts) {
515 EXPECT_NE(part.monotonic_start_time, aos::monotonic_clock::min_time)
516 << ": " << part;
517 missing_rt_count +=
518 part.realtime_start_time == aos::realtime_clock::min_time;
519
520 EXPECT_TRUE(log_event_uuids.find(part.log_event_uuid) !=
521 log_event_uuids.end());
522 EXPECT_NE(part.node, "");
523 parts_uuids.insert(part.parts_uuid);
524 both_uuids.insert(part.parts_uuid);
525 }
526 }
527
528 // We won't have RT timestamps for 5 log files. We don't log the RT start
529 // time on remote nodes because we don't know it and would be guessing. And
530 // the log reader can actually do a better job.
531 EXPECT_EQ(missing_rt_count, 5u);
532
533 EXPECT_EQ(log_event_uuids.size(), 2u);
534 EXPECT_EQ(parts_uuids.size(), ToLogReaderVector(sorted_parts).size());
535 EXPECT_EQ(log_event_uuids.size() + parts_uuids.size(), both_uuids.size());
536
537 // Test that each list of parts is in order. Don't worry about the ordering
538 // between part file lists though.
539 // (inner vectors all need to be in order, but outer one doesn't matter).
540 EXPECT_THAT(ToLogReaderVector(sorted_parts),
541 ::testing::UnorderedElementsAreArray(structured_logfiles_));
542
543 EXPECT_THAT(logger_nodes, ::testing::UnorderedElementsAre("pi1", "pi2"));
544
545 EXPECT_NE(sorted_parts[0].realtime_start_time,
546 aos::realtime_clock::min_time);
547 EXPECT_NE(sorted_parts[1].realtime_start_time,
548 aos::realtime_clock::min_time);
549
550 EXPECT_NE(sorted_parts[0].monotonic_start_time,
551 aos::monotonic_clock::min_time);
552 EXPECT_NE(sorted_parts[1].monotonic_start_time,
553 aos::monotonic_clock::min_time);
554
555 EXPECT_THAT(sorted_parts[0].corrupted, ::testing::Eq(corrupted_parts));
556 EXPECT_THAT(sorted_parts[1].corrupted, ::testing::Eq(corrupted_parts));
557 }
558
Austin Schuhc9049732020-12-21 22:27:15 -0800559 void ConfirmReadable(const std::vector<std::string> &files) {
560 LogReader reader(SortParts(files));
561
562 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
563 reader.Register(&log_reader_factory);
564
565 log_reader_factory.Run();
566
567 reader.Deregister();
568 }
569
Austin Schuh3bd4c402020-11-06 18:19:06 -0800570 void AddExtension(std::string_view extension) {
571 std::transform(logfiles_.begin(), logfiles_.end(), logfiles_.begin(),
572 [extension](const std::string &in) {
573 return absl::StrCat(in, extension);
574 });
575
576 std::transform(structured_logfiles_.begin(), structured_logfiles_.end(),
577 structured_logfiles_.begin(),
578 [extension](std::vector<std::string> in) {
579 std::transform(in.begin(), in.end(), in.begin(),
580 [extension](const std::string &in_str) {
581 return absl::StrCat(in_str, extension);
582 });
583 return in;
584 });
585 }
586
Austin Schuh15649d62019-12-28 16:36:38 -0800587 // Config and factory.
588 aos::FlatbufferDetachedBuffer<aos::Configuration> config_;
589 SimulatedEventLoopFactory event_loop_factory_;
590
Austin Schuhcde938c2020-02-02 17:30:07 -0800591 const Node *pi1_;
592 const Node *pi2_;
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700593
594 std::string tmp_dir_;
595 std::string logfile_base_;
Austin Schuh315b96b2020-12-11 21:21:12 -0800596 std::vector<std::string> pi1_reboot_logfiles_;
Austin Schuh2f8fd752020-09-01 22:38:28 -0700597 std::vector<std::string> logfiles_;
Austin Schuhc9049732020-12-21 22:27:15 -0800598 std::vector<std::string> pi1_single_direction_logfiles_;
Austin Schuh2f8fd752020-09-01 22:38:28 -0700599
600 std::vector<std::vector<std::string>> structured_logfiles_;
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700601
602 std::unique_ptr<EventLoop> ping_event_loop_;
603 Ping ping_;
604 std::unique_ptr<EventLoop> pong_event_loop_;
605 Pong pong_;
Austin Schuh15649d62019-12-28 16:36:38 -0800606};
607
Austin Schuh391e3172020-09-01 22:48:18 -0700608// Counts the number of messages on a channel. Returns (channel name, channel
609// type, count) for every message matching matcher()
610std::vector<std::tuple<std::string, std::string, int>> CountChannelsMatching(
Austin Schuh6f3babe2020-01-26 20:34:50 -0800611 std::string_view filename,
612 std::function<bool(const MessageHeader *)> matcher) {
613 MessageReader message_reader(filename);
614 std::vector<int> counts(
615 message_reader.log_file_header()->configuration()->channels()->size(), 0);
Austin Schuh15649d62019-12-28 16:36:38 -0800616
Austin Schuh6f3babe2020-01-26 20:34:50 -0800617 while (true) {
Austin Schuhadd6eb32020-11-09 21:24:26 -0800618 std::optional<SizePrefixedFlatbufferVector<MessageHeader>> msg =
Austin Schuh6f3babe2020-01-26 20:34:50 -0800619 message_reader.ReadMessage();
620 if (!msg) {
621 break;
622 }
623
624 if (matcher(&msg.value().message())) {
625 counts[msg.value().message().channel_index()]++;
626 }
627 }
628
Austin Schuh391e3172020-09-01 22:48:18 -0700629 std::vector<std::tuple<std::string, std::string, int>> result;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800630 int channel = 0;
631 for (size_t i = 0; i < counts.size(); ++i) {
632 if (counts[i] != 0) {
Austin Schuh391e3172020-09-01 22:48:18 -0700633 const Channel *channel =
634 message_reader.log_file_header()->configuration()->channels()->Get(i);
635 result.push_back(std::make_tuple(channel->name()->str(),
636 channel->type()->str(), counts[i]));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800637 }
638 ++channel;
639 }
640
641 return result;
642}
643
644// Counts the number of messages (channel, count) for all data messages.
Austin Schuh391e3172020-09-01 22:48:18 -0700645std::vector<std::tuple<std::string, std::string, int>> CountChannelsData(
646 std::string_view filename) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800647 return CountChannelsMatching(filename, [](const MessageHeader *msg) {
648 if (msg->has_data()) {
649 CHECK(!msg->has_monotonic_remote_time());
650 CHECK(!msg->has_realtime_remote_time());
651 CHECK(!msg->has_remote_queue_index());
652 return true;
653 }
654 return false;
655 });
656}
657
658// Counts the number of messages (channel, count) for all timestamp messages.
Austin Schuh391e3172020-09-01 22:48:18 -0700659std::vector<std::tuple<std::string, std::string, int>> CountChannelsTimestamp(
Austin Schuh6f3babe2020-01-26 20:34:50 -0800660 std::string_view filename) {
661 return CountChannelsMatching(filename, [](const MessageHeader *msg) {
662 if (!msg->has_data()) {
663 CHECK(msg->has_monotonic_remote_time());
664 CHECK(msg->has_realtime_remote_time());
665 CHECK(msg->has_remote_queue_index());
666 return true;
667 }
668 return false;
669 });
670}
671
Austin Schuhcde938c2020-02-02 17:30:07 -0800672// Tests that we can write and read simple multi-node log files.
673TEST_F(MultinodeLoggerTest, SimpleMultiNode) {
Austin Schuh15649d62019-12-28 16:36:38 -0800674 {
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700675 LoggerState pi1_logger = MakeLogger(pi1_);
676 LoggerState pi2_logger = MakeLogger(pi2_);
Austin Schuh15649d62019-12-28 16:36:38 -0800677
678 event_loop_factory_.RunFor(chrono::milliseconds(95));
679
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700680 StartLogger(&pi1_logger);
681 StartLogger(&pi2_logger);
Austin Schuhcde938c2020-02-02 17:30:07 -0800682
Austin Schuh15649d62019-12-28 16:36:38 -0800683 event_loop_factory_.RunFor(chrono::milliseconds(20000));
684 }
685
Austin Schuh6f3babe2020-01-26 20:34:50 -0800686 {
Austin Schuh64fab802020-09-09 22:47:47 -0700687 std::set<std::string> logfile_uuids;
688 std::set<std::string> parts_uuids;
689 // Confirm that we have the expected number of UUIDs for both the logfile
690 // UUIDs and parts UUIDs.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800691 std::vector<SizePrefixedFlatbufferVector<LogFileHeader>> log_header;
Austin Schuh64fab802020-09-09 22:47:47 -0700692 for (std::string_view f : logfiles_) {
Austin Schuh3bd4c402020-11-06 18:19:06 -0800693 log_header.emplace_back(ReadHeader(f).value());
Brian Silvermanae7c0332020-09-30 16:58:23 -0700694 logfile_uuids.insert(log_header.back().message().log_event_uuid()->str());
Austin Schuh64fab802020-09-09 22:47:47 -0700695 parts_uuids.insert(log_header.back().message().parts_uuid()->str());
696 }
Austin Schuh15649d62019-12-28 16:36:38 -0800697
Austin Schuh64fab802020-09-09 22:47:47 -0700698 EXPECT_EQ(logfile_uuids.size(), 2u);
699 EXPECT_EQ(parts_uuids.size(), 7u);
700
701 // And confirm everything is on the correct node.
702 EXPECT_EQ(log_header[0].message().node()->name()->string_view(), "pi1");
703 EXPECT_EQ(log_header[1].message().node()->name()->string_view(), "pi2");
704 EXPECT_EQ(log_header[2].message().node()->name()->string_view(), "pi2");
705 EXPECT_EQ(log_header[3].message().node()->name()->string_view(), "pi2");
706 EXPECT_EQ(log_header[4].message().node()->name()->string_view(), "pi2");
707 EXPECT_EQ(log_header[5].message().node()->name()->string_view(), "pi2");
708 EXPECT_EQ(log_header[6].message().node()->name()->string_view(), "pi1");
709 EXPECT_EQ(log_header[7].message().node()->name()->string_view(), "pi1");
710 EXPECT_EQ(log_header[8].message().node()->name()->string_view(), "pi1");
711 EXPECT_EQ(log_header[9].message().node()->name()->string_view(), "pi1");
712 EXPECT_EQ(log_header[10].message().node()->name()->string_view(), "pi2");
713 EXPECT_EQ(log_header[11].message().node()->name()->string_view(), "pi2");
714
715 // And the parts index matches.
716 EXPECT_EQ(log_header[0].message().parts_index(), 0);
717 EXPECT_EQ(log_header[1].message().parts_index(), 0);
718 EXPECT_EQ(log_header[2].message().parts_index(), 1);
719 EXPECT_EQ(log_header[3].message().parts_index(), 0);
720 EXPECT_EQ(log_header[4].message().parts_index(), 0);
721 EXPECT_EQ(log_header[5].message().parts_index(), 1);
722 EXPECT_EQ(log_header[6].message().parts_index(), 0);
723 EXPECT_EQ(log_header[7].message().parts_index(), 1);
724 EXPECT_EQ(log_header[8].message().parts_index(), 0);
725 EXPECT_EQ(log_header[9].message().parts_index(), 1);
726 EXPECT_EQ(log_header[10].message().parts_index(), 0);
727 EXPECT_EQ(log_header[11].message().parts_index(), 1);
728 }
729
730 {
Austin Schuh391e3172020-09-01 22:48:18 -0700731 using ::testing::UnorderedElementsAre;
732
Austin Schuh6f3babe2020-01-26 20:34:50 -0800733 // Timing reports, pings
Austin Schuh2f8fd752020-09-01 22:38:28 -0700734 EXPECT_THAT(
735 CountChannelsData(logfiles_[0]),
736 UnorderedElementsAre(
737 std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 200),
738 std::make_tuple("/pi1/aos", "aos.timing.Report", 40),
Austin Schuh20ac95d2020-12-05 17:24:19 -0800739 std::make_tuple("/test", "aos.examples.Ping", 2001)))
740 << " : " << logfiles_[0];
Austin Schuh6f3babe2020-01-26 20:34:50 -0800741 // Timestamps for pong
Austin Schuh2f8fd752020-09-01 22:38:28 -0700742 EXPECT_THAT(
743 CountChannelsTimestamp(logfiles_[0]),
744 UnorderedElementsAre(
745 std::make_tuple("/test", "aos.examples.Pong", 2001),
Austin Schuh20ac95d2020-12-05 17:24:19 -0800746 std::make_tuple("/pi2/aos", "aos.message_bridge.Timestamp", 200)))
747 << " : " << logfiles_[0];
Austin Schuh6f3babe2020-01-26 20:34:50 -0800748
749 // Pong data.
Austin Schuh20ac95d2020-12-05 17:24:19 -0800750 EXPECT_THAT(
751 CountChannelsData(logfiles_[1]),
752 UnorderedElementsAre(std::make_tuple("/test", "aos.examples.Pong", 91)))
753 << " : " << logfiles_[1];
Austin Schuh2f8fd752020-09-01 22:38:28 -0700754 EXPECT_THAT(CountChannelsData(logfiles_[2]),
755 UnorderedElementsAre(
Austin Schuh20ac95d2020-12-05 17:24:19 -0800756 std::make_tuple("/test", "aos.examples.Pong", 1910)))
757 << " : " << logfiles_[1];
Austin Schuh391e3172020-09-01 22:48:18 -0700758
Austin Schuh6f3babe2020-01-26 20:34:50 -0800759 // No timestamps
Austin Schuh20ac95d2020-12-05 17:24:19 -0800760 EXPECT_THAT(CountChannelsTimestamp(logfiles_[1]), UnorderedElementsAre())
761 << " : " << logfiles_[1];
762 EXPECT_THAT(CountChannelsTimestamp(logfiles_[2]), UnorderedElementsAre())
763 << " : " << logfiles_[2];
Austin Schuh6f3babe2020-01-26 20:34:50 -0800764
765 // Timing reports and pongs.
Austin Schuh2f8fd752020-09-01 22:38:28 -0700766 EXPECT_THAT(
767 CountChannelsData(logfiles_[3]),
768 UnorderedElementsAre(
769 std::make_tuple("/pi2/aos", "aos.message_bridge.Timestamp", 200),
770 std::make_tuple("/pi2/aos", "aos.timing.Report", 40),
Austin Schuh20ac95d2020-12-05 17:24:19 -0800771 std::make_tuple("/test", "aos.examples.Pong", 2001)))
772 << " : " << logfiles_[3];
Austin Schuh6f3babe2020-01-26 20:34:50 -0800773 // And ping timestamps.
Austin Schuh2f8fd752020-09-01 22:38:28 -0700774 EXPECT_THAT(
775 CountChannelsTimestamp(logfiles_[3]),
776 UnorderedElementsAre(
777 std::make_tuple("/test", "aos.examples.Ping", 2001),
Austin Schuh20ac95d2020-12-05 17:24:19 -0800778 std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 200)))
779 << " : " << logfiles_[3];
Austin Schuh2f8fd752020-09-01 22:38:28 -0700780
781 // Timestamps from pi2 on pi1, and the other way.
Austin Schuh20ac95d2020-12-05 17:24:19 -0800782 EXPECT_THAT(CountChannelsData(logfiles_[4]), UnorderedElementsAre())
783 << " : " << logfiles_[4];
784 EXPECT_THAT(CountChannelsData(logfiles_[5]), UnorderedElementsAre())
785 << " : " << logfiles_[5];
786 EXPECT_THAT(CountChannelsData(logfiles_[6]), UnorderedElementsAre())
787 << " : " << logfiles_[6];
788 EXPECT_THAT(CountChannelsData(logfiles_[7]), UnorderedElementsAre())
789 << " : " << logfiles_[7];
Austin Schuh2f8fd752020-09-01 22:38:28 -0700790 EXPECT_THAT(
791 CountChannelsTimestamp(logfiles_[4]),
792 UnorderedElementsAre(
Austin Schuh20ac95d2020-12-05 17:24:19 -0800793 std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 9),
794 std::make_tuple("/test", "aos.examples.Ping", 91)))
795 << " : " << logfiles_[4];
Austin Schuh2f8fd752020-09-01 22:38:28 -0700796 EXPECT_THAT(
797 CountChannelsTimestamp(logfiles_[5]),
798 UnorderedElementsAre(
Austin Schuh20ac95d2020-12-05 17:24:19 -0800799 std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 191),
800 std::make_tuple("/test", "aos.examples.Ping", 1910)))
801 << " : " << logfiles_[5];
Austin Schuh2f8fd752020-09-01 22:38:28 -0700802 EXPECT_THAT(CountChannelsTimestamp(logfiles_[6]),
803 UnorderedElementsAre(std::make_tuple(
Austin Schuh20ac95d2020-12-05 17:24:19 -0800804 "/pi2/aos", "aos.message_bridge.Timestamp", 9)))
805 << " : " << logfiles_[6];
Austin Schuh2f8fd752020-09-01 22:38:28 -0700806 EXPECT_THAT(CountChannelsTimestamp(logfiles_[7]),
807 UnorderedElementsAre(std::make_tuple(
Austin Schuh20ac95d2020-12-05 17:24:19 -0800808 "/pi2/aos", "aos.message_bridge.Timestamp", 191)))
809 << " : " << logfiles_[7];
Austin Schuh2f8fd752020-09-01 22:38:28 -0700810
811 // And then test that the remotely logged timestamp data files only have
812 // timestamps in them.
Austin Schuh20ac95d2020-12-05 17:24:19 -0800813 EXPECT_THAT(CountChannelsTimestamp(logfiles_[8]), UnorderedElementsAre())
814 << " : " << logfiles_[8];
815 EXPECT_THAT(CountChannelsTimestamp(logfiles_[9]), UnorderedElementsAre())
816 << " : " << logfiles_[9];
817 EXPECT_THAT(CountChannelsTimestamp(logfiles_[10]), UnorderedElementsAre())
818 << " : " << logfiles_[10];
819 EXPECT_THAT(CountChannelsTimestamp(logfiles_[11]), UnorderedElementsAre())
820 << " : " << logfiles_[11];
Austin Schuh2f8fd752020-09-01 22:38:28 -0700821
822 EXPECT_THAT(CountChannelsData(logfiles_[8]),
823 UnorderedElementsAre(std::make_tuple(
Austin Schuh20ac95d2020-12-05 17:24:19 -0800824 "/pi1/aos", "aos.message_bridge.Timestamp", 9)))
825 << " : " << logfiles_[8];
Austin Schuh2f8fd752020-09-01 22:38:28 -0700826 EXPECT_THAT(CountChannelsData(logfiles_[9]),
827 UnorderedElementsAre(std::make_tuple(
Austin Schuh20ac95d2020-12-05 17:24:19 -0800828 "/pi1/aos", "aos.message_bridge.Timestamp", 191)))
829 << " : " << logfiles_[9];
Austin Schuh2f8fd752020-09-01 22:38:28 -0700830
831 EXPECT_THAT(CountChannelsData(logfiles_[10]),
832 UnorderedElementsAre(std::make_tuple(
Austin Schuh20ac95d2020-12-05 17:24:19 -0800833 "/pi2/aos", "aos.message_bridge.Timestamp", 9)))
834 << " : " << logfiles_[10];
Austin Schuh2f8fd752020-09-01 22:38:28 -0700835 EXPECT_THAT(CountChannelsData(logfiles_[11]),
836 UnorderedElementsAre(std::make_tuple(
Austin Schuh20ac95d2020-12-05 17:24:19 -0800837 "/pi2/aos", "aos.message_bridge.Timestamp", 191)))
838 << " : " << logfiles_[11];
Austin Schuh6f3babe2020-01-26 20:34:50 -0800839 }
840
Austin Schuh287d43d2020-12-04 20:19:33 -0800841 LogReader reader(SortParts(logfiles_));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800842
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700843 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
Austin Schuh6331ef92020-01-07 18:28:09 -0800844 log_reader_factory.set_send_delay(chrono::microseconds(0));
Austin Schuh15649d62019-12-28 16:36:38 -0800845
846 // This sends out the fetched messages and advances time to the start of the
847 // log file.
Austin Schuh6331ef92020-01-07 18:28:09 -0800848 reader.Register(&log_reader_factory);
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800849
Austin Schuhac0771c2020-01-07 18:36:30 -0800850 const Node *pi1 =
851 configuration::GetNode(log_reader_factory.configuration(), "pi1");
Austin Schuh6f3babe2020-01-26 20:34:50 -0800852 const Node *pi2 =
853 configuration::GetNode(log_reader_factory.configuration(), "pi2");
Austin Schuhac0771c2020-01-07 18:36:30 -0800854
Austin Schuh2f8fd752020-09-01 22:38:28 -0700855 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi1) << " pi1";
856 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi2) << " pi2";
857 LOG(INFO) << "now pi1 "
858 << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now();
859 LOG(INFO) << "now pi2 "
860 << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now();
861
Austin Schuh6f3babe2020-01-26 20:34:50 -0800862 EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(pi1, pi2));
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800863
864 reader.event_loop_factory()->set_send_delay(chrono::microseconds(0));
Austin Schuh15649d62019-12-28 16:36:38 -0800865
Austin Schuh6f3babe2020-01-26 20:34:50 -0800866 std::unique_ptr<EventLoop> pi1_event_loop =
Austin Schuhac0771c2020-01-07 18:36:30 -0800867 log_reader_factory.MakeEventLoop("test", pi1);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800868 std::unique_ptr<EventLoop> pi2_event_loop =
869 log_reader_factory.MakeEventLoop("test", pi2);
Austin Schuh15649d62019-12-28 16:36:38 -0800870
Austin Schuh6f3babe2020-01-26 20:34:50 -0800871 int pi1_ping_count = 10;
872 int pi2_ping_count = 10;
873 int pi1_pong_count = 10;
874 int pi2_pong_count = 10;
Austin Schuh15649d62019-12-28 16:36:38 -0800875
876 // Confirm that the ping value matches.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800877 pi1_event_loop->MakeWatcher(
878 "/test", [&pi1_ping_count, &pi1_event_loop](const examples::Ping &ping) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700879 VLOG(1) << "Pi1 ping " << FlatbufferToJson(&ping) << " at "
Austin Schuh6f3babe2020-01-26 20:34:50 -0800880 << pi1_event_loop->context().monotonic_remote_time << " -> "
881 << pi1_event_loop->context().monotonic_event_time;
882 EXPECT_EQ(ping.value(), pi1_ping_count + 1);
883 EXPECT_EQ(pi1_event_loop->context().monotonic_remote_time,
884 pi1_ping_count * chrono::milliseconds(10) +
885 monotonic_clock::epoch());
886 EXPECT_EQ(pi1_event_loop->context().realtime_remote_time,
887 pi1_ping_count * chrono::milliseconds(10) +
888 realtime_clock::epoch());
889 EXPECT_EQ(pi1_event_loop->context().monotonic_remote_time,
890 pi1_event_loop->context().monotonic_event_time);
891 EXPECT_EQ(pi1_event_loop->context().realtime_remote_time,
892 pi1_event_loop->context().realtime_event_time);
Austin Schuh15649d62019-12-28 16:36:38 -0800893
Austin Schuh6f3babe2020-01-26 20:34:50 -0800894 ++pi1_ping_count;
895 });
896 pi2_event_loop->MakeWatcher(
897 "/test", [&pi2_ping_count, &pi2_event_loop](const examples::Ping &ping) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700898 VLOG(1) << "Pi2 ping " << FlatbufferToJson(&ping) << " at "
Austin Schuh6f3babe2020-01-26 20:34:50 -0800899 << pi2_event_loop->context().monotonic_remote_time << " -> "
900 << pi2_event_loop->context().monotonic_event_time;
901 EXPECT_EQ(ping.value(), pi2_ping_count + 1);
902
903 EXPECT_EQ(pi2_event_loop->context().monotonic_remote_time,
904 pi2_ping_count * chrono::milliseconds(10) +
905 monotonic_clock::epoch());
906 EXPECT_EQ(pi2_event_loop->context().realtime_remote_time,
907 pi2_ping_count * chrono::milliseconds(10) +
908 realtime_clock::epoch());
909 EXPECT_EQ(pi2_event_loop->context().monotonic_remote_time +
910 chrono::microseconds(150),
911 pi2_event_loop->context().monotonic_event_time);
912 EXPECT_EQ(pi2_event_loop->context().realtime_remote_time +
913 chrono::microseconds(150),
914 pi2_event_loop->context().realtime_event_time);
915 ++pi2_ping_count;
Austin Schuh15649d62019-12-28 16:36:38 -0800916 });
917
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700918 constexpr ssize_t kQueueIndexOffset = -9;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800919 // Confirm that the ping and pong counts both match, and the value also
920 // matches.
921 pi1_event_loop->MakeWatcher(
922 "/test", [&pi1_event_loop, &pi1_ping_count,
923 &pi1_pong_count](const examples::Pong &pong) {
924 VLOG(1) << "Pi1 pong " << FlatbufferToJson(&pong) << " at "
925 << pi1_event_loop->context().monotonic_remote_time << " -> "
926 << pi1_event_loop->context().monotonic_event_time;
927
928 EXPECT_EQ(pi1_event_loop->context().remote_queue_index,
929 pi1_pong_count + kQueueIndexOffset);
930 EXPECT_EQ(pi1_event_loop->context().monotonic_remote_time,
931 chrono::microseconds(200) +
932 pi1_pong_count * chrono::milliseconds(10) +
933 monotonic_clock::epoch());
934 EXPECT_EQ(pi1_event_loop->context().realtime_remote_time,
935 chrono::microseconds(200) +
936 pi1_pong_count * chrono::milliseconds(10) +
937 realtime_clock::epoch());
938
939 EXPECT_EQ(pi1_event_loop->context().monotonic_remote_time +
940 chrono::microseconds(150),
941 pi1_event_loop->context().monotonic_event_time);
942 EXPECT_EQ(pi1_event_loop->context().realtime_remote_time +
943 chrono::microseconds(150),
944 pi1_event_loop->context().realtime_event_time);
945
946 EXPECT_EQ(pong.value(), pi1_pong_count + 1);
947 ++pi1_pong_count;
948 EXPECT_EQ(pi1_ping_count, pi1_pong_count);
949 });
950 pi2_event_loop->MakeWatcher(
951 "/test", [&pi2_event_loop, &pi2_ping_count,
952 &pi2_pong_count](const examples::Pong &pong) {
953 VLOG(1) << "Pi2 pong " << FlatbufferToJson(&pong) << " at "
954 << pi2_event_loop->context().monotonic_remote_time << " -> "
955 << pi2_event_loop->context().monotonic_event_time;
956
957 EXPECT_EQ(pi2_event_loop->context().remote_queue_index,
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700958 pi2_pong_count + kQueueIndexOffset);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800959
960 EXPECT_EQ(pi2_event_loop->context().monotonic_remote_time,
961 chrono::microseconds(200) +
962 pi2_pong_count * chrono::milliseconds(10) +
963 monotonic_clock::epoch());
964 EXPECT_EQ(pi2_event_loop->context().realtime_remote_time,
965 chrono::microseconds(200) +
966 pi2_pong_count * chrono::milliseconds(10) +
967 realtime_clock::epoch());
968
969 EXPECT_EQ(pi2_event_loop->context().monotonic_remote_time,
970 pi2_event_loop->context().monotonic_event_time);
971 EXPECT_EQ(pi2_event_loop->context().realtime_remote_time,
972 pi2_event_loop->context().realtime_event_time);
973
974 EXPECT_EQ(pong.value(), pi2_pong_count + 1);
975 ++pi2_pong_count;
976 EXPECT_EQ(pi2_ping_count, pi2_pong_count);
977 });
978
979 log_reader_factory.Run();
980 EXPECT_EQ(pi1_ping_count, 2010);
981 EXPECT_EQ(pi2_ping_count, 2010);
982 EXPECT_EQ(pi1_pong_count, 2010);
983 EXPECT_EQ(pi2_pong_count, 2010);
Austin Schuh6331ef92020-01-07 18:28:09 -0800984
985 reader.Deregister();
Austin Schuh15649d62019-12-28 16:36:38 -0800986}
987
James Kuszmaul46d82582020-05-09 19:50:09 -0700988typedef MultinodeLoggerTest MultinodeLoggerDeathTest;
989
990// Test that if we feed the replay with a mismatched node list that we die on
991// the LogReader constructor.
992TEST_F(MultinodeLoggerDeathTest, MultiNodeBadReplayConfig) {
James Kuszmaul46d82582020-05-09 19:50:09 -0700993 {
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700994 LoggerState pi1_logger = MakeLogger(pi1_);
995 LoggerState pi2_logger = MakeLogger(pi2_);
James Kuszmaul46d82582020-05-09 19:50:09 -0700996
997 event_loop_factory_.RunFor(chrono::milliseconds(95));
998
James Kuszmaulbb28ef22020-05-09 22:30:38 -0700999 StartLogger(&pi1_logger);
1000 StartLogger(&pi2_logger);
James Kuszmaul46d82582020-05-09 19:50:09 -07001001
James Kuszmaul46d82582020-05-09 19:50:09 -07001002 event_loop_factory_.RunFor(chrono::milliseconds(20000));
1003 }
1004
1005 // Test that, if we add an additional node to the replay config that the
1006 // logger complains about the mismatch in number of nodes.
1007 FlatbufferDetachedBuffer<Configuration> extra_nodes_config =
1008 configuration::MergeWithConfig(&config_.message(), R"({
1009 "nodes": [
1010 {
1011 "name": "extra-node"
1012 }
1013 ]
1014 }
1015 )");
1016
Austin Schuh287d43d2020-12-04 20:19:33 -08001017 const std::vector<LogFile> sorted_parts = SortParts(logfiles_);
1018 EXPECT_DEATH(LogReader(sorted_parts, &extra_nodes_config.message()),
James Kuszmaul46d82582020-05-09 19:50:09 -07001019 "Log file and replay config need to have matching nodes lists.");
James Kuszmaul46d82582020-05-09 19:50:09 -07001020}
1021
Austin Schuhcde938c2020-02-02 17:30:07 -08001022// Tests that we can read log files where they don't start at the same monotonic
1023// time.
1024TEST_F(MultinodeLoggerTest, StaggeredStart) {
Austin Schuhcde938c2020-02-02 17:30:07 -08001025 {
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001026 LoggerState pi1_logger = MakeLogger(pi1_);
1027 LoggerState pi2_logger = MakeLogger(pi2_);
Austin Schuhcde938c2020-02-02 17:30:07 -08001028
1029 event_loop_factory_.RunFor(chrono::milliseconds(95));
1030
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001031 StartLogger(&pi1_logger);
Austin Schuhcde938c2020-02-02 17:30:07 -08001032
1033 event_loop_factory_.RunFor(chrono::milliseconds(200));
1034
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001035 StartLogger(&pi2_logger);
1036
Austin Schuhcde938c2020-02-02 17:30:07 -08001037 event_loop_factory_.RunFor(chrono::milliseconds(20000));
1038 }
1039
Austin Schuh287d43d2020-12-04 20:19:33 -08001040 LogReader reader(SortParts(logfiles_));
Austin Schuhcde938c2020-02-02 17:30:07 -08001041
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001042 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
Austin Schuhcde938c2020-02-02 17:30:07 -08001043 log_reader_factory.set_send_delay(chrono::microseconds(0));
1044
1045 // This sends out the fetched messages and advances time to the start of the
1046 // log file.
1047 reader.Register(&log_reader_factory);
1048
1049 const Node *pi1 =
1050 configuration::GetNode(log_reader_factory.configuration(), "pi1");
1051 const Node *pi2 =
1052 configuration::GetNode(log_reader_factory.configuration(), "pi2");
1053
1054 EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(pi1, pi2));
1055
1056 reader.event_loop_factory()->set_send_delay(chrono::microseconds(0));
1057
1058 std::unique_ptr<EventLoop> pi1_event_loop =
1059 log_reader_factory.MakeEventLoop("test", pi1);
1060 std::unique_ptr<EventLoop> pi2_event_loop =
1061 log_reader_factory.MakeEventLoop("test", pi2);
1062
1063 int pi1_ping_count = 30;
1064 int pi2_ping_count = 30;
1065 int pi1_pong_count = 30;
1066 int pi2_pong_count = 30;
1067
1068 // Confirm that the ping value matches.
1069 pi1_event_loop->MakeWatcher(
1070 "/test", [&pi1_ping_count, &pi1_event_loop](const examples::Ping &ping) {
1071 VLOG(1) << "Pi1 ping " << FlatbufferToJson(&ping)
1072 << pi1_event_loop->context().monotonic_remote_time << " -> "
1073 << pi1_event_loop->context().monotonic_event_time;
1074 EXPECT_EQ(ping.value(), pi1_ping_count + 1);
1075
1076 ++pi1_ping_count;
1077 });
1078 pi2_event_loop->MakeWatcher(
1079 "/test", [&pi2_ping_count, &pi2_event_loop](const examples::Ping &ping) {
1080 VLOG(1) << "Pi2 ping " << FlatbufferToJson(&ping)
1081 << pi2_event_loop->context().monotonic_remote_time << " -> "
1082 << pi2_event_loop->context().monotonic_event_time;
1083 EXPECT_EQ(ping.value(), pi2_ping_count + 1);
1084
1085 ++pi2_ping_count;
1086 });
1087
1088 // Confirm that the ping and pong counts both match, and the value also
1089 // matches.
1090 pi1_event_loop->MakeWatcher(
1091 "/test", [&pi1_event_loop, &pi1_ping_count,
1092 &pi1_pong_count](const examples::Pong &pong) {
1093 VLOG(1) << "Pi1 pong " << FlatbufferToJson(&pong) << " at "
1094 << pi1_event_loop->context().monotonic_remote_time << " -> "
1095 << pi1_event_loop->context().monotonic_event_time;
1096
1097 EXPECT_EQ(pong.value(), pi1_pong_count + 1);
1098 ++pi1_pong_count;
1099 EXPECT_EQ(pi1_ping_count, pi1_pong_count);
1100 });
1101 pi2_event_loop->MakeWatcher(
1102 "/test", [&pi2_event_loop, &pi2_ping_count,
1103 &pi2_pong_count](const examples::Pong &pong) {
1104 VLOG(1) << "Pi2 pong " << FlatbufferToJson(&pong) << " at "
1105 << pi2_event_loop->context().monotonic_remote_time << " -> "
1106 << pi2_event_loop->context().monotonic_event_time;
1107
1108 EXPECT_EQ(pong.value(), pi2_pong_count + 1);
1109 ++pi2_pong_count;
1110 EXPECT_EQ(pi2_ping_count, pi2_pong_count);
1111 });
1112
1113 log_reader_factory.Run();
1114 EXPECT_EQ(pi1_ping_count, 2030);
1115 EXPECT_EQ(pi2_ping_count, 2030);
1116 EXPECT_EQ(pi1_pong_count, 2030);
1117 EXPECT_EQ(pi2_pong_count, 2030);
1118
1119 reader.Deregister();
1120}
Austin Schuh6f3babe2020-01-26 20:34:50 -08001121
Austin Schuh8bd96322020-02-13 21:18:22 -08001122// Tests that we can read log files where the monotonic clocks drift and don't
1123// match correctly. While we are here, also test that different ending times
1124// also is readable.
1125TEST_F(MultinodeLoggerTest, MismatchedClocks) {
Austin Schuhcde938c2020-02-02 17:30:07 -08001126 {
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001127 LoggerState pi2_logger = MakeLogger(pi2_);
1128
Austin Schuh8bd96322020-02-13 21:18:22 -08001129 NodeEventLoopFactory *pi2 =
1130 event_loop_factory_.GetNodeEventLoopFactory(pi2_);
1131 LOG(INFO) << "pi2 times: " << pi2->monotonic_now() << " "
1132 << pi2->realtime_now() << " distributed "
1133 << pi2->ToDistributedClock(pi2->monotonic_now());
Austin Schuhcde938c2020-02-02 17:30:07 -08001134
Austin Schuh8bd96322020-02-13 21:18:22 -08001135 const chrono::nanoseconds initial_pi2_offset = -chrono::seconds(1000);
1136 chrono::nanoseconds pi2_offset = initial_pi2_offset;
Austin Schuhcde938c2020-02-02 17:30:07 -08001137
Austin Schuhbe69cf32020-08-27 11:38:33 -07001138 pi2->SetDistributedOffset(-pi2_offset, 1.0);
Austin Schuh8bd96322020-02-13 21:18:22 -08001139 LOG(INFO) << "pi2 times: " << pi2->monotonic_now() << " "
1140 << pi2->realtime_now() << " distributed "
1141 << pi2->ToDistributedClock(pi2->monotonic_now());
Austin Schuhcde938c2020-02-02 17:30:07 -08001142
Austin Schuh8bd96322020-02-13 21:18:22 -08001143 for (int i = 0; i < 95; ++i) {
1144 pi2_offset += chrono::nanoseconds(200);
Austin Schuhbe69cf32020-08-27 11:38:33 -07001145 pi2->SetDistributedOffset(-pi2_offset, 1.0);
Austin Schuh8bd96322020-02-13 21:18:22 -08001146 event_loop_factory_.RunFor(chrono::milliseconds(1));
1147 }
Austin Schuhcde938c2020-02-02 17:30:07 -08001148
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001149 StartLogger(&pi2_logger);
Austin Schuhcde938c2020-02-02 17:30:07 -08001150
Austin Schuh8bd96322020-02-13 21:18:22 -08001151 event_loop_factory_.RunFor(chrono::milliseconds(200));
Austin Schuhcde938c2020-02-02 17:30:07 -08001152
Austin Schuh8bd96322020-02-13 21:18:22 -08001153 {
1154 // Run pi1's logger for only part of the time.
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001155 LoggerState pi1_logger = MakeLogger(pi1_);
Austin Schuh8bd96322020-02-13 21:18:22 -08001156
James Kuszmaulbb28ef22020-05-09 22:30:38 -07001157 StartLogger(&pi1_logger);
Austin Schuh8bd96322020-02-13 21:18:22 -08001158
1159 for (int i = 0; i < 20000; ++i) {
1160 pi2_offset += chrono::nanoseconds(200);
Austin Schuhbe69cf32020-08-27 11:38:33 -07001161 pi2->SetDistributedOffset(-pi2_offset, 1.0);
Austin Schuh8bd96322020-02-13 21:18:22 -08001162 event_loop_factory_.RunFor(chrono::milliseconds(1));
1163 }
1164
1165 EXPECT_GT(pi2_offset - initial_pi2_offset,
1166 event_loop_factory_.send_delay() +
1167 event_loop_factory_.network_delay());
1168
1169 for (int i = 0; i < 40000; ++i) {
1170 pi2_offset -= chrono::nanoseconds(200);
Austin Schuhbe69cf32020-08-27 11:38:33 -07001171 pi2->SetDistributedOffset(-pi2_offset, 1.0);
Austin Schuh8bd96322020-02-13 21:18:22 -08001172 event_loop_factory_.RunFor(chrono::milliseconds(1));
1173 }
1174 }
1175
1176 // And log a bit more on pi2.
1177 event_loop_factory_.RunFor(chrono::milliseconds(400));
Austin Schuhcde938c2020-02-02 17:30:07 -08001178 }
1179
Austin Schuh287d43d2020-12-04 20:19:33 -08001180 LogReader reader(SortParts(logfiles_));
Austin Schuhcde938c2020-02-02 17:30:07 -08001181
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001182 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
Austin Schuhcde938c2020-02-02 17:30:07 -08001183 log_reader_factory.set_send_delay(chrono::microseconds(0));
1184
Austin Schuhcde938c2020-02-02 17:30:07 -08001185 const Node *pi1 =
1186 configuration::GetNode(log_reader_factory.configuration(), "pi1");
1187 const Node *pi2 =
1188 configuration::GetNode(log_reader_factory.configuration(), "pi2");
1189
Austin Schuh2f8fd752020-09-01 22:38:28 -07001190 // This sends out the fetched messages and advances time to the start of the
1191 // log file.
1192 reader.Register(&log_reader_factory);
1193
1194 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi1) << " pi1";
1195 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi2) << " pi2";
1196 LOG(INFO) << "now pi1 "
1197 << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now();
1198 LOG(INFO) << "now pi2 "
1199 << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now();
1200
Austin Schuhcde938c2020-02-02 17:30:07 -08001201 LOG(INFO) << "Done registering (pi1) "
Austin Schuh391e3172020-09-01 22:48:18 -07001202 << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now()
1203 << " "
Austin Schuhcde938c2020-02-02 17:30:07 -08001204 << log_reader_factory.GetNodeEventLoopFactory(pi1)->realtime_now();
1205 LOG(INFO) << "Done registering (pi2) "
Austin Schuh391e3172020-09-01 22:48:18 -07001206 << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now()
1207 << " "
Austin Schuhcde938c2020-02-02 17:30:07 -08001208 << log_reader_factory.GetNodeEventLoopFactory(pi2)->realtime_now();
1209
1210 EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(pi1, pi2));
1211
1212 reader.event_loop_factory()->set_send_delay(chrono::microseconds(0));
1213
1214 std::unique_ptr<EventLoop> pi1_event_loop =
1215 log_reader_factory.MakeEventLoop("test", pi1);
1216 std::unique_ptr<EventLoop> pi2_event_loop =
1217 log_reader_factory.MakeEventLoop("test", pi2);
1218
1219 int pi1_ping_count = 30;
1220 int pi2_ping_count = 30;
1221 int pi1_pong_count = 30;
1222 int pi2_pong_count = 30;
1223
1224 // Confirm that the ping value matches.
1225 pi1_event_loop->MakeWatcher(
1226 "/test", [&pi1_ping_count, &pi1_event_loop](const examples::Ping &ping) {
1227 VLOG(1) << "Pi1 ping " << FlatbufferToJson(&ping)
1228 << pi1_event_loop->context().monotonic_remote_time << " -> "
1229 << pi1_event_loop->context().monotonic_event_time;
1230 EXPECT_EQ(ping.value(), pi1_ping_count + 1);
1231
1232 ++pi1_ping_count;
1233 });
1234 pi2_event_loop->MakeWatcher(
1235 "/test", [&pi2_ping_count, &pi2_event_loop](const examples::Ping &ping) {
1236 VLOG(1) << "Pi2 ping " << FlatbufferToJson(&ping)
1237 << pi2_event_loop->context().monotonic_remote_time << " -> "
1238 << pi2_event_loop->context().monotonic_event_time;
1239 EXPECT_EQ(ping.value(), pi2_ping_count + 1);
1240
1241 ++pi2_ping_count;
1242 });
1243
1244 // Confirm that the ping and pong counts both match, and the value also
1245 // matches.
1246 pi1_event_loop->MakeWatcher(
1247 "/test", [&pi1_event_loop, &pi1_ping_count,
1248 &pi1_pong_count](const examples::Pong &pong) {
1249 VLOG(1) << "Pi1 pong " << FlatbufferToJson(&pong) << " at "
1250 << pi1_event_loop->context().monotonic_remote_time << " -> "
1251 << pi1_event_loop->context().monotonic_event_time;
1252
1253 EXPECT_EQ(pong.value(), pi1_pong_count + 1);
1254 ++pi1_pong_count;
1255 EXPECT_EQ(pi1_ping_count, pi1_pong_count);
1256 });
1257 pi2_event_loop->MakeWatcher(
1258 "/test", [&pi2_event_loop, &pi2_ping_count,
1259 &pi2_pong_count](const examples::Pong &pong) {
1260 VLOG(1) << "Pi2 pong " << FlatbufferToJson(&pong) << " at "
1261 << pi2_event_loop->context().monotonic_remote_time << " -> "
1262 << pi2_event_loop->context().monotonic_event_time;
1263
1264 EXPECT_EQ(pong.value(), pi2_pong_count + 1);
1265 ++pi2_pong_count;
1266 EXPECT_EQ(pi2_ping_count, pi2_pong_count);
1267 });
1268
1269 log_reader_factory.Run();
Austin Schuh8bd96322020-02-13 21:18:22 -08001270 EXPECT_EQ(pi1_ping_count, 6030);
1271 EXPECT_EQ(pi2_ping_count, 6030);
1272 EXPECT_EQ(pi1_pong_count, 6030);
1273 EXPECT_EQ(pi2_pong_count, 6030);
Austin Schuhcde938c2020-02-02 17:30:07 -08001274
1275 reader.Deregister();
1276}
1277
Austin Schuh5212cad2020-09-09 23:12:09 -07001278// Tests that we can sort a bunch of parts into the pre-determined sorted parts.
1279TEST_F(MultinodeLoggerTest, SortParts) {
1280 // Make a bunch of parts.
1281 {
1282 LoggerState pi1_logger = MakeLogger(pi1_);
1283 LoggerState pi2_logger = MakeLogger(pi2_);
1284
1285 event_loop_factory_.RunFor(chrono::milliseconds(95));
1286
1287 StartLogger(&pi1_logger);
1288 StartLogger(&pi2_logger);
1289
1290 event_loop_factory_.RunFor(chrono::milliseconds(2000));
1291 }
1292
Austin Schuh11d43732020-09-21 17:28:30 -07001293 const std::vector<LogFile> sorted_parts = SortParts(logfiles_);
Austin Schuh3bd4c402020-11-06 18:19:06 -08001294 VerifyParts(sorted_parts);
1295}
Austin Schuh11d43732020-09-21 17:28:30 -07001296
Austin Schuh3bd4c402020-11-06 18:19:06 -08001297// Tests that we can sort a bunch of parts with an empty part. We should ignore
1298// it and remove it from the sorted list.
1299TEST_F(MultinodeLoggerTest, SortEmptyParts) {
1300 // Make a bunch of parts.
1301 {
1302 LoggerState pi1_logger = MakeLogger(pi1_);
1303 LoggerState pi2_logger = MakeLogger(pi2_);
Austin Schuh11d43732020-09-21 17:28:30 -07001304
Austin Schuh3bd4c402020-11-06 18:19:06 -08001305 event_loop_factory_.RunFor(chrono::milliseconds(95));
Austin Schuh11d43732020-09-21 17:28:30 -07001306
Austin Schuh3bd4c402020-11-06 18:19:06 -08001307 StartLogger(&pi1_logger);
1308 StartLogger(&pi2_logger);
Austin Schuh11d43732020-09-21 17:28:30 -07001309
Austin Schuh3bd4c402020-11-06 18:19:06 -08001310 event_loop_factory_.RunFor(chrono::milliseconds(2000));
Austin Schuh11d43732020-09-21 17:28:30 -07001311 }
1312
Austin Schuh3bd4c402020-11-06 18:19:06 -08001313 // TODO(austin): Should we flip out if the file can't open?
1314 const std::string kEmptyFile("foobarinvalidfiledoesnotexist.bfbs");
Austin Schuh11d43732020-09-21 17:28:30 -07001315
Austin Schuh3bd4c402020-11-06 18:19:06 -08001316 aos::util::WriteStringToFileOrDie(kEmptyFile, "");
1317 logfiles_.emplace_back(kEmptyFile);
Austin Schuh5212cad2020-09-09 23:12:09 -07001318
Austin Schuh3bd4c402020-11-06 18:19:06 -08001319 const std::vector<LogFile> sorted_parts = SortParts(logfiles_);
1320 VerifyParts(sorted_parts, {kEmptyFile});
Austin Schuh5212cad2020-09-09 23:12:09 -07001321}
1322
Austin Schuh3bd4c402020-11-06 18:19:06 -08001323#ifdef LZMA
1324// Tests that we can sort a bunch of parts with an empty .xz file in there. The
1325// empty file should be ignored.
1326TEST_F(MultinodeLoggerTest, SortEmptyCompressedParts) {
1327 // Make a bunch of parts.
1328 {
1329 LoggerState pi1_logger = MakeLogger(pi1_);
1330 LoggerState pi2_logger = MakeLogger(pi2_);
1331
1332 event_loop_factory_.RunFor(chrono::milliseconds(95));
1333
1334 StartLogger(&pi1_logger, "", true);
1335 StartLogger(&pi2_logger, "", true);
1336
1337 event_loop_factory_.RunFor(chrono::milliseconds(2000));
1338 }
1339
1340 // TODO(austin): Should we flip out if the file can't open?
1341 const std::string kEmptyFile("foobarinvalidfiledoesnotexist.bfbs.xz");
1342
1343 AddExtension(".xz");
1344
1345 aos::util::WriteStringToFileOrDie(kEmptyFile, "");
1346 logfiles_.emplace_back(kEmptyFile);
1347
1348 const std::vector<LogFile> sorted_parts = SortParts(logfiles_);
1349 VerifyParts(sorted_parts, {kEmptyFile});
1350}
1351
1352// Tests that we can sort a bunch of parts with the end missing off a compressed
1353// file. We should use the part we can read.
1354TEST_F(MultinodeLoggerTest, SortTruncatedCompressedParts) {
1355 // Make a bunch of parts.
1356 {
1357 LoggerState pi1_logger = MakeLogger(pi1_);
1358 LoggerState pi2_logger = MakeLogger(pi2_);
1359
1360 event_loop_factory_.RunFor(chrono::milliseconds(95));
1361
1362 StartLogger(&pi1_logger, "", true);
1363 StartLogger(&pi2_logger, "", true);
1364
1365 event_loop_factory_.RunFor(chrono::milliseconds(2000));
1366 }
1367
1368 // Append everything with .xz.
1369 AddExtension(".xz");
1370
1371 // Strip off the end of one of the files. Pick one with a lot of data.
1372 ::std::string compressed_contents =
1373 aos::util::ReadFileToStringOrDie(logfiles_[0]);
1374
1375 aos::util::WriteStringToFileOrDie(
1376 logfiles_[0],
1377 compressed_contents.substr(0, compressed_contents.size() - 100));
1378
1379 const std::vector<LogFile> sorted_parts = SortParts(logfiles_);
1380 VerifyParts(sorted_parts);
1381}
1382#endif
1383
Austin Schuh01b4c352020-09-21 23:09:39 -07001384// Tests that if we remap a remapped channel, it shows up correctly.
1385TEST_F(MultinodeLoggerTest, RemapLoggedChannel) {
1386 {
1387 LoggerState pi1_logger = MakeLogger(pi1_);
1388 LoggerState pi2_logger = MakeLogger(pi2_);
1389
1390 event_loop_factory_.RunFor(chrono::milliseconds(95));
1391
1392 StartLogger(&pi1_logger);
1393 StartLogger(&pi2_logger);
1394
1395 event_loop_factory_.RunFor(chrono::milliseconds(20000));
1396 }
1397
Austin Schuh287d43d2020-12-04 20:19:33 -08001398 LogReader reader(SortParts(logfiles_));
Austin Schuh01b4c352020-09-21 23:09:39 -07001399
1400 // Remap just on pi1.
1401 reader.RemapLoggedChannel<aos::timing::Report>(
1402 "/aos", configuration::GetNode(reader.configuration(), "pi1"));
1403
1404 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
1405 log_reader_factory.set_send_delay(chrono::microseconds(0));
1406
1407 reader.Register(&log_reader_factory);
1408
1409 const Node *pi1 =
1410 configuration::GetNode(log_reader_factory.configuration(), "pi1");
1411 const Node *pi2 =
1412 configuration::GetNode(log_reader_factory.configuration(), "pi2");
1413
1414 // Confirm we can read the data on the remapped channel, just for pi1. Nothing
1415 // else should have moved.
1416 std::unique_ptr<EventLoop> pi1_event_loop =
1417 log_reader_factory.MakeEventLoop("test", pi1);
1418 pi1_event_loop->SkipTimingReport();
1419 std::unique_ptr<EventLoop> full_pi1_event_loop =
1420 log_reader_factory.MakeEventLoop("test", pi1);
1421 full_pi1_event_loop->SkipTimingReport();
1422 std::unique_ptr<EventLoop> pi2_event_loop =
1423 log_reader_factory.MakeEventLoop("test", pi2);
1424 pi2_event_loop->SkipTimingReport();
1425
1426 MessageCounter<aos::timing::Report> pi1_timing_report(pi1_event_loop.get(),
1427 "/aos");
1428 MessageCounter<aos::timing::Report> full_pi1_timing_report(
1429 full_pi1_event_loop.get(), "/pi1/aos");
1430 MessageCounter<aos::timing::Report> pi1_original_timing_report(
1431 pi1_event_loop.get(), "/original/aos");
1432 MessageCounter<aos::timing::Report> full_pi1_original_timing_report(
1433 full_pi1_event_loop.get(), "/original/pi1/aos");
1434 MessageCounter<aos::timing::Report> pi2_timing_report(pi2_event_loop.get(),
1435 "/aos");
1436
1437 log_reader_factory.Run();
1438
1439 EXPECT_EQ(pi1_timing_report.count(), 0u);
1440 EXPECT_EQ(full_pi1_timing_report.count(), 0u);
1441 EXPECT_NE(pi1_original_timing_report.count(), 0u);
1442 EXPECT_NE(full_pi1_original_timing_report.count(), 0u);
1443 EXPECT_NE(pi2_timing_report.count(), 0u);
1444
1445 reader.Deregister();
1446}
1447
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001448// Tests that we properly recreate forwarded timestamps when replaying a log.
1449// This should be enough that we can then re-run the logger and get a valid log
1450// back.
1451TEST_F(MultinodeLoggerTest, MessageHeader) {
1452 {
1453 LoggerState pi1_logger = MakeLogger(pi1_);
1454 LoggerState pi2_logger = MakeLogger(pi2_);
1455
1456 event_loop_factory_.RunFor(chrono::milliseconds(95));
1457
1458 StartLogger(&pi1_logger);
1459 StartLogger(&pi2_logger);
1460
1461 event_loop_factory_.RunFor(chrono::milliseconds(20000));
1462 }
1463
Austin Schuh287d43d2020-12-04 20:19:33 -08001464 LogReader reader(SortParts(logfiles_));
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001465
1466 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
1467 log_reader_factory.set_send_delay(chrono::microseconds(0));
1468
1469 // This sends out the fetched messages and advances time to the start of the
1470 // log file.
1471 reader.Register(&log_reader_factory);
1472
1473 const Node *pi1 =
1474 configuration::GetNode(log_reader_factory.configuration(), "pi1");
1475 const Node *pi2 =
1476 configuration::GetNode(log_reader_factory.configuration(), "pi2");
1477
1478 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi1) << " pi1";
1479 LOG(INFO) << "Start time " << reader.monotonic_start_time(pi2) << " pi2";
1480 LOG(INFO) << "now pi1 "
1481 << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now();
1482 LOG(INFO) << "now pi2 "
1483 << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now();
1484
1485 EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(pi1, pi2));
1486
1487 reader.event_loop_factory()->set_send_delay(chrono::microseconds(0));
1488
1489 std::unique_ptr<EventLoop> pi1_event_loop =
1490 log_reader_factory.MakeEventLoop("test", pi1);
1491 std::unique_ptr<EventLoop> pi2_event_loop =
1492 log_reader_factory.MakeEventLoop("test", pi2);
1493
Austin Schuh0de30f32020-12-06 12:44:28 -08001494 MessageCounter<RemoteMessage> pi1_original_message_header_counter(
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001495 pi1_event_loop.get(), "/original/aos/remote_timestamps/pi2");
Austin Schuh0de30f32020-12-06 12:44:28 -08001496 MessageCounter<RemoteMessage> pi2_original_message_header_counter(
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001497 pi2_event_loop.get(), "/original/aos/remote_timestamps/pi1");
1498
1499 aos::Fetcher<message_bridge::Timestamp> pi1_timestamp_on_pi1_fetcher =
1500 pi1_event_loop->MakeFetcher<message_bridge::Timestamp>("/pi1/aos");
1501 aos::Fetcher<message_bridge::Timestamp> pi1_timestamp_on_pi2_fetcher =
1502 pi2_event_loop->MakeFetcher<message_bridge::Timestamp>("/pi1/aos");
1503
1504 aos::Fetcher<examples::Ping> ping_on_pi1_fetcher =
1505 pi1_event_loop->MakeFetcher<examples::Ping>("/test");
1506 aos::Fetcher<examples::Ping> ping_on_pi2_fetcher =
1507 pi2_event_loop->MakeFetcher<examples::Ping>("/test");
1508
1509 aos::Fetcher<message_bridge::Timestamp> pi2_timestamp_on_pi2_fetcher =
1510 pi2_event_loop->MakeFetcher<message_bridge::Timestamp>("/pi2/aos");
1511 aos::Fetcher<message_bridge::Timestamp> pi2_timestamp_on_pi1_fetcher =
1512 pi1_event_loop->MakeFetcher<message_bridge::Timestamp>("/pi2/aos");
1513
1514 aos::Fetcher<examples::Pong> pong_on_pi2_fetcher =
1515 pi2_event_loop->MakeFetcher<examples::Pong>("/test");
1516 aos::Fetcher<examples::Pong> pong_on_pi1_fetcher =
1517 pi1_event_loop->MakeFetcher<examples::Pong>("/test");
1518
1519 const size_t pi1_timestamp_channel = configuration::ChannelIndex(
1520 pi1_event_loop->configuration(), pi1_timestamp_on_pi1_fetcher.channel());
1521 const size_t ping_timestamp_channel = configuration::ChannelIndex(
1522 pi2_event_loop->configuration(), ping_on_pi2_fetcher.channel());
1523
1524 const size_t pi2_timestamp_channel = configuration::ChannelIndex(
1525 pi2_event_loop->configuration(), pi2_timestamp_on_pi2_fetcher.channel());
1526 const size_t pong_timestamp_channel = configuration::ChannelIndex(
1527 pi1_event_loop->configuration(), pong_on_pi1_fetcher.channel());
1528
1529 pi1_event_loop->MakeWatcher(
1530 "/aos/remote_timestamps/pi2",
Austin Schuh315b96b2020-12-11 21:21:12 -08001531 [&pi1_event_loop, &pi2_event_loop, pi1_timestamp_channel,
1532 ping_timestamp_channel, &pi1_timestamp_on_pi1_fetcher,
1533 &pi1_timestamp_on_pi2_fetcher, &ping_on_pi1_fetcher,
Austin Schuh0de30f32020-12-06 12:44:28 -08001534 &ping_on_pi2_fetcher](const RemoteMessage &header) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001535 const aos::monotonic_clock::time_point header_monotonic_sent_time(
1536 chrono::nanoseconds(header.monotonic_sent_time()));
1537 const aos::realtime_clock::time_point header_realtime_sent_time(
1538 chrono::nanoseconds(header.realtime_sent_time()));
1539 const aos::monotonic_clock::time_point header_monotonic_remote_time(
1540 chrono::nanoseconds(header.monotonic_remote_time()));
1541 const aos::realtime_clock::time_point header_realtime_remote_time(
1542 chrono::nanoseconds(header.realtime_remote_time()));
1543
1544 const Context *pi1_context = nullptr;
1545 const Context *pi2_context = nullptr;
1546
1547 if (header.channel_index() == pi1_timestamp_channel) {
1548 ASSERT_TRUE(pi1_timestamp_on_pi1_fetcher.FetchNext());
1549 ASSERT_TRUE(pi1_timestamp_on_pi2_fetcher.FetchNext());
1550 pi1_context = &pi1_timestamp_on_pi1_fetcher.context();
1551 pi2_context = &pi1_timestamp_on_pi2_fetcher.context();
1552 } else if (header.channel_index() == ping_timestamp_channel) {
1553 ASSERT_TRUE(ping_on_pi1_fetcher.FetchNext());
1554 ASSERT_TRUE(ping_on_pi2_fetcher.FetchNext());
1555 pi1_context = &ping_on_pi1_fetcher.context();
1556 pi2_context = &ping_on_pi2_fetcher.context();
1557 } else {
1558 LOG(FATAL) << "Unknown channel " << FlatbufferToJson(&header) << " "
1559 << configuration::CleanedChannelToString(
1560 pi1_event_loop->configuration()->channels()->Get(
1561 header.channel_index()));
1562 }
1563
Austin Schuh315b96b2020-12-11 21:21:12 -08001564 ASSERT_TRUE(header.has_boot_uuid());
1565 EXPECT_EQ(header.boot_uuid()->string_view(),
1566 pi2_event_loop->boot_uuid().string_view());
1567
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001568 EXPECT_EQ(pi1_context->queue_index, header.remote_queue_index());
1569 EXPECT_EQ(pi2_context->remote_queue_index, header.remote_queue_index());
1570 EXPECT_EQ(pi2_context->queue_index, header.queue_index());
1571
1572 EXPECT_EQ(pi2_context->monotonic_event_time,
1573 header_monotonic_sent_time);
1574 EXPECT_EQ(pi2_context->realtime_event_time, header_realtime_sent_time);
1575 EXPECT_EQ(pi2_context->realtime_remote_time,
1576 header_realtime_remote_time);
1577 EXPECT_EQ(pi2_context->monotonic_remote_time,
1578 header_monotonic_remote_time);
1579
1580 EXPECT_EQ(pi1_context->realtime_event_time,
1581 header_realtime_remote_time);
1582 EXPECT_EQ(pi1_context->monotonic_event_time,
1583 header_monotonic_remote_time);
1584 });
1585 pi2_event_loop->MakeWatcher(
1586 "/aos/remote_timestamps/pi1",
Austin Schuh315b96b2020-12-11 21:21:12 -08001587 [&pi2_event_loop, &pi1_event_loop, pi2_timestamp_channel,
1588 pong_timestamp_channel, &pi2_timestamp_on_pi2_fetcher,
1589 &pi2_timestamp_on_pi1_fetcher, &pong_on_pi2_fetcher,
Austin Schuh0de30f32020-12-06 12:44:28 -08001590 &pong_on_pi1_fetcher](const RemoteMessage &header) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001591 const aos::monotonic_clock::time_point header_monotonic_sent_time(
1592 chrono::nanoseconds(header.monotonic_sent_time()));
1593 const aos::realtime_clock::time_point header_realtime_sent_time(
1594 chrono::nanoseconds(header.realtime_sent_time()));
1595 const aos::monotonic_clock::time_point header_monotonic_remote_time(
1596 chrono::nanoseconds(header.monotonic_remote_time()));
1597 const aos::realtime_clock::time_point header_realtime_remote_time(
1598 chrono::nanoseconds(header.realtime_remote_time()));
1599
1600 const Context *pi2_context = nullptr;
1601 const Context *pi1_context = nullptr;
1602
1603 if (header.channel_index() == pi2_timestamp_channel) {
1604 ASSERT_TRUE(pi2_timestamp_on_pi2_fetcher.FetchNext());
1605 ASSERT_TRUE(pi2_timestamp_on_pi1_fetcher.FetchNext());
1606 pi2_context = &pi2_timestamp_on_pi2_fetcher.context();
1607 pi1_context = &pi2_timestamp_on_pi1_fetcher.context();
1608 } else if (header.channel_index() == pong_timestamp_channel) {
1609 ASSERT_TRUE(pong_on_pi2_fetcher.FetchNext());
1610 ASSERT_TRUE(pong_on_pi1_fetcher.FetchNext());
1611 pi2_context = &pong_on_pi2_fetcher.context();
1612 pi1_context = &pong_on_pi1_fetcher.context();
1613 } else {
1614 LOG(FATAL) << "Unknown channel " << FlatbufferToJson(&header) << " "
1615 << configuration::CleanedChannelToString(
1616 pi2_event_loop->configuration()->channels()->Get(
1617 header.channel_index()));
1618 }
1619
Austin Schuh315b96b2020-12-11 21:21:12 -08001620 ASSERT_TRUE(header.has_boot_uuid());
1621 EXPECT_EQ(header.boot_uuid()->string_view(),
1622 pi1_event_loop->boot_uuid().string_view());
1623
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001624 EXPECT_EQ(pi2_context->queue_index, header.remote_queue_index());
1625 EXPECT_EQ(pi1_context->remote_queue_index, header.remote_queue_index());
1626 EXPECT_EQ(pi1_context->queue_index, header.queue_index());
1627
1628 EXPECT_EQ(pi1_context->monotonic_event_time,
1629 header_monotonic_sent_time);
1630 EXPECT_EQ(pi1_context->realtime_event_time, header_realtime_sent_time);
1631 EXPECT_EQ(pi1_context->realtime_remote_time,
1632 header_realtime_remote_time);
1633 EXPECT_EQ(pi1_context->monotonic_remote_time,
1634 header_monotonic_remote_time);
1635
1636 EXPECT_EQ(pi2_context->realtime_event_time,
1637 header_realtime_remote_time);
1638 EXPECT_EQ(pi2_context->monotonic_event_time,
1639 header_monotonic_remote_time);
1640 });
1641
1642 // And confirm we can re-create a log again, while checking the contents.
1643 {
1644 LoggerState pi1_logger = MakeLogger(
1645 configuration::GetNode(log_reader_factory.configuration(), pi1_),
1646 &log_reader_factory);
1647 LoggerState pi2_logger = MakeLogger(
1648 configuration::GetNode(log_reader_factory.configuration(), pi2_),
1649 &log_reader_factory);
1650
1651 StartLogger(&pi1_logger, "relogged");
1652 StartLogger(&pi2_logger, "relogged");
1653
1654 log_reader_factory.Run();
1655 }
1656
1657 EXPECT_EQ(pi2_original_message_header_counter.count(), 0u);
1658 EXPECT_EQ(pi1_original_message_header_counter.count(), 0u);
1659
1660 reader.Deregister();
1661}
1662
Austin Schuh315b96b2020-12-11 21:21:12 -08001663// Tests that we properly populate and extract the logger_start time by setting
1664// up a clock difference between 2 nodes and looking at the resulting parts.
1665TEST_F(MultinodeLoggerTest, LoggerStartTime) {
1666 {
1667 LoggerState pi1_logger = MakeLogger(pi1_);
1668 LoggerState pi2_logger = MakeLogger(pi2_);
1669
1670 NodeEventLoopFactory *pi2 =
1671 event_loop_factory_.GetNodeEventLoopFactory(pi2_);
1672
1673 pi2->SetDistributedOffset(chrono::seconds(1000), 1.0);
1674
1675 StartLogger(&pi1_logger);
1676 StartLogger(&pi2_logger);
1677
1678 event_loop_factory_.RunFor(chrono::milliseconds(10000));
1679 }
1680
1681 for (const LogFile &log_file : SortParts(logfiles_)) {
1682 for (const LogParts &log_part : log_file.parts) {
1683 if (log_part.node == log_file.logger_node) {
1684 EXPECT_EQ(log_part.logger_monotonic_start_time,
1685 aos::monotonic_clock::min_time);
1686 EXPECT_EQ(log_part.logger_realtime_start_time,
1687 aos::realtime_clock::min_time);
1688 } else {
1689 const chrono::seconds offset = log_file.logger_node == "pi1"
1690 ? -chrono::seconds(1000)
1691 : chrono::seconds(1000);
1692 EXPECT_EQ(log_part.logger_monotonic_start_time,
1693 log_part.monotonic_start_time + offset);
1694 EXPECT_EQ(log_part.logger_realtime_start_time,
1695 log_file.realtime_start_time +
1696 (log_part.logger_monotonic_start_time -
1697 log_file.monotonic_start_time));
1698 }
1699 }
1700 }
1701}
1702
Austin Schuh8bd96322020-02-13 21:18:22 -08001703// TODO(austin): We can write a test which recreates a logfile and confirms that
1704// we get it back. That is the ultimate test.
1705
Austin Schuh315b96b2020-12-11 21:21:12 -08001706// Tests that we properly recreate forwarded timestamps when replaying a log.
1707// This should be enough that we can then re-run the logger and get a valid log
1708// back.
1709TEST_F(MultinodeLoggerDeathTest, RemoteReboot) {
1710 std::string pi2_boot1;
1711 std::string pi2_boot2;
1712 {
1713 pi2_boot1 = event_loop_factory_.GetNodeEventLoopFactory(pi2_)
1714 ->boot_uuid()
1715 .string_view();
1716 LoggerState pi1_logger = MakeLogger(pi1_);
1717
1718 event_loop_factory_.RunFor(chrono::milliseconds(95));
1719
1720 StartLogger(&pi1_logger);
1721
1722 event_loop_factory_.RunFor(chrono::milliseconds(10000));
1723
1724 event_loop_factory_.GetNodeEventLoopFactory(pi2_)->Reboot();
1725
1726 pi2_boot2 = event_loop_factory_.GetNodeEventLoopFactory(pi2_)
1727 ->boot_uuid()
1728 .string_view();
1729
1730 event_loop_factory_.RunFor(chrono::milliseconds(20000));
1731 }
1732
1733 // Confirm that we refuse to replay logs with missing boot uuids.
1734 EXPECT_DEATH(
1735 {
1736 LogReader reader(SortParts(pi1_reboot_logfiles_));
1737
1738 SimulatedEventLoopFactory log_reader_factory(reader.configuration());
1739 log_reader_factory.set_send_delay(chrono::microseconds(0));
1740
1741 // This sends out the fetched messages and advances time to the start of
1742 // the log file.
1743 reader.Register(&log_reader_factory);
1744 },
1745 absl::StrFormat("(%s|%s).*(%s|%s).*Found parts from different boots",
1746 pi2_boot1, pi2_boot2, pi2_boot2, pi2_boot1));
1747}
1748
Austin Schuhc9049732020-12-21 22:27:15 -08001749// Tests that we properly handle one direction of message_bridge being
1750// unavailable.
1751TEST_F(MultinodeLoggerTest, OneDirectionWithNegativeSlope) {
1752 event_loop_factory_.GetNodeEventLoopFactory(pi1_)->Disconnect(pi2_);
1753 event_loop_factory_.GetNodeEventLoopFactory(pi2_)->SetDistributedOffset(
1754 chrono::seconds(1000), 0.99999);
1755 {
1756 LoggerState pi1_logger = MakeLogger(pi1_);
1757
1758 event_loop_factory_.RunFor(chrono::milliseconds(95));
1759
1760 StartLogger(&pi1_logger);
1761
1762 event_loop_factory_.RunFor(chrono::milliseconds(10000));
1763 }
1764
1765 // Confirm that we can parse the result. LogReader has enough internal CHECKs
1766 // to confirm the right thing happened.
1767 ConfirmReadable(pi1_single_direction_logfiles_);
1768}
1769
1770// Tests that we properly handle one direction of message_bridge being
1771// unavailable.
1772TEST_F(MultinodeLoggerTest, OneDirectionWithPositiveSlope) {
1773 event_loop_factory_.GetNodeEventLoopFactory(pi1_)->Disconnect(pi2_);
1774 event_loop_factory_.GetNodeEventLoopFactory(pi2_)->SetDistributedOffset(
1775 chrono::seconds(500), 1.00001);
1776 {
1777 LoggerState pi1_logger = MakeLogger(pi1_);
1778
1779 event_loop_factory_.RunFor(chrono::milliseconds(95));
1780
1781 StartLogger(&pi1_logger);
1782
1783 event_loop_factory_.RunFor(chrono::milliseconds(10000));
1784 }
1785
1786 // Confirm that we can parse the result. LogReader has enough internal CHECKs
1787 // to confirm the right thing happened.
1788 ConfirmReadable(pi1_single_direction_logfiles_);
1789}
1790
Austin Schuhe309d2a2019-11-29 13:25:21 -08001791} // namespace testing
1792} // namespace logger
1793} // namespace aos