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" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 6 | #include "glog/logging.h" |
| 7 | #include "gmock/gmock.h" |
| 8 | #include "gtest/gtest.h" |
| 9 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 10 | #include "aos/events/event_loop.h" |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 11 | #include "aos/events/logging/log_reader.h" |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 12 | #include "aos/events/logging/log_writer.h" |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 13 | #include "aos/events/logging/snappy_encoder.h" |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 14 | #include "aos/events/message_counter.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 15 | #include "aos/events/ping_lib.h" |
| 16 | #include "aos/events/pong_lib.h" |
| 17 | #include "aos/events/simulated_event_loop.h" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 18 | #include "aos/network/remote_message_generated.h" |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 19 | #include "aos/network/testing_time_converter.h" |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 20 | #include "aos/network/timestamp_generated.h" |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 21 | #include "aos/testing/path.h" |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 22 | #include "aos/testing/tmpdir.h" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 23 | #include "aos/util/file.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 24 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 25 | #ifdef LZMA |
| 26 | #include "aos/events/logging/lzma_encoder.h" |
| 27 | #endif |
| 28 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 29 | namespace aos::logger::testing { |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 30 | |
| 31 | namespace chrono = std::chrono; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 32 | using aos::message_bridge::RemoteMessage; |
Naman Gupta | a63aa13 | 2023-03-22 20:06:34 -0700 | [diff] [blame] | 33 | using aos::testing::ArtifactPath; |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 34 | using aos::testing::MessageCounter; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 35 | |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 36 | constexpr std::string_view kSingleConfigSha256( |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 37 | "bbe1b563139273b23a5405eebc2f2740cefcda5f96681acd0a84b8ff9ab93ea4"); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 38 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 39 | class LoggerTest : public ::testing::Test { |
| 40 | public: |
| 41 | LoggerTest() |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 42 | : config_(aos::configuration::ReadConfig( |
| 43 | ArtifactPath("aos/events/pingpong_config.json"))), |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 44 | event_loop_factory_(&config_.message()), |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 45 | ping_event_loop_(event_loop_factory_.MakeEventLoop("ping")), |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 46 | ping_(ping_event_loop_.get()), |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 47 | pong_event_loop_(event_loop_factory_.MakeEventLoop("pong")), |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 48 | pong_(pong_event_loop_.get()) {} |
| 49 | |
| 50 | // Config and factory. |
| 51 | aos::FlatbufferDetachedBuffer<aos::Configuration> config_; |
| 52 | SimulatedEventLoopFactory event_loop_factory_; |
| 53 | |
| 54 | // Event loop and app for Ping |
| 55 | std::unique_ptr<EventLoop> ping_event_loop_; |
| 56 | Ping ping_; |
| 57 | |
| 58 | // Event loop and app for Pong |
| 59 | std::unique_ptr<EventLoop> pong_event_loop_; |
| 60 | Pong pong_; |
| 61 | }; |
| 62 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 63 | using LoggerDeathTest = LoggerTest; |
| 64 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 65 | // Tests that we can startup at all. This confirms that the channels are all in |
| 66 | // the config. |
| 67 | TEST_F(LoggerTest, Starts) { |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 68 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 69 | const ::std::string base_name = tmpdir + "/logfile"; |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 70 | const ::std::string config = |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 71 | absl::StrCat(base_name, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 72 | const ::std::string logfile = base_name + "_data.part0.bfbs"; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 73 | // Remove it. |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 74 | unlink(config.c_str()); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 75 | unlink(logfile.c_str()); |
| 76 | |
| 77 | LOG(INFO) << "Logging data to " << logfile; |
| 78 | |
| 79 | { |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 80 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 81 | |
Austin Schuh | eeb86fc | 2024-04-04 20:12:39 -0700 | [diff] [blame] | 82 | std::unique_ptr<EventLoop> logger_event_loop = |
| 83 | event_loop_factory_.MakeEventLoop("logger"); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 84 | Logger logger(logger_event_loop.get()); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 85 | logger.set_separate_config(false); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 86 | logger.set_polling_period(std::chrono::milliseconds(100)); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 87 | logger.StartLoggingOnRun(base_name); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 88 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 89 | } |
| 90 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 91 | ASSERT_TRUE(std::filesystem::exists(logfile)); |
| 92 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 93 | // Even though it doesn't make any difference here, exercise the logic for |
| 94 | // passing in a separate config. |
| 95 | LogReader reader(logfile, &config_.message()); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 96 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 97 | // This sends out the fetched messages and advances time to the start of the |
| 98 | // log file. |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 99 | reader.Register(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 100 | |
Austin Schuh | 0767662 | 2021-01-21 18:59:17 -0800 | [diff] [blame] | 101 | EXPECT_THAT(reader.LoggedNodes(), ::testing::ElementsAre(nullptr)); |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 102 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 103 | std::unique_ptr<EventLoop> test_event_loop = |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 104 | reader.event_loop_factory()->MakeEventLoop("log_reader"); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 105 | |
| 106 | int ping_count = 10; |
| 107 | int pong_count = 10; |
| 108 | |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame] | 109 | // Confirm that the ping value matches. |
| 110 | test_event_loop->MakeWatcher("/test", |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 111 | [&ping_count](const examples::Ping &ping) { |
| 112 | EXPECT_EQ(ping.value(), ping_count + 1); |
| 113 | ++ping_count; |
| 114 | }); |
| 115 | // Confirm that the ping and pong counts both match, and the value also |
| 116 | // matches. |
| 117 | test_event_loop->MakeWatcher( |
| 118 | "/test", [&pong_count, &ping_count](const examples::Pong &pong) { |
| 119 | EXPECT_EQ(pong.value(), pong_count + 1); |
| 120 | ++pong_count; |
| 121 | EXPECT_EQ(ping_count, pong_count); |
| 122 | }); |
| 123 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 124 | reader.event_loop_factory()->RunFor(std::chrono::seconds(100)); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 125 | EXPECT_EQ(ping_count, 2010); |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame] | 126 | EXPECT_EQ(pong_count, ping_count); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Eric Schmiedeberg | ae00e73 | 2023-04-12 15:53:17 -0600 | [diff] [blame] | 129 | // Tests that we can mutate a message before sending |
| 130 | TEST_F(LoggerTest, MutateCallback) { |
| 131 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
| 132 | const ::std::string base_name = tmpdir + "/logfile"; |
| 133 | const ::std::string config = |
| 134 | absl::StrCat(base_name, kSingleConfigSha256, ".bfbs"); |
| 135 | const ::std::string logfile = base_name + "_data.part0.bfbs"; |
| 136 | // Remove it. |
| 137 | unlink(config.c_str()); |
| 138 | unlink(logfile.c_str()); |
| 139 | |
| 140 | LOG(INFO) << "Logging data to " << logfile; |
| 141 | |
| 142 | { |
Eric Schmiedeberg | ae00e73 | 2023-04-12 15:53:17 -0600 | [diff] [blame] | 143 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 144 | |
Austin Schuh | eeb86fc | 2024-04-04 20:12:39 -0700 | [diff] [blame] | 145 | std::unique_ptr<EventLoop> logger_event_loop = |
| 146 | event_loop_factory_.MakeEventLoop("logger"); |
Eric Schmiedeberg | ae00e73 | 2023-04-12 15:53:17 -0600 | [diff] [blame] | 147 | Logger logger(logger_event_loop.get()); |
| 148 | logger.set_separate_config(false); |
| 149 | logger.set_polling_period(std::chrono::milliseconds(100)); |
| 150 | logger.StartLoggingOnRun(base_name); |
| 151 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 152 | } |
| 153 | |
| 154 | // Even though it doesn't make any difference here, exercise the logic for |
| 155 | // passing in a separate config. |
| 156 | LogReader reader(logfile, &config_.message()); |
| 157 | |
| 158 | reader.AddBeforeSendCallback("/test", [](aos::examples::Ping *ping) { |
| 159 | ping->mutate_value(ping->value() + 1); |
| 160 | }); |
| 161 | |
| 162 | // This sends out the fetched messages and advances time to the start of the |
| 163 | // log file. |
| 164 | reader.Register(); |
| 165 | |
| 166 | EXPECT_THAT(reader.LoggedNodes(), ::testing::ElementsAre(nullptr)); |
| 167 | |
| 168 | std::unique_ptr<EventLoop> test_event_loop = |
| 169 | reader.event_loop_factory()->MakeEventLoop("log_reader"); |
| 170 | |
| 171 | // Confirm that the ping and pong counts both match, and the value also |
| 172 | // matches. |
| 173 | int ping_count = 10; |
| 174 | test_event_loop->MakeWatcher("/test", |
| 175 | [&ping_count](const examples::Ping &ping) { |
| 176 | ++ping_count; |
| 177 | EXPECT_EQ(ping.value(), ping_count); |
| 178 | }); |
| 179 | |
| 180 | reader.event_loop_factory()->RunFor(std::chrono::seconds(100)); |
| 181 | EXPECT_EQ(ping_count, 2010); |
| 182 | } |
| 183 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 184 | // Tests calling StartLogging twice. |
| 185 | TEST_F(LoggerDeathTest, ExtraStart) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 186 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 187 | const ::std::string base_name1 = tmpdir + "/logfile1"; |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 188 | const ::std::string config1 = |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 189 | absl::StrCat(base_name1, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 190 | const ::std::string logfile1 = base_name1 + "_data.part0.bfbs"; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 191 | const ::std::string base_name2 = tmpdir + "/logfile2"; |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 192 | const ::std::string config2 = |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 193 | absl::StrCat(base_name2, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 194 | const ::std::string logfile2 = base_name2 + "_data.part0.bfbs"; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 195 | unlink(logfile1.c_str()); |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 196 | unlink(config1.c_str()); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 197 | unlink(logfile2.c_str()); |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 198 | unlink(config2.c_str()); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 199 | |
| 200 | LOG(INFO) << "Logging data to " << logfile1 << " then " << logfile2; |
| 201 | |
| 202 | { |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 203 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 204 | |
Austin Schuh | eeb86fc | 2024-04-04 20:12:39 -0700 | [diff] [blame] | 205 | std::unique_ptr<EventLoop> logger_event_loop = |
| 206 | event_loop_factory_.MakeEventLoop("logger"); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 207 | Logger logger(logger_event_loop.get()); |
| 208 | logger.set_polling_period(std::chrono::milliseconds(100)); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 209 | logger_event_loop->OnRun([base_name1, base_name2, &logger_event_loop, |
| 210 | &logger]() { |
| 211 | logger.StartLogging(std::make_unique<MultiNodeFilesLogNamer>( |
| 212 | base_name1, logger_event_loop->configuration(), |
| 213 | logger_event_loop.get(), logger_event_loop->node())); |
| 214 | EXPECT_DEATH(logger.StartLogging(std::make_unique<MultiNodeFilesLogNamer>( |
| 215 | base_name2, logger_event_loop->configuration(), |
| 216 | logger_event_loop.get(), logger_event_loop->node())), |
| 217 | "Already logging"); |
| 218 | }); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 219 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 220 | } |
| 221 | } |
| 222 | |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 223 | // Tests that we die if the replayer attempts to send on a logged channel. |
| 224 | TEST_F(LoggerDeathTest, DieOnDuplicateReplayChannels) { |
| 225 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 226 | aos::configuration::ReadConfig( |
| 227 | ArtifactPath("aos/events/pingpong_config.json")); |
| 228 | SimulatedEventLoopFactory event_loop_factory(&config.message()); |
| 229 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
| 230 | const ::std::string base_name = tmpdir + "/logfile"; |
| 231 | const ::std::string config_file = |
| 232 | absl::StrCat(base_name, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 233 | const ::std::string logfile = base_name + "_data.part0.bfbs"; |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 234 | // Remove the log file. |
| 235 | unlink(config_file.c_str()); |
| 236 | unlink(logfile.c_str()); |
| 237 | |
| 238 | LOG(INFO) << "Logging data to " << logfile; |
| 239 | |
| 240 | { |
| 241 | std::unique_ptr<EventLoop> logger_event_loop = |
| 242 | event_loop_factory.MakeEventLoop("logger"); |
| 243 | |
| 244 | Logger logger(logger_event_loop.get()); |
| 245 | logger.set_separate_config(false); |
| 246 | logger.set_polling_period(std::chrono::milliseconds(100)); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 247 | logger.StartLoggingOnRun(base_name); |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 248 | |
| 249 | event_loop_factory.RunFor(chrono::seconds(2)); |
| 250 | } |
| 251 | |
| 252 | LogReader reader(logfile); |
| 253 | |
| 254 | reader.Register(); |
| 255 | |
| 256 | std::unique_ptr<EventLoop> test_event_loop = |
| 257 | reader.event_loop_factory()->MakeEventLoop("log_reader"); |
| 258 | |
| 259 | EXPECT_DEATH(test_event_loop->MakeSender<examples::Ping>("/test"), |
| 260 | "exclusive channel.*examples.Ping"); |
| 261 | } |
| 262 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 263 | // Tests calling StopLogging twice. |
| 264 | TEST_F(LoggerDeathTest, ExtraStop) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 265 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 266 | const ::std::string base_name = tmpdir + "/logfile"; |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 267 | const ::std::string config = |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 268 | absl::StrCat(base_name, kSingleConfigSha256, ".bfbs"); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 269 | const ::std::string logfile = base_name + ".part0.bfbs"; |
| 270 | // Remove it. |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 271 | unlink(config.c_str()); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 272 | unlink(logfile.c_str()); |
| 273 | |
| 274 | LOG(INFO) << "Logging data to " << logfile; |
| 275 | |
| 276 | { |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 277 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 278 | |
Austin Schuh | eeb86fc | 2024-04-04 20:12:39 -0700 | [diff] [blame] | 279 | std::unique_ptr<EventLoop> logger_event_loop = |
| 280 | event_loop_factory_.MakeEventLoop("logger"); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 281 | Logger logger(logger_event_loop.get()); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 282 | logger.set_separate_config(false); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 283 | logger.set_polling_period(std::chrono::milliseconds(100)); |
| 284 | logger_event_loop->OnRun([base_name, &logger_event_loop, &logger]() { |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 285 | logger.StartLogging(std::make_unique<MultiNodeFilesLogNamer>( |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 286 | base_name, logger_event_loop->configuration(), |
| 287 | logger_event_loop.get(), logger_event_loop->node())); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 288 | logger.StopLogging(aos::monotonic_clock::min_time); |
| 289 | EXPECT_DEATH(logger.StopLogging(aos::monotonic_clock::min_time), |
| 290 | "Not logging right now"); |
| 291 | }); |
| 292 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // Tests that we can startup twice. |
| 297 | TEST_F(LoggerTest, StartsTwice) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 298 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 299 | const ::std::string base_name1 = tmpdir + "/logfile1"; |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 300 | const ::std::string config1 = |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 301 | absl::StrCat(base_name1, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 302 | const ::std::string logfile1 = base_name1 + "_data.part0.bfbs"; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 303 | const ::std::string base_name2 = tmpdir + "/logfile2"; |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 304 | const ::std::string config2 = |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 305 | absl::StrCat(base_name2, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 306 | const ::std::string logfile2 = base_name2 + "_data.part0.bfbs"; |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 307 | unlink(logfile1.c_str()); |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 308 | unlink(config1.c_str()); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 309 | unlink(logfile2.c_str()); |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 310 | unlink(config2.c_str()); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 311 | |
| 312 | LOG(INFO) << "Logging data to " << logfile1 << " then " << logfile2; |
| 313 | |
| 314 | { |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 315 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 316 | |
Austin Schuh | eeb86fc | 2024-04-04 20:12:39 -0700 | [diff] [blame] | 317 | std::unique_ptr<EventLoop> logger_event_loop = |
| 318 | event_loop_factory_.MakeEventLoop("logger"); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 319 | Logger logger(logger_event_loop.get()); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 320 | logger.set_separate_config(false); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 321 | logger.set_polling_period(std::chrono::milliseconds(100)); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 322 | logger.StartLogging(std::make_unique<MultiNodeFilesLogNamer>( |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 323 | base_name1, logger_event_loop->configuration(), logger_event_loop.get(), |
| 324 | logger_event_loop->node())); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 325 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
| 326 | logger.StopLogging(logger_event_loop->monotonic_now()); |
| 327 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 328 | logger.StartLogging(std::make_unique<MultiNodeFilesLogNamer>( |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 329 | base_name2, logger_event_loop->configuration(), logger_event_loop.get(), |
| 330 | logger_event_loop->node())); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 331 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
| 332 | } |
| 333 | |
| 334 | for (const auto &logfile : |
| 335 | {std::make_tuple(logfile1, 10), std::make_tuple(logfile2, 2010)}) { |
| 336 | SCOPED_TRACE(std::get<0>(logfile)); |
| 337 | LogReader reader(std::get<0>(logfile)); |
| 338 | reader.Register(); |
| 339 | |
Austin Schuh | 0767662 | 2021-01-21 18:59:17 -0800 | [diff] [blame] | 340 | EXPECT_THAT(reader.LoggedNodes(), ::testing::ElementsAre(nullptr)); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 341 | |
| 342 | std::unique_ptr<EventLoop> test_event_loop = |
| 343 | reader.event_loop_factory()->MakeEventLoop("log_reader"); |
| 344 | |
| 345 | int ping_count = std::get<1>(logfile); |
| 346 | int pong_count = std::get<1>(logfile); |
| 347 | |
| 348 | // Confirm that the ping and pong counts both match, and the value also |
| 349 | // matches. |
| 350 | test_event_loop->MakeWatcher("/test", |
| 351 | [&ping_count](const examples::Ping &ping) { |
| 352 | EXPECT_EQ(ping.value(), ping_count + 1); |
| 353 | ++ping_count; |
| 354 | }); |
| 355 | test_event_loop->MakeWatcher( |
| 356 | "/test", [&pong_count, &ping_count](const examples::Pong &pong) { |
| 357 | EXPECT_EQ(pong.value(), pong_count + 1); |
| 358 | ++pong_count; |
| 359 | EXPECT_EQ(ping_count, pong_count); |
| 360 | }); |
| 361 | |
| 362 | reader.event_loop_factory()->RunFor(std::chrono::seconds(100)); |
| 363 | EXPECT_EQ(ping_count, std::get<1>(logfile) + 1000); |
| 364 | } |
| 365 | } |
| 366 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 367 | // Tests that we can read and write rotated log files. |
| 368 | TEST_F(LoggerTest, RotatedLogFile) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 369 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 370 | const ::std::string base_name = tmpdir + "/logfile"; |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 371 | const ::std::string config = |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 372 | absl::StrCat(base_name, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 373 | const ::std::string logfile0 = base_name + "_data.part0.bfbs"; |
| 374 | const ::std::string logfile1 = base_name + "_data.part1.bfbs"; |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 375 | // Remove it. |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 376 | unlink(config.c_str()); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 377 | unlink(logfile0.c_str()); |
| 378 | unlink(logfile1.c_str()); |
| 379 | |
| 380 | LOG(INFO) << "Logging data to " << logfile0 << " and " << logfile1; |
| 381 | |
| 382 | { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 383 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 384 | |
Austin Schuh | eeb86fc | 2024-04-04 20:12:39 -0700 | [diff] [blame] | 385 | std::unique_ptr<EventLoop> logger_event_loop = |
| 386 | event_loop_factory_.MakeEventLoop("logger"); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 387 | Logger logger(logger_event_loop.get()); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 388 | logger.set_separate_config(false); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 389 | logger.set_polling_period(std::chrono::milliseconds(100)); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 390 | logger.StartLoggingOnRun(base_name); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 391 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 392 | logger.Rotate(); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 393 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
| 394 | } |
| 395 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 396 | { |
| 397 | // Confirm that the UUIDs match for both the parts and the logger, and the |
| 398 | // parts_index increments. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 399 | std::vector<SizePrefixedFlatbufferVector<LogFileHeader>> log_header; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 400 | for (std::string_view f : {logfile0, logfile1}) { |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 401 | log_header.emplace_back(ReadHeader(f).value()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 404 | EXPECT_EQ(log_header[0].message().log_event_uuid()->string_view(), |
| 405 | log_header[1].message().log_event_uuid()->string_view()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 406 | EXPECT_EQ(log_header[0].message().parts_uuid()->string_view(), |
| 407 | log_header[1].message().parts_uuid()->string_view()); |
| 408 | |
| 409 | EXPECT_EQ(log_header[0].message().parts_index(), 0); |
| 410 | EXPECT_EQ(log_header[1].message().parts_index(), 1); |
| 411 | } |
| 412 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 413 | // Even though it doesn't make any difference here, exercise the logic for |
| 414 | // passing in a separate config. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 415 | LogReader reader(SortParts({logfile0, logfile1}), &config_.message()); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 416 | |
| 417 | // Confirm that we can remap logged channels to point to new buses. |
| 418 | reader.RemapLoggedChannel<aos::examples::Ping>("/test", "/original"); |
| 419 | |
| 420 | // This sends out the fetched messages and advances time to the start of the |
| 421 | // log file. |
| 422 | reader.Register(); |
| 423 | |
Austin Schuh | 0767662 | 2021-01-21 18:59:17 -0800 | [diff] [blame] | 424 | EXPECT_THAT(reader.LoggedNodes(), ::testing::ElementsAre(nullptr)); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 425 | |
| 426 | std::unique_ptr<EventLoop> test_event_loop = |
| 427 | reader.event_loop_factory()->MakeEventLoop("log_reader"); |
| 428 | |
| 429 | int ping_count = 10; |
| 430 | int pong_count = 10; |
| 431 | |
| 432 | // Confirm that the ping value matches in the remapped channel location. |
| 433 | test_event_loop->MakeWatcher("/original/test", |
| 434 | [&ping_count](const examples::Ping &ping) { |
| 435 | EXPECT_EQ(ping.value(), ping_count + 1); |
| 436 | ++ping_count; |
| 437 | }); |
| 438 | // Confirm that the ping and pong counts both match, and the value also |
| 439 | // matches. |
| 440 | test_event_loop->MakeWatcher( |
| 441 | "/test", [&pong_count, &ping_count](const examples::Pong &pong) { |
| 442 | EXPECT_EQ(pong.value(), pong_count + 1); |
| 443 | ++pong_count; |
| 444 | EXPECT_EQ(ping_count, pong_count); |
| 445 | }); |
| 446 | |
| 447 | reader.event_loop_factory()->RunFor(std::chrono::seconds(100)); |
| 448 | EXPECT_EQ(ping_count, 2010); |
| 449 | } |
| 450 | |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 451 | // Tests that a large number of messages per second doesn't overwhelm writev. |
| 452 | TEST_F(LoggerTest, ManyMessages) { |
Austin Schuh | c6f8f1b | 2020-12-02 23:23:39 -0800 | [diff] [blame] | 453 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 454 | const ::std::string base_name = tmpdir + "/logfile"; |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 455 | const ::std::string config = |
Austin Schuh | ee4713b | 2021-03-21 19:25:17 -0700 | [diff] [blame] | 456 | absl::StrCat(base_name, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 457 | const ::std::string logfile = base_name + ".part0.bfbs"; |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 458 | // Remove the log file. |
Austin Schuh | 25b4671 | 2021-01-03 00:04:38 -0800 | [diff] [blame] | 459 | unlink(config.c_str()); |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 460 | unlink(logfile.c_str()); |
| 461 | |
| 462 | LOG(INFO) << "Logging data to " << logfile; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 463 | ping_.set_quiet(true); |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 464 | |
| 465 | { |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 466 | std::unique_ptr<EventLoop> logger_event_loop = |
| 467 | event_loop_factory_.MakeEventLoop("logger"); |
| 468 | |
| 469 | std::unique_ptr<EventLoop> ping_spammer_event_loop = |
| 470 | event_loop_factory_.MakeEventLoop("ping_spammer"); |
| 471 | aos::Sender<examples::Ping> ping_sender = |
| 472 | ping_spammer_event_loop->MakeSender<examples::Ping>("/test"); |
| 473 | |
| 474 | aos::TimerHandler *timer_handler = |
| 475 | ping_spammer_event_loop->AddTimer([&ping_sender]() { |
| 476 | aos::Sender<examples::Ping>::Builder builder = |
| 477 | ping_sender.MakeBuilder(); |
| 478 | examples::Ping::Builder ping_builder = |
| 479 | builder.MakeBuilder<examples::Ping>(); |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 480 | CHECK_EQ(builder.Send(ping_builder.Finish()), RawSender::Error::kOk); |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 481 | }); |
| 482 | |
| 483 | // 100 ms / 0.05 ms -> 2000 messages. Should be enough to crash it. |
| 484 | ping_spammer_event_loop->OnRun([&ping_spammer_event_loop, timer_handler]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 485 | timer_handler->Schedule(ping_spammer_event_loop->monotonic_now(), |
| 486 | chrono::microseconds(50)); |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 487 | }); |
| 488 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 489 | Logger logger(logger_event_loop.get()); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 490 | logger.set_separate_config(false); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 491 | logger.set_polling_period(std::chrono::milliseconds(100)); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 492 | logger.StartLoggingOnRun(base_name); |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 493 | |
| 494 | event_loop_factory_.RunFor(chrono::milliseconds(1000)); |
| 495 | } |
| 496 | } |
| 497 | |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 498 | // Tests that we can read a logfile that has channels which were sent too fast. |
| 499 | TEST(SingleNodeLoggerNoFixtureTest, ReadTooFast) { |
| 500 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 501 | aos::configuration::ReadConfig( |
| 502 | ArtifactPath("aos/events/pingpong_config.json")); |
| 503 | SimulatedEventLoopFactory event_loop_factory(&config.message()); |
| 504 | const ::std::string tmpdir = aos::testing::TestTmpDir(); |
| 505 | const ::std::string base_name = tmpdir + "/logfile"; |
| 506 | const ::std::string config_file = |
| 507 | absl::StrCat(base_name, kSingleConfigSha256, ".bfbs"); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 508 | const ::std::string logfile = base_name + "_data.part0.bfbs"; |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 509 | // Remove the log file. |
| 510 | unlink(config_file.c_str()); |
| 511 | unlink(logfile.c_str()); |
| 512 | |
| 513 | LOG(INFO) << "Logging data to " << logfile; |
| 514 | |
| 515 | int sent_messages = 0; |
| 516 | |
| 517 | { |
| 518 | std::unique_ptr<EventLoop> logger_event_loop = |
| 519 | event_loop_factory.MakeEventLoop("logger"); |
| 520 | |
| 521 | std::unique_ptr<EventLoop> ping_spammer_event_loop = |
| 522 | event_loop_factory.GetNodeEventLoopFactory(nullptr)->MakeEventLoop( |
| 523 | "ping_spammer", {NodeEventLoopFactory::CheckSentTooFast::kNo, |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 524 | NodeEventLoopFactory::ExclusiveSenders::kNo, |
| 525 | {}}); |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 526 | aos::Sender<examples::Ping> ping_sender = |
| 527 | ping_spammer_event_loop->MakeSender<examples::Ping>("/test"); |
| 528 | |
| 529 | aos::TimerHandler *timer_handler = |
| 530 | ping_spammer_event_loop->AddTimer([&ping_sender, &sent_messages]() { |
| 531 | aos::Sender<examples::Ping>::Builder builder = |
| 532 | ping_sender.MakeBuilder(); |
| 533 | examples::Ping::Builder ping_builder = |
| 534 | builder.MakeBuilder<examples::Ping>(); |
| 535 | CHECK_EQ(builder.Send(ping_builder.Finish()), RawSender::Error::kOk); |
| 536 | ++sent_messages; |
| 537 | }); |
| 538 | |
| 539 | constexpr std::chrono::microseconds kSendPeriod{10}; |
Austin Schuh | fff9c3a | 2023-06-16 18:48:23 -0700 | [diff] [blame] | 540 | const int max_legal_messages = configuration::QueueSize( |
| 541 | event_loop_factory.configuration(), ping_sender.channel()); |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 542 | |
| 543 | ping_spammer_event_loop->OnRun( |
| 544 | [&ping_spammer_event_loop, kSendPeriod, timer_handler]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 545 | timer_handler->Schedule( |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 546 | ping_spammer_event_loop->monotonic_now() + kSendPeriod / 2, |
| 547 | kSendPeriod); |
| 548 | }); |
| 549 | |
| 550 | Logger logger(logger_event_loop.get()); |
| 551 | logger.set_separate_config(false); |
| 552 | logger.set_polling_period(std::chrono::milliseconds(100)); |
Austin Schuh | 1171769 | 2022-10-16 17:11:28 -0700 | [diff] [blame] | 553 | logger.StartLoggingOnRun(base_name); |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 554 | |
| 555 | event_loop_factory.RunFor(kSendPeriod * max_legal_messages * 2); |
| 556 | } |
| 557 | |
| 558 | LogReader reader(logfile); |
| 559 | |
| 560 | reader.Register(); |
| 561 | |
| 562 | std::unique_ptr<EventLoop> test_event_loop = |
| 563 | reader.event_loop_factory()->MakeEventLoop("log_reader"); |
| 564 | |
| 565 | int replay_count = 0; |
| 566 | |
| 567 | test_event_loop->MakeWatcher( |
| 568 | "/test", [&replay_count](const examples::Ping &) { ++replay_count; }); |
| 569 | |
| 570 | reader.event_loop_factory()->Run(); |
| 571 | EXPECT_EQ(replay_count, sent_messages); |
| 572 | } |
| 573 | |
Maxwell Gumley | d26e629 | 2024-04-24 10:45:07 -0600 | [diff] [blame^] | 574 | // Helper function to verify the contents of the profiling data file. |
| 575 | void VerifyProfilingData(const std::filesystem::path &profiling_path) { |
| 576 | std::ifstream file(profiling_path); |
| 577 | ASSERT_TRUE(file.is_open()) << "Failed to open profiling data file."; |
| 578 | |
| 579 | std::string line; |
| 580 | |
| 581 | // Verify that the header is a comment starting with '#'. |
| 582 | std::getline(file, line); |
| 583 | ASSERT_THAT(line, ::testing::StartsWith("#")); |
| 584 | |
| 585 | // Now, verify the contents of each line. |
| 586 | int record_count = 0; |
| 587 | |
| 588 | // Track the total encoding duration. |
| 589 | uint64_t total_encoding_duration_ns = 0; |
| 590 | |
| 591 | while (std::getline(file, line)) { |
| 592 | std::stringstream line_stream(line); |
| 593 | std::string cell; |
| 594 | |
| 595 | // Extract each cell from the CSV line. |
| 596 | std::vector<std::string> cells; |
| 597 | while (std::getline(line_stream, cell, ',')) { |
| 598 | cells.push_back(cell); |
| 599 | } |
| 600 | |
| 601 | // Expecting 5 fields: channel_name, channel_type, encode_duration_ns, |
| 602 | // encoding_start_time_ns, message_time_s. |
| 603 | ASSERT_EQ(cells.size(), 5) << "Incorrect number of fields in the CSV line."; |
| 604 | |
| 605 | // Channel name and type are strings and just need to not be empty. |
| 606 | EXPECT_FALSE(cells[0].empty()) << "Channel name is empty."; |
| 607 | EXPECT_FALSE(cells[1].empty()) << "Channel type is empty."; |
| 608 | |
| 609 | // Encode duration, encoding start time should be positive numbers. |
| 610 | const int64_t encode_duration_ns = std::stoll(cells[2]); |
| 611 | const int64_t encoding_start_time_ns = std::stoll(cells[3]); |
| 612 | |
| 613 | ASSERT_GT(encode_duration_ns, 0) |
| 614 | << "Encode duration is not positive. Line: " << line; |
| 615 | ASSERT_GT(encoding_start_time_ns, 0) |
| 616 | << "Encoding start time is not positive. Line: " << line; |
| 617 | |
| 618 | // Message time should be non-negative. |
| 619 | const double message_time_s = std::stod(cells[4]); |
| 620 | EXPECT_GE(message_time_s, 0) << "Message time is negative"; |
| 621 | ++record_count; |
| 622 | total_encoding_duration_ns += encode_duration_ns; |
| 623 | } |
| 624 | |
| 625 | EXPECT_GT(record_count, 0) << "Profiling data file is empty."; |
| 626 | LOG(INFO) << "Total encoding duration: " << total_encoding_duration_ns; |
| 627 | } |
| 628 | |
| 629 | // Tests logging many messages with LZMA compression. |
| 630 | TEST_F(LoggerTest, ManyMessagesLzmaWithProfiling) { |
| 631 | const std::string tmpdir = aos::testing::TestTmpDir(); |
| 632 | const std::string base_name = tmpdir + "/lzma_logfile"; |
| 633 | const std::string config_sha256 = |
| 634 | absl::StrCat(base_name, kSingleConfigSha256, ".bfbs"); |
| 635 | const std::string logfile = absl::StrCat(base_name, ".part0.xz"); |
| 636 | const std::string profiling_path = |
| 637 | absl::StrCat(tmpdir, "/encoding_profile.csv"); |
| 638 | |
| 639 | // Clean up any previous test artifacts. |
| 640 | unlink(config_sha256.c_str()); |
| 641 | unlink(logfile.c_str()); |
| 642 | |
| 643 | LOG(INFO) << "Logging data to " << logfile; |
| 644 | ping_.set_quiet(true); |
| 645 | |
| 646 | { |
| 647 | std::unique_ptr<aos::EventLoop> logger_event_loop = |
| 648 | event_loop_factory_.MakeEventLoop("logger"); |
| 649 | |
| 650 | std::unique_ptr<aos::EventLoop> ping_spammer_event_loop = |
| 651 | event_loop_factory_.MakeEventLoop("ping_spammer"); |
| 652 | aos::Sender<examples::Ping> ping_sender = |
| 653 | ping_spammer_event_loop->MakeSender<examples::Ping>("/test"); |
| 654 | |
| 655 | aos::TimerHandler *timer_handler = |
| 656 | ping_spammer_event_loop->AddTimer([&ping_sender]() { |
| 657 | aos::Sender<examples::Ping>::Builder builder = |
| 658 | ping_sender.MakeBuilder(); |
| 659 | examples::Ping::Builder ping_builder = |
| 660 | builder.MakeBuilder<examples::Ping>(); |
| 661 | CHECK_EQ(builder.Send(ping_builder.Finish()), |
| 662 | aos::RawSender::Error::kOk); |
| 663 | }); |
| 664 | |
| 665 | // Send a message every 50 microseconds to simulate high throughput. |
| 666 | ping_spammer_event_loop->OnRun([&ping_spammer_event_loop, timer_handler]() { |
| 667 | timer_handler->Schedule(ping_spammer_event_loop->monotonic_now(), |
| 668 | std::chrono::microseconds(50)); |
| 669 | }); |
| 670 | |
| 671 | aos::logger::Logger logger(logger_event_loop.get()); |
| 672 | logger.set_separate_config(false); |
| 673 | logger.set_polling_period(std::chrono::milliseconds(100)); |
| 674 | |
| 675 | // Enable logger profiling. |
| 676 | logger.SetProfilingPath(profiling_path); |
| 677 | |
| 678 | std::unique_ptr<aos::logger::MultiNodeFilesLogNamer> log_namer = |
| 679 | std::make_unique<aos::logger::MultiNodeFilesLogNamer>( |
| 680 | base_name, logger_event_loop->configuration(), |
| 681 | logger_event_loop.get(), logger_event_loop->node()); |
| 682 | #ifdef LZMA |
| 683 | // Set up LZMA encoder. |
| 684 | log_namer->set_encoder_factory([](size_t max_message_size) { |
| 685 | return std::make_unique<aos::logger::LzmaEncoder>(max_message_size, 1); |
| 686 | }); |
| 687 | #endif |
| 688 | |
| 689 | logger.StartLogging(std::move(log_namer)); |
| 690 | |
| 691 | event_loop_factory_.RunFor(std::chrono::seconds(1)); |
| 692 | } |
| 693 | |
| 694 | #ifdef LZMA |
| 695 | VerifyProfilingData(profiling_path); |
| 696 | #endif |
| 697 | } |
| 698 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 699 | } // namespace aos::logger::testing |