Skip to content

Commit

Permalink
feat: add module type to multitype_queue functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasTurina committed Jan 10, 2025
1 parent c44388c commit ed2e1f6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
18 changes: 13 additions & 5 deletions src/agent/multitype_queue/include/imultitype_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,36 +66,44 @@ class IMultiTypeQueue
/// @brief Deletes a message from the queue.
/// @param type The type of the queue from which to pop the message.
/// @param moduleName The name of the module requesting the pop.
/// @param moduleType The type of the module requesting the pop.
/// @return true If the message was popped successfully.
/// @return false If the message could not be popped.
virtual bool pop(MessageType type, const std::string moduleName = "") = 0;
virtual bool pop(MessageType type, const std::string moduleName = "", const std::string moduleType = "") = 0;

/// @brief Deletes N messages from the queue.
/// @param type The type of the queue from which to pop the messages.
/// @param messageQuantity The quantity of messages to pop.
/// @param moduleName The name of the module requesting the pop.
/// @param moduleType The type of the module requesting the pop.
/// @return int The number of messages deleted.
virtual int popN(MessageType type, int messageQuantity, const std::string moduleName = "") = 0;
virtual int popN(MessageType type,
int messageQuantity,
const std::string moduleName = "",
const std::string moduleType = "") = 0;

/// @brief Checks if a queue is empty.
/// @param type The type of the queue.
/// @param moduleName The name of the module requesting the check.
/// @param moduleType The type of the module requesting the check.
/// @return true If the queue is empty.
/// @return false If the queue is not empty.
virtual bool isEmpty(MessageType type, const std::string moduleName = "") = 0;
virtual bool isEmpty(MessageType type, const std::string moduleName = "", const std::string moduleType = "") = 0;

/// @brief Checks if a queue is full
/// @param type The type of the queue.
/// @param moduleName The name of the module requesting the check.
/// @param moduleType The type of the module requesting the check.
/// @return true If the queue is full.
/// @return false If the queue is not full.
virtual bool isFull(MessageType type, const std::string moduleName = "") = 0;
virtual bool isFull(MessageType type, const std::string moduleName = "", const std::string moduleType = "") = 0;

/// @brief Returns the number of items stored in the queue.
/// @param type The type of the queue.
/// @param moduleName The name of the module requesting the count.
/// @param moduleType The type of the module requesting the count.
/// @return int The number of items in the queue.
virtual int storedItems(MessageType type, const std::string moduleName = "") = 0;
virtual int storedItems(MessageType type, const std::string moduleName = "", const std::string moduleType = "") = 0;

/// @brief Returns the size of the queue per type
/// @param type The type of the queue.
Expand Down
23 changes: 13 additions & 10 deletions src/agent/multitype_queue/include/multitype_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,23 @@ class MultiTypeQueue : public IMultiTypeQueue
const std::string moduleName = "",
const std::string moduleType = "") override;

/// @copydoc IMultiTypeQueue::pop(MessageType, const std::string)
bool pop(MessageType type, const std::string moduleName = "") override;
/// @copydoc IMultiTypeQueue::pop(MessageType, const std::string, const std::string)
bool pop(MessageType type, const std::string moduleName = "", const std::string moduleType = "") override;

/// @copydoc IMultiTypeQueue::popN(MessageType, int, const std::string)
int popN(MessageType type, int messageQuantity, const std::string moduleName = "") override;
/// @copydoc IMultiTypeQueue::popN(MessageType, int, const std::string, const std::string)
int popN(MessageType type,
int messageQuantity,
const std::string moduleName = "",
const std::string moduleType = "") override;

/// @copydoc IMultiTypeQueue::isEmpty(MessageType, const std::string)
bool isEmpty(MessageType type, const std::string moduleName = "") override;
/// @copydoc IMultiTypeQueue::isEmpty(MessageType, const std::string, const std::string)
bool isEmpty(MessageType type, const std::string moduleName = "", const std::string moduleType = "") override;

/// @copydoc IMultiTypeQueue::isFull(MessageType, const std::string)
bool isFull(MessageType type, const std::string moduleName = "") override;
/// @copydoc IMultiTypeQueue::isFull(MessageType, const std::string, const std::string)
bool isFull(MessageType type, const std::string moduleName = "", const std::string moduleType = "") override;

/// @copydoc IMultiTypeQueue::storedItems(MessageType, const std::string)
int storedItems(MessageType type, const std::string moduleName = "") override;
/// @copydoc IMultiTypeQueue::storedItems(MessageType, const std::string, const std::string)
int storedItems(MessageType type, const std::string moduleName = "", const std::string moduleType = "") override;

/// @copydoc IMultiTypeQueue::sizePerType(MessageType type)
size_t sizePerType(MessageType type) override;
Expand Down
26 changes: 15 additions & 11 deletions src/agent/multitype_queue/src/multitype_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ std::vector<Message> MultiTypeQueue::getNextBytes(MessageType type,
return result;
}

