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