2018-05-22 14:59:27 -04:00
|
|
|
#include "includes.h"
|
|
|
|
#include "spdlog/async.h"
|
2018-07-07 05:12:45 -04:00
|
|
|
#include "spdlog/sinks/basic_file_sink.h"
|
2018-05-26 11:48:39 -04:00
|
|
|
#include "test_sink.h"
|
2018-05-22 14:59:27 -04:00
|
|
|
|
|
|
|
TEST_CASE("basic async test ", "[async]")
|
|
|
|
{
|
2018-05-26 11:48:39 -04:00
|
|
|
using namespace spdlog;
|
|
|
|
auto test_sink = std::make_shared<sinks::test_sink_mt>();
|
|
|
|
size_t queue_size = 128;
|
|
|
|
size_t messages = 256;
|
|
|
|
{
|
|
|
|
auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
|
2018-07-07 17:53:50 -04:00
|
|
|
auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block);
|
2018-05-26 11:48:39 -04:00
|
|
|
for (size_t i = 0; i < messages; i++)
|
|
|
|
{
|
|
|
|
logger->info("Hello message #{}", i);
|
|
|
|
}
|
|
|
|
logger->flush();
|
|
|
|
}
|
|
|
|
REQUIRE(test_sink->msg_counter() == messages);
|
2018-05-26 19:53:16 -04:00
|
|
|
REQUIRE(test_sink->flush_counter() == 1);
|
2018-05-22 14:59:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("discard policy ", "[async]")
|
|
|
|
{
|
2018-05-26 11:48:39 -04:00
|
|
|
using namespace spdlog;
|
|
|
|
auto test_sink = std::make_shared<sinks::test_sink_mt>();
|
|
|
|
size_t queue_size = 2;
|
2018-07-21 16:30:26 -04:00
|
|
|
size_t messages = 10240;
|
|
|
|
|
|
|
|
auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
|
|
|
|
auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::overrun_oldest);
|
|
|
|
for (size_t i = 0; i < messages; i++)
|
2018-05-26 11:48:39 -04:00
|
|
|
{
|
2018-07-21 16:30:26 -04:00
|
|
|
logger->info("Hello message");
|
2018-05-26 11:48:39 -04:00
|
|
|
}
|
2018-05-26 20:14:55 -04:00
|
|
|
REQUIRE(test_sink->msg_counter() < messages);
|
2018-05-22 14:59:27 -04:00
|
|
|
}
|
|
|
|
|
2018-07-20 17:33:27 -04:00
|
|
|
TEST_CASE("discard policy using factory ", "[async]")
|
|
|
|
{
|
|
|
|
using namespace spdlog;
|
|
|
|
size_t queue_size = 2;
|
2018-07-21 16:30:26 -04:00
|
|
|
size_t messages = 10240;
|
|
|
|
spdlog::init_thread_pool(queue_size, 1);
|
|
|
|
|
|
|
|
auto logger = spdlog::create_async_nb<sinks::test_sink_mt>("as2");
|
|
|
|
for (size_t i = 0; i < messages; i++)
|
|
|
|
{
|
|
|
|
logger->info("Hello message");
|
|
|
|
}
|
|
|
|
auto sink = std::static_pointer_cast<sinks::test_sink_mt>(logger->sinks()[0]);
|
|
|
|
REQUIRE(sink->msg_counter() < messages);
|
|
|
|
spdlog::drop_all();
|
2018-07-20 17:33:27 -04:00
|
|
|
}
|
|
|
|
|
2018-05-22 14:59:27 -04:00
|
|
|
TEST_CASE("flush", "[async]")
|
|
|
|
{
|
2018-05-26 11:48:39 -04:00
|
|
|
using namespace spdlog;
|
|
|
|
auto test_sink = std::make_shared<sinks::test_sink_mt>();
|
|
|
|
size_t queue_size = 256;
|
|
|
|
size_t messages = 256;
|
|
|
|
{
|
|
|
|
auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
|
2018-07-07 17:53:50 -04:00
|
|
|
auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block);
|
2018-05-26 11:48:39 -04:00
|
|
|
for (size_t i = 0; i < messages; i++)
|
|
|
|
{
|
|
|
|
logger->info("Hello message #{}", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
logger->flush();
|
|
|
|
}
|
2018-07-21 16:48:07 -04:00
|
|
|
// std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
2018-05-26 11:48:39 -04:00
|
|
|
REQUIRE(test_sink->msg_counter() == messages);
|
2018-05-26 19:53:16 -04:00
|
|
|
REQUIRE(test_sink->flush_counter() == 1);
|
|
|
|
}
|
|
|
|
|
2018-07-21 16:30:26 -04:00
|
|
|
TEST_CASE("async periodic flush", "[async]")
|
|
|
|
{
|
|
|
|
using namespace spdlog;
|
|
|
|
|
|
|
|
auto logger = spdlog::create_async<sinks::test_sink_mt>("as");
|
|
|
|
|
|
|
|
auto test_sink = std::static_pointer_cast<sinks::test_sink_mt>(logger->sinks()[0]);
|
|
|
|
|
|
|
|
spdlog::flush_every(std::chrono::seconds(1));
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1100));
|
|
|
|
REQUIRE(test_sink->flush_counter() == 1);
|
|
|
|
spdlog::flush_every(std::chrono::seconds(0));
|
|
|
|
spdlog::drop_all();
|
|
|
|
}
|
|
|
|
|
2018-05-26 19:53:16 -04:00
|
|
|
TEST_CASE("tp->wait_empty() ", "[async]")
|
|
|
|
{
|
|
|
|
using namespace spdlog;
|
|
|
|
auto test_sink = std::make_shared<sinks::test_sink_mt>();
|
|
|
|
test_sink->set_delay(std::chrono::milliseconds(5));
|
2018-06-01 10:25:23 -04:00
|
|
|
size_t messages = 100;
|
2018-05-26 19:53:16 -04:00
|
|
|
|
|
|
|
auto tp = std::make_shared<details::thread_pool>(messages, 2);
|
2018-07-07 17:53:50 -04:00
|
|
|
auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block);
|
2018-05-26 19:53:16 -04:00
|
|
|
for (size_t i = 0; i < messages; i++)
|
|
|
|
{
|
|
|
|
logger->info("Hello message #{}", i);
|
|
|
|
}
|
|
|
|
logger->flush();
|
2018-06-01 10:25:23 -04:00
|
|
|
tp.reset();
|
|
|
|
|
2018-05-26 19:53:16 -04:00
|
|
|
REQUIRE(test_sink->msg_counter() == messages);
|
|
|
|
REQUIRE(test_sink->flush_counter() == 1);
|
2018-05-22 14:59:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("multi threads", "[async]")
|
|
|
|
{
|
2018-05-26 11:48:39 -04:00
|
|
|
using namespace spdlog;
|
|
|
|
auto test_sink = std::make_shared<sinks::test_sink_mt>();
|
|
|
|
size_t queue_size = 128;
|
|
|
|
size_t messages = 256;
|
|
|
|
size_t n_threads = 10;
|
|
|
|
{
|
|
|
|
auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
|
2018-07-07 17:53:50 -04:00
|
|
|
auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block);
|
2018-05-26 11:48:39 -04:00
|
|
|
|
|
|
|
std::vector<std::thread> threads;
|
|
|
|
for (size_t i = 0; i < n_threads; i++)
|
|
|
|
{
|
|
|
|
threads.emplace_back([logger, messages] {
|
|
|
|
for (size_t j = 0; j < messages; j++)
|
|
|
|
{
|
|
|
|
logger->info("Hello message #{}", j);
|
|
|
|
}
|
|
|
|
});
|
2018-05-26 20:14:55 -04:00
|
|
|
logger->flush();
|
2018-05-26 11:48:39 -04:00
|
|
|
}
|
2018-05-22 14:59:27 -04:00
|
|
|
|
2018-05-26 11:48:39 -04:00
|
|
|
for (auto &t : threads)
|
|
|
|
{
|
|
|
|
t.join();
|
|
|
|
}
|
|
|
|
}
|
2018-05-22 14:59:27 -04:00
|
|
|
|
2018-05-26 11:48:39 -04:00
|
|
|
REQUIRE(test_sink->msg_counter() == messages * n_threads);
|
2018-05-26 19:53:16 -04:00
|
|
|
REQUIRE(test_sink->flush_counter() == n_threads);
|
2018-05-22 14:59:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("to_file", "[async]")
|
|
|
|
{
|
2018-05-26 19:21:49 -04:00
|
|
|
prepare_logdir();
|
|
|
|
size_t messages = 1024;
|
|
|
|
size_t tp_threads = 1;
|
|
|
|
std::string filename = "logs/async_test.log";
|
|
|
|
{
|
2018-07-07 05:12:45 -04:00
|
|
|
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
|
2018-05-26 19:21:49 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
REQUIRE(count_lines(filename) == messages);
|
|
|
|
auto contents = file_contents(filename);
|
2018-07-20 17:13:37 -04:00
|
|
|
REQUIRE(ends_with(contents, std::string("Hello message #1023\n")));
|
2018-05-26 19:21:31 -04:00
|
|
|
}
|
2018-05-22 14:59:27 -04:00
|
|
|
|
2018-05-26 19:21:31 -04:00
|
|
|
TEST_CASE("to_file multi-workers", "[async]")
|
|
|
|
{
|
2018-05-26 19:21:49 -04:00
|
|
|
prepare_logdir();
|
|
|
|
size_t messages = 1024 * 10;
|
|
|
|
size_t tp_threads = 10;
|
|
|
|
std::string filename = "logs/async_test.log";
|
|
|
|
{
|
2018-07-07 05:12:45 -04:00
|
|
|
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
|
2018-05-26 19:21:49 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
REQUIRE(count_lines(filename) == messages);
|
2018-05-22 14:59:27 -04:00
|
|
|
}
|