bool MultiTypeQueue::pop(MessageType type, const std::string moduleName)
bool MultiTypeQueue::pop(MessageType type, const std::string moduleName, const std::string moduleType)
{
bool result = false;
if (m_mapMessageTypeName.contains(type))
{
result = m_persistenceDest->RemoveMultiple(1, m_mapMessageTypeName.at(type), moduleName);
result = m_persistenceDest->RemoveMultiple(1, m_mapMessageTypeName.at(type), moduleName, moduleType);
}
else
{
Expand All @@ -216,12 +216,16 @@ bool MultiTypeQueue::pop(MessageType type, const std::string moduleName)
return result;
}

int MultiTypeQueue::popN(MessageType type, int messageQuantity, const std::string moduleName)
int MultiTypeQueue::popN(MessageType type,
int messageQuantity,
const std::string moduleName,
const std::string moduleType)
{
int result = 0;
if (m_mapMessageTypeName.contains(type))
{
result = m_persistenceDest->RemoveMultiple(messageQuantity, m_mapMessageTypeName.at(type), moduleName);
result =
m_persistenceDest->RemoveMultiple(messageQuantity, m_mapMessageTypeName.at(type), moduleName, moduleType);
}
else
{
Expand All @@ -230,11 +234,11 @@ int MultiTypeQueue::popN(MessageType type, int messageQuantity, const std::strin
return result;
}

bool MultiTypeQueue::isEmpty(MessageType type, const std::string moduleName)
bool MultiTypeQueue::isEmpty(MessageType type, const std::string moduleName, const std::string moduleType)
{
if (m_mapMessageTypeName.contains(type))
{
return m_persistenceDest->GetElementCount(m_mapMessageTypeName.at(type), moduleName) == 0;
return m_persistenceDest->GetElementCount(m_mapMessageTypeName.at(type), moduleName, moduleType) == 0;
}
else
{
Expand All @@ -243,12 +247,12 @@ bool MultiTypeQueue::isEmpty(MessageType type, const std::string moduleName)
return false;
}

bool MultiTypeQueue::isFull(MessageType type, const std::string moduleName)
bool MultiTypeQueue::isFull(MessageType type, const std::string moduleName, const std::string moduleType)
{
if (m_mapMessageTypeName.contains(type))
{
return static_cast<size_t>(m_persistenceDest->GetElementCount(m_mapMessageTypeName.at(type), moduleName)) ==
m_maxItems;
return static_cast<size_t>(m_persistenceDest->GetElementCount(
m_mapMessageTypeName.at(type), moduleName, moduleType)) == m_maxItems;
}
else
{
Expand All @@ -257,11 +261,11 @@ bool MultiTypeQueue::isFull(MessageType type, const std::string moduleName)
return false;
}

int MultiTypeQueue::storedItems(MessageType type, const std::string moduleName)
int MultiTypeQueue::storedItems(MessageType type, const std::string moduleName, const std::string moduleType)
{
if (m_mapMessageTypeName.contains(type))
{
return m_persistenceDest->GetElementCount(m_mapMessageTypeName.at(type), moduleName);
return m_persistenceDest->GetElementCount(m_mapMessageTypeName.at(type), moduleName, moduleType);
}
else
{
Expand Down
10 changes: 5 additions & 5 deletions src/agent/tests/message_queue_utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class MockMultiTypeQueue : public MultiTypeQueue
getNextBytesAwaitable,
(MessageType, const size_t, const std::string, const std::string),
(override));
MOCK_METHOD(int, popN, (MessageType, int, const std::string), (override));
MOCK_METHOD(int, popN, (MessageType, int, const std::string, const std::string), (override));
MOCK_METHOD(int, push, (Message, bool), (override));
MOCK_METHOD(int, push, (std::vector<Message>), (override));
MOCK_METHOD(bool, isEmpty, (MessageType, const std::string), (override));
MOCK_METHOD(bool, isEmpty, (MessageType, const std::string, const std::string), (override));
MOCK_METHOD(Message, getNext, (MessageType, const std::string, const std::string), (override));
};

Expand Down Expand Up @@ -157,7 +157,7 @@ TEST_F(MessageQueueUtilsTest, GetEmptyMessagesFromQueueTest)

TEST_F(MessageQueueUtilsTest, PopMessagesFromQueueTest)
{
EXPECT_CALL(*mockQueue, popN(MessageType::STATEFUL, 1, "")).Times(1);
EXPECT_CALL(*mockQueue, popN(MessageType::STATEFUL, 1, "", "")).Times(1);
PopMessagesFromQueue(mockQueue, MessageType::STATEFUL, 1);
}

Expand Down Expand Up @@ -189,7 +189,7 @@ TEST_F(MessageQueueUtilsTest, NoCommandsToPushTest)

TEST_F(MessageQueueUtilsTest, GetCommandFromQueueEmptyTest)
{
EXPECT_CALL(*mockQueue, isEmpty(MessageType::COMMAND, "")).WillOnce(testing::Return(true));
EXPECT_CALL(*mockQueue, isEmpty(MessageType::COMMAND, "", "")).WillOnce(testing::Return(true));

ASSERT_EQ(std::nullopt, GetCommandFromQueue(mockQueue));
}
Expand All @@ -198,7 +198,7 @@ TEST_F(MessageQueueUtilsTest, GetCommandFromQueueTest)
{
Message testMessage {MessageType::COMMAND, BASE_DATA_CONTENT};

EXPECT_CALL(*mockQueue, isEmpty(MessageType::COMMAND, "")).WillOnce(testing::Return(false));
EXPECT_CALL(*mockQueue, isEmpty(MessageType::COMMAND, "", "")).WillOnce(testing::Return(false));

EXPECT_CALL(*mockQueue, getNext(MessageType::COMMAND, "", "")).WillOnce(testing::Return(testMessage));

Expand Down

0 comments on commit ed2e1f6

Please sign in to comment.