2022-07-25 19:42:05 +02:00
|
|
|
#include "MessageQueueMock.h"
|
2022-07-25 14:04:06 +02:00
|
|
|
|
2022-07-25 19:36:56 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <stdexcept>
|
2022-07-25 14:04:06 +02:00
|
|
|
|
2024-12-12 16:15:02 +01:00
|
|
|
MessageQueueMock::MessageQueueMock(MessageQueueId_t queueId, MessageQueueId_t defaultDest)
|
|
|
|
: MessageQueueBase(queueId, defaultDest, nullptr) {}
|
2022-07-25 14:04:06 +02:00
|
|
|
|
2022-07-25 19:42:05 +02:00
|
|
|
bool MessageQueueMock::wasMessageSent() const {
|
2022-07-25 19:36:56 +02:00
|
|
|
return std::any_of(
|
|
|
|
sendMap.begin(), sendMap.end(),
|
|
|
|
[](const std::pair<MessageQueueId_t, SendInfo>& pair) { return pair.second.callCount > 0; });
|
|
|
|
}
|
|
|
|
|
2022-07-25 20:31:06 +02:00
|
|
|
size_t MessageQueueMock::numberOfSentMessages() const {
|
2022-07-25 19:36:56 +02:00
|
|
|
size_t callCount = 0;
|
|
|
|
for (auto& destInfo : sendMap) {
|
|
|
|
callCount += destInfo.second.callCount;
|
2022-07-25 14:04:06 +02:00
|
|
|
}
|
2022-07-25 19:36:56 +02:00
|
|
|
return callCount;
|
|
|
|
}
|
|
|
|
|
2022-07-25 20:31:06 +02:00
|
|
|
size_t MessageQueueMock::numberOfSentMessagesToDest(MessageQueueId_t id) const {
|
2022-07-25 19:36:56 +02:00
|
|
|
auto iter = sendMap.find(id);
|
|
|
|
if (iter == sendMap.end()) {
|
|
|
|
return 0;
|
2022-07-25 14:04:06 +02:00
|
|
|
}
|
2022-07-25 19:36:56 +02:00
|
|
|
return iter->second.callCount;
|
2022-07-25 14:04:06 +02:00
|
|
|
}
|
|
|
|
|
2022-07-25 20:31:06 +02:00
|
|
|
size_t MessageQueueMock::numberOfSentMessagesToDefault() const {
|
|
|
|
return numberOfSentMessagesToDest(MessageQueueBase::getDefaultDestination());
|
|
|
|
}
|
|
|
|
|
2022-07-25 19:42:05 +02:00
|
|
|
ReturnValue_t MessageQueueMock::clearLastReceivedMessage(bool clearCmdMsg) {
|
2022-07-25 19:36:56 +02:00
|
|
|
if (receivedMsgs.empty()) {
|
|
|
|
return MessageQueueIF::EMPTY;
|
|
|
|
}
|
|
|
|
if (clearCmdMsg) {
|
|
|
|
CommandMessage message;
|
|
|
|
std::memcpy(message.getBuffer(), receivedMsgs.front().getBuffer(), message.getMessageSize());
|
|
|
|
message.clearCommandMessage();
|
|
|
|
}
|
2022-07-25 19:42:05 +02:00
|
|
|
receivedMsgs.pop();
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::OK;
|
2022-07-25 14:04:06 +02:00
|
|
|
}
|
|
|
|
|
2022-07-25 19:42:05 +02:00
|
|
|
ReturnValue_t MessageQueueMock::receiveMessage(MessageQueueMessageIF* message) {
|
2022-07-25 19:36:56 +02:00
|
|
|
if (receivedMsgs.empty()) {
|
2022-07-25 14:04:06 +02:00
|
|
|
return MessageQueueIF::EMPTY;
|
|
|
|
}
|
2022-07-25 19:36:56 +02:00
|
|
|
std::memcpy(message->getBuffer(), receivedMsgs.front().getBuffer(), message->getMessageSize());
|
2022-07-25 19:42:05 +02:00
|
|
|
receivedMsgs.pop();
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::OK;
|
2022-07-25 14:04:06 +02:00
|
|
|
}
|
|
|
|
|
2022-08-16 01:08:26 +02:00
|
|
|
ReturnValue_t MessageQueueMock::flush(uint32_t* count) { return returnvalue::FAILED; }
|
2022-07-25 14:04:06 +02:00
|
|
|
|
2022-07-25 19:42:05 +02:00
|
|
|
ReturnValue_t MessageQueueMock::sendMessageFrom(MessageQueueId_t sendTo,
|
|
|
|
MessageQueueMessageIF* message,
|
|
|
|
MessageQueueId_t sentFrom, bool ignoreFault) {
|
2022-07-25 19:36:56 +02:00
|
|
|
if (message == nullptr) {
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-07-25 19:36:56 +02:00
|
|
|
}
|
2022-07-25 20:31:06 +02:00
|
|
|
if (nextSendFailsPair.first) {
|
|
|
|
nextSendFailsPair.first = false;
|
|
|
|
return nextSendFailsPair.second;
|
|
|
|
}
|
2022-07-25 14:31:57 +02:00
|
|
|
auto iter = sendMap.find(sendTo);
|
2022-07-25 19:36:56 +02:00
|
|
|
MessageQueueMessage messageCopy;
|
2022-07-25 14:31:57 +02:00
|
|
|
if (iter == sendMap.end()) {
|
2022-07-25 19:36:56 +02:00
|
|
|
createMsgCopy(messageCopy, *message);
|
|
|
|
sendMap.emplace(sendTo, SendInfo(messageCopy, 1));
|
2022-07-25 14:31:57 +02:00
|
|
|
} else {
|
|
|
|
iter->second.callCount += 1;
|
2022-07-25 19:36:56 +02:00
|
|
|
createMsgCopy(messageCopy, *message);
|
2022-07-25 19:42:05 +02:00
|
|
|
iter->second.msgs.push(messageCopy);
|
2022-07-25 14:31:57 +02:00
|
|
|
}
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::OK;
|
2022-07-25 14:04:06 +02:00
|
|
|
}
|
|
|
|
|
2022-07-25 19:42:05 +02:00
|
|
|
ReturnValue_t MessageQueueMock::reply(MessageQueueMessageIF* message) {
|
2022-07-25 14:04:06 +02:00
|
|
|
return sendMessageFrom(MessageQueueIF::NO_QUEUE, message, this->getId(), false);
|
|
|
|
}
|
|
|
|
|
2022-07-25 19:42:05 +02:00
|
|
|
void MessageQueueMock::clearMessages(bool clearCommandMessages) {
|
2022-07-25 19:36:56 +02:00
|
|
|
if (not clearCommandMessages) {
|
|
|
|
sendMap.clear();
|
|
|
|
return;
|
2022-07-25 14:31:57 +02:00
|
|
|
}
|
2022-07-25 19:36:56 +02:00
|
|
|
for (auto& destInfo : sendMap) {
|
2022-07-25 19:42:05 +02:00
|
|
|
while (!destInfo.second.msgs.empty()) {
|
2022-07-25 14:04:06 +02:00
|
|
|
CommandMessage message;
|
2022-07-25 19:36:56 +02:00
|
|
|
std::memcpy(message.getBuffer(), destInfo.second.msgs.front().getBuffer(),
|
2022-07-25 14:04:06 +02:00
|
|
|
message.getMessageSize());
|
|
|
|
message.clear();
|
2022-07-25 19:42:05 +02:00
|
|
|
destInfo.second.msgs.pop();
|
2022-07-25 19:36:56 +02:00
|
|
|
destInfo.second.callCount--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sendMap.clear();
|
|
|
|
}
|
|
|
|
|
2022-07-25 19:42:05 +02:00
|
|
|
void MessageQueueMock::addReceivedMessage(MessageQueueMessageIF& msg) {
|
2022-07-25 19:36:56 +02:00
|
|
|
MessageQueueMessage messageCopy;
|
|
|
|
createMsgCopy(messageCopy, msg);
|
2022-07-25 19:42:05 +02:00
|
|
|
receivedMsgs.push(messageCopy);
|
2022-07-25 19:36:56 +02:00
|
|
|
}
|
|
|
|
|
2022-07-25 19:42:05 +02:00
|
|
|
void MessageQueueMock::createMsgCopy(MessageQueueMessageIF& into, MessageQueueMessageIF& from) {
|
2022-07-25 19:36:56 +02:00
|
|
|
if (from.getMessageSize() > into.getMaximumDataSize()) {
|
|
|
|
throw std::invalid_argument("Passed message does not fit into message copy");
|
|
|
|
}
|
|
|
|
std::memcpy(into.getBuffer(), from.getBuffer(), from.getMaximumDataSize());
|
|
|
|
}
|
|
|
|
|
2022-07-25 19:42:05 +02:00
|
|
|
ReturnValue_t MessageQueueMock::getNextSentMessage(MessageQueueId_t id,
|
|
|
|
MessageQueueMessageIF& message) {
|
2022-07-25 19:36:56 +02:00
|
|
|
auto iter = sendMap.find(id);
|
|
|
|
if (iter == sendMap.end() or iter->second.callCount == 0) {
|
|
|
|
return MessageQueueIF::EMPTY;
|
|
|
|
}
|
|
|
|
createMsgCopy(message, iter->second.msgs.front());
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::OK;
|
2022-07-25 19:36:56 +02:00
|
|
|
}
|
|
|
|
|
2023-08-03 13:03:58 +02:00
|
|
|
ReturnValue_t MessageQueueMock::getNextSentMessageToDefaultDest(MessageQueueMessageIF& message) {
|
2022-07-25 19:36:56 +02:00
|
|
|
return getNextSentMessage(MessageQueueBase::getDefaultDestination(), message);
|
|
|
|
}
|
|
|
|
|
2022-07-25 19:42:05 +02:00
|
|
|
ReturnValue_t MessageQueueMock::clearLastSentMessage(MessageQueueId_t destId, bool clearCmdMsg) {
|
2022-07-25 19:36:56 +02:00
|
|
|
auto iter = sendMap.find(destId);
|
|
|
|
if (iter == sendMap.end()) {
|
|
|
|
return MessageQueueIF::EMPTY;
|
|
|
|
}
|
|
|
|
return clearLastSentMessage(iter, clearCmdMsg);
|
|
|
|
}
|
|
|
|
|
2022-07-25 19:42:05 +02:00
|
|
|
ReturnValue_t MessageQueueMock::clearLastSentMessage(bool clearCmdMsg) {
|
2022-07-25 19:36:56 +02:00
|
|
|
auto iter = sendMap.find(getDefaultDestination());
|
|
|
|
if (iter == sendMap.end()) {
|
|
|
|
return MessageQueueIF::EMPTY;
|
|
|
|
}
|
|
|
|
ReturnValue_t result = clearLastSentMessage(iter, clearCmdMsg);
|
|
|
|
clearEmptyEntries();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-07-25 19:42:05 +02:00
|
|
|
ReturnValue_t MessageQueueMock::clearLastSentMessage(
|
2022-07-25 19:36:56 +02:00
|
|
|
std::map<MessageQueueId_t, SendInfo>::iterator& iter, bool clearCmdMsg) {
|
|
|
|
if (clearCmdMsg) {
|
|
|
|
CommandMessage message;
|
|
|
|
std::memcpy(message.getBuffer(), iter->second.msgs.front().getBuffer(),
|
|
|
|
message.getMessageSize());
|
|
|
|
message.clear();
|
|
|
|
}
|
2022-07-25 19:42:05 +02:00
|
|
|
iter->second.msgs.pop();
|
2022-07-25 19:36:56 +02:00
|
|
|
iter->second.callCount--;
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::OK;
|
2022-07-25 19:36:56 +02:00
|
|
|
}
|
2022-07-25 19:42:05 +02:00
|
|
|
void MessageQueueMock::clearEmptyEntries() {
|
2022-07-25 19:36:56 +02:00
|
|
|
for (auto it = sendMap.cbegin(); it != sendMap.cend();) {
|
|
|
|
if (it->second.callCount == 0) {
|
|
|
|
sendMap.erase(it++);
|
|
|
|
} else {
|
|
|
|
++it;
|
2022-07-25 14:04:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 20:31:06 +02:00
|
|
|
void MessageQueueMock::makeNextSendFail(ReturnValue_t retval) {
|
|
|
|
nextSendFailsPair.first = true;
|
|
|
|
nextSendFailsPair.second = retval;
|
|
|
|
}
|