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