test_async.cpp 6.01 KB
Newer Older
gabime's avatar
gabime committed
1 2
#include "includes.h"
#include "spdlog/async.h"
3
#include "spdlog/sinks/basic_file_sink.h"
gabime's avatar
gabime committed
4
#include "test_sink.h"
gabime's avatar
gabime committed
5 6 7

TEST_CASE("basic async test ", "[async]")
{
gabime's avatar
gabime committed
8 9
    using namespace spdlog;
    auto test_sink = std::make_shared<sinks::test_sink_mt>();
10
    size_t overrun_counter = 0;
gabime's avatar
gabime committed
11 12 13 14
    size_t queue_size = 128;
    size_t messages = 256;
    {
        auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
gabime's avatar
gabime committed
15
        auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block);
gabime's avatar
gabime committed
16 17 18 19 20
        for (size_t i = 0; i < messages; i++)
        {
            logger->info("Hello message #{}", i);
        }
        logger->flush();
21
        overrun_counter = tp->overrun_counter();
gabime's avatar
gabime committed
22 23
    }
    REQUIRE(test_sink->msg_counter() == messages);
gabime's avatar
gabime committed
24
    REQUIRE(test_sink->flush_counter() == 1);
25
    REQUIRE(overrun_counter == 0);
gabime's avatar
gabime committed
26 27 28 29
}

TEST_CASE("discard policy ", "[async]")
{
gabime's avatar
gabime committed
30 31
    using namespace spdlog;
    auto test_sink = std::make_shared<sinks::test_sink_mt>();
32 33 34
    test_sink->set_delay(std::chrono::milliseconds(1));
    size_t queue_size = 4;
    size_t messages = 1024;
35 36 37 38

    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++)
gabime's avatar
gabime committed
39
    {
40
        logger->info("Hello message");
gabime's avatar
gabime committed
41
    }
gabime's avatar
gabime committed
42
    REQUIRE(test_sink->msg_counter() < messages);
43
    REQUIRE(tp->overrun_counter() > 0);
gabime's avatar
gabime committed
44 45
}

gabime's avatar
gabime committed
46 47 48
TEST_CASE("discard policy using factory ", "[async]")
{
    using namespace spdlog;
49 50
    size_t queue_size = 4;
    size_t messages = 1024;
51 52 53
    spdlog::init_thread_pool(queue_size, 1);

    auto logger = spdlog::create_async_nb<sinks::test_sink_mt>("as2");
54 55 56
    auto test_sink = std::static_pointer_cast<sinks::test_sink_mt>(logger->sinks()[0]);
    test_sink->set_delay(std::chrono::milliseconds(1));

57 58 59 60
    for (size_t i = 0; i < messages; i++)
    {
        logger->info("Hello message");
    }
61 62

    REQUIRE(test_sink->msg_counter() < messages);
63
    spdlog::drop_all();
gabime's avatar
gabime committed
64 65
}

gabime's avatar
gabime committed
66 67
TEST_CASE("flush", "[async]")
{
gabime's avatar
gabime committed
68 69 70 71 72 73
    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);
gabime's avatar
gabime committed
74
        auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block);
gabime's avatar
gabime committed
75 76 77 78 79 80 81
        for (size_t i = 0; i < messages; i++)
        {
            logger->info("Hello message #{}", i);
        }

        logger->flush();
    }
gabime's avatar
gabime committed
82
    // std::this_thread::sleep_for(std::chrono::milliseconds(250));
gabime's avatar
gabime committed
83
    REQUIRE(test_sink->msg_counter() == messages);
gabime's avatar
gabime committed
84 85 86
    REQUIRE(test_sink->flush_counter() == 1);
}

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
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();
}

gabime's avatar
gabime committed
102 103 104 105 106
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));
107
    size_t messages = 100;
gabime's avatar
gabime committed
108 109

    auto tp = std::make_shared<details::thread_pool>(messages, 2);
gabime's avatar
gabime committed
110
    auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block);
gabime's avatar
gabime committed
111 112 113 114 115
    for (size_t i = 0; i < messages; i++)
    {
        logger->info("Hello message #{}", i);
    }
    logger->flush();
116 117
    tp.reset();

gabime's avatar
gabime committed
118 119
    REQUIRE(test_sink->msg_counter() == messages);
    REQUIRE(test_sink->flush_counter() == 1);
gabime's avatar
gabime committed
120 121 122 123
}

TEST_CASE("multi threads", "[async]")
{
gabime's avatar
gabime committed
124 125 126 127 128 129 130
    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);
gabime's avatar
gabime committed
131
        auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block);
gabime's avatar
gabime committed
132 133 134 135 136 137 138 139 140 141

        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);
                }
            });
gabime's avatar
gabime committed
142
            logger->flush();
gabime's avatar
gabime committed
143
        }
gabime's avatar
gabime committed
144

gabime's avatar
gabime committed
145 146 147 148 149
        for (auto &t : threads)
        {
            t.join();
        }
    }
gabime's avatar
gabime committed
150

gabime's avatar
gabime committed
151
    REQUIRE(test_sink->msg_counter() == messages * n_threads);
gabime's avatar
gabime committed
152
    REQUIRE(test_sink->flush_counter() == n_threads);
gabime's avatar
gabime committed
153 154 155 156
}

TEST_CASE("to_file", "[async]")
{
gabime's avatar
gabime committed
157 158 159 160 161
    prepare_logdir();
    size_t messages = 1024;
    size_t tp_threads = 1;
    std::string filename = "logs/async_test.log";
    {
162
        auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
gabime's avatar
gabime committed
163 164 165 166 167 168 169 170 171 172 173
        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);
gabime's avatar
gabime committed
174
    REQUIRE(ends_with(contents, std::string("Hello message #1023\n")));
gabime's avatar
gabime committed
175
}
gabime's avatar
gabime committed
176

gabime's avatar
gabime committed
177 178
TEST_CASE("to_file multi-workers", "[async]")
{
gabime's avatar
gabime committed
179 180 181 182 183
    prepare_logdir();
    size_t messages = 1024 * 10;
    size_t tp_threads = 10;
    std::string filename = "logs/async_test.log";
    {
184
        auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
gabime's avatar
gabime committed
185 186 187 188 189 190 191 192 193 194
        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);
gabime's avatar
gabime committed
195
}