Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 1 | #include "aos/events/logging/log_backend.h" |
| 2 | |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 3 | #include <algorithm> |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 4 | #include <filesystem> |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 5 | #include <fstream> |
| 6 | #include <random> |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 7 | |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 8 | #include "absl/strings/str_cat.h" |
| 9 | #include "absl/strings/str_join.h" |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 10 | #include "glog/logging.h" |
| 11 | #include "gmock/gmock.h" |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 12 | #include "gtest/gtest.h" |
| 13 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 14 | #include "aos/containers/resizeable_buffer.h" |
| 15 | #include "aos/testing/tmpdir.h" |
| 16 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 17 | namespace aos::logger::testing { |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 18 | namespace { |
| 19 | // Helper to write simple string to the log sink |
| 20 | WriteResult Write(LogSink *log_sink, std::string_view content) { |
| 21 | auto span = absl::Span<const uint8_t>( |
| 22 | reinterpret_cast<const unsigned char *>(content.data()), content.size()); |
| 23 | auto queue = absl::Span<const absl::Span<const uint8_t>>(&span, 1); |
| 24 | return log_sink->Write(queue); |
| 25 | } |
| 26 | } // namespace |
| 27 | |
Austin Schuh | 95460cc | 2023-06-26 11:53:10 -0700 | [diff] [blame^] | 28 | MATCHER_P(FileEq, o, "") { return arg.name == o.name && arg.size == o.size; } |
| 29 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 30 | TEST(LogBackendTest, CreateSimpleFile) { |
| 31 | const std::string logevent = aos::testing::TestTmpDir() + "/logevent/"; |
Alexei Strots | f08b8fb | 2023-04-21 19:46:08 -0700 | [diff] [blame] | 32 | const std::string filename = "test.bfbs"; |
colleen | 61276dc | 2023-06-01 09:23:29 -0700 | [diff] [blame] | 33 | FileBackend backend(logevent, false); |
Alexei Strots | f08b8fb | 2023-04-21 19:46:08 -0700 | [diff] [blame] | 34 | auto file = backend.RequestFile(filename); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 35 | ASSERT_EQ(file->OpenForWrite(), WriteCode::kOk); |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 36 | auto result = Write(file.get(), "test"); |
| 37 | EXPECT_EQ(result.code, WriteCode::kOk); |
| 38 | EXPECT_EQ(result.messages_written, 1); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 39 | EXPECT_EQ(file->Close(), WriteCode::kOk); |
Alexei Strots | f08b8fb | 2023-04-21 19:46:08 -0700 | [diff] [blame] | 40 | EXPECT_TRUE(std::filesystem::exists(logevent + filename)); |
| 41 | |
Austin Schuh | 95460cc | 2023-06-26 11:53:10 -0700 | [diff] [blame^] | 42 | EXPECT_THAT(backend.ListFiles(), |
| 43 | ::testing::ElementsAre(FileEq(LogSource::File{ |
| 44 | .name = filename, |
| 45 | .size = 4, |
| 46 | }))); |
Alexei Strots | f08b8fb | 2023-04-21 19:46:08 -0700 | [diff] [blame] | 47 | |
| 48 | auto decoder = backend.GetDecoder(filename); |
| 49 | std::vector<uint8_t> buffer; |
| 50 | buffer.resize(10); |
| 51 | const auto count = decoder->Read(buffer.data(), buffer.data() + 10); |
| 52 | ASSERT_EQ(count, 4); |
| 53 | buffer.resize(4); |
| 54 | std::string view(buffer.begin(), buffer.end()); |
| 55 | EXPECT_EQ(view, "test"); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | TEST(LogBackendTest, CreateRenamableFile) { |
| 59 | const std::string logevent = aos::testing::TestTmpDir() + "/logevent/"; |
colleen | 61276dc | 2023-06-01 09:23:29 -0700 | [diff] [blame] | 60 | RenamableFileBackend backend(logevent, false); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 61 | auto file = backend.RequestFile("test.log"); |
| 62 | ASSERT_EQ(file->OpenForWrite(), WriteCode::kOk); |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 63 | auto result = Write(file.get(), "test"); |
| 64 | EXPECT_EQ(result.code, WriteCode::kOk); |
| 65 | EXPECT_EQ(result.messages_written, 1); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 66 | EXPECT_EQ(file->Close(), WriteCode::kOk); |
| 67 | EXPECT_TRUE(std::filesystem::exists(logevent + "test.log")); |
| 68 | } |
| 69 | |
| 70 | TEST(LogBackendTest, UseTempRenamableFile) { |
| 71 | const std::string logevent = aos::testing::TestTmpDir() + "/logevent/"; |
colleen | 61276dc | 2023-06-01 09:23:29 -0700 | [diff] [blame] | 72 | RenamableFileBackend backend(logevent, false); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 73 | backend.EnableTempFiles(); |
| 74 | auto file = backend.RequestFile("test.log"); |
| 75 | ASSERT_EQ(file->OpenForWrite(), WriteCode::kOk); |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 76 | auto result = Write(file.get(), "test"); |
| 77 | EXPECT_EQ(result.code, WriteCode::kOk); |
| 78 | EXPECT_EQ(result.messages_written, 1); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 79 | EXPECT_TRUE(std::filesystem::exists(logevent + "test.log.tmp")); |
| 80 | |
| 81 | EXPECT_EQ(file->Close(), WriteCode::kOk); |
| 82 | // Check that file is renamed. |
| 83 | EXPECT_TRUE(std::filesystem::exists(logevent + "test.log")); |
| 84 | } |
| 85 | |
| 86 | TEST(LogBackendTest, RenameBaseAfterWrite) { |
| 87 | const std::string logevent = aos::testing::TestTmpDir() + "/logevent/"; |
colleen | 61276dc | 2023-06-01 09:23:29 -0700 | [diff] [blame] | 88 | RenamableFileBackend backend(logevent, false); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 89 | auto file = backend.RequestFile("test.log"); |
| 90 | ASSERT_EQ(file->OpenForWrite(), WriteCode::kOk); |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 91 | auto result = Write(file.get(), "test"); |
| 92 | EXPECT_EQ(result.code, WriteCode::kOk); |
| 93 | EXPECT_EQ(result.messages_written, 1); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 94 | EXPECT_TRUE(std::filesystem::exists(logevent + "test.log")); |
| 95 | |
| 96 | std::string renamed = aos::testing::TestTmpDir() + "/renamed/"; |
| 97 | backend.RenameLogBase(renamed); |
| 98 | |
| 99 | EXPECT_FALSE(std::filesystem::exists(logevent + "test.log")); |
| 100 | EXPECT_TRUE(std::filesystem::exists(renamed + "test.log")); |
| 101 | |
| 102 | EXPECT_EQ(file->Close(), WriteCode::kOk); |
| 103 | // Check that file is renamed. |
| 104 | EXPECT_TRUE(std::filesystem::exists(renamed + "test.log")); |
| 105 | } |
| 106 | |
| 107 | TEST(LogBackendTest, UseTestAndRenameBaseAfterWrite) { |
| 108 | const std::string logevent = aos::testing::TestTmpDir() + "/logevent/"; |
colleen | 61276dc | 2023-06-01 09:23:29 -0700 | [diff] [blame] | 109 | RenamableFileBackend backend(logevent, false); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 110 | backend.EnableTempFiles(); |
| 111 | auto file = backend.RequestFile("test.log"); |
| 112 | ASSERT_EQ(file->OpenForWrite(), WriteCode::kOk); |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 113 | auto result = Write(file.get(), "test"); |
| 114 | EXPECT_EQ(result.code, WriteCode::kOk); |
| 115 | EXPECT_EQ(result.messages_written, 1); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 116 | EXPECT_TRUE(std::filesystem::exists(logevent + "test.log.tmp")); |
| 117 | |
| 118 | std::string renamed = aos::testing::TestTmpDir() + "/renamed/"; |
| 119 | backend.RenameLogBase(renamed); |
| 120 | |
| 121 | EXPECT_FALSE(std::filesystem::exists(logevent + "test.log.tmp")); |
| 122 | EXPECT_TRUE(std::filesystem::exists(renamed + "test.log.tmp")); |
| 123 | |
| 124 | EXPECT_EQ(file->Close(), WriteCode::kOk); |
| 125 | // Check that file is renamed. |
| 126 | EXPECT_TRUE(std::filesystem::exists(renamed + "test.log")); |
| 127 | } |
| 128 | |
Alexei Strots | a0b99d7 | 2023-04-11 15:12:42 -0700 | [diff] [blame] | 129 | TEST(QueueAlignmentTest, Cases) { |
| 130 | QueueAligner aligner; |
| 131 | uint8_t *start = nullptr; |
| 132 | { |
| 133 | // Only prefix |
| 134 | std::vector<absl::Span<const uint8_t>> queue; |
| 135 | const uint8_t *current = start + 1; |
| 136 | queue.emplace_back(current, FileHandler::kSector - 2); |
| 137 | aligner.FillAlignedQueue(queue); |
| 138 | ASSERT_EQ(aligner.aligned_queue().size(), 1); |
| 139 | const auto &prefix = aligner.aligned_queue()[0]; |
| 140 | EXPECT_FALSE(prefix.aligned); |
| 141 | EXPECT_EQ(prefix.size, FileHandler::kSector - 2); |
| 142 | } |
| 143 | { |
| 144 | // Only main |
| 145 | std::vector<absl::Span<const uint8_t>> queue; |
| 146 | const uint8_t *current = start; |
| 147 | queue.emplace_back(current, FileHandler::kSector); |
| 148 | aligner.FillAlignedQueue(queue); |
| 149 | ASSERT_EQ(aligner.aligned_queue().size(), 1); |
| 150 | const auto &main = aligner.aligned_queue()[0]; |
| 151 | EXPECT_TRUE(main.aligned); |
| 152 | EXPECT_EQ(main.size, FileHandler::kSector); |
| 153 | } |
| 154 | { |
| 155 | // Empty |
| 156 | std::vector<absl::Span<const uint8_t>> queue; |
| 157 | const uint8_t *current = start; |
| 158 | queue.emplace_back(current, 0); |
| 159 | EXPECT_DEATH(aligner.FillAlignedQueue(queue), |
| 160 | "Nobody should be sending empty messages"); |
| 161 | } |
| 162 | { |
| 163 | // Main and suffix |
| 164 | std::vector<absl::Span<const uint8_t>> queue; |
| 165 | const uint8_t *current = start; |
| 166 | queue.emplace_back(current, FileHandler::kSector + 1); |
| 167 | aligner.FillAlignedQueue(queue); |
| 168 | ASSERT_EQ(aligner.aligned_queue().size(), 2); |
| 169 | |
| 170 | const auto &main = aligner.aligned_queue()[0]; |
| 171 | EXPECT_TRUE(main.aligned); |
| 172 | EXPECT_EQ(main.size, FileHandler::kSector); |
| 173 | |
| 174 | const auto &suffix = aligner.aligned_queue()[1]; |
| 175 | EXPECT_FALSE(suffix.aligned); |
| 176 | EXPECT_EQ(suffix.size, 1); |
| 177 | } |
| 178 | { |
| 179 | // Prefix, main |
| 180 | std::vector<absl::Span<const uint8_t>> queue; |
| 181 | const uint8_t *current = start + 1; |
| 182 | queue.emplace_back(current, 2 * FileHandler::kSector - 1); |
| 183 | aligner.FillAlignedQueue(queue); |
| 184 | ASSERT_EQ(aligner.aligned_queue().size(), 2); |
| 185 | |
| 186 | const auto &prefix = aligner.aligned_queue()[0]; |
| 187 | EXPECT_FALSE(prefix.aligned); |
| 188 | EXPECT_EQ(prefix.size, FileHandler::kSector - 1); |
| 189 | |
| 190 | const auto &main = aligner.aligned_queue()[1]; |
| 191 | EXPECT_TRUE(main.aligned); |
| 192 | EXPECT_EQ(main.size, FileHandler::kSector); |
| 193 | } |
| 194 | { |
| 195 | // Prefix and suffix |
| 196 | std::vector<absl::Span<const uint8_t>> queue; |
| 197 | const uint8_t *current = start + 1; |
| 198 | queue.emplace_back(current, 2 * FileHandler::kSector - 2); |
| 199 | aligner.FillAlignedQueue(queue); |
| 200 | ASSERT_EQ(aligner.aligned_queue().size(), 2); |
| 201 | |
| 202 | const auto &prefix = aligner.aligned_queue()[0]; |
| 203 | EXPECT_FALSE(prefix.aligned); |
| 204 | EXPECT_EQ(prefix.size, FileHandler::kSector - 1); |
| 205 | |
| 206 | const auto &suffix = aligner.aligned_queue()[1]; |
| 207 | EXPECT_FALSE(suffix.aligned); |
| 208 | EXPECT_EQ(suffix.size, FileHandler::kSector - 1); |
| 209 | } |
| 210 | { |
| 211 | // Prefix, main and suffix |
| 212 | std::vector<absl::Span<const uint8_t>> queue; |
| 213 | const uint8_t *current = start + 1; |
| 214 | queue.emplace_back(current, 3 * FileHandler::kSector - 2); |
| 215 | aligner.FillAlignedQueue(queue); |
| 216 | ASSERT_EQ(aligner.aligned_queue().size(), 3); |
| 217 | |
| 218 | const auto &prefix = aligner.aligned_queue()[0]; |
| 219 | EXPECT_FALSE(prefix.aligned); |
| 220 | EXPECT_EQ(prefix.size, FileHandler::kSector - 1); |
| 221 | |
| 222 | const auto &main = aligner.aligned_queue()[1]; |
| 223 | EXPECT_TRUE(main.aligned); |
| 224 | EXPECT_EQ(main.size, FileHandler::kSector); |
| 225 | |
| 226 | const auto &suffix = aligner.aligned_queue()[2]; |
| 227 | EXPECT_FALSE(suffix.aligned); |
| 228 | EXPECT_EQ(suffix.size, FileHandler::kSector - 1); |
| 229 | } |
| 230 | } |
| 231 | |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 232 | // It represents calls to Write function (batching of calls and messages) where |
| 233 | // int values are sizes of each message in the queue. |
| 234 | using WriteRecipe = std::vector<std::vector<int>>; |
| 235 | |
| 236 | struct FileWriteTestBase : public ::testing::Test { |
| 237 | uint8_t NextRandom() { return distribution(engine); } |
| 238 | |
| 239 | class AlignedReallocator { |
| 240 | public: |
| 241 | static void *Realloc(void *old, size_t old_size, size_t new_capacity) { |
| 242 | void *new_memory = std::aligned_alloc(512, new_capacity); |
| 243 | if (old) { |
| 244 | memcpy(new_memory, old, old_size); |
| 245 | free(old); |
| 246 | } |
| 247 | return new_memory; |
| 248 | } |
| 249 | }; |
| 250 | |
| 251 | AllocatorResizeableBuffer<AlignedReallocator> buffer; |
| 252 | |
| 253 | void TestRecipe(const WriteRecipe &recipe) { |
| 254 | VLOG(1) << "Starting"; |
| 255 | for (const std::vector<int> &r : recipe) { |
| 256 | VLOG(1) << " chunk " << absl::StrJoin(r, ", "); |
| 257 | } |
| 258 | size_t requested_size = 0; |
| 259 | for (const auto &call : recipe) { |
| 260 | for (const auto &message_size : call) { |
| 261 | requested_size += message_size; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // Grow the cached buffer if it needs to grow. Building a random buffer is |
| 266 | // the most expensive part of the test. |
| 267 | if (buffer.size() < requested_size) { |
| 268 | // Make sure it is 512 aligned... That makes sure we have the best chance |
| 269 | // of transitioning to and from being aligned. |
| 270 | buffer.resize((requested_size + FileHandler::kSector - 1) & |
| 271 | (~(FileHandler::kSector - 1))); |
| 272 | std::generate(std::begin(buffer), std::end(buffer), |
| 273 | [this]() { return NextRandom(); }); |
| 274 | } |
| 275 | |
| 276 | // Back align the data to the buffer so we make sure the contents of the |
| 277 | // file keep changing in case a file somehow doesn't get deleted, or |
| 278 | // collides with something else. |
| 279 | const uint8_t *adjusted_start = |
| 280 | buffer.data() + buffer.size() - requested_size; |
| 281 | |
| 282 | // logevent has to end with '/' to be recognized as a folder. |
| 283 | const std::string logevent = aos::testing::TestTmpDir() + "/"; |
| 284 | const auto file = std::filesystem::path(logevent) / "test.log"; |
| 285 | std::filesystem::remove_all(file); |
| 286 | VLOG(1) << "Writing to " << file.c_str(); |
| 287 | |
colleen | 61276dc | 2023-06-01 09:23:29 -0700 | [diff] [blame] | 288 | FileBackend backend(logevent, false); |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 289 | auto handler = backend.RequestFile("test.log"); |
| 290 | ASSERT_EQ(handler->OpenForWrite(), WriteCode::kOk); |
| 291 | |
| 292 | // Build arguments for Write. |
| 293 | size_t position = 0; |
| 294 | for (const auto &call : recipe) { |
| 295 | std::vector<absl::Span<const uint8_t>> queue; |
| 296 | for (const auto &message_size : call) { |
| 297 | const uint8_t *current = adjusted_start + position; |
| 298 | queue.emplace_back(current, message_size); |
| 299 | position += message_size; |
| 300 | } |
| 301 | auto result = handler->Write(queue); |
| 302 | EXPECT_EQ(result.code, WriteCode::kOk); |
| 303 | EXPECT_EQ(result.messages_written, call.size()); |
| 304 | } |
| 305 | |
| 306 | ASSERT_EQ(handler->Close(), WriteCode::kOk); |
| 307 | EXPECT_TRUE(std::filesystem::exists(file)); |
| 308 | EXPECT_EQ(std::filesystem::file_size(file), requested_size); |
| 309 | |
| 310 | // Confirm that the contents then match the original buffer. |
| 311 | std::ifstream file_stream(file, std::ios::in | std::ios::binary); |
| 312 | std::vector<uint8_t> content((std::istreambuf_iterator<char>(file_stream)), |
| 313 | std::istreambuf_iterator<char>()); |
| 314 | ASSERT_EQ(content.size(), requested_size); |
| 315 | bool matches = true; |
| 316 | for (size_t i = 0; i < content.size(); ++i) { |
| 317 | if (content[i] != adjusted_start[i]) { |
| 318 | matches = false; |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | if (!matches) { |
| 323 | ASSERT_TRUE(false); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | std::random_device random; |
| 328 | std::mt19937 engine{random()}; |
| 329 | std::uniform_int_distribution<uint8_t> distribution{0, 0xFF}; |
| 330 | }; |
| 331 | |
| 332 | // Tests that random sets of reads and writes always result in all the data |
| 333 | // being written. |
| 334 | TEST_F(FileWriteTestBase, RandomTest) { |
| 335 | std::mt19937 engine2{random()}; |
| 336 | std::uniform_int_distribution<int> count_distribution{1, 5}; |
| 337 | |
| 338 | // Pick a bunch of lengths that will result in things that add up to multiples |
| 339 | // of 512 and end up transitioning across the aligned and unaligned boundary. |
| 340 | const std::vector<int> lengths = { |
| 341 | 0x100b5, 0xff4b, 0x10000, 1024 - 7, 1024 - 6, 1024 - 5, 1024 - 4, |
| 342 | 1024 - 3, 1024 - 2, 1024 - 1, 1024, 1024 + 1, 1024 + 2, 1024 + 3, |
| 343 | 1024 + 4, 1024 + 5, 1024 + 6, 1024 + 7, 512 - 7, 512 - 6, 512 - 5, |
| 344 | 512 - 4, 512 - 3, 512 - 2, 512 - 1, 512, 512 + 1, 512 + 2, |
| 345 | 512 + 3, 512 + 4, 512 + 5, 512 + 6, 512 + 7}; |
| 346 | std::uniform_int_distribution<int> lengths_distribution{ |
| 347 | 0, static_cast<int>(lengths.size() - 1)}; |
| 348 | |
Austin Schuh | 9e8df9e | 2023-05-03 08:28:29 -0700 | [diff] [blame] | 349 | for (int i = 0; i < 1000; ++i) { |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 350 | WriteRecipe recipe; |
| 351 | int number_of_writes = count_distribution(engine2); |
| 352 | for (int j = 0; j < number_of_writes; ++j) { |
| 353 | int number_of_chunks = count_distribution(engine2); |
| 354 | std::vector<int> r; |
| 355 | for (int k = 0; k < number_of_chunks; ++k) { |
| 356 | r.emplace_back(lengths[lengths_distribution(engine2)]); |
| 357 | } |
| 358 | recipe.emplace_back(std::move(r)); |
| 359 | } |
| 360 | |
| 361 | TestRecipe(recipe); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // Test an aligned to unaligned transition to make sure everything works. |
| 366 | TEST_F(FileWriteTestBase, AlignedToUnaligned) { |
| 367 | AllocatorResizeableBuffer<AlignedReallocator> aligned_buffer; |
| 368 | AllocatorResizeableBuffer<AlignedReallocator> unaligned_buffer; |
| 369 | |
| 370 | aligned_buffer.resize(FileHandler::kSector * 4); |
| 371 | std::generate(std::begin(aligned_buffer), std::end(aligned_buffer), |
| 372 | [this]() { return NextRandom(); }); |
| 373 | |
| 374 | unaligned_buffer.resize(FileHandler::kSector * 4); |
| 375 | std::generate(std::begin(unaligned_buffer), std::end(unaligned_buffer), |
| 376 | [this]() { return NextRandom(); }); |
| 377 | |
| 378 | const size_t kOffset = 53; |
| 379 | absl::Span<const uint8_t> unaligned_span(unaligned_buffer.data() + kOffset, |
| 380 | aligned_buffer.size() - kOffset); |
| 381 | |
| 382 | std::vector<absl::Span<const uint8_t>> queue; |
| 383 | |
| 384 | queue.emplace_back(aligned_buffer.data(), aligned_buffer.size()); |
| 385 | queue.emplace_back(unaligned_span); |
| 386 | LOG(INFO) << "Queue 0 " << queue[0].size(); |
| 387 | LOG(INFO) << "Queue 1 " << queue[1].size(); |
| 388 | |
| 389 | const std::string logevent = aos::testing::TestTmpDir() + "/"; |
| 390 | const auto file = std::filesystem::path(logevent) / "test.log"; |
| 391 | std::filesystem::remove_all(file); |
| 392 | VLOG(1) << "Writing to " << file.c_str(); |
| 393 | |
colleen | 61276dc | 2023-06-01 09:23:29 -0700 | [diff] [blame] | 394 | FileBackend backend(logevent, false); |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 395 | auto handler = backend.RequestFile("test.log"); |
| 396 | ASSERT_EQ(handler->OpenForWrite(), WriteCode::kOk); |
| 397 | |
| 398 | auto result = handler->Write(queue); |
| 399 | EXPECT_EQ(result.code, WriteCode::kOk); |
| 400 | EXPECT_EQ(result.messages_written, queue.size()); |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 401 | FileHandler *file_handler = reinterpret_cast<FileHandler *>(handler.get()); |
| 402 | EXPECT_GT(file_handler->written_aligned(), 0); |
Austin Schuh | 3ebaf78 | 2023-04-07 16:03:28 -0700 | [diff] [blame] | 403 | |
| 404 | ASSERT_EQ(handler->Close(), WriteCode::kOk); |
| 405 | EXPECT_TRUE(std::filesystem::exists(file)); |
| 406 | const size_t requested_size = queue[0].size() + queue[1].size(); |
| 407 | EXPECT_EQ(std::filesystem::file_size(file), requested_size); |
| 408 | |
| 409 | // Confirm that the contents then match the original buffer. |
| 410 | std::ifstream file_stream(file, std::ios::in | std::ios::binary); |
| 411 | std::vector<uint8_t> content((std::istreambuf_iterator<char>(file_stream)), |
| 412 | std::istreambuf_iterator<char>()); |
| 413 | ASSERT_EQ(content.size(), requested_size); |
| 414 | bool matches = true; |
| 415 | for (size_t i = 0; i < queue[0].size(); ++i) { |
| 416 | if (content[i] != aligned_buffer.data()[i]) { |
| 417 | matches = false; |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | for (size_t i = 0; i < queue[1].size(); ++i) { |
| 422 | if (content[i + queue[0].size()] != unaligned_span.data()[i]) { |
| 423 | matches = false; |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | if (!matches) { |
| 428 | ASSERT_TRUE(false); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | struct FileWriteTestFixture : public ::testing::WithParamInterface<WriteRecipe>, |
| 433 | public FileWriteTestBase {}; |
| 434 | |
| 435 | TEST_P(FileWriteTestFixture, CheckSizeOfWrittenFile) { |
| 436 | auto recipe = GetParam(); |
| 437 | TestRecipe(recipe); |
| 438 | } |
| 439 | |
| 440 | // Try out some well known failure cases transitioning across the alignment |
| 441 | // boundary. |
| 442 | INSTANTIATE_TEST_SUITE_P( |
| 443 | FileWriteTest, FileWriteTestFixture, |
| 444 | ::testing::Values(WriteRecipe{{0x10000}}, WriteRecipe{{0x10000, 0x1000b5}}, |
| 445 | WriteRecipe{{0x10000, 0x1000b5}, {0xfff4b, 0x10000}}, |
| 446 | WriteRecipe{{0x1000b5, 0xfff4b}, {0x10000}}, |
| 447 | WriteRecipe{{65536, 517, 65717}}, |
| 448 | WriteRecipe{{65536, 517, 518, 511}, |
| 449 | {514}, |
| 450 | {505, 514}, |
| 451 | {505, 514, 65355, 519}}, |
| 452 | WriteRecipe{{65536, 518, 511, 511}, |
| 453 | {65717}, |
| 454 | {65717, 65717, 518}, |
| 455 | {65536, 65536, 508, 65355}, |
| 456 | {515, 519}}, |
| 457 | WriteRecipe{{0x1000b5, 0xfff4b, 0x100000}, {0x10000}})); |
| 458 | |
| 459 | } // namespace aos::logger::testing |