2018-08-14 10:57:55 -04:00
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
using std::chrono::milliseconds;
|
2018-08-25 10:55:31 -04:00
|
|
|
using test_clock = std::chrono::high_resolution_clock;
|
2018-08-17 10:17:16 -04:00
|
|
|
|
|
|
|
static milliseconds millis_from(const test_clock::time_point &tp0)
|
2018-08-14 10:57:55 -04:00
|
|
|
{
|
2018-08-25 10:55:31 -04:00
|
|
|
return std::chrono::duration_cast<milliseconds>(test_clock::now() - tp0);
|
2018-08-14 10:57:55 -04:00
|
|
|
}
|
|
|
|
TEST_CASE("dequeue-empty-nowait", "[mpmc_blocking_q]")
|
|
|
|
{
|
|
|
|
size_t q_size = 100;
|
2018-08-17 10:30:33 -04:00
|
|
|
milliseconds tolerance_wait(10);
|
2018-08-14 10:57:55 -04:00
|
|
|
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
|
|
|
int popped_item;
|
|
|
|
|
2018-08-17 10:17:16 -04:00
|
|
|
auto start = test_clock::now();
|
2018-08-14 10:57:55 -04:00
|
|
|
auto rv = q.dequeue_for(popped_item, milliseconds::zero());
|
2018-08-17 10:17:16 -04:00
|
|
|
auto delta_ms = millis_from(start);
|
2018-08-14 10:57:55 -04:00
|
|
|
|
|
|
|
REQUIRE(rv == false);
|
2018-08-17 10:17:16 -04:00
|
|
|
INFO("Delta " << delta_ms.count() << " millis");
|
|
|
|
REQUIRE(delta_ms <= tolerance_wait);
|
2018-08-14 10:57:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("dequeue-empty-wait", "[mpmc_blocking_q]")
|
|
|
|
{
|
|
|
|
|
|
|
|
size_t q_size = 100;
|
|
|
|
milliseconds wait_ms(250);
|
2018-09-26 07:45:57 -04:00
|
|
|
milliseconds tolerance_wait(50);
|
2018-08-14 10:57:55 -04:00
|
|
|
|
|
|
|
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
|
|
|
int popped_item;
|
2018-08-17 10:17:16 -04:00
|
|
|
auto start = test_clock::now();
|
2018-08-14 10:57:55 -04:00
|
|
|
auto rv = q.dequeue_for(popped_item, wait_ms);
|
2018-08-17 10:17:16 -04:00
|
|
|
auto delta_ms = millis_from(start);
|
2018-08-14 10:57:55 -04:00
|
|
|
|
|
|
|
REQUIRE(rv == false);
|
2018-08-17 10:17:16 -04:00
|
|
|
|
|
|
|
INFO("Delta " << delta_ms.count() << " millis");
|
2018-09-26 07:45:57 -04:00
|
|
|
REQUIRE(delta_ms >= wait_ms - tolerance_wait);
|
2018-08-14 10:57:55 -04:00
|
|
|
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);
|
2018-08-17 10:30:33 -04:00
|
|
|
milliseconds tolerance_wait(10);
|
2018-08-14 10:57:55 -04:00
|
|
|
|
|
|
|
q.enqueue(1);
|
|
|
|
REQUIRE(q.overrun_counter() == 0);
|
|
|
|
|
2018-08-17 10:17:16 -04:00
|
|
|
auto start = test_clock::now();
|
2018-08-14 10:57:55 -04:00
|
|
|
q.enqueue_nowait(2);
|
2018-08-17 10:17:16 -04:00
|
|
|
auto delta_ms = millis_from(start);
|
|
|
|
|
|
|
|
INFO("Delta " << delta_ms.count() << " millis");
|
|
|
|
REQUIRE(delta_ms <= tolerance_wait);
|
2018-08-14 10:57:55 -04:00
|
|
|
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);
|
2018-08-15 12:01:54 -04:00
|
|
|
for (int i = 0; i < static_cast<int>(q_size); i++)
|
2018-08-14 10:57:55 -04:00
|
|
|
{
|
|
|
|
q.enqueue(std::move(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
q.enqueue_nowait(123456);
|
|
|
|
REQUIRE(q.overrun_counter() == 1);
|
|
|
|
|
2018-08-15 12:01:54 -04:00
|
|
|
for (int i = 1; i < static_cast<int>(q_size); i++)
|
2018-08-14 10:57:55 -04:00
|
|
|
{
|
2018-08-15 12:01:54 -04:00
|
|
|
int item = -1;
|
2018-08-14 10:57:55 -04:00
|
|
|
q.dequeue_for(item, milliseconds(0));
|
|
|
|
REQUIRE(item == i);
|
|
|
|
}
|
2018-08-14 10:59:14 -04:00
|
|
|
|
2018-08-14 10:57:55 -04:00
|
|
|
// last item pushed has overridden the oldest.
|
2018-08-15 12:01:54 -04:00
|
|
|
int item = -1;
|
2018-08-14 10:57:55 -04:00
|
|
|
q.dequeue_for(item, milliseconds(0));
|
|
|
|
REQUIRE(item == 123456);
|
|
|
|
}
|