|
| 1 | +#include "doctest/doctest.h" |
| 2 | + |
| 3 | +#include "misc/TestUtilities.h" |
| 4 | +#include "quill/Backend.h" |
| 5 | +#include "quill/Frontend.h" |
| 6 | +#include "quill/LogMacros.h" |
| 7 | +#include "quill/sinks/FileSink.h" |
| 8 | + |
| 9 | +#include <cstdio> |
| 10 | +#include <string> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +using namespace quill; |
| 14 | + |
| 15 | +// Define custom Frontend Options |
| 16 | +struct CustomFrontendOptions |
| 17 | +{ |
| 18 | + static constexpr quill::QueueType queue_type = quill::QueueType::UnboundedBlocking; |
| 19 | + static constexpr uint32_t initial_queue_capacity = 16 * 1024; // 16 KiB |
| 20 | + static constexpr uint32_t blocking_queue_retry_interval_ns = 800; |
| 21 | + static constexpr quill::HugePagesPolicy huge_pages_policy = quill::HugePagesPolicy::Never; |
| 22 | +}; |
| 23 | + |
| 24 | +using CustomFrontend = FrontendImpl<CustomFrontendOptions>; |
| 25 | +using CustomLogger = LoggerImpl<CustomFrontendOptions>; |
| 26 | + |
| 27 | +/***/ |
| 28 | +TEST_CASE("shrink_thread_local_queue") |
| 29 | +{ |
| 30 | + static constexpr size_t number_of_messages = 5000; |
| 31 | + static constexpr size_t iterations = 5; |
| 32 | + static constexpr char const* filename = "shrink_thread_local_queue.log"; |
| 33 | + static std::string const logger_name = "logger"; |
| 34 | + |
| 35 | + // Start the logging backend thread |
| 36 | + Backend::start(); |
| 37 | + |
| 38 | + // just for testing - call before logging anything |
| 39 | + CustomFrontend::shrink_thread_local_queue(8 * 1024); |
| 40 | + REQUIRE_EQ(CustomFrontend::get_thread_local_queue_capacity(), 8 * 1024); |
| 41 | + |
| 42 | + // Set writing logging to a file |
| 43 | + auto file_sink = CustomFrontend::create_or_get_sink<FileSink>( |
| 44 | + filename, |
| 45 | + []() |
| 46 | + { |
| 47 | + FileSinkConfig cfg; |
| 48 | + cfg.set_open_mode('w'); |
| 49 | + |
| 50 | + // For this test only we use the default buffer size, it should not make any difference it is just for testing the default behaviour and code coverage |
| 51 | + cfg.set_write_buffer_size(0); |
| 52 | + |
| 53 | + return cfg; |
| 54 | + }(), |
| 55 | + FileEventNotifier{}); |
| 56 | + |
| 57 | + CustomLogger* logger = CustomFrontend::create_or_get_logger(logger_name, std::move(file_sink)); |
| 58 | + |
| 59 | + REQUIRE_EQ(CustomFrontend::get_thread_local_queue_capacity(), 8 * 1024); |
| 60 | + |
| 61 | + size_t cnt{0}; |
| 62 | + for (size_t iter = 0; iter < iterations; ++iter) |
| 63 | + { |
| 64 | + for (size_t i = 0; i < number_of_messages; ++i) |
| 65 | + { |
| 66 | + LOG_INFO(logger, "This is message {}", cnt++); |
| 67 | + } |
| 68 | + |
| 69 | + CustomFrontend::shrink_thread_local_queue(16 * 1024); |
| 70 | + REQUIRE_EQ(CustomFrontend::get_thread_local_queue_capacity(), 16 * 1024); |
| 71 | + } |
| 72 | + |
| 73 | + logger->flush_log(); |
| 74 | + CustomFrontend::remove_logger(logger); |
| 75 | + |
| 76 | + // Wait until the backend thread stops for test stability |
| 77 | + Backend::stop(); |
| 78 | + |
| 79 | + // Read file and check |
| 80 | + std::vector<std::string> const file_contents = quill::testing::file_contents(filename); |
| 81 | + REQUIRE_EQ(file_contents.size(), number_of_messages * iterations); |
| 82 | + |
| 83 | + for (size_t i = 0; i < number_of_messages * iterations; ++i) |
| 84 | + { |
| 85 | + std::string expected_string = logger_name + " This is message " + std::to_string(i); |
| 86 | + REQUIRE(quill::testing::file_contains(file_contents, expected_string)); |
| 87 | + } |
| 88 | + |
| 89 | + testing::remove_file(filename); |
| 90 | +} |
0 commit comments