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 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 3 | #include "absl/strings/str_format.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 4 | #include "aos/events/event_loop.h" |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 5 | #include "aos/events/message_counter.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 6 | #include "aos/events/ping_lib.h" |
| 7 | #include "aos/events/pong_lib.h" |
| 8 | #include "aos/events/simulated_event_loop.h" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 9 | #include "aos/network/remote_message_generated.h" |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 10 | #include "aos/network/timestamp_generated.h" |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 11 | #include "aos/testing/tmpdir.h" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 12 | #include "aos/util/file.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 13 | #include "glog/logging.h" |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 14 | #include "gmock/gmock.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 15 | #include "gtest/gtest.h" |
| 16 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 17 | #ifdef LZMA |
| 18 | #include "aos/events/logging/lzma_encoder.h" |
| 19 | #endif |
| 20 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 21 | namespace aos { |
| 22 | namespace logger { |
| 23 | namespace testing { |
| 24 | |
| 25 | namespace chrono = std::chrono; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 26 | using aos::message_bridge::RemoteMessage; |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 27 | using aos::testing::MessageCounter; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 28 | |
| 29 | class 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 Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 35 | ping_event_loop_(event_loop_factory_.MakeEventLoop("ping")), |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 36 | ping_(ping_event_loop_.get()), |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 37 | pong_event_loop_(event_loop_factory_.MakeEventLoop("pong")), |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 38 | 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 Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 53 | using LoggerDeathTest = LoggerTest; |
| 54 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 55 | // Tests that we can startup at all. This confirms that the channels are all in |
| 56 | // the config. |
| 57 | TEST_F(LoggerTest, Starts) { |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 58 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 59 | const ::std::string base_name = tmpdir + "/logfile"; |
| 60 | const ::std::string logfile = base_name + ".part0.bfbs"; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 61 | // Remove it. |
| 62 | unlink(logfile.c_str()); |
| 63 | |
| 64 | LOG(INFO) << "Logging data to " << logfile; |
| 65 | |
| 66 | { |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 67 | std::unique_ptr<EventLoop> logger_event_loop = |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 68 | event_loop_factory_.MakeEventLoop("logger"); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 69 | |
| 70 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 71 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 72 | Logger logger(logger_event_loop.get()); |
| 73 | logger.set_polling_period(std::chrono::milliseconds(100)); |
| 74 | logger.StartLoggingLocalNamerOnRun(base_name); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 75 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 76 | } |
| 77 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 78 | // 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 Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 81 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 82 | // Confirm that we can remap logged channels to point to new buses. |
| 83 | reader.RemapLoggedChannel<aos::examples::Ping>("/test", "/original"); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 84 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 85 | // This sends out the fetched messages and advances time to the start of the |
| 86 | // log file. |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 87 | reader.Register(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 88 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 89 | EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(nullptr)); |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 90 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 91 | std::unique_ptr<EventLoop> test_event_loop = |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 92 | reader.event_loop_factory()->MakeEventLoop("log_reader"); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 93 | |
| 94 | int ping_count = 10; |
| 95 | int pong_count = 10; |
| 96 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 97 | // Confirm that the ping value matches in the remapped channel location. |
| 98 | test_event_loop->MakeWatcher("/original/test", |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 99 | [&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 Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 112 | reader.event_loop_factory()->RunFor(std::chrono::seconds(100)); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 113 | EXPECT_EQ(ping_count, 2010); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 116 | // Tests calling StartLogging twice. |
| 117 | TEST_F(LoggerDeathTest, ExtraStart) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 118 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 119 | 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. |
| 149 | TEST_F(LoggerDeathTest, ExtraStop) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 150 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 151 | 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. |
| 178 | TEST_F(LoggerTest, StartsTwice) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 179 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 180 | 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 Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 240 | // Tests that we can read and write rotated log files. |
| 241 | TEST_F(LoggerTest, RotatedLogFile) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 242 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 243 | 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 Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 246 | // 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 Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 253 | std::unique_ptr<EventLoop> logger_event_loop = |
| 254 | event_loop_factory_.MakeEventLoop("logger"); |
| 255 | |
| 256 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 257 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 258 | Logger logger(logger_event_loop.get()); |
| 259 | logger.set_polling_period(std::chrono::milliseconds(100)); |
| 260 | logger.StartLoggingLocalNamerOnRun(base_name); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 261 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 262 | logger.Rotate(); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 263 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
| 264 | } |
| 265 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 266 | { |
| 267 | // Confirm that the UUIDs match for both the parts and the logger, and the |
| 268 | // parts_index increments. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 269 | std::vector<SizePrefixedFlatbufferVector<LogFileHeader>> log_header; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 270 | for (std::string_view f : {logfile0, logfile1}) { |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 271 | log_header.emplace_back(ReadHeader(f).value()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 274 | EXPECT_EQ(log_header[0].message().log_event_uuid()->string_view(), |
| 275 | log_header[1].message().log_event_uuid()->string_view()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 276 | 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 Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 283 | // Even though it doesn't make any difference here, exercise the logic for |
| 284 | // passing in a separate config. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 285 | LogReader reader(SortParts({logfile0, logfile1}), &config_.message()); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 286 | |
| 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 Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 294 | EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(nullptr)); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 295 | |
| 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 Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 321 | // Tests that a large number of messages per second doesn't overwhelm writev. |
| 322 | TEST_F(LoggerTest, ManyMessages) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 323 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 324 | const ::std::string base_name = tmpdir + "/logfile"; |
| 325 | const ::std::string logfile = base_name + ".part0.bfbs"; |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 326 | // Remove the log file. |
| 327 | unlink(logfile.c_str()); |
| 328 | |
| 329 | LOG(INFO) << "Logging data to " << logfile; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 330 | ping_.set_quiet(true); |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 331 | |
| 332 | { |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 333 | 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 Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 356 | Logger logger(logger_event_loop.get()); |
| 357 | logger.set_polling_period(std::chrono::milliseconds(100)); |
| 358 | logger.StartLoggingLocalNamerOnRun(base_name); |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 359 | |
| 360 | event_loop_factory_.RunFor(chrono::milliseconds(1000)); |
| 361 | } |
| 362 | } |
| 363 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 364 | std::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 Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 388 | class MultinodeLoggerTest : public ::testing::Test { |
| 389 | public: |
| 390 | MultinodeLoggerTest() |
| 391 | : config_(aos::configuration::ReadConfig( |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 392 | "aos/events/logging/multinode_pingpong_config.json")), |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 393 | event_loop_factory_(&config_.message()), |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 394 | pi1_( |
| 395 | configuration::GetNode(event_loop_factory_.configuration(), "pi1")), |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 396 | pi2_( |
| 397 | configuration::GetNode(event_loop_factory_.configuration(), "pi2")), |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 398 | tmp_dir_(aos::testing::TestTmpDir()), |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 399 | logfile_base_(tmp_dir_ + "/multi_logfile"), |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 400 | pi1_reboot_logfiles_( |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 401 | {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 Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 404 | logfile_base_ + "_pi2_data/test/aos.examples.Pong.part2.bfbs", |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 405 | logfile_base_ + "_timestamps/pi1/aos/remote_timestamps/pi2/" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 406 | "aos.message_bridge.RemoteMessage.part0.bfbs", |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 407 | logfile_base_ + "_timestamps/pi1/aos/remote_timestamps/pi2/" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 408 | "aos.message_bridge.RemoteMessage.part1.bfbs", |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 409 | logfile_base_ + "_timestamps/pi1/aos/remote_timestamps/pi2/" |
| 410 | "aos.message_bridge.RemoteMessage.part2.bfbs", |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 411 | logfile_base_ + |
| 412 | "_pi2_data/pi2/aos/aos.message_bridge.Timestamp.part0.bfbs", |
| 413 | logfile_base_ + |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 414 | "_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 Schuh | c904973 | 2020-12-21 22:27:15 -0800 | [diff] [blame^] | 418 | 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 Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 425 | 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 Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 433 | 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 Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 440 | unlink((file + ".xz").c_str()); |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 443 | 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 Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 451 | 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 Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 460 | 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 Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 468 | void StartLogger(LoggerState *logger, std::string logfile_base = "", |
| 469 | bool compress = false) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 470 | if (logfile_base.empty()) { |
| 471 | logfile_base = logfile_base_; |
| 472 | } |
| 473 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 474 | logger->logger = std::make_unique<Logger>(logger->event_loop.get()); |
| 475 | logger->logger->set_polling_period(std::chrono::milliseconds(100)); |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 476 | 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 Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 492 | }); |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 493 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 494 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 495 | 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 Schuh | c904973 | 2020-12-21 22:27:15 -0800 | [diff] [blame^] | 559 | 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 Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 570 | 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 Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 587 | // Config and factory. |
| 588 | aos::FlatbufferDetachedBuffer<aos::Configuration> config_; |
| 589 | SimulatedEventLoopFactory event_loop_factory_; |
| 590 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 591 | const Node *pi1_; |
| 592 | const Node *pi2_; |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 593 | |
| 594 | std::string tmp_dir_; |
| 595 | std::string logfile_base_; |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 596 | std::vector<std::string> pi1_reboot_logfiles_; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 597 | std::vector<std::string> logfiles_; |
Austin Schuh | c904973 | 2020-12-21 22:27:15 -0800 | [diff] [blame^] | 598 | std::vector<std::string> pi1_single_direction_logfiles_; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 599 | |
| 600 | std::vector<std::vector<std::string>> structured_logfiles_; |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 601 | |
| 602 | std::unique_ptr<EventLoop> ping_event_loop_; |
| 603 | Ping ping_; |
| 604 | std::unique_ptr<EventLoop> pong_event_loop_; |
| 605 | Pong pong_; |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 606 | }; |
| 607 | |
Austin Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 608 | // Counts the number of messages on a channel. Returns (channel name, channel |
| 609 | // type, count) for every message matching matcher() |
| 610 | std::vector<std::tuple<std::string, std::string, int>> CountChannelsMatching( |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 611 | 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 Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 616 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 617 | while (true) { |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 618 | std::optional<SizePrefixedFlatbufferVector<MessageHeader>> msg = |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 619 | 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 Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 629 | std::vector<std::tuple<std::string, std::string, int>> result; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 630 | int channel = 0; |
| 631 | for (size_t i = 0; i < counts.size(); ++i) { |
| 632 | if (counts[i] != 0) { |
Austin Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 633 | 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 Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 637 | } |
| 638 | ++channel; |
| 639 | } |
| 640 | |
| 641 | return result; |
| 642 | } |
| 643 | |
| 644 | // Counts the number of messages (channel, count) for all data messages. |
Austin Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 645 | std::vector<std::tuple<std::string, std::string, int>> CountChannelsData( |
| 646 | std::string_view filename) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 647 | 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 Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 659 | std::vector<std::tuple<std::string, std::string, int>> CountChannelsTimestamp( |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 660 | 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 Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 672 | // Tests that we can write and read simple multi-node log files. |
| 673 | TEST_F(MultinodeLoggerTest, SimpleMultiNode) { |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 674 | { |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 675 | LoggerState pi1_logger = MakeLogger(pi1_); |
| 676 | LoggerState pi2_logger = MakeLogger(pi2_); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 677 | |
| 678 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 679 | |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 680 | StartLogger(&pi1_logger); |
| 681 | StartLogger(&pi2_logger); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 682 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 683 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 684 | } |
| 685 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 686 | { |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 687 | 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 Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 691 | std::vector<SizePrefixedFlatbufferVector<LogFileHeader>> log_header; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 692 | for (std::string_view f : logfiles_) { |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 693 | log_header.emplace_back(ReadHeader(f).value()); |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 694 | logfile_uuids.insert(log_header.back().message().log_event_uuid()->str()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 695 | parts_uuids.insert(log_header.back().message().parts_uuid()->str()); |
| 696 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 697 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 698 | 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 Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 731 | using ::testing::UnorderedElementsAre; |
| 732 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 733 | // Timing reports, pings |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 734 | 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 Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 739 | std::make_tuple("/test", "aos.examples.Ping", 2001))) |
| 740 | << " : " << logfiles_[0]; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 741 | // Timestamps for pong |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 742 | EXPECT_THAT( |
| 743 | CountChannelsTimestamp(logfiles_[0]), |
| 744 | UnorderedElementsAre( |
| 745 | std::make_tuple("/test", "aos.examples.Pong", 2001), |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 746 | std::make_tuple("/pi2/aos", "aos.message_bridge.Timestamp", 200))) |
| 747 | << " : " << logfiles_[0]; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 748 | |
| 749 | // Pong data. |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 750 | EXPECT_THAT( |
| 751 | CountChannelsData(logfiles_[1]), |
| 752 | UnorderedElementsAre(std::make_tuple("/test", "aos.examples.Pong", 91))) |
| 753 | << " : " << logfiles_[1]; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 754 | EXPECT_THAT(CountChannelsData(logfiles_[2]), |
| 755 | UnorderedElementsAre( |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 756 | std::make_tuple("/test", "aos.examples.Pong", 1910))) |
| 757 | << " : " << logfiles_[1]; |
Austin Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 758 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 759 | // No timestamps |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 760 | EXPECT_THAT(CountChannelsTimestamp(logfiles_[1]), UnorderedElementsAre()) |
| 761 | << " : " << logfiles_[1]; |
| 762 | EXPECT_THAT(CountChannelsTimestamp(logfiles_[2]), UnorderedElementsAre()) |
| 763 | << " : " << logfiles_[2]; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 764 | |
| 765 | // Timing reports and pongs. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 766 | 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 Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 771 | std::make_tuple("/test", "aos.examples.Pong", 2001))) |
| 772 | << " : " << logfiles_[3]; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 773 | // And ping timestamps. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 774 | EXPECT_THAT( |
| 775 | CountChannelsTimestamp(logfiles_[3]), |
| 776 | UnorderedElementsAre( |
| 777 | std::make_tuple("/test", "aos.examples.Ping", 2001), |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 778 | std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 200))) |
| 779 | << " : " << logfiles_[3]; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 780 | |
| 781 | // Timestamps from pi2 on pi1, and the other way. |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 782 | 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 Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 790 | EXPECT_THAT( |
| 791 | CountChannelsTimestamp(logfiles_[4]), |
| 792 | UnorderedElementsAre( |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 793 | std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 9), |
| 794 | std::make_tuple("/test", "aos.examples.Ping", 91))) |
| 795 | << " : " << logfiles_[4]; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 796 | EXPECT_THAT( |
| 797 | CountChannelsTimestamp(logfiles_[5]), |
| 798 | UnorderedElementsAre( |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 799 | std::make_tuple("/pi1/aos", "aos.message_bridge.Timestamp", 191), |
| 800 | std::make_tuple("/test", "aos.examples.Ping", 1910))) |
| 801 | << " : " << logfiles_[5]; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 802 | EXPECT_THAT(CountChannelsTimestamp(logfiles_[6]), |
| 803 | UnorderedElementsAre(std::make_tuple( |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 804 | "/pi2/aos", "aos.message_bridge.Timestamp", 9))) |
| 805 | << " : " << logfiles_[6]; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 806 | EXPECT_THAT(CountChannelsTimestamp(logfiles_[7]), |
| 807 | UnorderedElementsAre(std::make_tuple( |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 808 | "/pi2/aos", "aos.message_bridge.Timestamp", 191))) |
| 809 | << " : " << logfiles_[7]; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 810 | |
| 811 | // And then test that the remotely logged timestamp data files only have |
| 812 | // timestamps in them. |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 813 | 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 Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 821 | |
| 822 | EXPECT_THAT(CountChannelsData(logfiles_[8]), |
| 823 | UnorderedElementsAre(std::make_tuple( |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 824 | "/pi1/aos", "aos.message_bridge.Timestamp", 9))) |
| 825 | << " : " << logfiles_[8]; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 826 | EXPECT_THAT(CountChannelsData(logfiles_[9]), |
| 827 | UnorderedElementsAre(std::make_tuple( |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 828 | "/pi1/aos", "aos.message_bridge.Timestamp", 191))) |
| 829 | << " : " << logfiles_[9]; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 830 | |
| 831 | EXPECT_THAT(CountChannelsData(logfiles_[10]), |
| 832 | UnorderedElementsAre(std::make_tuple( |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 833 | "/pi2/aos", "aos.message_bridge.Timestamp", 9))) |
| 834 | << " : " << logfiles_[10]; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 835 | EXPECT_THAT(CountChannelsData(logfiles_[11]), |
| 836 | UnorderedElementsAre(std::make_tuple( |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 837 | "/pi2/aos", "aos.message_bridge.Timestamp", 191))) |
| 838 | << " : " << logfiles_[11]; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 839 | } |
| 840 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 841 | LogReader reader(SortParts(logfiles_)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 842 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 843 | SimulatedEventLoopFactory log_reader_factory(reader.configuration()); |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 844 | log_reader_factory.set_send_delay(chrono::microseconds(0)); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 845 | |
| 846 | // This sends out the fetched messages and advances time to the start of the |
| 847 | // log file. |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 848 | reader.Register(&log_reader_factory); |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 849 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 850 | const Node *pi1 = |
| 851 | configuration::GetNode(log_reader_factory.configuration(), "pi1"); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 852 | const Node *pi2 = |
| 853 | configuration::GetNode(log_reader_factory.configuration(), "pi2"); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 854 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 855 | 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 Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 862 | EXPECT_THAT(reader.Nodes(), ::testing::ElementsAre(pi1, pi2)); |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 863 | |
| 864 | reader.event_loop_factory()->set_send_delay(chrono::microseconds(0)); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 865 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 866 | std::unique_ptr<EventLoop> pi1_event_loop = |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 867 | log_reader_factory.MakeEventLoop("test", pi1); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 868 | std::unique_ptr<EventLoop> pi2_event_loop = |
| 869 | log_reader_factory.MakeEventLoop("test", pi2); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 870 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 871 | int pi1_ping_count = 10; |
| 872 | int pi2_ping_count = 10; |
| 873 | int pi1_pong_count = 10; |
| 874 | int pi2_pong_count = 10; |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 875 | |
| 876 | // Confirm that the ping value matches. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 877 | pi1_event_loop->MakeWatcher( |
| 878 | "/test", [&pi1_ping_count, &pi1_event_loop](const examples::Ping &ping) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 879 | VLOG(1) << "Pi1 ping " << FlatbufferToJson(&ping) << " at " |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 880 | << 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 Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 893 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 894 | ++pi1_ping_count; |
| 895 | }); |
| 896 | pi2_event_loop->MakeWatcher( |
| 897 | "/test", [&pi2_ping_count, &pi2_event_loop](const examples::Ping &ping) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 898 | VLOG(1) << "Pi2 ping " << FlatbufferToJson(&ping) << " at " |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 899 | << 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 Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 916 | }); |
| 917 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 918 | constexpr ssize_t kQueueIndexOffset = -9; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 919 | // 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 Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 958 | pi2_pong_count + kQueueIndexOffset); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 959 | |
| 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 Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 984 | |
| 985 | reader.Deregister(); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 986 | } |
| 987 | |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 988 | typedef MultinodeLoggerTest MultinodeLoggerDeathTest; |
| 989 | |
| 990 | // Test that if we feed the replay with a mismatched node list that we die on |
| 991 | // the LogReader constructor. |
| 992 | TEST_F(MultinodeLoggerDeathTest, MultiNodeBadReplayConfig) { |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 993 | { |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 994 | LoggerState pi1_logger = MakeLogger(pi1_); |
| 995 | LoggerState pi2_logger = MakeLogger(pi2_); |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 996 | |
| 997 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 998 | |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 999 | StartLogger(&pi1_logger); |
| 1000 | StartLogger(&pi2_logger); |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 1001 | |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 1002 | 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 Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1017 | const std::vector<LogFile> sorted_parts = SortParts(logfiles_); |
| 1018 | EXPECT_DEATH(LogReader(sorted_parts, &extra_nodes_config.message()), |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 1019 | "Log file and replay config need to have matching nodes lists."); |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1022 | // Tests that we can read log files where they don't start at the same monotonic |
| 1023 | // time. |
| 1024 | TEST_F(MultinodeLoggerTest, StaggeredStart) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1025 | { |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 1026 | LoggerState pi1_logger = MakeLogger(pi1_); |
| 1027 | LoggerState pi2_logger = MakeLogger(pi2_); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1028 | |
| 1029 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 1030 | |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 1031 | StartLogger(&pi1_logger); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1032 | |
| 1033 | event_loop_factory_.RunFor(chrono::milliseconds(200)); |
| 1034 | |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 1035 | StartLogger(&pi2_logger); |
| 1036 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1037 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 1038 | } |
| 1039 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1040 | LogReader reader(SortParts(logfiles_)); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1041 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1042 | SimulatedEventLoopFactory log_reader_factory(reader.configuration()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1043 | 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 Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1121 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1122 | // 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. |
| 1125 | TEST_F(MultinodeLoggerTest, MismatchedClocks) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1126 | { |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 1127 | LoggerState pi2_logger = MakeLogger(pi2_); |
| 1128 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1129 | 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 Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1134 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1135 | const chrono::nanoseconds initial_pi2_offset = -chrono::seconds(1000); |
| 1136 | chrono::nanoseconds pi2_offset = initial_pi2_offset; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1137 | |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 1138 | pi2->SetDistributedOffset(-pi2_offset, 1.0); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1139 | LOG(INFO) << "pi2 times: " << pi2->monotonic_now() << " " |
| 1140 | << pi2->realtime_now() << " distributed " |
| 1141 | << pi2->ToDistributedClock(pi2->monotonic_now()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1142 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1143 | for (int i = 0; i < 95; ++i) { |
| 1144 | pi2_offset += chrono::nanoseconds(200); |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 1145 | pi2->SetDistributedOffset(-pi2_offset, 1.0); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1146 | event_loop_factory_.RunFor(chrono::milliseconds(1)); |
| 1147 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1148 | |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 1149 | StartLogger(&pi2_logger); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1150 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1151 | event_loop_factory_.RunFor(chrono::milliseconds(200)); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1152 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1153 | { |
| 1154 | // Run pi1's logger for only part of the time. |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 1155 | LoggerState pi1_logger = MakeLogger(pi1_); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1156 | |
James Kuszmaul | bb28ef2 | 2020-05-09 22:30:38 -0700 | [diff] [blame] | 1157 | StartLogger(&pi1_logger); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1158 | |
| 1159 | for (int i = 0; i < 20000; ++i) { |
| 1160 | pi2_offset += chrono::nanoseconds(200); |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 1161 | pi2->SetDistributedOffset(-pi2_offset, 1.0); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1162 | 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 Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 1171 | pi2->SetDistributedOffset(-pi2_offset, 1.0); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1172 | 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 Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1178 | } |
| 1179 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1180 | LogReader reader(SortParts(logfiles_)); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1181 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1182 | SimulatedEventLoopFactory log_reader_factory(reader.configuration()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1183 | log_reader_factory.set_send_delay(chrono::microseconds(0)); |
| 1184 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1185 | 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 Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1190 | // 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 Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1201 | LOG(INFO) << "Done registering (pi1) " |
Austin Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 1202 | << log_reader_factory.GetNodeEventLoopFactory(pi1)->monotonic_now() |
| 1203 | << " " |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1204 | << log_reader_factory.GetNodeEventLoopFactory(pi1)->realtime_now(); |
| 1205 | LOG(INFO) << "Done registering (pi2) " |
Austin Schuh | 391e317 | 2020-09-01 22:48:18 -0700 | [diff] [blame] | 1206 | << log_reader_factory.GetNodeEventLoopFactory(pi2)->monotonic_now() |
| 1207 | << " " |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1208 | << 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 Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1270 | 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 Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1274 | |
| 1275 | reader.Deregister(); |
| 1276 | } |
| 1277 | |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 1278 | // Tests that we can sort a bunch of parts into the pre-determined sorted parts. |
| 1279 | TEST_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 Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1293 | const std::vector<LogFile> sorted_parts = SortParts(logfiles_); |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1294 | VerifyParts(sorted_parts); |
| 1295 | } |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1296 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1297 | // 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. |
| 1299 | TEST_F(MultinodeLoggerTest, SortEmptyParts) { |
| 1300 | // Make a bunch of parts. |
| 1301 | { |
| 1302 | LoggerState pi1_logger = MakeLogger(pi1_); |
| 1303 | LoggerState pi2_logger = MakeLogger(pi2_); |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1304 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1305 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1306 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1307 | StartLogger(&pi1_logger); |
| 1308 | StartLogger(&pi2_logger); |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1309 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1310 | event_loop_factory_.RunFor(chrono::milliseconds(2000)); |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1311 | } |
| 1312 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1313 | // TODO(austin): Should we flip out if the file can't open? |
| 1314 | const std::string kEmptyFile("foobarinvalidfiledoesnotexist.bfbs"); |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 1315 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1316 | aos::util::WriteStringToFileOrDie(kEmptyFile, ""); |
| 1317 | logfiles_.emplace_back(kEmptyFile); |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 1318 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1319 | const std::vector<LogFile> sorted_parts = SortParts(logfiles_); |
| 1320 | VerifyParts(sorted_parts, {kEmptyFile}); |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 1321 | } |
| 1322 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 1323 | #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. |
| 1326 | TEST_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. |
| 1354 | TEST_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 Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1384 | // Tests that if we remap a remapped channel, it shows up correctly. |
| 1385 | TEST_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 Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1398 | LogReader reader(SortParts(logfiles_)); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1399 | |
| 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 Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1448 | // 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. |
| 1451 | TEST_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 Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1464 | LogReader reader(SortParts(logfiles_)); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1465 | |
| 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 Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1494 | MessageCounter<RemoteMessage> pi1_original_message_header_counter( |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1495 | pi1_event_loop.get(), "/original/aos/remote_timestamps/pi2"); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1496 | MessageCounter<RemoteMessage> pi2_original_message_header_counter( |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1497 | 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 Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1531 | [&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 Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1534 | &ping_on_pi2_fetcher](const RemoteMessage &header) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1535 | 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 Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1564 | ASSERT_TRUE(header.has_boot_uuid()); |
| 1565 | EXPECT_EQ(header.boot_uuid()->string_view(), |
| 1566 | pi2_event_loop->boot_uuid().string_view()); |
| 1567 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1568 | 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 Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1587 | [&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 Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1590 | &pong_on_pi1_fetcher](const RemoteMessage &header) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1591 | 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 Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1620 | ASSERT_TRUE(header.has_boot_uuid()); |
| 1621 | EXPECT_EQ(header.boot_uuid()->string_view(), |
| 1622 | pi1_event_loop->boot_uuid().string_view()); |
| 1623 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1624 | 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 Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1663 | // 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. |
| 1665 | TEST_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 Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1703 | // 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 Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1706 | // 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. |
| 1709 | TEST_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 Schuh | c904973 | 2020-12-21 22:27:15 -0800 | [diff] [blame^] | 1749 | // Tests that we properly handle one direction of message_bridge being |
| 1750 | // unavailable. |
| 1751 | TEST_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. |
| 1772 | TEST_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 Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1791 | } // namespace testing |
| 1792 | } // namespace logger |
| 1793 | } // namespace aos |