Commit f964dbc5 authored by Thomas Markwalder's avatar Thomas Markwalder
Browse files

[3052] Added peekAt and dequeueAt methods to D2QueueMgr.

In order to add some flexibility in manipulating queue contents
these two methods provide access to queue entries based on their
ordinal position.
parent a33cd74d
......@@ -142,17 +142,39 @@ D2QueueMgr::removeListener() {
const dhcp_ddns::NameChangeRequestPtr&
D2QueueMgr::peek() const {
if (getQueueSize() == 0) {
isc_throw(D2QueueMgrQueEmpty,
isc_throw(D2QueueMgrQueueEmpty,
"D2QueueMgr peek attempted on an empty queue");
}
return (ncr_queue_.front());
}
const dhcp_ddns::NameChangeRequestPtr&
D2QueueMgr::peekAt(size_t index) const {
if (index >= getQueueSize()) {
isc_throw(D2QueueMgrInvalidIndex,
"D2QueueMgr peek beyond end of queue attempted");
}
return (ncr_queue_.at(index));
}
void
D2QueueMgr::dequeueAt(size_t index) {
if (index >= getQueueSize()) {
isc_throw(D2QueueMgrInvalidIndex,
"D2QueueMgr dequeue beyond end of queue attempted");
}
RequestQueue::iterator pos = ncr_queue_.begin() + index;
ncr_queue_.erase(pos);
}
void
D2QueueMgr::dequeue() {
if (getQueueSize() == 0) {
isc_throw(D2QueueMgrQueEmpty,
isc_throw(D2QueueMgrQueueEmpty,
"D2QueueMgr dequeue attempted on an empty queue");
}
......
......@@ -49,19 +49,27 @@ public:
/// @brief Thrown if the request queue is full when an enqueue is attempted.
class D2QueueMgrQueFull : public isc::Exception {
class D2QueueMgrQueueFull : public isc::Exception {
public:
D2QueueMgrQueFull(const char* file, size_t line, const char* what) :
D2QueueMgrQueueFull(const char* file, size_t line, const char* what) :
isc::Exception(file, line, what) { };
};
/// @brief Thrown if the request queue empty and a read is attempted.
class D2QueueMgrQueEmpty : public isc::Exception {
class D2QueueMgrQueueEmpty : public isc::Exception {
public:
D2QueueMgrQueEmpty(const char* file, size_t line, const char* what) :
D2QueueMgrQueueEmpty(const char* file, size_t line, const char* what) :
isc::Exception(file, line, what) { };
};
/// @brief Thrown if a queue index is beyond the end of the queue
class D2QueueMgrInvalidIndex : public isc::Exception {
public:
D2QueueMgrInvalidIndex(const char* file, size_t line, const char* what) :
isc::Exception(file, line, what) { };
};
/// @brief D2QueueMgr creates and manages a queue of DNS update requests.
///
/// D2QueueMgr is class specifically designed as an integral part of DHCP-DDNS.
......@@ -216,7 +224,7 @@ public:
///
/// @param stop_state is one of the three stopped state values.
///
/// @throw Throws D2QueueMgrError if stop_state is a valid stop state.
/// @throw D2QueueMgrError if stop_state is a valid stop state.
void stopListening(const State stop_state = STOPPED);
/// @brief Deletes the current listener
......@@ -242,7 +250,7 @@ public:
///
/// @param max_queue_size is the new maximum size of the queue.
///
/// @throw Throws D2QueueMgrError if the new value is less than one or if
/// @throw D2QueueMgrError if the new value is less than one or if
/// the new value is less than the number of entries currently in the
/// queue.
void setMaxQueueSize(const size_t max_queue_size);
......@@ -258,12 +266,35 @@ public:
/// approach to task selection. Note, the entry is not removed from the
/// queue.
///
/// @throw Throws D2QueueMgrQueEmpty if there are no entries in the queue.
/// @return Pointer reference to the queue entry.
///
/// @throw D2QueueMgrQueEmpty if there are no entries in the queue.
const dhcp_ddns::NameChangeRequestPtr& peek() const;
/// @brief Returns the entry at a given position in the queue.
///
/// Note that the entry is not removed from the queue.
/// @param index the index of the entry in the queue to fetch.
/// Valid values are 0 (front of the queue) to (queue size - 1).
///
/// @return Pointer reference to the queue entry.
///
/// @throw D2QueueMgrInvalidIndex if the given index is beyond the
/// end of the queue.
const dhcp_ddns::NameChangeRequestPtr& peekAt(size_t index) const;
/// @brief Removes the entry at a given position in the queue.
///
/// @param index the index of the entry in the queue to remove.
/// Valid values are 0 (front of the queue) to (queue size - 1).
///
/// @throw D2QueueMgrInvalidIndex if the given index is beyond the
/// end of the queue.
void dequeueAt(size_t index);
/// @brief Removes the entry at the front of the queue.
///
/// @throw Throws D2QueueMgrQueEmpty if there are no entries in the queue.
/// @throw D2QueueMgrQueEmpty if there are no entries in the queue.
void dequeue();
/// @brief Adds a request to the end of the queue.
......
......@@ -119,8 +119,8 @@ TEST(D2QueueMgrBasicTest, basicQueueTests) {
EXPECT_EQ(0, queue_mgr->getQueueSize());
// Verify that peek and dequeue both throw when queue is empty.
EXPECT_THROW(queue_mgr->peek(), D2QueueMgrQueEmpty);
EXPECT_THROW(queue_mgr->dequeue(), D2QueueMgrQueEmpty);
EXPECT_THROW(queue_mgr->peek(), D2QueueMgrQueueEmpty);
EXPECT_THROW(queue_mgr->dequeue(), D2QueueMgrQueueEmpty);
// Vector to keep track of the NCRs we que.
std::vector<NameChangeRequestPtr>ref_msgs;
......@@ -158,6 +158,32 @@ TEST(D2QueueMgrBasicTest, basicQueueTests) {
// Verify queue size decrements following dequeue.
EXPECT_EQ(VALID_MSG_CNT-(i+1), queue_mgr->getQueueSize());
}
// Iterate over the list of requests and add each to the queue.
for (int i = 0; i < VALID_MSG_CNT; i++) {
// Create the ncr and add to our reference list.
ASSERT_NO_THROW(ncr = NameChangeRequest::fromJSON(valid_msgs[i]));
ref_msgs.push_back(ncr);
EXPECT_NO_THROW(queue_mgr->enqueue(ncr));
}
// Verify queue count is correct.
EXPECT_EQ(VALID_MSG_CNT, queue_mgr->getQueueSize());
// Verfiy that peekAt returns the correct entry.
EXPECT_NO_THROW(ncr = queue_mgr->peekAt(1));
EXPECT_TRUE (*(ref_msgs[1]) == *ncr);
// Verfiy that dequeueAt removes the correct entry.
// Removing it, this should shift the queued entries forward by one.
EXPECT_NO_THROW(queue_mgr->dequeueAt(1));
EXPECT_NO_THROW(ncr = queue_mgr->peekAt(1));
EXPECT_TRUE (*(ref_msgs[2]) == *ncr);
// Verify the peekAt and dequeueAt throw when given indexes beyond the end.
EXPECT_THROW(queue_mgr->peekAt(VALID_MSG_CNT + 1), D2QueueMgrInvalidIndex);
EXPECT_THROW(queue_mgr->dequeueAt(VALID_MSG_CNT + 1),
D2QueueMgrInvalidIndex);
}
/// @brief Compares two NameChangeRequests for equality.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment