more async tests

This commit is contained in:
gabime 2018-05-27 02:21:31 +03:00
parent a43a44bb88
commit 31ff43ef81

View File

@ -104,29 +104,41 @@ TEST_CASE("multi threads", "[async]")
TEST_CASE("to_file", "[async]") TEST_CASE("to_file", "[async]")
{ {
prepare_logdir(); prepare_logdir();
size_t queue_size = 512; size_t messages = 1024;
size_t messages = 512; size_t tp_threads = 1;
size_t n_threads = 4; std::string filename = "logs/async_test.log";
spdlog::init_thread_pool(queue_size, n_threads);
auto logger = spdlog::basic_logger_mt<spdlog::create_async>("as", "logs/async_test.log", true);
std::vector<std::thread> threads;
for (size_t i = 0; i < n_threads; i++)
{ {
threads.emplace_back([logger, messages] { auto file_sink = std::make_shared<spdlog::sinks::simple_file_sink_mt>(filename, true);
auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads);
auto logger = std::make_shared<spdlog::async_logger>("as", std::move(file_sink), std::move(tp));
for (size_t j = 0; j < messages; j++) for (size_t j = 0; j < messages; j++)
{ {
logger->info("Hello message #{}", j); logger->info("Hello message #{}", j);
} }
});
} }
for (auto &t : threads) REQUIRE(count_lines(filename) == messages);
auto contents = file_contents(filename);
REQUIRE(ends_with (contents, std::string("Hello message #1023") + SPDLOG_EOL));
}
TEST_CASE("to_file multi-workers", "[async]")
{ {
t.join(); prepare_logdir();
size_t messages = 1024*10;
size_t tp_threads = 10;
std::string filename = "logs/async_test.log";
{
auto file_sink = std::make_shared<spdlog::sinks::simple_file_sink_mt>(filename, true);
auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads);
auto logger = std::make_shared<spdlog::async_logger>("as", std::move(file_sink), std::move(tp));
for (size_t j = 0; j < messages; j++)
{
logger->info("Hello message #{}", j);
} }
logger.reset(); }
spdlog::drop("as");
std::this_thread::sleep_for(std::chrono::seconds(1)); REQUIRE(count_lines(filename) == messages);
REQUIRE(count_lines("logs/async_test.log") == messages * n_threads);
} }