spdlog/tests/test_mpmc_q.cpp

115 lines
3.2 KiB
C++
Raw Permalink Normal View History

2018-08-14 22:57:55 +08:00
#include "includes.h"
using std::chrono::milliseconds;
2018-08-25 22:55:31 +08:00
using test_clock = std::chrono::high_resolution_clock;
2018-08-17 22:17:16 +08:00
2023-09-25 21:08:29 +08:00
static milliseconds millis_from(const test_clock::time_point &tp0) {
2018-08-25 22:55:31 +08:00
return std::chrono::duration_cast<milliseconds>(test_clock::now() - tp0);
2018-08-14 22:57:55 +08:00
}
2023-09-25 21:08:29 +08:00
TEST_CASE("dequeue-empty-nowait", "[mpmc_blocking_q]") {
2018-08-14 22:57:55 +08:00
size_t q_size = 100;
milliseconds tolerance_wait(20);
2018-08-14 22:57:55 +08:00
spdlog::details::mpmc_blocking_queue<int> q(q_size);
2020-04-08 22:04:10 +08:00
int popped_item = 0;
2018-08-14 22:57:55 +08:00
2018-08-17 22:17:16 +08:00
auto start = test_clock::now();
2018-08-14 22:57:55 +08:00
auto rv = q.dequeue_for(popped_item, milliseconds::zero());
2018-08-17 22:17:16 +08:00
auto delta_ms = millis_from(start);
2018-08-14 22:57:55 +08:00
REQUIRE(rv == false);
2018-08-17 22:17:16 +08:00
INFO("Delta " << delta_ms.count() << " millis");
REQUIRE(delta_ms <= tolerance_wait);
2018-08-14 22:57:55 +08:00
}
2023-09-25 21:08:29 +08:00
TEST_CASE("dequeue-empty-wait", "[mpmc_blocking_q]") {
2018-08-14 22:57:55 +08:00
size_t q_size = 100;
milliseconds wait_ms(250);
2019-09-21 20:49:54 +08:00
milliseconds tolerance_wait(250);
2018-08-14 22:57:55 +08:00
spdlog::details::mpmc_blocking_queue<int> q(q_size);
2020-04-08 22:04:10 +08:00
int popped_item = 0;
2018-08-17 22:17:16 +08:00
auto start = test_clock::now();
2018-08-14 22:57:55 +08:00
auto rv = q.dequeue_for(popped_item, wait_ms);
2018-08-17 22:17:16 +08:00
auto delta_ms = millis_from(start);
2018-08-14 22:57:55 +08:00
REQUIRE(rv == false);
2018-08-17 22:17:16 +08:00
INFO("Delta " << delta_ms.count() << " millis");
2018-09-26 19:45:57 +08:00
REQUIRE(delta_ms >= wait_ms - tolerance_wait);
2018-08-14 22:57:55 +08:00
REQUIRE(delta_ms <= wait_ms + tolerance_wait);
}
2023-09-25 21:08:29 +08:00
TEST_CASE("dequeue-full-nowait", "[mpmc_blocking_q]") {
spdlog::details::mpmc_blocking_queue<int> q(1);
q.enqueue(42);
int item = 0;
q.dequeue_for(item, milliseconds::zero());
REQUIRE(item == 42);
}
2023-09-25 21:08:29 +08:00
TEST_CASE("dequeue-full-wait", "[mpmc_blocking_q]") {
spdlog::details::mpmc_blocking_queue<int> q(1);
q.enqueue(42);
int item = 0;
q.dequeue(item);
REQUIRE(item == 42);
}
2023-09-25 21:08:29 +08:00
TEST_CASE("enqueue_nowait", "[mpmc_blocking_q]") {
2018-08-14 22:57:55 +08:00
size_t q_size = 1;
spdlog::details::mpmc_blocking_queue<int> q(q_size);
2018-08-17 22:30:33 +08:00
milliseconds tolerance_wait(10);
2018-08-14 22:57:55 +08:00
q.enqueue(1);
REQUIRE(q.overrun_counter() == 0);
2018-08-17 22:17:16 +08:00
auto start = test_clock::now();
2018-08-14 22:57:55 +08:00
q.enqueue_nowait(2);
2018-08-17 22:17:16 +08:00
auto delta_ms = millis_from(start);
INFO("Delta " << delta_ms.count() << " millis");
REQUIRE(delta_ms <= tolerance_wait);
2018-08-14 22:57:55 +08:00
REQUIRE(q.overrun_counter() == 1);
}
2023-09-25 21:08:29 +08:00
TEST_CASE("bad_queue", "[mpmc_blocking_q]") {
2018-08-14 22:57:55 +08:00
size_t q_size = 0;
spdlog::details::mpmc_blocking_queue<int> q(q_size);
q.enqueue_nowait(1);
REQUIRE(q.overrun_counter() == 1);
2020-04-08 22:04:10 +08:00
int i = 0;
2018-08-14 22:57:55 +08:00
REQUIRE(q.dequeue_for(i, milliseconds(0)) == false);
}
2023-09-25 21:08:29 +08:00
TEST_CASE("empty_queue", "[mpmc_blocking_q]") {
2018-08-14 22:57:55 +08:00
size_t q_size = 10;
spdlog::details::mpmc_blocking_queue<int> q(q_size);
2020-04-08 22:04:10 +08:00
int i = 0;
2018-08-14 22:57:55 +08:00
REQUIRE(q.dequeue_for(i, milliseconds(10)) == false);
}
2023-09-25 21:08:29 +08:00
TEST_CASE("full_queue", "[mpmc_blocking_q]") {
2018-08-14 22:57:55 +08:00
size_t q_size = 100;
spdlog::details::mpmc_blocking_queue<int> q(q_size);
2023-09-25 21:08:29 +08:00
for (int i = 0; i < static_cast<int>(q_size); i++) {
2023-09-25 21:40:36 +08:00
q.enqueue(i + 0); // i+0 to force rvalue and avoid tidy warnings on the same time if we
// std::move(i) instead
2018-08-14 22:57:55 +08:00
}
q.enqueue_nowait(123456);
REQUIRE(q.overrun_counter() == 1);
2023-09-25 21:08:29 +08:00
for (int i = 1; i < static_cast<int>(q_size); i++) {
2018-08-16 00:01:54 +08:00
int item = -1;
q.dequeue(item);
2018-08-14 22:57:55 +08:00
REQUIRE(item == i);
}
2018-08-14 22:59:14 +08:00
2018-08-14 22:57:55 +08:00
// last item pushed has overridden the oldest.
2018-08-16 00:01:54 +08:00
int item = -1;
q.dequeue(item);
2018-08-14 22:57:55 +08:00
REQUIRE(item == 123456);
2019-09-21 20:49:54 +08:00
}