mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-23 22:22:05 +08:00
Added some mpmc_q tests
This commit is contained in:
parent
750b520f41
commit
057bf1b92d
@ -14,7 +14,7 @@ set(SPDLOG_UTESTS_SOURCES
|
||||
test_macros.cpp
|
||||
utils.cpp
|
||||
utils.h
|
||||
main.cpp)
|
||||
main.cpp test_mpmc_q.cpp)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SPDLOG_UTESTS_SOURCES})
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE Threads::Threads)
|
||||
|
105
tests/test_mpmc_q.cpp
Normal file
105
tests/test_mpmc_q.cpp
Normal file
@ -0,0 +1,105 @@
|
||||
#include "includes.h"
|
||||
|
||||
using namespace std::chrono;
|
||||
using std::chrono::system_clock;
|
||||
using std::chrono::milliseconds;
|
||||
|
||||
system_clock::time_point now_millis()
|
||||
{
|
||||
return time_point_cast<milliseconds>(system_clock::now());
|
||||
}
|
||||
TEST_CASE("dequeue-empty-nowait", "[mpmc_blocking_q]")
|
||||
{
|
||||
size_t q_size = 100;
|
||||
milliseconds tolerance_wait(10);
|
||||
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
||||
int popped_item;
|
||||
|
||||
auto millis_0 = now_millis();
|
||||
auto rv = q.dequeue_for(popped_item, milliseconds::zero());
|
||||
auto millis_1 = now_millis();
|
||||
|
||||
REQUIRE(rv == false);
|
||||
REQUIRE((millis_1-millis_0) <= tolerance_wait);
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("dequeue-empty-wait", "[mpmc_blocking_q]")
|
||||
{
|
||||
|
||||
size_t q_size = 100;
|
||||
milliseconds wait_ms(250);
|
||||
milliseconds tolerance_wait(10);
|
||||
|
||||
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
||||
int popped_item;
|
||||
auto millis_0 = now_millis();
|
||||
auto rv = q.dequeue_for(popped_item, wait_ms);
|
||||
auto millis_1 = now_millis();
|
||||
auto delta_ms = millis_1 - millis_0;
|
||||
|
||||
REQUIRE(rv == false);
|
||||
REQUIRE(delta_ms >= wait_ms);
|
||||
REQUIRE(delta_ms <= wait_ms + tolerance_wait);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("enqueue_nowait", "[mpmc_blocking_q]")
|
||||
{
|
||||
|
||||
size_t q_size = 1;
|
||||
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
||||
milliseconds tolerance_wait(10);
|
||||
|
||||
q.enqueue(1);
|
||||
REQUIRE(q.overrun_counter() == 0);
|
||||
|
||||
auto millis_0 = now_millis();
|
||||
q.enqueue_nowait(2);
|
||||
auto millis_1 = now_millis();
|
||||
REQUIRE((millis_1-millis_0) <= tolerance_wait);
|
||||
REQUIRE(q.overrun_counter() == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("bad_queue", "[mpmc_blocking_q]")
|
||||
{
|
||||
size_t q_size = 0;
|
||||
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
||||
q.enqueue_nowait(1);
|
||||
REQUIRE(q.overrun_counter() == 1);
|
||||
int i;
|
||||
REQUIRE(q.dequeue_for(i, milliseconds(0)) == false);
|
||||
}
|
||||
|
||||
TEST_CASE("empty_queue", "[mpmc_blocking_q]")
|
||||
{
|
||||
size_t q_size = 10;
|
||||
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
||||
int i;
|
||||
REQUIRE(q.dequeue_for(i, milliseconds(10)) == false);
|
||||
}
|
||||
|
||||
TEST_CASE("full_queue", "[mpmc_blocking_q]")
|
||||
{
|
||||
size_t q_size = 100;
|
||||
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
||||
for(int i = 0; i < static_cast<int>(q_size); i++)
|
||||
{
|
||||
q.enqueue(std::move(i));
|
||||
}
|
||||
|
||||
q.enqueue_nowait(123456);
|
||||
REQUIRE(q.overrun_counter() == 1);
|
||||
|
||||
for(int i = 1; i < static_cast<int>(q_size); i++)
|
||||
{
|
||||
int item;
|
||||
q.dequeue_for(item, milliseconds(0));
|
||||
REQUIRE(item == i);
|
||||
}
|
||||
|
||||
// last item pushed has overridden the oldest.
|
||||
int item;
|
||||
q.dequeue_for(item, milliseconds(0));
|
||||
REQUIRE(item == 123456);
|
||||
}
|
Loading…
Reference in New Issue
Block a user