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