Skip to content

Commit

Permalink
add test for enqueuing to a full queue
Browse files Browse the repository at this point in the history
  • Loading branch information
csegarragonz committed Dec 27, 2021
1 parent 3c51095 commit bc8aec4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
9 changes: 7 additions & 2 deletions include/faabric/util/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <faabric/util/logging.h>

#include <boost/circular_buffer.hpp>
#include <boost/lockfree/spsc_queue.hpp>
#include <condition_variable>
#include <queue>

Expand Down Expand Up @@ -141,7 +140,7 @@ class Queue
std::mutex mx;
};

// Queue on top of a circular buffer
// Fixed size queue using a circular buffer as underlying container
template<typename T>
class FixedCapacityQueue
{
Expand All @@ -156,6 +155,12 @@ class FixedCapacityQueue
{
UniqueLock lock(mx);

if (timeoutMs <= 0) {
SPDLOG_ERROR("Invalid queue timeout: {} <= 0", timeoutMs);
throw std::runtime_error("Invalid queue timeout");
}

// If the queue is full, wait until elements are consumed
while (mq.size() == mq.capacity()) {
std::cv_status returnVal = notFullNotifier.wait_for(
lock, std::chrono::milliseconds(timeoutMs));
Expand Down
11 changes: 11 additions & 0 deletions tests/test/util/test_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,15 @@ TEST_CASE("Test queue timeout must be positive", "[util]")
q.enqueue(10);
REQUIRE_THROWS(q.dequeue(timeoutValueMs));
}

TEST_CASE("Test fixed capacity queue blocks if queue is full", "[util]")
{
FixedCapIntQueue q(2);

q.enqueue(1);
q.enqueue(2);

// Enqueue with a short timeout so the operation fails quickly
REQUIRE_THROWS(q.enqueue(100));
}
}

0 comments on commit bc8aec4

Please sign in to comment.