From 057bf1b92d018040393dda1e1ec1318fbd82000e Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 14 Aug 2018 17:57:55 +0300 Subject: [PATCH] Added some mpmc_q tests --- tests/CMakeLists.txt | 2 +- tests/test_mpmc_q.cpp | 105 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 tests/test_mpmc_q.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 18af9770..90a2ab9e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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) diff --git a/tests/test_mpmc_q.cpp b/tests/test_mpmc_q.cpp new file mode 100644 index 00000000..37f0be44 --- /dev/null +++ b/tests/test_mpmc_q.cpp @@ -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(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 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 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 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 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 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 q(q_size); + for(int i = 0; i < static_cast(q_size); i++) + { + q.enqueue(std::move(i)); + } + + q.enqueue_nowait(123456); + REQUIRE(q.overrun_counter() == 1); + + for(int i = 1; i < static_cast(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); +} \ No newline at end of file