James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 1 | #include "aos/events/logging/logger.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 2 | |
| 3 | #include "aos/events/event_loop.h" |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 4 | #include "aos/events/message_counter.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 5 | #include "aos/events/ping_lib.h" |
| 6 | #include "aos/events/pong_lib.h" |
| 7 | #include "aos/events/simulated_event_loop.h" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame^] | 8 | #include "aos/network/remote_message_generated.h" |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 9 | #include "aos/network/timestamp_generated.h" |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 10 | #include "aos/testing/tmpdir.h" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 11 | #include "aos/util/file.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 12 | #include "glog/logging.h" |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 13 | #include "gmock/gmock.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 14 | #include "gtest/gtest.h" |
| 15 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 16 | #ifdef LZMA |
| 17 | #include "aos/events/logging/lzma_encoder.h" |
| 18 | #endif |
| 19 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 20 | namespace aos { |
| 21 | namespace logger { |
| 22 | namespace testing { |
| 23 | |
| 24 | namespace chrono = std::chrono; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame^] | 25 | using aos::message_bridge::RemoteMessage; |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 26 | using aos::testing::MessageCounter; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 27 | |
| 28 | class LoggerTest : public ::testing::Test { |
| 29 | public: |
| 30 | LoggerTest() |
| 31 | : config_( |
| 32 | aos::configuration::ReadConfig("aos/events/pingpong_config.json")), |
| 33 | event_loop_factory_(&config_.message()), |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 34 | ping_event_loop_(event_loop_factory_.MakeEventLoop("ping")), |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 35 | ping_(ping_event_loop_.get()), |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 36 | pong_event_loop_(event_loop_factory_.MakeEventLoop("pong")), |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 37 | pong_(pong_event_loop_.get()) {} |
| 38 | |
| 39 | // Config and factory. |
| 40 | aos::FlatbufferDetachedBuffer<aos::Configuration> config_; |
| 41 | SimulatedEventLoopFactory event_loop_factory_; |
| 42 | |
| 43 | // Event loop and app for Ping |
| 44 | std::unique_ptr<EventLoop> ping_event_loop_; |
| 45 | Ping ping_; |
| 46 | |
| 47 | // Event loop and app for Pong |
| 48 | std::unique_ptr<EventLoop> pong_event_loop_; |
| 49 | Pong pong_; |
| 50 | }; |
| 51 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 52 | using LoggerDeathTest = LoggerTest; |
| 53 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 54 | // Tests that we can startup at all. This confirms that the channels are all in |
| 55 | // the config. |
| 56 | TEST_F(LoggerTest, Starts) { |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 57 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 58 | const ::std::string base_name = tmpdir + "/logfile"; |
| 59 | const ::std::string logfile = base_name + ".part0.bfbs"; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 60 | // Remove it. |
| 61 | unlink(logfile.c_str()); |
| 62 | |
| 63 | LOG(INFO) << "Logging data to " << logfile; |
| 64 | |
| 65 | { |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 66 | std::unique_ptr<EventLoop> logger_event_loop = |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 67 | event_loop_factory_.MakeEventLoop("logger"); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 68 | |
| 69 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 70 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 71 | Logger logger(logger_event_loop.get()); |
| 72 | logger.set_polling_period(std::chrono::milliseconds(100)); |
| 73 | logger.StartLoggingLocalNamerOnRun(base_name); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 74 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 75 | } |
| 76 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 77 | // Even though it doesn't make any difference here, exercise the logic for |
| 78 | // passing in a separate config. |
| 79 | LogReader reader(logfile, &config_.message()); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 80 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 81 | // Confirm that we can remap logged channels to point to new buses. |
| 82 | reader.RemapLoggedChannel<aos::examples::Ping>("/test", "/original"); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 83 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 84 | // This sends out the fetched messages and advances time to the start of the |
| 85 | // log file. |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 86 | reader.Register(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 87 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 88 | EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(nullptr)); |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 89 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 90 | std::unique_ptr<EventLoop> test_event_loop = |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 91 | reader.event_loop_factory()->MakeEventLoop("log_reader"); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 92 | |
| 93 | int ping_count = 10; |
| 94 | int pong_count = 10; |
| 95 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 96 | // Confirm that the ping value matches in the remapped channel location. |
| 97 | test_event_loop->MakeWatcher("/original/test", |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 98 | [&ping_count](const examples::Ping &ping) { |
| 99 | EXPECT_EQ(ping.value(), ping_count + 1); |
| 100 | ++ping_count; |
| 101 | }); |
| 102 | // Confirm that the ping and pong counts both match, and the value also |
| 103 | // matches. |
| 104 | test_event_loop->MakeWatcher( |
| 105 | "/test", [&pong_count, &ping_count](const examples::Pong &pong) { |
| 106 | EXPECT_EQ(pong.value(), pong_count + 1); |
| 107 | ++pong_count; |
| 108 | EXPECT_EQ(ping_count, pong_count); |
| 109 | }); |
| 110 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 111 | reader.event_loop_factory()->RunFor(std::chrono::seconds(100)); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 112 | EXPECT_EQ(ping_count, 2010); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 115 | // Tests calling StartLogging twice. |
| 116 | TEST_F(LoggerDeathTest, ExtraStart) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 117 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 118 | const ::std::string base_name1 = tmpdir + "/logfile1"; |
| 119 | const ::std::string logfile1 = base_name1 + ".part0.bfbs"; |
| 120 | const ::std::string base_name2 = tmpdir + "/logfile2"; |
| 121 | const ::std::string logfile2 = base_name2 + ".part0.bfbs"; |
| 122 | unlink(logfile1.c_str()); |
| 123 | unlink(logfile2.c_str()); |
| 124 | |
| 125 | LOG(INFO) << "Logging data to " << logfile1 << " then " << logfile2; |
| 126 | |
| 127 | { |
| 128 | std::unique_ptr<EventLoop> logger_event_loop = |
| 129 | event_loop_factory_.MakeEventLoop("logger"); |
| 130 | |
| 131 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 132 | |
| 133 | Logger logger(logger_event_loop.get()); |
| 134 | logger.set_polling_period(std::chrono::milliseconds(100)); |
| 135 | logger_event_loop->OnRun( |
| 136 | [base_name1, base_name2, &logger_event_loop, &logger]() { |
| 137 | logger.StartLogging(std::make_unique<LocalLogNamer>( |
| 138 | base_name1, logger_event_loop->node())); |
| 139 | EXPECT_DEATH(logger.StartLogging(std::make_unique<LocalLogNamer>( |
| 140 | base_name2, logger_event_loop->node())), |
| 141 | "Already logging"); |
| 142 | }); |
| 143 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Tests calling StopLogging twice. |
| 148 | TEST_F(LoggerDeathTest, ExtraStop) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 149 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 150 | const ::std::string base_name = tmpdir + "/logfile"; |
| 151 | const ::std::string logfile = base_name + ".part0.bfbs"; |
| 152 | // Remove it. |
| 153 | unlink(logfile.c_str()); |
| 154 | |
| 155 | LOG(INFO) << "Logging data to " << logfile; |
| 156 | |
| 157 | { |
| 158 | std::unique_ptr<EventLoop> logger_event_loop = |
| 159 | event_loop_factory_.MakeEventLoop("logger"); |
| 160 | |
| 161 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 162 | |
| 163 | Logger logger(logger_event_loop.get()); |
| 164 | logger.set_polling_period(std::chrono::milliseconds(100)); |
| 165 | logger_event_loop->OnRun([base_name, &logger_event_loop, &logger]() { |
| 166 | logger.StartLogging(std::make_unique<LocalLogNamer>( |
| 167 | base_name, logger_event_loop->node())); |
| 168 | logger.StopLogging(aos::monotonic_clock::min_time); |
| 169 | EXPECT_DEATH(logger.StopLogging(aos::monotonic_clock::min_time), |
| 170 | "Not logging right now"); |
| 171 | }); |
| 172 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // Tests that we can startup twice. |
| 177 | TEST_F(LoggerTest, StartsTwice) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 178 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 179 | const ::std::string base_name1 = tmpdir + "/logfile1"; |
| 180 | const ::std::string logfile1 = base_name1 + ".part0.bfbs"; |
| 181 | const ::std::string base_name2 = tmpdir + "/logfile2"; |
| 182 | const ::std::string logfile2 = base_name2 + ".part0.bfbs"; |
| 183 | unlink(logfile1.c_str()); |
| 184 | unlink(logfile2.c_str()); |
| 185 | |
| 186 | LOG(INFO) << "Logging data to " << logfile1 << " then " << logfile2; |
| 187 | |
| 188 | { |
| 189 | std::unique_ptr<EventLoop> logger_event_loop = |
| 190 | event_loop_factory_.MakeEventLoop("logger"); |
| 191 | |
| 192 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 193 | |
| 194 | Logger logger(logger_event_loop.get()); |
| 195 | logger.set_polling_period(std::chrono::milliseconds(100)); |
| 196 | logger.StartLogging( |
| 197 | std::make_unique<LocalLogNamer>(base_name1, logger_event_loop->node())); |
| 198 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
| 199 | logger.StopLogging(logger_event_loop->monotonic_now()); |
| 200 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
| 201 | logger.StartLogging( |
| 202 | std::make_unique<LocalLogNamer>(base_name2, logger_event_loop->node())); |
| 203 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
| 204 | } |
| 205 | |
| 206 | for (const auto &logfile : |
| 207 | {std::make_tuple(logfile1, 10), std::make_tuple(logfile2, 2010)}) { |
| 208 | SCOPED_TRACE(std::get<0>(logfile)); |
| 209 | LogReader reader(std::get<0>(logfile)); |
| 210 | reader.Register(); |
| 211 | |
| 212 | EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(nullptr)); |
| 213 | |
| 214 | std::unique_ptr<EventLoop> test_event_loop = |
| 215 | reader.event_loop_factory()->MakeEventLoop("log_reader"); |
| 216 | |
| 217 | int ping_count = std::get<1>(logfile); |
| 218 | int pong_count = std::get<1>(logfile); |
| 219 | |
| 220 | // Confirm that the ping and pong counts both match, and the value also |
| 221 | // matches. |
| 222 | test_event_loop->MakeWatcher("/test", |
| 223 | [&ping_count](const examples::Ping &ping) { |
| 224 | EXPECT_EQ(ping.value(), ping_count + 1); |
| 225 | ++ping_count; |
| 226 | }); |
| 227 | test_event_loop->MakeWatcher( |
| 228 | "/test", [&pong_count, &ping_count](const examples::Pong &pong) { |
| 229 | EXPECT_EQ(pong.value(), pong_count + 1); |
| 230 | ++pong_count; |
| 231 | EXPECT_EQ(ping_count, pong_count); |
| 232 | }); |
| 233 | |
| 234 | reader.event_loop_factory()->RunFor(std::chrono::seconds(100)); |
| 235 | EXPECT_EQ(ping_count, std::get<1>(logfile) + 1000); |
| 236 | } |
| 237 | } |
| 238 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 239 | // Tests that we can read and write rotated log files. |
| 240 | TEST_F(LoggerTest, RotatedLogFile) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 241 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 242 | const ::std::string base_name = tmpdir + "/logfile"; |
| 243 | const ::std::string logfile0 = base_name + ".part0.bfbs"; |
| 244 | const ::std::string logfile1 = base_name + ".part1.bfbs"; |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 245 | // Remove it. |
| 246 | unlink(logfile0.c_str()); |
| 247 | unlink(logfile1.c_str()); |
| 248 | |
| 249 | LOG(INFO) << "Logging data to " << logfile0 << " and " << logfile1; |
| 250 | |
| 251 | { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 252 | std::unique_ptr<EventLoop> logger_event_loop = |
| 253 | event_loop_factory_.MakeEventLoop("logger"); |
| 254 | |
| 255 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 256 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 257 | Logger logger(logger_event_loop.get()); |
| 258 | logger.set_polling_period(std::chrono::milliseconds(100)); |
| 259 | logger.StartLoggingLocalNamerOnRun(base_name); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 260 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 261 | logger.Rotate(); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 262 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
| 263 | } |
| 264 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 265 | { |
| 266 | // Confirm that the UUIDs match for both the parts and the logger, and the |
| 267 | // parts_index increments. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 268 | std::vector<SizePrefixedFlatbufferVector<LogFileHeader>> log_header; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 269 | for (std::string_view f : {logfile0, logfile1}) { |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 270 | log_header.emplace_back(ReadHeader(f).value()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 273 | EXPECT_EQ(log_header[0].message().log_event_uuid()->string_view(), |
| 274 | log_header[1].message().log_event_uuid()->string_view()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 275 | EXPECT_EQ(log_header[0].message().parts_uuid()->string_view(), |
| 276 | log_header[1].message().parts_uuid()->string_view()); |
| 277 | |
| 278 | EXPECT_EQ(log_header[0].message().parts_index(), 0); |
| 279 | EXPECT_EQ(log_header[1].message().parts_index(), 1); |
| 280 | } |
| 281 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 282 | // Even though it doesn't make any difference here, exercise the logic for |
| 283 | // passing in a separate config. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 284 | LogReader reader(SortParts({logfile0, logfile1}), &config_.message()); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 285 | |
| 286 | // Confirm that we can remap logged channels to point to new buses. |
| 287 | reader.RemapLoggedChannel<aos::examples::Ping>("/test", "/original"); |
| 288 | |
| 289 | // This sends out the fetched messages and advances time to the start of the |
| 290 | // log file. |
| 291 | reader.Register(); |
| 292 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 293 | EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(nullptr)); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 294 | |
| 295 | std::unique_ptr<EventLoop> test_event_loop = |
| 296 | reader.event_loop_factory()->MakeEventLoop("log_reader"); |
| 297 | |
| 298 | int ping_count = 10; |
| 299 | int pong_count = 10; |
| 300 | |
| 301 | // Confirm that the ping value matches in the remapped channel location. |
| 302 | test_event_loop->MakeWatcher("/original/test", |
| 303 | [&ping_count](const examples::Ping &ping) { |
| 304 | EXPECT_EQ(ping.value(), ping_count + 1); |
| 305 | ++ping_count; |
| 306 | }); |
| 307 | // Confirm that the ping and pong counts both match, and the value also |
| 308 | // matches. |
| 309 | test_event_loop->MakeWatcher( |
| 310 | "/test", [&pong_count, &ping_count](const examples::Pong &pong) { |
| 311 | EXPECT_EQ(pong.value(), pong_count + 1); |
| 312 | ++pong_count; |
| 313 | EXPECT_EQ(ping_count, pong_count); |
| 314 | }); |
| 315 | |
| 316 | reader.event_loop_factory()->RunFor(std::chrono::seconds(100)); |
| 317 | EXPECT_EQ(ping_count, 2010); |
| 318 | } |
| 319 | |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 320 | // Tests that a large number of messages per second doesn't overwhelm writev. |
| 321 | TEST_F(LoggerTest, ManyMessages) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 322 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 323 | const ::std::string base_name = tmpdir + "/logfile"; |
| 324 | const ::std::string logfile = base_name + ".part0.bfbs"; |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 325 | // Remove the log file. |
| 326 | unlink(logfile.c_str()); |
| 327 | |
| 328 | LOG(INFO) << "Logging data to " << logfile; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 329 | ping_.set_quiet(true); |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 330 | |
| 331 | { |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 332 | std::unique_ptr<EventLoop> logger_event_loop = |
| 333 | event_loop_factory_.MakeEventLoop("logger"); |
| 334 | |
| 335 | std::unique_ptr<EventLoop> ping_spammer_event_loop = |
| 336 | event_loop_factory_.MakeEventLoop("ping_spammer"); |
| 337 | aos::Sender<examples::Ping> ping_sender = |
| 338 | ping_spammer_event_loop->MakeSender<examples::Ping>("/test"); |
| 339 | |
| 340 | aos::TimerHandler *timer_handler = |
| 341 | ping_spammer_event_loop->AddTimer([&ping_sender]() { |
| 342 | aos::Sender<examples::Ping>::Builder builder = |
| 343 | ping_sender.MakeBuilder(); |
| 344 | examples::Ping::Builder ping_builder = |
| 345 | builder.MakeBuilder<examples::Ping>(); |
| 346 | CHECK(builder.Send(ping_builder.Finish())); |
| 347 | }); |
| 348 | |
| 349 | // 100 ms / 0.05 ms -> 2000 messages. Should be enough to crash it. |
| 350 | ping_spammer_event_loop->OnRun([&ping_spammer_event_loop, timer_handler]() { |
| 351 | timer_handler->Setup(ping_spammer_event_loop->monotonic_now(), |
| 352 | chrono::microseconds(50)); |
| 353 | }); |
| 354 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 355 | Logger logger(logger_event_loop.get()); |
| 356 | logger.set_polling_period(std::chrono::milliseconds(100)); |
| 357 | logger.StartLoggingLocalNamerOnRun(base_name); |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 358 | |
| 359 | event_loop_factory_.RunFor(chrono::milliseconds(1000)); |
| 360 | } |
| 361 | } |
| 362 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 363 | class MultinodeLoggerTest : public ::testing::Test { |
| 364 | public: |
| 365 | MultinodeLoggerTest() |
| 366 | : config_(aos::configuration::ReadConfig( |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 367 | "aos/events/logging/multinode_pingpong_config.json")), |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 368 | event_loop_factory_(&config_.message()), |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 369 | pi1_( |
| 370 | configuration::GetNode(event_loop_factory_.configuration(), "pi1")), |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 371 | pi2_( |
| 372 | configuration::GetNode(event_loop_factory_.configuration(), "pi2")), |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 373 | tmp_dir_(aos::testing::TestTmpDir()), |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 374 | logfile_base_(tmp_dir_ + "/multi_logfile"), |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 375 | logfiles_( |
| 376 | {logfile_base_ + "_pi1_data.part0.bfbs", |
| 377 | logfile_base_ + "_pi2_data/test/aos.examples.Pong.part0.bfbs", |
| 378 | logfile_base_ + "_pi2_data/test/aos.examples.Pong.part1.bfbs", |
| 379 | logfile_base_ + "_pi2_data.part0.bfbs", |
| 380 | logfile_base_ + "_timestamps/pi1/aos/remote_timestamps/pi2/" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame^] | 381 | "aos.message_bridge.RemoteMessage.part0.bfbs", |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 382 | logfile_base_ + "_timestamps/pi1/aos/remote_timestamps/pi2/" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame^] | 383 | "aos.message_bridge.RemoteMessage.part1.bfbs", |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 384 | logfile_base_ + "_timestamps/pi2/aos/remote_timestamps/pi1/" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame^] | 385 | "aos.message_bridge.RemoteMessage.part0.bfbs", |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 386 | logfile_base_ + "_timestamps/pi2/aos/remote_timestamps/pi1/" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame^] | 387 | "aos.message_bridge.RemoteMessage.part1.bfbs", |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 388 | logfile_base_ + |
| 389 | "_pi1_data/pi1/aos/aos.message_bridge.Timestamp.part0.bfbs", |
| 390 | logfile_base_ + |
| 391 | "_pi1_data/pi1/aos/aos.message_bridge.Timestamp.part1.bfbs", |
| 392 | logfile_base_ + |
| 393 | "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part0.bfbs", |
| 394 | logfile_base_ + |
| 395 | "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part1.bfbs"}), |
| 396 | structured_logfiles_{ |
| 397 | std::vector<std::string>{logfiles_[0]}, |
| 398 | std::vector<std::string>{logfiles_[1], logfiles_[2]}, |
| 399 | std::vector<std::string>{logfiles_[3]}, |
| 400 | std::vector<std::string>{logfiles_[4], logfiles_[5]}, |
| 401 | std::vector<std::string>{logfiles_[6], logfiles_[7]}, |
| 402 | std::vector<std::string>{logfiles_[8], logfiles_[9]}, |
| 403 | std::vector<std::string>{logfiles_[10], logfiles_[11]}}, |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 404 | ping_event_loop_(event_loop_factory_.MakeEventLoop("ping", pi1_)), |
| 405 | ping_(ping_event_loop_.get()), |
| 406 | pong_event_loop_(event_loop_factory_.MakeEventLoop("pong", pi2_)), |
| 407 | pong_(pong_event_loop_.get()) { |
| 408 | // Go through and remove the logfiles if they already exist. |
| 409 | for (const auto file : logfiles_) { |
| 410 | unlink(file.c_str()); |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 411 | unlink((file + ".xz").c_str()); |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | LOG(INFO) << "Logging data to " << logfiles_[0] << ", " << logfiles_[1] |
| 415 | << " and " << logfiles_[2]; |
| 416 | } |
| 417 | |
| 418 | struct LoggerState { |
| 419 | std::unique_ptr<EventLoop> event_loop; |
| 420 | std::unique_ptr<Logger> logger; |
| 421 | }; |
| 422 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 423 | LoggerState MakeLogger(const Node *node, |
| 424 | SimulatedEventLoopFactory *factory = nullptr) { |
| 425 | if (factory == nullptr) { |
| 426 | factory = &event_loop_factory_; |
| 427 | } |
| 428 | return {factory->MakeEventLoop("logger", node), {}}; |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 431 | void StartLogger(LoggerState *logger, std::string logfile_base = "", |
| 432 | bool compress = false) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 433 | if (logfile_base.empty()) { |
| 434 | logfile_base = logfile_base_; |
| 435 | } |
| 436 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 437 | logger->logger = std::make_unique<Logger>(logger->event_loop.get()); |
| 438 | logger->logger->set_polling_period(std::chrono::milliseconds(100)); |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 439 | logger->event_loop->OnRun([logger, logfile_base, compress]() { |
| 440 | std::unique_ptr<MultiNodeLogNamer> namer = |
| 441 | std::make_unique<MultiNodeLogNamer>( |
| 442 | logfile_base, logger->event_loop->configuration(), |
| 443 | logger->event_loop->node()); |
| 444 | if (compress) { |
| 445 | #ifdef LZMA |
| 446 | namer->set_extension(".xz"); |
| 447 | namer->set_encoder_factory( |
| 448 | []() { return std::make_unique<aos::logger::LzmaEncoder>(3); }); |
| 449 | #else |
| 450 | LOG(FATAL) << "Compression unsupported"; |
| 451 | #endif |
| 452 | } |
| 453 | |
| 454 | logger->logger->StartLogging(std::move(namer)); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 455 | }); |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 456 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 457 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 458 | void VerifyParts(const std::vector<LogFile> &sorted_parts, |
| 459 | const std::vector<std::string> &corrupted_parts = {}) { |
| 460 | EXPECT_EQ(sorted_parts.size(), 2u); |
| 461 | |
| 462 | // Count up the number of UUIDs and make sure they are what we expect as a |
| 463 | // sanity check. |
| 464 | std::set<std::string> log_event_uuids; |
| 465 | std::set<std::string> parts_uuids; |
| 466 | std::set<std::string> both_uuids; |
| 467 | |
| 468 | size_t missing_rt_count = 0; |
| 469 | |
| 470 | std::vector<std::string> logger_nodes; |
| 471 | for (const LogFile &log_file : sorted_parts) { |
| 472 | EXPECT_FALSE(log_file.log_event_uuid.empty()); |
| 473 | log_event_uuids.insert(log_file.log_event_uuid); |
| 474 | logger_nodes.emplace_back(log_file.logger_node); |
| 475 | both_uuids.insert(log_file.log_event_uuid); |
| 476 | |
| 477 | for (const LogParts &part : log_file.parts) { |
| 478 | EXPECT_NE(part.monotonic_start_time, aos::monotonic_clock::min_time) |
| 479 | << ": " << part; |
| 480 | missing_rt_count += |
| 481 | part.realtime_start_time == aos::realtime_clock::min_time; |
| 482 | |
| 483 | EXPECT_TRUE(log_event_uuids.find(part.log_event_uuid) != |
| 484 | log_event_uuids.end()); |
| 485 | EXPECT_NE(part.node, ""); |
| 486 | parts_uuids.insert(part.parts_uuid); |
| 487 | both_uuids.insert(part.parts_uuid); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | // We won't have RT timestamps for 5 log files. We don't log the RT start |
| 492 | // time on remote nodes because we don't know it and would be guessing. And |
| 493 | // the log reader can actually do a better job. |
| 494 | EXPECT_EQ(missing_rt_count, 5u); |
| 495 | |
| 496 | EXPECT_EQ(log_event_uuids.size(), 2u); |
| 497 | EXPECT_EQ(parts_uuids.size(), ToLogReaderVector(sorted_parts).size()); |
| 498 | EXPECT_EQ(log_event_uuids.size() + parts_uuids.size(), both_uuids.size()); |
| 499 | |
| 500 | // Test that each list of parts is in order. Don't worry about the ordering |
| 501 | // between part file lists though. |
| 502 | // (inner vectors all need to be in order, but outer one doesn't matter). |
| 503 | EXPECT_THAT(ToLogReaderVector(sorted_parts), |
| 504 | ::testing::UnorderedElementsAreArray(structured_logfiles_)); |
| 505 | |
| 506 | EXPECT_THAT(logger_nodes, ::testing::UnorderedElementsAre("pi1", "pi2")); |
| 507 | |
| 508 | EXPECT_NE(sorted_parts[0].realtime_start_time, |
| 509 | aos::realtime_clock::min_time); |
| 510 | EXPECT_NE(sorted_parts[1].realtime_start_time, |
| 511 | aos::realtime_clock::min_time); |
| 512 | |
| 513 | EXPECT_NE(sorted_parts[0].monotonic_start_time, |
| 514 | aos::monotonic_clock::min_time); |
| 515 | EXPECT_NE(sorted_parts[1].monotonic_start_time, |
| 516 | aos::monotonic_clock::min_time); |
| 517 | |
| 518 | EXPECT_THAT(sorted_parts[0].corrupted, ::testing::Eq(corrupted_parts)); |
| 519 | EXPECT_THAT(sorted_parts[1].corrupted, ::testing::Eq(corrupted_parts)); |
| 520 | } |
| 521 | |
| 522 | void AddExtension(std::string_view extension) { |
| 523 | std::transform(logfiles_.begin(), logfiles_.end(), logfiles_.begin(), |
| 524 | [extension](const std::string &in) { |
| 525 | return absl::StrCat(in, extension); |
| 526 | }); |
| 527 | |
| 528 | std::transform(structured_logfiles_.begin(), structured_logfiles_.end(), |
| 529 | structured_logfiles_.begin(), |
| 530 | [extension](std::vector<std::string> in) { |
| 531 | std::transform(in.begin(), in.end(), in.begin(), |
| 532 | [extension](const std::string &in_str) { |
| 533 | return absl::StrCat(in_str, extension); |
| 534 | }); |
| 535 | return in; |
| 536 | }); |
| 537 | } |
| 538 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 539 | // Config and factory. |
| 540 | aos::FlatbufferDetachedBuffer<aos::Configuration> config_; |
| 541 | SimulatedEventLoopFactory event_loop_factory_; |
| 542 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 543 | const Node *pi1_; |
| 544 | const Node *pi2_; |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 545 | |
| 546 | std::string tmp_dir_; |
| 547 | std::string logfile_base_; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 548 | std::vector<std::string> logfiles_; |
| 549 | |
| 550 | std::vector<std::vector<std::string>> structured_logfiles_; |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 551 | |
| 552 | std::unique_ptr<EventLoop> ping_event_loop_; |
| 553 | Ping ping_; |
| 554 | std::unique_ptr<EventLoop> pong_event_loop_; |
| 555 | Pong pong_; |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 556 | }; |
| 557 | |
Austin Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 558 | // Counts the number of messages on a channel. Returns (channel name, channel |
| 559 | // type, count) for every message matching matcher() |
| 560 | std::vector<std::tuple<std::string, std::string, int>> CountChannelsMatching( |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 561 | std::string_view filename, |
| 562 | std::function<bool(const MessageHeader *)> matcher) { |
| 563 | MessageReader message_reader(filename); |
| 564 | std::vector<int> counts( |
| 565 | message_reader.log_file_header()->configuration()->channels()->size(), 0); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 566 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 567 | while (true) { |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 568 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> msg = |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 569 | message_reader.ReadMessage(); |
| 570 | if (!msg) { |
| 571 | break; |
| 572 | } |
| 573 | |
| 574 | if (matcher(&msg.value().message())) { |
| 575 | counts[msg.value().message().channel_index()]++; |
| 576 | } |
| 577 | } |
| 578 | |
Austin Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 579 | std::vector<std::tuple<std::string, std::string, int>> result; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 580 | int channel = 0; |
| 581 | for (size_t i = 0; i < counts.size(); ++i) { |
| 582 | if (counts[i] != 0) { |
Austin Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 583 | const Channel *channel = |
| 584 | message_reader.log_file_header()->configuration()->channels()->Get(i); |
| 585 | result.push_back(std::make_tuple(channel->name()->str(), |
| 586 | channel->type()->str(), counts[i])); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 587 | } |
| 588 | ++channel; |
| 589 | } |
| 590 | |
| 591 | return result; |
| 592 | } |
| 593 | |
| 594 | // Counts the number of messages (channel, count) for all data messages. |
Austin Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 595 | std::vector<std::tuple<std::string, std::string, int>> CountChannelsData( |
| 596 | std::string_view filename) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 597 | return CountChannelsMatching(filename, [](const MessageHeader *msg) { |
| 598 | if (msg->has_data()) { |
| 599 | CHECK(!msg->has_monotonic_remote_time()); |
| 600 | CHECK(!msg->has_realtime_remote_time()); |
| 601 | CHECK(!msg->has_remote_queue_index()); |
| 602 | return true; |
| 603 | } |
| 604 | return false; |
| 605 | }); |
| 606 | } |
| 607 | |
| 608 | // Counts the number of messages (channel, count) for all timestamp messages. |
Austin Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 609 | std::vector<std::tuple<std::string, std::string, int>> CountChannelsTimestamp( |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 610 | std::string_view filename) { |
| 611 | return CountChannelsMatching(filename, [](const MessageHeader *msg) { |
| 612 | if (!msg->has_data()) { |
| 613 | CHECK(msg->has_monotonic_remote_time()); |
| 614 | CHECK(msg->has_realtime_remote_time()); |
| 615 | CHECK(msg->has_remote_queue_index()); |
| 616 | return true; |
| 617 | } |
| 618 | return false; |
| 619 | }); |
| 620 | } |
| 621 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 622 | // Tests that we can write and read simple multi-node log files. |
| 623 | TEST_F(MultinodeLoggerTest, SimpleMultiNode) { |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 624 | { |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 625 | LoggerState pi1_logger = MakeLogger(pi1_); |
| 626 | LoggerState pi2_logger = MakeLogger(pi2_); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 627 | |
| 628 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 629 | |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 630 | StartLogger(&pi1_logger); |
| 631 | StartLogger(&pi2_logger); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 632 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 633 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 634 | } |
| 635 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 636 | { |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 637 | std::set<std::string> logfile_uuids; |
| 638 | std::set<std::string> parts_uuids; |
| 639 | // Confirm that we have the expected number of UUIDs for both the logfile |
| 640 | // UUIDs and parts UUIDs. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 641 | std::vector<SizePrefixedFlatbufferVector<LogFileHeader>> log_header; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 642 | for (std::string_view f : logfiles_) { |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 643 | log_header.emplace_back(ReadHeader(f).value()); |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 644 | logfile_uuids.insert(log_header.back().message().log_event_uuid()->str()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 645 | parts_uuids.insert(log_header.back().message().parts_uuid()->str()); |
| 646 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 647 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 648 | EXPECT_EQ(logfile_uuids.size(), 2u); |
| 649 | EXPECT_EQ(parts_uuids.size(), 7u); |
| 650 | |
| 651 | // And confirm everything is on the correct node. |
| 652 | EXPECT_EQ(log_header[0].message().node()->name()->string_view(), "pi1"); |
| 653 | EXPECT_EQ(log_header[1].message().node()->name()->string_view(), "pi2"); |
| 654 | EXPECT_EQ(log_header[2].message().node()->name()->string_view(), "pi2"); |
| 655 | EXPECT_EQ(log_header[3].message().node()->name()->string_view(), "pi2"); |
| 656 | EXPECT_EQ(log_header[4].message().node()->name()->string_view(), "pi2"); |
| 657 | EXPECT_EQ(log_header[5].message().node()->name()->string_view(), "pi2"); |
| 658 | EXPECT_EQ(log_header[6].message().node()->name()->string_view(), "pi1"); |
| 659 | EXPECT_EQ(log_header[7].message().node()->name()->string_view(), "pi1"); |
| 660 | EXPECT_EQ(log_header[8].message().node()->name()->string_view(), "pi1"); |
| 661 | EXPECT_EQ(log_header[9].message().node()->name()->string_view(), "pi1"); |
| 662 | EXPECT_EQ(log_header[10].message().node()->name()->string_view(), "pi2"); |
| 663 | EXPECT_EQ(log_header[11].message().node()->name()->string_view(), "pi2"); |
| 664 | |
| 665 | // And the parts index matches. |
| 666 | EXPECT_EQ(log_header[0].message().parts_index(), 0); |
| 667 | EXPECT_EQ(log_header[1].message().parts_index(), 0); |
| 668 | EXPECT_EQ(log_header[2].message().parts_index(), 1); |
| 669 | EXPECT_EQ(log_header[3].message().parts_index(), 0); |
| 670 | EXPECT_EQ(log_header[4].message().parts_index(), 0); |
| 671 | EXPECT_EQ(log_header[5].message().parts_index(), 1); |
| 672 | EXPECT_EQ(log_header[6].message().parts_index(), 0); |
| 673 | EXPECT_EQ(log_header[7].message().parts_index(), 1); |
| 674 | EXPECT_EQ(log_header[8].message().parts_index(), 0); |
| 675 | EXPECT_EQ(log_header[9].message().parts_index(), 1); |
| 676 | EXPECT_EQ(log_header[10].message().parts_index(), 0); |
| 677 | EXPECT_EQ(log_header[11].message().parts_index(), 1); |
| 678 | } |
| 679 | |
| 680 | { |
Austin Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 681 | using ::testing::UnorderedElementsAre; |
| 682 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 683 | // Timing reports, pings |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 684 | EXPECT_THAT( |
| 685 | CountChannelsData(logfiles_[0]), |
| 686 | UnorderedElementsAre( |
| 687 | std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 200), |
| 688 | std::make_tuple("/pi1/aos", "aos.timing.Report", 40), |
| 689 | std::make_tuple("/test", "aos.examples.Ping", 2001))); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 690 | // Timestamps for pong |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 691 | EXPECT_THAT( |
| 692 | CountChannelsTimestamp(logfiles_[0]), |
| 693 | UnorderedElementsAre( |
| 694 | std::make_tuple("/test", "aos.examples.Pong", 2001), |
| 695 | std::make_tuple("/pi2/aos", "aos.message_bridge.Timestamp", 200))); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 696 | |
| 697 | // Pong data. |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 698 | EXPECT_THAT(CountChannelsData(logfiles_[1]), |
Austin Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 699 | UnorderedElementsAre( |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 700 | std::make_tuple("/test", "aos.examples.Pong", 101))); |
| 701 | EXPECT_THAT(CountChannelsData(logfiles_[2]), |
| 702 | UnorderedElementsAre( |
| 703 | std::make_tuple("/test", "aos.examples.Pong", 1900))); |
Austin Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 704 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 705 | // No timestamps |
Austin Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 706 | EXPECT_THAT(CountChannelsTimestamp(logfiles_[1]), UnorderedElementsAre()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 707 | EXPECT_THAT(CountChannelsTimestamp(logfiles_[2]), UnorderedElementsAre()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 708 | |
| 709 | // Timing reports and pongs. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 710 | EXPECT_THAT( |
| 711 | CountChannelsData(logfiles_[3]), |
| 712 | UnorderedElementsAre( |
| 713 | std::make_tuple("/pi2/aos", "aos.message_bridge.Timestamp", 200), |
| 714 | std::make_tuple("/pi2/aos", "aos.timing.Report", 40), |
| 715 | std::make_tuple("/test", "aos.examples.Pong", 2001))); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 716 | // And ping timestamps. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 717 | EXPECT_THAT( |
| 718 | CountChannelsTimestamp(logfiles_[3]), |
| 719 | UnorderedElementsAre( |
| 720 | std::make_tuple("/test", "aos.examples.Ping", 2001), |
| 721 | std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 200))); |
| 722 | |
| 723 | // Timestamps from pi2 on pi1, and the other way. |
| 724 | EXPECT_THAT(CountChannelsData(logfiles_[4]), UnorderedElementsAre()); |
| 725 | EXPECT_THAT(CountChannelsData(logfiles_[5]), UnorderedElementsAre()); |
| 726 | EXPECT_THAT(CountChannelsData(logfiles_[6]), UnorderedElementsAre()); |
| 727 | EXPECT_THAT(CountChannelsData(logfiles_[7]), UnorderedElementsAre()); |
| 728 | EXPECT_THAT( |
| 729 | CountChannelsTimestamp(logfiles_[4]), |
| 730 | UnorderedElementsAre( |
| 731 | std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 10), |
| 732 | std::make_tuple("/test", "aos.examples.Ping", 101))); |
| 733 | EXPECT_THAT( |
| 734 | CountChannelsTimestamp(logfiles_[5]), |
| 735 | UnorderedElementsAre( |
| 736 | std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 190), |
| 737 | std::make_tuple("/test", "aos.examples.Ping", 1900))); |
| 738 | EXPECT_THAT(CountChannelsTimestamp(logfiles_[6]), |
| 739 | UnorderedElementsAre(std::make_tuple( |
| 740 | "/pi2/aos", "aos.message_bridge.Timestamp", 10))); |
| 741 | EXPECT_THAT(CountChannelsTimestamp(logfiles_[7]), |
| 742 | UnorderedElementsAre(std::make_tuple( |
| 743 | "/pi2/aos", "aos.message_bridge.Timestamp", 190))); |
| 744 | |
| 745 | // And then test that the remotely logged timestamp data files only have |
| 746 | // timestamps in them. |
| 747 | EXPECT_THAT(CountChannelsTimestamp(logfiles_[8]), UnorderedElementsAre()); |
| 748 | EXPECT_THAT(CountChannelsTimestamp(logfiles_[9]), UnorderedElementsAre()); |
| 749 | EXPECT_THAT(CountChannelsTimestamp(logfiles_[10]), UnorderedElementsAre()); |
| 750 | EXPECT_THAT(CountChannelsTimestamp(logfiles_[11]), UnorderedElementsAre()); |
| 751 | |
| 752 | EXPECT_THAT(CountChannelsData(logfiles_[8]), |
| 753 | UnorderedElementsAre(std::make_tuple( |
| 754 | "/pi1/aos", "aos.message_bridge.Timestamp", 10))); |
| 755 | EXPECT_THAT(CountChannelsData(logfiles_[9]), |
| 756 | UnorderedElementsAre(std::make_tuple( |
| 757 | "/pi1/aos", "aos.message_bridge.Timestamp", 190))); |
| 758 | |
| 759 | EXPECT_THAT(CountChannelsData(logfiles_[10]), |
| 760 | UnorderedElementsAre(std::make_tuple( |
| 761 | "/pi2/aos", "aos.message_bridge.Timestamp", 10))); |
| 762 | EXPECT_THAT(CountChannelsData(logfiles_[11]), |
| 763 | UnorderedElementsAre(std::make_tuple( |
| 764 | "/pi2/aos", "aos.message_bridge.Timestamp", 190))); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 765 | } |
| 766 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 767 | LogReader reader(SortParts(logfiles_)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 768 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 769 | SimulatedEventLoopFactory log_reader_factory(reader.configuration()); |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 770 | log_reader_factory.set_send_delay(chrono::microseconds(0)); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 771 | |
| 772 | // This sends out the fetched messages and advances time to the start of the |
| 773 | // log file. |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 774 | reader.Register(&log_reader_factory); |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 775 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 776 | const Node *pi1 = |
| 777 | configuration::GetNode(log_reader_factory.configuration(), "pi1"); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 778 | const Node *pi2 = |
| 779 | configuration::GetNode(log_reader_factory.configuration(), "pi2"); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 780 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 781 | LOG(INFO) << "Start time " << reader.monotonic_start_time(pi1) << " pi1"; |
| 782 | LOG(INFO) << "Start time " << reader.monotonic_start_time(pi2) << " pi2"; |
| 783 | LOG(INFO) << "now pi1 " |
| 784 | << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now(); |
| 785 | LOG(INFO) << "now pi2 " |
| 786 | << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now(); |
| 787 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 788 | EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(pi1, pi2)); |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 789 | |
| 790 | reader.event_loop_factory()->set_send_delay(chrono::microseconds(0)); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 791 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 792 | std::unique_ptr<EventLoop> pi1_event_loop = |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 793 | log_reader_factory.MakeEventLoop("test", pi1); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 794 | std::unique_ptr<EventLoop> pi2_event_loop = |
| 795 | log_reader_factory.MakeEventLoop("test", pi2); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 796 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 797 | int pi1_ping_count = 10; |
| 798 | int pi2_ping_count = 10; |
| 799 | int pi1_pong_count = 10; |
| 800 | int pi2_pong_count = 10; |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 801 | |
| 802 | // Confirm that the ping value matches. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 803 | pi1_event_loop->MakeWatcher( |
| 804 | "/test", [&pi1_ping_count, &pi1_event_loop](const examples::Ping &ping) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 805 | VLOG(1) << "Pi1 ping " << FlatbufferToJson(&ping) << " at " |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 806 | << pi1_event_loop->context().monotonic_remote_time << " -> " |
| 807 | << pi1_event_loop->context().monotonic_event_time; |
| 808 | EXPECT_EQ(ping.value(), pi1_ping_count + 1); |
| 809 | EXPECT_EQ(pi1_event_loop->context().monotonic_remote_time, |
| 810 | pi1_ping_count * chrono::milliseconds(10) + |
| 811 | monotonic_clock::epoch()); |
| 812 | EXPECT_EQ(pi1_event_loop->context().realtime_remote_time, |
| 813 | pi1_ping_count * chrono::milliseconds(10) + |
| 814 | realtime_clock::epoch()); |
| 815 | EXPECT_EQ(pi1_event_loop->context().monotonic_remote_time, |
| 816 | pi1_event_loop->context().monotonic_event_time); |
| 817 | EXPECT_EQ(pi1_event_loop->context().realtime_remote_time, |
| 818 | pi1_event_loop->context().realtime_event_time); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 819 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 820 | ++pi1_ping_count; |
| 821 | }); |
| 822 | pi2_event_loop->MakeWatcher( |
| 823 | "/test", [&pi2_ping_count, &pi2_event_loop](const examples::Ping &ping) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 824 | VLOG(1) << "Pi2 ping " << FlatbufferToJson(&ping) << " at " |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 825 | << pi2_event_loop->context().monotonic_remote_time << " -> " |
| 826 | << pi2_event_loop->context().monotonic_event_time; |
| 827 | EXPECT_EQ(ping.value(), pi2_ping_count + 1); |
| 828 | |
| 829 | EXPECT_EQ(pi2_event_loop->context().monotonic_remote_time, |
| 830 | pi2_ping_count * chrono::milliseconds(10) + |
| 831 | monotonic_clock::epoch()); |
| 832 | EXPECT_EQ(pi2_event_loop->context().realtime_remote_time, |
| 833 | pi2_ping_count * chrono::milliseconds(10) + |
| 834 | realtime_clock::epoch()); |
| 835 | EXPECT_EQ(pi2_event_loop->context().monotonic_remote_time + |
| 836 | chrono::microseconds(150), |
| 837 | pi2_event_loop->context().monotonic_event_time); |
| 838 | EXPECT_EQ(pi2_event_loop->context().realtime_remote_time + |
| 839 | chrono::microseconds(150), |
| 840 | pi2_event_loop->context().realtime_event_time); |
| 841 | ++pi2_ping_count; |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 842 | }); |
| 843 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 844 | constexpr ssize_t kQueueIndexOffset = -9; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 845 | // Confirm that the ping and pong counts both match, and the value also |
| 846 | // matches. |
| 847 | pi1_event_loop->MakeWatcher( |
| 848 | "/test", [&pi1_event_loop, &pi1_ping_count, |
| 849 | &pi1_pong_count](const examples::Pong &pong) { |
| 850 | VLOG(1) << "Pi1 pong " << FlatbufferToJson(&pong) << " at " |
| 851 | << pi1_event_loop->context().monotonic_remote_time << " -> " |
| 852 | << pi1_event_loop->context().monotonic_event_time; |
| 853 | |
| 854 | EXPECT_EQ(pi1_event_loop->context().remote_queue_index, |
| 855 | pi1_pong_count + kQueueIndexOffset); |
| 856 | EXPECT_EQ(pi1_event_loop->context().monotonic_remote_time, |
| 857 | chrono::microseconds(200) + |
| 858 | pi1_pong_count * chrono::milliseconds(10) + |
| 859 | monotonic_clock::epoch()); |
| 860 | EXPECT_EQ(pi1_event_loop->context().realtime_remote_time, |
| 861 | chrono::microseconds(200) + |
| 862 | pi1_pong_count * chrono::milliseconds(10) + |
| 863 | realtime_clock::epoch()); |
| 864 | |
| 865 | EXPECT_EQ(pi1_event_loop->context().monotonic_remote_time + |
| 866 | chrono::microseconds(150), |
| 867 | pi1_event_loop->context().monotonic_event_time); |
| 868 | EXPECT_EQ(pi1_event_loop->context().realtime_remote_time + |
| 869 | chrono::microseconds(150), |
| 870 | pi1_event_loop->context().realtime_event_time); |
| 871 | |
| 872 | EXPECT_EQ(pong.value(), pi1_pong_count + 1); |
| 873 | ++pi1_pong_count; |
| 874 | EXPECT_EQ(pi1_ping_count, pi1_pong_count); |
| 875 | }); |
| 876 | pi2_event_loop->MakeWatcher( |
| 877 | "/test", [&pi2_event_loop, &pi2_ping_count, |
| 878 | &pi2_pong_count](const examples::Pong &pong) { |
| 879 | VLOG(1) << "Pi2 pong " << FlatbufferToJson(&pong) << " at " |
| 880 | << pi2_event_loop->context().monotonic_remote_time << " -> " |
| 881 | << pi2_event_loop->context().monotonic_event_time; |
| 882 | |
| 883 | EXPECT_EQ(pi2_event_loop->context().remote_queue_index, |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 884 | pi2_pong_count + kQueueIndexOffset); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 885 | |
| 886 | EXPECT_EQ(pi2_event_loop->context().monotonic_remote_time, |
| 887 | chrono::microseconds(200) + |
| 888 | pi2_pong_count * chrono::milliseconds(10) + |
| 889 | monotonic_clock::epoch()); |
| 890 | EXPECT_EQ(pi2_event_loop->context().realtime_remote_time, |
| 891 | chrono::microseconds(200) + |
| 892 | pi2_pong_count * chrono::milliseconds(10) + |
| 893 | realtime_clock::epoch()); |
| 894 | |
| 895 | EXPECT_EQ(pi2_event_loop->context().monotonic_remote_time, |
| 896 | pi2_event_loop->context().monotonic_event_time); |
| 897 | EXPECT_EQ(pi2_event_loop->context().realtime_remote_time, |
| 898 | pi2_event_loop->context().realtime_event_time); |
| 899 | |
| 900 | EXPECT_EQ(pong.value(), pi2_pong_count + 1); |
| 901 | ++pi2_pong_count; |
| 902 | EXPECT_EQ(pi2_ping_count, pi2_pong_count); |
| 903 | }); |
| 904 | |
| 905 | log_reader_factory.Run(); |
| 906 | EXPECT_EQ(pi1_ping_count, 2010); |
| 907 | EXPECT_EQ(pi2_ping_count, 2010); |
| 908 | EXPECT_EQ(pi1_pong_count, 2010); |
| 909 | EXPECT_EQ(pi2_pong_count, 2010); |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 910 | |
| 911 | reader.Deregister(); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 912 | } |
| 913 | |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 914 | typedef MultinodeLoggerTest MultinodeLoggerDeathTest; |
| 915 | |
| 916 | // Test that if we feed the replay with a mismatched node list that we die on |
| 917 | // the LogReader constructor. |
| 918 | TEST_F(MultinodeLoggerDeathTest, MultiNodeBadReplayConfig) { |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 919 | { |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 920 | LoggerState pi1_logger = MakeLogger(pi1_); |
| 921 | LoggerState pi2_logger = MakeLogger(pi2_); |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 922 | |
| 923 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 924 | |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 925 | StartLogger(&pi1_logger); |
| 926 | StartLogger(&pi2_logger); |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 927 | |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 928 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 929 | } |
| 930 | |
| 931 | // Test that, if we add an additional node to the replay config that the |
| 932 | // logger complains about the mismatch in number of nodes. |
| 933 | FlatbufferDetachedBuffer<Configuration> extra_nodes_config = |
| 934 | configuration::MergeWithConfig(&config_.message(), R"({ |
| 935 | "nodes": [ |
| 936 | { |
| 937 | "name": "extra-node" |
| 938 | } |
| 939 | ] |
| 940 | } |
| 941 | )"); |
| 942 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 943 | const std::vector<LogFile> sorted_parts = SortParts(logfiles_); |
| 944 | EXPECT_DEATH(LogReader(sorted_parts, &extra_nodes_config.message()), |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 945 | "Log file and replay config need to have matching nodes lists."); |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 948 | // Tests that we can read log files where they don't start at the same monotonic |
| 949 | // time. |
| 950 | TEST_F(MultinodeLoggerTest, StaggeredStart) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 951 | { |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 952 | LoggerState pi1_logger = MakeLogger(pi1_); |
| 953 | LoggerState pi2_logger = MakeLogger(pi2_); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 954 | |
| 955 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 956 | |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 957 | StartLogger(&pi1_logger); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 958 | |
| 959 | event_loop_factory_.RunFor(chrono::milliseconds(200)); |
| 960 | |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 961 | StartLogger(&pi2_logger); |
| 962 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 963 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 964 | } |
| 965 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 966 | LogReader reader(SortParts(logfiles_)); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 967 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 968 | SimulatedEventLoopFactory log_reader_factory(reader.configuration()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 969 | log_reader_factory.set_send_delay(chrono::microseconds(0)); |
| 970 | |
| 971 | // This sends out the fetched messages and advances time to the start of the |
| 972 | // log file. |
| 973 | reader.Register(&log_reader_factory); |
| 974 | |
| 975 | const Node *pi1 = |
| 976 | configuration::GetNode(log_reader_factory.configuration(), "pi1"); |
| 977 | const Node *pi2 = |
| 978 | configuration::GetNode(log_reader_factory.configuration(), "pi2"); |
| 979 | |
| 980 | EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(pi1, pi2)); |
| 981 | |
| 982 | reader.event_loop_factory()->set_send_delay(chrono::microseconds(0)); |
| 983 | |
| 984 | std::unique_ptr<EventLoop> pi1_event_loop = |
| 985 | log_reader_factory.MakeEventLoop("test", pi1); |
| 986 | std::unique_ptr<EventLoop> pi2_event_loop = |
| 987 | log_reader_factory.MakeEventLoop("test", pi2); |
| 988 | |
| 989 | int pi1_ping_count = 30; |
| 990 | int pi2_ping_count = 30; |
| 991 | int pi1_pong_count = 30; |
| 992 | int pi2_pong_count = 30; |
| 993 | |
| 994 | // Confirm that the ping value matches. |
| 995 | pi1_event_loop->MakeWatcher( |
| 996 | "/test", [&pi1_ping_count, &pi1_event_loop](const examples::Ping &ping) { |
| 997 | VLOG(1) << "Pi1 ping " << FlatbufferToJson(&ping) |
| 998 | << pi1_event_loop->context().monotonic_remote_time << " -> " |
| 999 | << pi1_event_loop->context().monotonic_event_time; |
| 1000 | EXPECT_EQ(ping.value(), pi1_ping_count + 1); |
| 1001 | |
| 1002 | ++pi1_ping_count; |
| 1003 | }); |
| 1004 | pi2_event_loop->MakeWatcher( |
| 1005 | "/test", [&pi2_ping_count, &pi2_event_loop](const examples::Ping &ping) { |
| 1006 | VLOG(1) << "Pi2 ping " << FlatbufferToJson(&ping) |
| 1007 | << pi2_event_loop->context().monotonic_remote_time << " -> " |
| 1008 | << pi2_event_loop->context().monotonic_event_time; |
| 1009 | EXPECT_EQ(ping.value(), pi2_ping_count + 1); |
| 1010 | |
| 1011 | ++pi2_ping_count; |
| 1012 | }); |
| 1013 | |
| 1014 | // Confirm that the ping and pong counts both match, and the value also |
| 1015 | // matches. |
| 1016 | pi1_event_loop->MakeWatcher( |
| 1017 | "/test", [&pi1_event_loop, &pi1_ping_count, |
| 1018 | &pi1_pong_count](const examples::Pong &pong) { |
| 1019 | VLOG(1) << "Pi1 pong " << FlatbufferToJson(&pong) << " at " |
| 1020 | << pi1_event_loop->context().monotonic_remote_time << " -> " |
| 1021 | << pi1_event_loop->context().monotonic_event_time; |
| 1022 | |
| 1023 | EXPECT_EQ(pong.value(), pi1_pong_count + 1); |
| 1024 | ++pi1_pong_count; |
| 1025 | EXPECT_EQ(pi1_ping_count, pi1_pong_count); |
| 1026 | }); |
| 1027 | pi2_event_loop->MakeWatcher( |
| 1028 | "/test", [&pi2_event_loop, &pi2_ping_count, |
| 1029 | &pi2_pong_count](const examples::Pong &pong) { |
| 1030 | VLOG(1) << "Pi2 pong " << FlatbufferToJson(&pong) << " at " |
| 1031 | << pi2_event_loop->context().monotonic_remote_time << " -> " |
| 1032 | << pi2_event_loop->context().monotonic_event_time; |
| 1033 | |
| 1034 | EXPECT_EQ(pong.value(), pi2_pong_count + 1); |
| 1035 | ++pi2_pong_count; |
| 1036 | EXPECT_EQ(pi2_ping_count, pi2_pong_count); |
| 1037 | }); |
| 1038 | |
| 1039 | log_reader_factory.Run(); |
| 1040 | EXPECT_EQ(pi1_ping_count, 2030); |
| 1041 | EXPECT_EQ(pi2_ping_count, 2030); |
| 1042 | EXPECT_EQ(pi1_pong_count, 2030); |
| 1043 | EXPECT_EQ(pi2_pong_count, 2030); |
| 1044 | |
| 1045 | reader.Deregister(); |
| 1046 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1047 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1048 | // Tests that we can read log files where the monotonic clocks drift and don't |
| 1049 | // match correctly. While we are here, also test that different ending times |
| 1050 | // also is readable. |
| 1051 | TEST_F(MultinodeLoggerTest, MismatchedClocks) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1052 | { |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 1053 | LoggerState pi2_logger = MakeLogger(pi2_); |
| 1054 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1055 | NodeEventLoopFactory *pi2 = |
| 1056 | event_loop_factory_.GetNodeEventLoopFactory(pi2_); |
| 1057 | LOG(INFO) << "pi2 times: " << pi2->monotonic_now() << " " |
| 1058 | << pi2->realtime_now() << " distributed " |
| 1059 | << pi2->ToDistributedClock(pi2->monotonic_now()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1060 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1061 | const chrono::nanoseconds initial_pi2_offset = -chrono::seconds(1000); |
| 1062 | chrono::nanoseconds pi2_offset = initial_pi2_offset; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1063 | |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 1064 | pi2->SetDistributedOffset(-pi2_offset, 1.0); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1065 | LOG(INFO) << "pi2 times: " << pi2->monotonic_now() << " " |
| 1066 | << pi2->realtime_now() << " distributed " |
| 1067 | << pi2->ToDistributedClock(pi2->monotonic_now()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1068 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1069 | for (int i = 0; i < 95; ++i) { |
| 1070 | pi2_offset += chrono::nanoseconds(200); |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 1071 | pi2->SetDistributedOffset(-pi2_offset, 1.0); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1072 | event_loop_factory_.RunFor(chrono::milliseconds(1)); |
| 1073 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1074 | |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 1075 | StartLogger(&pi2_logger); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1076 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1077 | event_loop_factory_.RunFor(chrono::milliseconds(200)); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1078 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1079 | { |
| 1080 | // Run pi1's logger for only part of the time. |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 1081 | LoggerState pi1_logger = MakeLogger(pi1_); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1082 | |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 1083 | StartLogger(&pi1_logger); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1084 | |
| 1085 | for (int i = 0; i < 20000; ++i) { |
| 1086 | pi2_offset += chrono::nanoseconds(200); |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 1087 | pi2->SetDistributedOffset(-pi2_offset, 1.0); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1088 | event_loop_factory_.RunFor(chrono::milliseconds(1)); |
| 1089 | } |
| 1090 | |
| 1091 | EXPECT_GT(pi2_offset - initial_pi2_offset, |
| 1092 | event_loop_factory_.send_delay() + |
| 1093 | event_loop_factory_.network_delay()); |
| 1094 | |
| 1095 | for (int i = 0; i < 40000; ++i) { |
| 1096 | pi2_offset -= chrono::nanoseconds(200); |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 1097 | pi2->SetDistributedOffset(-pi2_offset, 1.0); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1098 | event_loop_factory_.RunFor(chrono::milliseconds(1)); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | // And log a bit more on pi2. |
| 1103 | event_loop_factory_.RunFor(chrono::milliseconds(400)); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1104 | } |
| 1105 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1106 | LogReader reader(SortParts(logfiles_)); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1107 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1108 | SimulatedEventLoopFactory log_reader_factory(reader.configuration()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1109 | log_reader_factory.set_send_delay(chrono::microseconds(0)); |
| 1110 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1111 | const Node *pi1 = |
| 1112 | configuration::GetNode(log_reader_factory.configuration(), "pi1"); |
| 1113 | const Node *pi2 = |
| 1114 | configuration::GetNode(log_reader_factory.configuration(), "pi2"); |
| 1115 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1116 | // This sends out the fetched messages and advances time to the start of the |
| 1117 | // log file. |
| 1118 | reader.Register(&log_reader_factory); |
| 1119 | |
| 1120 | LOG(INFO) << "Start time " << reader.monotonic_start_time(pi1) << " pi1"; |
| 1121 | LOG(INFO) << "Start time " << reader.monotonic_start_time(pi2) << " pi2"; |
| 1122 | LOG(INFO) << "now pi1 " |
| 1123 | << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now(); |
| 1124 | LOG(INFO) << "now pi2 " |
| 1125 | << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now(); |
| 1126 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1127 | LOG(INFO) << "Done registering (pi1) " |
Austin Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 1128 | << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now() |
| 1129 | << " " |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1130 | << log_reader_factory.GetNodeEventLoopFactory(pi1)->realtime_now(); |
| 1131 | LOG(INFO) << "Done registering (pi2) " |
Austin Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 1132 | << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now() |
| 1133 | << " " |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1134 | << log_reader_factory.GetNodeEventLoopFactory(pi2)->realtime_now(); |
| 1135 | |
| 1136 | EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(pi1, pi2)); |
| 1137 | |
| 1138 | reader.event_loop_factory()->set_send_delay(chrono::microseconds(0)); |
| 1139 | |
| 1140 | std::unique_ptr<EventLoop> pi1_event_loop = |
| 1141 | log_reader_factory.MakeEventLoop("test", pi1); |
| 1142 | std::unique_ptr<EventLoop> pi2_event_loop = |
| 1143 | log_reader_factory.MakeEventLoop("test", pi2); |
| 1144 | |
| 1145 | int pi1_ping_count = 30; |
| 1146 | int pi2_ping_count = 30; |
| 1147 | int pi1_pong_count = 30; |
| 1148 | int pi2_pong_count = 30; |
| 1149 | |
| 1150 | // Confirm that the ping value matches. |
| 1151 | pi1_event_loop->MakeWatcher( |
| 1152 | "/test", [&pi1_ping_count, &pi1_event_loop](const examples::Ping &ping) { |
| 1153 | VLOG(1) << "Pi1 ping " << FlatbufferToJson(&ping) |
| 1154 | << pi1_event_loop->context().monotonic_remote_time << " -> " |
| 1155 | << pi1_event_loop->context().monotonic_event_time; |
| 1156 | EXPECT_EQ(ping.value(), pi1_ping_count + 1); |
| 1157 | |
| 1158 | ++pi1_ping_count; |
| 1159 | }); |
| 1160 | pi2_event_loop->MakeWatcher( |
| 1161 | "/test", [&pi2_ping_count, &pi2_event_loop](const examples::Ping &ping) { |
| 1162 | VLOG(1) << "Pi2 ping " << FlatbufferToJson(&ping) |
| 1163 | << pi2_event_loop->context().monotonic_remote_time << " -> " |
| 1164 | << pi2_event_loop->context().monotonic_event_time; |
| 1165 | EXPECT_EQ(ping.value(), pi2_ping_count + 1); |
| 1166 | |
| 1167 | ++pi2_ping_count; |
| 1168 | }); |
| 1169 | |
| 1170 | // Confirm that the ping and pong counts both match, and the value also |
| 1171 | // matches. |
| 1172 | pi1_event_loop->MakeWatcher( |
| 1173 | "/test", [&pi1_event_loop, &pi1_ping_count, |
| 1174 | &pi1_pong_count](const examples::Pong &pong) { |
| 1175 | VLOG(1) << "Pi1 pong " << FlatbufferToJson(&pong) << " at " |
| 1176 | << pi1_event_loop->context().monotonic_remote_time << " -> " |
| 1177 | << pi1_event_loop->context().monotonic_event_time; |
| 1178 | |
| 1179 | EXPECT_EQ(pong.value(), pi1_pong_count + 1); |
| 1180 | ++pi1_pong_count; |
| 1181 | EXPECT_EQ(pi1_ping_count, pi1_pong_count); |
| 1182 | }); |
| 1183 | pi2_event_loop->MakeWatcher( |
| 1184 | "/test", [&pi2_event_loop, &pi2_ping_count, |
| 1185 | &pi2_pong_count](const examples::Pong &pong) { |
| 1186 | VLOG(1) << "Pi2 pong " << FlatbufferToJson(&pong) << " at " |
| 1187 | << pi2_event_loop->context().monotonic_remote_time << " -> " |
| 1188 | << pi2_event_loop->context().monotonic_event_time; |
| 1189 | |
| 1190 | EXPECT_EQ(pong.value(), pi2_pong_count + 1); |
| 1191 | ++pi2_pong_count; |
| 1192 | EXPECT_EQ(pi2_ping_count, pi2_pong_count); |
| 1193 | }); |
| 1194 | |
| 1195 | log_reader_factory.Run(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1196 | EXPECT_EQ(pi1_ping_count, 6030); |
| 1197 | EXPECT_EQ(pi2_ping_count, 6030); |
| 1198 | EXPECT_EQ(pi1_pong_count, 6030); |
| 1199 | EXPECT_EQ(pi2_pong_count, 6030); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1200 | |
| 1201 | reader.Deregister(); |
| 1202 | } |
| 1203 | |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 1204 | // Tests that we can sort a bunch of parts into the pre-determined sorted parts. |
| 1205 | TEST_F(MultinodeLoggerTest, SortParts) { |
| 1206 | // Make a bunch of parts. |
| 1207 | { |
| 1208 | LoggerState pi1_logger = MakeLogger(pi1_); |
| 1209 | LoggerState pi2_logger = MakeLogger(pi2_); |
| 1210 | |
| 1211 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 1212 | |
| 1213 | StartLogger(&pi1_logger); |
| 1214 | StartLogger(&pi2_logger); |
| 1215 | |
| 1216 | event_loop_factory_.RunFor(chrono::milliseconds(2000)); |
| 1217 | } |
| 1218 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1219 | const std::vector<LogFile> sorted_parts = SortParts(logfiles_); |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1220 | VerifyParts(sorted_parts); |
| 1221 | } |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1222 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1223 | // Tests that we can sort a bunch of parts with an empty part. We should ignore |
| 1224 | // it and remove it from the sorted list. |
| 1225 | TEST_F(MultinodeLoggerTest, SortEmptyParts) { |
| 1226 | // Make a bunch of parts. |
| 1227 | { |
| 1228 | LoggerState pi1_logger = MakeLogger(pi1_); |
| 1229 | LoggerState pi2_logger = MakeLogger(pi2_); |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1230 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1231 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1232 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1233 | StartLogger(&pi1_logger); |
| 1234 | StartLogger(&pi2_logger); |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1235 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1236 | event_loop_factory_.RunFor(chrono::milliseconds(2000)); |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1239 | // TODO(austin): Should we flip out if the file can't open? |
| 1240 | const std::string kEmptyFile("foobarinvalidfiledoesnotexist.bfbs"); |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1241 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1242 | aos::util::WriteStringToFileOrDie(kEmptyFile, ""); |
| 1243 | logfiles_.emplace_back(kEmptyFile); |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 1244 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1245 | const std::vector<LogFile> sorted_parts = SortParts(logfiles_); |
| 1246 | VerifyParts(sorted_parts, {kEmptyFile}); |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1249 | #ifdef LZMA |
| 1250 | // Tests that we can sort a bunch of parts with an empty .xz file in there. The |
| 1251 | // empty file should be ignored. |
| 1252 | TEST_F(MultinodeLoggerTest, SortEmptyCompressedParts) { |
| 1253 | // Make a bunch of parts. |
| 1254 | { |
| 1255 | LoggerState pi1_logger = MakeLogger(pi1_); |
| 1256 | LoggerState pi2_logger = MakeLogger(pi2_); |
| 1257 | |
| 1258 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 1259 | |
| 1260 | StartLogger(&pi1_logger, "", true); |
| 1261 | StartLogger(&pi2_logger, "", true); |
| 1262 | |
| 1263 | event_loop_factory_.RunFor(chrono::milliseconds(2000)); |
| 1264 | } |
| 1265 | |
| 1266 | // TODO(austin): Should we flip out if the file can't open? |
| 1267 | const std::string kEmptyFile("foobarinvalidfiledoesnotexist.bfbs.xz"); |
| 1268 | |
| 1269 | AddExtension(".xz"); |
| 1270 | |
| 1271 | aos::util::WriteStringToFileOrDie(kEmptyFile, ""); |
| 1272 | logfiles_.emplace_back(kEmptyFile); |
| 1273 | |
| 1274 | const std::vector<LogFile> sorted_parts = SortParts(logfiles_); |
| 1275 | VerifyParts(sorted_parts, {kEmptyFile}); |
| 1276 | } |
| 1277 | |
| 1278 | // Tests that we can sort a bunch of parts with the end missing off a compressed |
| 1279 | // file. We should use the part we can read. |
| 1280 | TEST_F(MultinodeLoggerTest, SortTruncatedCompressedParts) { |
| 1281 | // Make a bunch of parts. |
| 1282 | { |
| 1283 | LoggerState pi1_logger = MakeLogger(pi1_); |
| 1284 | LoggerState pi2_logger = MakeLogger(pi2_); |
| 1285 | |
| 1286 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 1287 | |
| 1288 | StartLogger(&pi1_logger, "", true); |
| 1289 | StartLogger(&pi2_logger, "", true); |
| 1290 | |
| 1291 | event_loop_factory_.RunFor(chrono::milliseconds(2000)); |
| 1292 | } |
| 1293 | |
| 1294 | // Append everything with .xz. |
| 1295 | AddExtension(".xz"); |
| 1296 | |
| 1297 | // Strip off the end of one of the files. Pick one with a lot of data. |
| 1298 | ::std::string compressed_contents = |
| 1299 | aos::util::ReadFileToStringOrDie(logfiles_[0]); |
| 1300 | |
| 1301 | aos::util::WriteStringToFileOrDie( |
| 1302 | logfiles_[0], |
| 1303 | compressed_contents.substr(0, compressed_contents.size() - 100)); |
| 1304 | |
| 1305 | const std::vector<LogFile> sorted_parts = SortParts(logfiles_); |
| 1306 | VerifyParts(sorted_parts); |
| 1307 | } |
| 1308 | #endif |
| 1309 | |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1310 | // Tests that if we remap a remapped channel, it shows up correctly. |
| 1311 | TEST_F(MultinodeLoggerTest, RemapLoggedChannel) { |
| 1312 | { |
| 1313 | LoggerState pi1_logger = MakeLogger(pi1_); |
| 1314 | LoggerState pi2_logger = MakeLogger(pi2_); |
| 1315 | |
| 1316 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 1317 | |
| 1318 | StartLogger(&pi1_logger); |
| 1319 | StartLogger(&pi2_logger); |
| 1320 | |
| 1321 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 1322 | } |
| 1323 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1324 | LogReader reader(SortParts(logfiles_)); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1325 | |
| 1326 | // Remap just on pi1. |
| 1327 | reader.RemapLoggedChannel<aos::timing::Report>( |
| 1328 | "/aos", configuration::GetNode(reader.configuration(), "pi1")); |
| 1329 | |
| 1330 | SimulatedEventLoopFactory log_reader_factory(reader.configuration()); |
| 1331 | log_reader_factory.set_send_delay(chrono::microseconds(0)); |
| 1332 | |
| 1333 | reader.Register(&log_reader_factory); |
| 1334 | |
| 1335 | const Node *pi1 = |
| 1336 | configuration::GetNode(log_reader_factory.configuration(), "pi1"); |
| 1337 | const Node *pi2 = |
| 1338 | configuration::GetNode(log_reader_factory.configuration(), "pi2"); |
| 1339 | |
| 1340 | // Confirm we can read the data on the remapped channel, just for pi1. Nothing |
| 1341 | // else should have moved. |
| 1342 | std::unique_ptr<EventLoop> pi1_event_loop = |
| 1343 | log_reader_factory.MakeEventLoop("test", pi1); |
| 1344 | pi1_event_loop->SkipTimingReport(); |
| 1345 | std::unique_ptr<EventLoop> full_pi1_event_loop = |
| 1346 | log_reader_factory.MakeEventLoop("test", pi1); |
| 1347 | full_pi1_event_loop->SkipTimingReport(); |
| 1348 | std::unique_ptr<EventLoop> pi2_event_loop = |
| 1349 | log_reader_factory.MakeEventLoop("test", pi2); |
| 1350 | pi2_event_loop->SkipTimingReport(); |
| 1351 | |
| 1352 | MessageCounter<aos::timing::Report> pi1_timing_report(pi1_event_loop.get(), |
| 1353 | "/aos"); |
| 1354 | MessageCounter<aos::timing::Report> full_pi1_timing_report( |
| 1355 | full_pi1_event_loop.get(), "/pi1/aos"); |
| 1356 | MessageCounter<aos::timing::Report> pi1_original_timing_report( |
| 1357 | pi1_event_loop.get(), "/original/aos"); |
| 1358 | MessageCounter<aos::timing::Report> full_pi1_original_timing_report( |
| 1359 | full_pi1_event_loop.get(), "/original/pi1/aos"); |
| 1360 | MessageCounter<aos::timing::Report> pi2_timing_report(pi2_event_loop.get(), |
| 1361 | "/aos"); |
| 1362 | |
| 1363 | log_reader_factory.Run(); |
| 1364 | |
| 1365 | EXPECT_EQ(pi1_timing_report.count(), 0u); |
| 1366 | EXPECT_EQ(full_pi1_timing_report.count(), 0u); |
| 1367 | EXPECT_NE(pi1_original_timing_report.count(), 0u); |
| 1368 | EXPECT_NE(full_pi1_original_timing_report.count(), 0u); |
| 1369 | EXPECT_NE(pi2_timing_report.count(), 0u); |
| 1370 | |
| 1371 | reader.Deregister(); |
| 1372 | } |
| 1373 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1374 | // Tests that we properly recreate forwarded timestamps when replaying a log. |
| 1375 | // This should be enough that we can then re-run the logger and get a valid log |
| 1376 | // back. |
| 1377 | TEST_F(MultinodeLoggerTest, MessageHeader) { |
| 1378 | { |
| 1379 | LoggerState pi1_logger = MakeLogger(pi1_); |
| 1380 | LoggerState pi2_logger = MakeLogger(pi2_); |
| 1381 | |
| 1382 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 1383 | |
| 1384 | StartLogger(&pi1_logger); |
| 1385 | StartLogger(&pi2_logger); |
| 1386 | |
| 1387 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 1388 | } |
| 1389 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1390 | LogReader reader(SortParts(logfiles_)); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1391 | |
| 1392 | SimulatedEventLoopFactory log_reader_factory(reader.configuration()); |
| 1393 | log_reader_factory.set_send_delay(chrono::microseconds(0)); |
| 1394 | |
| 1395 | // This sends out the fetched messages and advances time to the start of the |
| 1396 | // log file. |
| 1397 | reader.Register(&log_reader_factory); |
| 1398 | |
| 1399 | const Node *pi1 = |
| 1400 | configuration::GetNode(log_reader_factory.configuration(), "pi1"); |
| 1401 | const Node *pi2 = |
| 1402 | configuration::GetNode(log_reader_factory.configuration(), "pi2"); |
| 1403 | |
| 1404 | LOG(INFO) << "Start time " << reader.monotonic_start_time(pi1) << " pi1"; |
| 1405 | LOG(INFO) << "Start time " << reader.monotonic_start_time(pi2) << " pi2"; |
| 1406 | LOG(INFO) << "now pi1 " |
| 1407 | << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now(); |
| 1408 | LOG(INFO) << "now pi2 " |
| 1409 | << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now(); |
| 1410 | |
| 1411 | EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(pi1, pi2)); |
| 1412 | |
| 1413 | reader.event_loop_factory()->set_send_delay(chrono::microseconds(0)); |
| 1414 | |
| 1415 | std::unique_ptr<EventLoop> pi1_event_loop = |
| 1416 | log_reader_factory.MakeEventLoop("test", pi1); |
| 1417 | std::unique_ptr<EventLoop> pi2_event_loop = |
| 1418 | log_reader_factory.MakeEventLoop("test", pi2); |
| 1419 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame^] | 1420 | MessageCounter<RemoteMessage> pi1_original_message_header_counter( |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1421 | pi1_event_loop.get(), "/original/aos/remote_timestamps/pi2"); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame^] | 1422 | MessageCounter<RemoteMessage> pi2_original_message_header_counter( |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1423 | pi2_event_loop.get(), "/original/aos/remote_timestamps/pi1"); |
| 1424 | |
| 1425 | aos::Fetcher<message_bridge::Timestamp> pi1_timestamp_on_pi1_fetcher = |
| 1426 | pi1_event_loop->MakeFetcher<message_bridge::Timestamp>("/pi1/aos"); |
| 1427 | aos::Fetcher<message_bridge::Timestamp> pi1_timestamp_on_pi2_fetcher = |
| 1428 | pi2_event_loop->MakeFetcher<message_bridge::Timestamp>("/pi1/aos"); |
| 1429 | |
| 1430 | aos::Fetcher<examples::Ping> ping_on_pi1_fetcher = |
| 1431 | pi1_event_loop->MakeFetcher<examples::Ping>("/test"); |
| 1432 | aos::Fetcher<examples::Ping> ping_on_pi2_fetcher = |
| 1433 | pi2_event_loop->MakeFetcher<examples::Ping>("/test"); |
| 1434 | |
| 1435 | aos::Fetcher<message_bridge::Timestamp> pi2_timestamp_on_pi2_fetcher = |
| 1436 | pi2_event_loop->MakeFetcher<message_bridge::Timestamp>("/pi2/aos"); |
| 1437 | aos::Fetcher<message_bridge::Timestamp> pi2_timestamp_on_pi1_fetcher = |
| 1438 | pi1_event_loop->MakeFetcher<message_bridge::Timestamp>("/pi2/aos"); |
| 1439 | |
| 1440 | aos::Fetcher<examples::Pong> pong_on_pi2_fetcher = |
| 1441 | pi2_event_loop->MakeFetcher<examples::Pong>("/test"); |
| 1442 | aos::Fetcher<examples::Pong> pong_on_pi1_fetcher = |
| 1443 | pi1_event_loop->MakeFetcher<examples::Pong>("/test"); |
| 1444 | |
| 1445 | const size_t pi1_timestamp_channel = configuration::ChannelIndex( |
| 1446 | pi1_event_loop->configuration(), pi1_timestamp_on_pi1_fetcher.channel()); |
| 1447 | const size_t ping_timestamp_channel = configuration::ChannelIndex( |
| 1448 | pi2_event_loop->configuration(), ping_on_pi2_fetcher.channel()); |
| 1449 | |
| 1450 | const size_t pi2_timestamp_channel = configuration::ChannelIndex( |
| 1451 | pi2_event_loop->configuration(), pi2_timestamp_on_pi2_fetcher.channel()); |
| 1452 | const size_t pong_timestamp_channel = configuration::ChannelIndex( |
| 1453 | pi1_event_loop->configuration(), pong_on_pi1_fetcher.channel()); |
| 1454 | |
| 1455 | pi1_event_loop->MakeWatcher( |
| 1456 | "/aos/remote_timestamps/pi2", |
| 1457 | [&pi1_event_loop, pi1_timestamp_channel, ping_timestamp_channel, |
| 1458 | &pi1_timestamp_on_pi1_fetcher, &pi1_timestamp_on_pi2_fetcher, |
| 1459 | &ping_on_pi1_fetcher, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame^] | 1460 | &ping_on_pi2_fetcher](const RemoteMessage &header) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1461 | const aos::monotonic_clock::time_point header_monotonic_sent_time( |
| 1462 | chrono::nanoseconds(header.monotonic_sent_time())); |
| 1463 | const aos::realtime_clock::time_point header_realtime_sent_time( |
| 1464 | chrono::nanoseconds(header.realtime_sent_time())); |
| 1465 | const aos::monotonic_clock::time_point header_monotonic_remote_time( |
| 1466 | chrono::nanoseconds(header.monotonic_remote_time())); |
| 1467 | const aos::realtime_clock::time_point header_realtime_remote_time( |
| 1468 | chrono::nanoseconds(header.realtime_remote_time())); |
| 1469 | |
| 1470 | const Context *pi1_context = nullptr; |
| 1471 | const Context *pi2_context = nullptr; |
| 1472 | |
| 1473 | if (header.channel_index() == pi1_timestamp_channel) { |
| 1474 | ASSERT_TRUE(pi1_timestamp_on_pi1_fetcher.FetchNext()); |
| 1475 | ASSERT_TRUE(pi1_timestamp_on_pi2_fetcher.FetchNext()); |
| 1476 | pi1_context = &pi1_timestamp_on_pi1_fetcher.context(); |
| 1477 | pi2_context = &pi1_timestamp_on_pi2_fetcher.context(); |
| 1478 | } else if (header.channel_index() == ping_timestamp_channel) { |
| 1479 | ASSERT_TRUE(ping_on_pi1_fetcher.FetchNext()); |
| 1480 | ASSERT_TRUE(ping_on_pi2_fetcher.FetchNext()); |
| 1481 | pi1_context = &ping_on_pi1_fetcher.context(); |
| 1482 | pi2_context = &ping_on_pi2_fetcher.context(); |
| 1483 | } else { |
| 1484 | LOG(FATAL) << "Unknown channel " << FlatbufferToJson(&header) << " " |
| 1485 | << configuration::CleanedChannelToString( |
| 1486 | pi1_event_loop->configuration()->channels()->Get( |
| 1487 | header.channel_index())); |
| 1488 | } |
| 1489 | |
| 1490 | EXPECT_EQ(pi1_context->queue_index, header.remote_queue_index()); |
| 1491 | EXPECT_EQ(pi2_context->remote_queue_index, header.remote_queue_index()); |
| 1492 | EXPECT_EQ(pi2_context->queue_index, header.queue_index()); |
| 1493 | |
| 1494 | EXPECT_EQ(pi2_context->monotonic_event_time, |
| 1495 | header_monotonic_sent_time); |
| 1496 | EXPECT_EQ(pi2_context->realtime_event_time, header_realtime_sent_time); |
| 1497 | EXPECT_EQ(pi2_context->realtime_remote_time, |
| 1498 | header_realtime_remote_time); |
| 1499 | EXPECT_EQ(pi2_context->monotonic_remote_time, |
| 1500 | header_monotonic_remote_time); |
| 1501 | |
| 1502 | EXPECT_EQ(pi1_context->realtime_event_time, |
| 1503 | header_realtime_remote_time); |
| 1504 | EXPECT_EQ(pi1_context->monotonic_event_time, |
| 1505 | header_monotonic_remote_time); |
| 1506 | }); |
| 1507 | pi2_event_loop->MakeWatcher( |
| 1508 | "/aos/remote_timestamps/pi1", |
| 1509 | [&pi2_event_loop, pi2_timestamp_channel, pong_timestamp_channel, |
| 1510 | &pi2_timestamp_on_pi2_fetcher, &pi2_timestamp_on_pi1_fetcher, |
| 1511 | &pong_on_pi2_fetcher, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame^] | 1512 | &pong_on_pi1_fetcher](const RemoteMessage &header) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1513 | const aos::monotonic_clock::time_point header_monotonic_sent_time( |
| 1514 | chrono::nanoseconds(header.monotonic_sent_time())); |
| 1515 | const aos::realtime_clock::time_point header_realtime_sent_time( |
| 1516 | chrono::nanoseconds(header.realtime_sent_time())); |
| 1517 | const aos::monotonic_clock::time_point header_monotonic_remote_time( |
| 1518 | chrono::nanoseconds(header.monotonic_remote_time())); |
| 1519 | const aos::realtime_clock::time_point header_realtime_remote_time( |
| 1520 | chrono::nanoseconds(header.realtime_remote_time())); |
| 1521 | |
| 1522 | const Context *pi2_context = nullptr; |
| 1523 | const Context *pi1_context = nullptr; |
| 1524 | |
| 1525 | if (header.channel_index() == pi2_timestamp_channel) { |
| 1526 | ASSERT_TRUE(pi2_timestamp_on_pi2_fetcher.FetchNext()); |
| 1527 | ASSERT_TRUE(pi2_timestamp_on_pi1_fetcher.FetchNext()); |
| 1528 | pi2_context = &pi2_timestamp_on_pi2_fetcher.context(); |
| 1529 | pi1_context = &pi2_timestamp_on_pi1_fetcher.context(); |
| 1530 | } else if (header.channel_index() == pong_timestamp_channel) { |
| 1531 | ASSERT_TRUE(pong_on_pi2_fetcher.FetchNext()); |
| 1532 | ASSERT_TRUE(pong_on_pi1_fetcher.FetchNext()); |
| 1533 | pi2_context = &pong_on_pi2_fetcher.context(); |
| 1534 | pi1_context = &pong_on_pi1_fetcher.context(); |
| 1535 | } else { |
| 1536 | LOG(FATAL) << "Unknown channel " << FlatbufferToJson(&header) << " " |
| 1537 | << configuration::CleanedChannelToString( |
| 1538 | pi2_event_loop->configuration()->channels()->Get( |
| 1539 | header.channel_index())); |
| 1540 | } |
| 1541 | |
| 1542 | EXPECT_EQ(pi2_context->queue_index, header.remote_queue_index()); |
| 1543 | EXPECT_EQ(pi1_context->remote_queue_index, header.remote_queue_index()); |
| 1544 | EXPECT_EQ(pi1_context->queue_index, header.queue_index()); |
| 1545 | |
| 1546 | EXPECT_EQ(pi1_context->monotonic_event_time, |
| 1547 | header_monotonic_sent_time); |
| 1548 | EXPECT_EQ(pi1_context->realtime_event_time, header_realtime_sent_time); |
| 1549 | EXPECT_EQ(pi1_context->realtime_remote_time, |
| 1550 | header_realtime_remote_time); |
| 1551 | EXPECT_EQ(pi1_context->monotonic_remote_time, |
| 1552 | header_monotonic_remote_time); |
| 1553 | |
| 1554 | EXPECT_EQ(pi2_context->realtime_event_time, |
| 1555 | header_realtime_remote_time); |
| 1556 | EXPECT_EQ(pi2_context->monotonic_event_time, |
| 1557 | header_monotonic_remote_time); |
| 1558 | }); |
| 1559 | |
| 1560 | // And confirm we can re-create a log again, while checking the contents. |
| 1561 | { |
| 1562 | LoggerState pi1_logger = MakeLogger( |
| 1563 | configuration::GetNode(log_reader_factory.configuration(), pi1_), |
| 1564 | &log_reader_factory); |
| 1565 | LoggerState pi2_logger = MakeLogger( |
| 1566 | configuration::GetNode(log_reader_factory.configuration(), pi2_), |
| 1567 | &log_reader_factory); |
| 1568 | |
| 1569 | StartLogger(&pi1_logger, "relogged"); |
| 1570 | StartLogger(&pi2_logger, "relogged"); |
| 1571 | |
| 1572 | log_reader_factory.Run(); |
| 1573 | } |
| 1574 | |
| 1575 | EXPECT_EQ(pi2_original_message_header_counter.count(), 0u); |
| 1576 | EXPECT_EQ(pi1_original_message_header_counter.count(), 0u); |
| 1577 | |
| 1578 | reader.Deregister(); |
| 1579 | } |
| 1580 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1581 | // TODO(austin): We can write a test which recreates a logfile and confirms that |
| 1582 | // we get it back. That is the ultimate test. |
| 1583 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1584 | } // namespace testing |
| 1585 | } // namespace logger |
| 1586 | } // namespace aos |