Austin Schuh | 6bb8a82 | 2021-03-31 23:04:39 -0700 | [diff] [blame] | 1 | #include <sys/stat.h> |
| 2 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 3 | #include <filesystem> |
| 4 | |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 5 | #include "absl/strings/str_format.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 6 | #include "aos/events/event_loop.h" |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 7 | #include "aos/events/logging/log_reader.h" |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 8 | #include "aos/events/logging/log_writer.h" |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 9 | #include "aos/events/logging/snappy_encoder.h" |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 10 | #include "aos/events/message_counter.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 11 | #include "aos/events/ping_lib.h" |
| 12 | #include "aos/events/pong_lib.h" |
| 13 | #include "aos/events/simulated_event_loop.h" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 14 | #include "aos/network/remote_message_generated.h" |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 15 | #include "aos/network/testing_time_converter.h" |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 16 | #include "aos/network/timestamp_generated.h" |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 17 | #include "aos/testing/path.h" |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 18 | #include "aos/testing/tmpdir.h" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 19 | #include "aos/util/file.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 20 | #include "glog/logging.h" |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 21 | #include "gmock/gmock.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 22 | #include "gtest/gtest.h" |
| 23 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 24 | #ifdef LZMA |
| 25 | #include "aos/events/logging/lzma_encoder.h" |
| 26 | #endif |
| 27 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 28 | namespace aos { |
| 29 | namespace logger { |
| 30 | namespace testing { |
| 31 | |
| 32 | namespace chrono = std::chrono; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 33 | using aos::message_bridge::RemoteMessage; |
Naman Gupta | a63aa13 | 2023-03-22 20:06:34 -0700 | [diff] [blame] | 34 | using aos::testing::ArtifactPath; |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 35 | using aos::testing::MessageCounter; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 36 | |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 37 | constexpr std::string_view kSingleConfigSha256( |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 38 | "bbe1b563139273b23a5405eebc2f2740cefcda5f96681acd0a84b8ff9ab93ea4"); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 39 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 40 | class LoggerTest : public ::testing::Test { |
| 41 | public: |
| 42 | LoggerTest() |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 43 | : config_(aos::configuration::ReadConfig( |
| 44 | ArtifactPath("aos/events/pingpong_config.json"))), |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 45 | event_loop_factory_(&config_.message()), |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 46 | ping_event_loop_(event_loop_factory_.MakeEventLoop("ping")), |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 47 | ping_(ping_event_loop_.get()), |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 48 | pong_event_loop_(event_loop_factory_.MakeEventLoop("pong")), |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 49 | pong_(pong_event_loop_.get()) {} |
| 50 | |
| 51 | // Config and factory. |
| 52 | aos::FlatbufferDetachedBuffer<aos::Configuration> config_; |
| 53 | SimulatedEventLoopFactory event_loop_factory_; |
| 54 | |
| 55 | // Event loop and app for Ping |
| 56 | std::unique_ptr<EventLoop> ping_event_loop_; |
| 57 | Ping ping_; |
| 58 | |
| 59 | // Event loop and app for Pong |
| 60 | std::unique_ptr<EventLoop> pong_event_loop_; |
| 61 | Pong pong_; |
| 62 | }; |
| 63 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 64 | using LoggerDeathTest = LoggerTest; |
| 65 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 66 | // Tests that we can startup at all. This confirms that the channels are all in |
| 67 | // the config. |
| 68 | TEST_F(LoggerTest, Starts) { |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 69 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 70 | const ::std::string base_name = tmpdir + "/logfile"; |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 71 | const ::std::string config = |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 72 | absl::StrCat(base_name, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 73 | const ::std::string logfile = base_name + "_data.part0.bfbs"; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 74 | // Remove it. |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 75 | unlink(config.c_str()); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 76 | unlink(logfile.c_str()); |
| 77 | |
| 78 | LOG(INFO) << "Logging data to " << logfile; |
| 79 | |
| 80 | { |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 81 | std::unique_ptr<EventLoop> logger_event_loop = |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 82 | event_loop_factory_.MakeEventLoop("logger"); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 83 | |
| 84 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 85 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 86 | Logger logger(logger_event_loop.get()); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 87 | logger.set_separate_config(false); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 88 | logger.set_polling_period(std::chrono::milliseconds(100)); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 89 | logger.StartLoggingOnRun(base_name); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 90 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 91 | } |
| 92 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 93 | ASSERT_TRUE(std::filesystem::exists(logfile)); |
| 94 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 95 | // Even though it doesn't make any difference here, exercise the logic for |
| 96 | // passing in a separate config. |
| 97 | LogReader reader(logfile, &config_.message()); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 98 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 99 | // This sends out the fetched messages and advances time to the start of the |
| 100 | // log file. |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 101 | reader.Register(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 102 | |
Austin Schuh | 0767662 | 2021-01-21 18:59:17 -0800 | [diff] [blame] | 103 | EXPECT_THAT(reader.LoggedNodes(), ::testing::ElementsAre(nullptr)); |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 104 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 105 | std::unique_ptr<EventLoop> test_event_loop = |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 106 | reader.event_loop_factory()->MakeEventLoop("log_reader"); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 107 | |
| 108 | int ping_count = 10; |
| 109 | int pong_count = 10; |
| 110 | |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame] | 111 | // Confirm that the ping value matches. |
| 112 | test_event_loop->MakeWatcher("/test", |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 113 | [&ping_count](const examples::Ping &ping) { |
| 114 | EXPECT_EQ(ping.value(), ping_count + 1); |
| 115 | ++ping_count; |
| 116 | }); |
| 117 | // Confirm that the ping and pong counts both match, and the value also |
| 118 | // matches. |
| 119 | test_event_loop->MakeWatcher( |
| 120 | "/test", [&pong_count, &ping_count](const examples::Pong &pong) { |
| 121 | EXPECT_EQ(pong.value(), pong_count + 1); |
| 122 | ++pong_count; |
| 123 | EXPECT_EQ(ping_count, pong_count); |
| 124 | }); |
| 125 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 126 | reader.event_loop_factory()->RunFor(std::chrono::seconds(100)); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 127 | EXPECT_EQ(ping_count, 2010); |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame] | 128 | EXPECT_EQ(pong_count, ping_count); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 131 | // Tests calling StartLogging twice. |
| 132 | TEST_F(LoggerDeathTest, ExtraStart) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 133 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 134 | const ::std::string base_name1 = tmpdir + "/logfile1"; |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 135 | const ::std::string config1 = |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 136 | absl::StrCat(base_name1, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 137 | const ::std::string logfile1 = base_name1 + "_data.part0.bfbs"; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 138 | const ::std::string base_name2 = tmpdir + "/logfile2"; |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 139 | const ::std::string config2 = |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 140 | absl::StrCat(base_name2, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 141 | const ::std::string logfile2 = base_name2 + "_data.part0.bfbs"; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 142 | unlink(logfile1.c_str()); |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 143 | unlink(config1.c_str()); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 144 | unlink(logfile2.c_str()); |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 145 | unlink(config2.c_str()); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 146 | |
| 147 | LOG(INFO) << "Logging data to " << logfile1 << " then " << logfile2; |
| 148 | |
| 149 | { |
| 150 | std::unique_ptr<EventLoop> logger_event_loop = |
| 151 | event_loop_factory_.MakeEventLoop("logger"); |
| 152 | |
| 153 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 154 | |
| 155 | Logger logger(logger_event_loop.get()); |
| 156 | logger.set_polling_period(std::chrono::milliseconds(100)); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 157 | logger_event_loop->OnRun([base_name1, base_name2, &logger_event_loop, |
| 158 | &logger]() { |
| 159 | logger.StartLogging(std::make_unique<MultiNodeFilesLogNamer>( |
| 160 | base_name1, logger_event_loop->configuration(), |
| 161 | logger_event_loop.get(), logger_event_loop->node())); |
| 162 | EXPECT_DEATH(logger.StartLogging(std::make_unique<MultiNodeFilesLogNamer>( |
| 163 | base_name2, logger_event_loop->configuration(), |
| 164 | logger_event_loop.get(), logger_event_loop->node())), |
| 165 | "Already logging"); |
| 166 | }); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 167 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 168 | } |
| 169 | } |
| 170 | |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 171 | // Tests that we die if the replayer attempts to send on a logged channel. |
| 172 | TEST_F(LoggerDeathTest, DieOnDuplicateReplayChannels) { |
| 173 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 174 | aos::configuration::ReadConfig( |
| 175 | ArtifactPath("aos/events/pingpong_config.json")); |
| 176 | SimulatedEventLoopFactory event_loop_factory(&config.message()); |
| 177 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
| 178 | const ::std::string base_name = tmpdir + "/logfile"; |
| 179 | const ::std::string config_file = |
| 180 | absl::StrCat(base_name, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 181 | const ::std::string logfile = base_name + "_data.part0.bfbs"; |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 182 | // Remove the log file. |
| 183 | unlink(config_file.c_str()); |
| 184 | unlink(logfile.c_str()); |
| 185 | |
| 186 | LOG(INFO) << "Logging data to " << logfile; |
| 187 | |
| 188 | { |
| 189 | std::unique_ptr<EventLoop> logger_event_loop = |
| 190 | event_loop_factory.MakeEventLoop("logger"); |
| 191 | |
| 192 | Logger logger(logger_event_loop.get()); |
| 193 | logger.set_separate_config(false); |
| 194 | logger.set_polling_period(std::chrono::milliseconds(100)); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 195 | logger.StartLoggingOnRun(base_name); |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 196 | |
| 197 | event_loop_factory.RunFor(chrono::seconds(2)); |
| 198 | } |
| 199 | |
| 200 | LogReader reader(logfile); |
| 201 | |
| 202 | reader.Register(); |
| 203 | |
| 204 | std::unique_ptr<EventLoop> test_event_loop = |
| 205 | reader.event_loop_factory()->MakeEventLoop("log_reader"); |
| 206 | |
| 207 | EXPECT_DEATH(test_event_loop->MakeSender<examples::Ping>("/test"), |
| 208 | "exclusive channel.*examples.Ping"); |
| 209 | } |
| 210 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 211 | // Tests calling StopLogging twice. |
| 212 | TEST_F(LoggerDeathTest, ExtraStop) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 213 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 214 | const ::std::string base_name = tmpdir + "/logfile"; |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 215 | const ::std::string config = |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 216 | absl::StrCat(base_name, kSingleConfigSha256, ".bfbs"); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 217 | const ::std::string logfile = base_name + ".part0.bfbs"; |
| 218 | // Remove it. |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 219 | unlink(config.c_str()); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 220 | unlink(logfile.c_str()); |
| 221 | |
| 222 | LOG(INFO) << "Logging data to " << logfile; |
| 223 | |
| 224 | { |
| 225 | std::unique_ptr<EventLoop> logger_event_loop = |
| 226 | event_loop_factory_.MakeEventLoop("logger"); |
| 227 | |
| 228 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 229 | |
| 230 | Logger logger(logger_event_loop.get()); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 231 | logger.set_separate_config(false); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 232 | logger.set_polling_period(std::chrono::milliseconds(100)); |
| 233 | logger_event_loop->OnRun([base_name, &logger_event_loop, &logger]() { |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 234 | logger.StartLogging(std::make_unique<MultiNodeFilesLogNamer>( |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 235 | base_name, logger_event_loop->configuration(), |
| 236 | logger_event_loop.get(), logger_event_loop->node())); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 237 | logger.StopLogging(aos::monotonic_clock::min_time); |
| 238 | EXPECT_DEATH(logger.StopLogging(aos::monotonic_clock::min_time), |
| 239 | "Not logging right now"); |
| 240 | }); |
| 241 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // Tests that we can startup twice. |
| 246 | TEST_F(LoggerTest, StartsTwice) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 247 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 248 | const ::std::string base_name1 = tmpdir + "/logfile1"; |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 249 | const ::std::string config1 = |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 250 | absl::StrCat(base_name1, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 251 | const ::std::string logfile1 = base_name1 + "_data.part0.bfbs"; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 252 | const ::std::string base_name2 = tmpdir + "/logfile2"; |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 253 | const ::std::string config2 = |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 254 | absl::StrCat(base_name2, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 255 | const ::std::string logfile2 = base_name2 + "_data.part0.bfbs"; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 256 | unlink(logfile1.c_str()); |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 257 | unlink(config1.c_str()); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 258 | unlink(logfile2.c_str()); |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 259 | unlink(config2.c_str()); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 260 | |
| 261 | LOG(INFO) << "Logging data to " << logfile1 << " then " << logfile2; |
| 262 | |
| 263 | { |
| 264 | std::unique_ptr<EventLoop> logger_event_loop = |
| 265 | event_loop_factory_.MakeEventLoop("logger"); |
| 266 | |
| 267 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 268 | |
| 269 | Logger logger(logger_event_loop.get()); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 270 | logger.set_separate_config(false); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 271 | logger.set_polling_period(std::chrono::milliseconds(100)); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 272 | logger.StartLogging(std::make_unique<MultiNodeFilesLogNamer>( |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 273 | base_name1, logger_event_loop->configuration(), logger_event_loop.get(), |
| 274 | logger_event_loop->node())); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 275 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
| 276 | logger.StopLogging(logger_event_loop->monotonic_now()); |
| 277 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 278 | logger.StartLogging(std::make_unique<MultiNodeFilesLogNamer>( |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 279 | base_name2, logger_event_loop->configuration(), logger_event_loop.get(), |
| 280 | logger_event_loop->node())); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 281 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
| 282 | } |
| 283 | |
| 284 | for (const auto &logfile : |
| 285 | {std::make_tuple(logfile1, 10), std::make_tuple(logfile2, 2010)}) { |
| 286 | SCOPED_TRACE(std::get<0>(logfile)); |
| 287 | LogReader reader(std::get<0>(logfile)); |
| 288 | reader.Register(); |
| 289 | |
Austin Schuh | 0767662 | 2021-01-21 18:59:17 -0800 | [diff] [blame] | 290 | EXPECT_THAT(reader.LoggedNodes(), ::testing::ElementsAre(nullptr)); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 291 | |
| 292 | std::unique_ptr<EventLoop> test_event_loop = |
| 293 | reader.event_loop_factory()->MakeEventLoop("log_reader"); |
| 294 | |
| 295 | int ping_count = std::get<1>(logfile); |
| 296 | int pong_count = std::get<1>(logfile); |
| 297 | |
| 298 | // Confirm that the ping and pong counts both match, and the value also |
| 299 | // matches. |
| 300 | test_event_loop->MakeWatcher("/test", |
| 301 | [&ping_count](const examples::Ping &ping) { |
| 302 | EXPECT_EQ(ping.value(), ping_count + 1); |
| 303 | ++ping_count; |
| 304 | }); |
| 305 | test_event_loop->MakeWatcher( |
| 306 | "/test", [&pong_count, &ping_count](const examples::Pong &pong) { |
| 307 | EXPECT_EQ(pong.value(), pong_count + 1); |
| 308 | ++pong_count; |
| 309 | EXPECT_EQ(ping_count, pong_count); |
| 310 | }); |
| 311 | |
| 312 | reader.event_loop_factory()->RunFor(std::chrono::seconds(100)); |
| 313 | EXPECT_EQ(ping_count, std::get<1>(logfile) + 1000); |
| 314 | } |
| 315 | } |
| 316 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 317 | // Tests that we can read and write rotated log files. |
| 318 | TEST_F(LoggerTest, RotatedLogFile) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 319 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 320 | const ::std::string base_name = tmpdir + "/logfile"; |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 321 | const ::std::string config = |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 322 | absl::StrCat(base_name, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 323 | const ::std::string logfile0 = base_name + "_data.part0.bfbs"; |
| 324 | const ::std::string logfile1 = base_name + "_data.part1.bfbs"; |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 325 | // Remove it. |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 326 | unlink(config.c_str()); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 327 | unlink(logfile0.c_str()); |
| 328 | unlink(logfile1.c_str()); |
| 329 | |
| 330 | LOG(INFO) << "Logging data to " << logfile0 << " and " << logfile1; |
| 331 | |
| 332 | { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 333 | std::unique_ptr<EventLoop> logger_event_loop = |
| 334 | event_loop_factory_.MakeEventLoop("logger"); |
| 335 | |
| 336 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 337 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 338 | Logger logger(logger_event_loop.get()); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 339 | logger.set_separate_config(false); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 340 | logger.set_polling_period(std::chrono::milliseconds(100)); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 341 | logger.StartLoggingOnRun(base_name); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 342 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 343 | logger.Rotate(); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 344 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
| 345 | } |
| 346 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 347 | { |
| 348 | // Confirm that the UUIDs match for both the parts and the logger, and the |
| 349 | // parts_index increments. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 350 | std::vector<SizePrefixedFlatbufferVector<LogFileHeader>> log_header; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 351 | for (std::string_view f : {logfile0, logfile1}) { |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 352 | log_header.emplace_back(ReadHeader(f).value()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 355 | EXPECT_EQ(log_header[0].message().log_event_uuid()->string_view(), |
| 356 | log_header[1].message().log_event_uuid()->string_view()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 357 | EXPECT_EQ(log_header[0].message().parts_uuid()->string_view(), |
| 358 | log_header[1].message().parts_uuid()->string_view()); |
| 359 | |
| 360 | EXPECT_EQ(log_header[0].message().parts_index(), 0); |
| 361 | EXPECT_EQ(log_header[1].message().parts_index(), 1); |
| 362 | } |
| 363 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 364 | // Even though it doesn't make any difference here, exercise the logic for |
| 365 | // passing in a separate config. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 366 | LogReader reader(SortParts({logfile0, logfile1}), &config_.message()); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 367 | |
| 368 | // Confirm that we can remap logged channels to point to new buses. |
| 369 | reader.RemapLoggedChannel<aos::examples::Ping>("/test", "/original"); |
| 370 | |
| 371 | // This sends out the fetched messages and advances time to the start of the |
| 372 | // log file. |
| 373 | reader.Register(); |
| 374 | |
Austin Schuh | 0767662 | 2021-01-21 18:59:17 -0800 | [diff] [blame] | 375 | EXPECT_THAT(reader.LoggedNodes(), ::testing::ElementsAre(nullptr)); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 376 | |
| 377 | std::unique_ptr<EventLoop> test_event_loop = |
| 378 | reader.event_loop_factory()->MakeEventLoop("log_reader"); |
| 379 | |
| 380 | int ping_count = 10; |
| 381 | int pong_count = 10; |
| 382 | |
| 383 | // Confirm that the ping value matches in the remapped channel location. |
| 384 | test_event_loop->MakeWatcher("/original/test", |
| 385 | [&ping_count](const examples::Ping &ping) { |
| 386 | EXPECT_EQ(ping.value(), ping_count + 1); |
| 387 | ++ping_count; |
| 388 | }); |
| 389 | // Confirm that the ping and pong counts both match, and the value also |
| 390 | // matches. |
| 391 | test_event_loop->MakeWatcher( |
| 392 | "/test", [&pong_count, &ping_count](const examples::Pong &pong) { |
| 393 | EXPECT_EQ(pong.value(), pong_count + 1); |
| 394 | ++pong_count; |
| 395 | EXPECT_EQ(ping_count, pong_count); |
| 396 | }); |
| 397 | |
| 398 | reader.event_loop_factory()->RunFor(std::chrono::seconds(100)); |
| 399 | EXPECT_EQ(ping_count, 2010); |
| 400 | } |
| 401 | |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 402 | // Tests that a large number of messages per second doesn't overwhelm writev. |
| 403 | TEST_F(LoggerTest, ManyMessages) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 404 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 405 | const ::std::string base_name = tmpdir + "/logfile"; |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 406 | const ::std::string config = |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 407 | absl::StrCat(base_name, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 408 | const ::std::string logfile = base_name + ".part0.bfbs"; |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 409 | // Remove the log file. |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 410 | unlink(config.c_str()); |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 411 | unlink(logfile.c_str()); |
| 412 | |
| 413 | LOG(INFO) << "Logging data to " << logfile; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 414 | ping_.set_quiet(true); |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 415 | |
| 416 | { |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 417 | std::unique_ptr<EventLoop> logger_event_loop = |
| 418 | event_loop_factory_.MakeEventLoop("logger"); |
| 419 | |
| 420 | std::unique_ptr<EventLoop> ping_spammer_event_loop = |
| 421 | event_loop_factory_.MakeEventLoop("ping_spammer"); |
| 422 | aos::Sender<examples::Ping> ping_sender = |
| 423 | ping_spammer_event_loop->MakeSender<examples::Ping>("/test"); |
| 424 | |
| 425 | aos::TimerHandler *timer_handler = |
| 426 | ping_spammer_event_loop->AddTimer([&ping_sender]() { |
| 427 | aos::Sender<examples::Ping>::Builder builder = |
| 428 | ping_sender.MakeBuilder(); |
| 429 | examples::Ping::Builder ping_builder = |
| 430 | builder.MakeBuilder<examples::Ping>(); |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 431 | CHECK_EQ(builder.Send(ping_builder.Finish()), RawSender::Error::kOk); |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 432 | }); |
| 433 | |
| 434 | // 100 ms / 0.05 ms -> 2000 messages. Should be enough to crash it. |
| 435 | ping_spammer_event_loop->OnRun([&ping_spammer_event_loop, timer_handler]() { |
| 436 | timer_handler->Setup(ping_spammer_event_loop->monotonic_now(), |
| 437 | chrono::microseconds(50)); |
| 438 | }); |
| 439 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 440 | Logger logger(logger_event_loop.get()); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 441 | logger.set_separate_config(false); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 442 | logger.set_polling_period(std::chrono::milliseconds(100)); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 443 | logger.StartLoggingOnRun(base_name); |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 444 | |
| 445 | event_loop_factory_.RunFor(chrono::milliseconds(1000)); |
| 446 | } |
| 447 | } |
| 448 | |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 449 | // Tests that we can read a logfile that has channels which were sent too fast. |
| 450 | TEST(SingleNodeLoggerNoFixtureTest, ReadTooFast) { |
| 451 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 452 | aos::configuration::ReadConfig( |
| 453 | ArtifactPath("aos/events/pingpong_config.json")); |
| 454 | SimulatedEventLoopFactory event_loop_factory(&config.message()); |
| 455 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
| 456 | const ::std::string base_name = tmpdir + "/logfile"; |
| 457 | const ::std::string config_file = |
| 458 | absl::StrCat(base_name, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 459 | const ::std::string logfile = base_name + "_data.part0.bfbs"; |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 460 | // Remove the log file. |
| 461 | unlink(config_file.c_str()); |
| 462 | unlink(logfile.c_str()); |
| 463 | |
| 464 | LOG(INFO) << "Logging data to " << logfile; |
| 465 | |
| 466 | int sent_messages = 0; |
| 467 | |
| 468 | { |
| 469 | std::unique_ptr<EventLoop> logger_event_loop = |
| 470 | event_loop_factory.MakeEventLoop("logger"); |
| 471 | |
| 472 | std::unique_ptr<EventLoop> ping_spammer_event_loop = |
| 473 | event_loop_factory.GetNodeEventLoopFactory(nullptr)->MakeEventLoop( |
| 474 | "ping_spammer", {NodeEventLoopFactory::CheckSentTooFast::kNo, |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 475 | NodeEventLoopFactory::ExclusiveSenders::kNo, |
| 476 | {}}); |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 477 | aos::Sender<examples::Ping> ping_sender = |
| 478 | ping_spammer_event_loop->MakeSender<examples::Ping>("/test"); |
| 479 | |
| 480 | aos::TimerHandler *timer_handler = |
| 481 | ping_spammer_event_loop->AddTimer([&ping_sender, &sent_messages]() { |
| 482 | aos::Sender<examples::Ping>::Builder builder = |
| 483 | ping_sender.MakeBuilder(); |
| 484 | examples::Ping::Builder ping_builder = |
| 485 | builder.MakeBuilder<examples::Ping>(); |
| 486 | CHECK_EQ(builder.Send(ping_builder.Finish()), RawSender::Error::kOk); |
| 487 | ++sent_messages; |
| 488 | }); |
| 489 | |
| 490 | constexpr std::chrono::microseconds kSendPeriod{10}; |
| 491 | const int max_legal_messages = |
| 492 | ping_sender.channel()->frequency() * |
| 493 | event_loop_factory.configuration()->channel_storage_duration() / |
| 494 | 1000000000; |
| 495 | |
| 496 | ping_spammer_event_loop->OnRun( |
| 497 | [&ping_spammer_event_loop, kSendPeriod, timer_handler]() { |
| 498 | timer_handler->Setup( |
| 499 | ping_spammer_event_loop->monotonic_now() + kSendPeriod / 2, |
| 500 | kSendPeriod); |
| 501 | }); |
| 502 | |
| 503 | Logger logger(logger_event_loop.get()); |
| 504 | logger.set_separate_config(false); |
| 505 | logger.set_polling_period(std::chrono::milliseconds(100)); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 506 | logger.StartLoggingOnRun(base_name); |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 507 | |
| 508 | event_loop_factory.RunFor(kSendPeriod * max_legal_messages * 2); |
| 509 | } |
| 510 | |
| 511 | LogReader reader(logfile); |
| 512 | |
| 513 | reader.Register(); |
| 514 | |
| 515 | std::unique_ptr<EventLoop> test_event_loop = |
| 516 | reader.event_loop_factory()->MakeEventLoop("log_reader"); |
| 517 | |
| 518 | int replay_count = 0; |
| 519 | |
| 520 | test_event_loop->MakeWatcher( |
| 521 | "/test", [&replay_count](const examples::Ping &) { ++replay_count; }); |
| 522 | |
| 523 | reader.event_loop_factory()->Run(); |
| 524 | EXPECT_EQ(replay_count, sent_messages); |
| 525 | } |
| 526 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 527 | } // namespace testing |
| 528 | } // namespace logger |
| 529 | } // namespace aos |