improving mocks

This commit is contained in:
Robin Müller 2022-07-25 14:31:57 +02:00
parent 1a7d7b172b
commit 9ee6da47e9
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814
8 changed files with 64 additions and 13 deletions

View File

@ -2,8 +2,7 @@
#include "fsfw/ipc/MessageQueueSenderIF.h" #include "fsfw/ipc/MessageQueueSenderIF.h"
TmSendHelper::TmSendHelper(MessageQueueIF* queue, MessageQueueId_t tmtcMsgDest, TmSendHelper::TmSendHelper(MessageQueueIF* queue, InternalErrorReporterIF *reporter, MessageQueueId_t tmtcMsgDest)
InternalErrorReporterIF *reporter)
: tmtcMsgDest(tmtcMsgDest), queue(queue), errReporter(reporter) {} : tmtcMsgDest(tmtcMsgDest), queue(queue), errReporter(reporter) {}
TmSendHelper::TmSendHelper(MessageQueueIF *queue, InternalErrorReporterIF *reporter) TmSendHelper::TmSendHelper(MessageQueueIF *queue, InternalErrorReporterIF *reporter)
@ -32,3 +31,8 @@ void TmSendHelper::setInternalErrorReporter(InternalErrorReporterIF *reporter) {
errReporter = reporter; errReporter = reporter;
} }
void TmSendHelper::setMsgQueue(MessageQueueIF *queue_) { queue = queue_; } void TmSendHelper::setMsgQueue(MessageQueueIF *queue_) { queue = queue_; }
ReturnValue_t TmSendHelper::sendPacket(MessageQueueId_t dest, const store_address_t &storeId) {
setMsgDestination(dest);
return sendPacket(storeId);
}

View File

@ -9,13 +9,15 @@
class TmSendHelper { class TmSendHelper {
public: public:
explicit TmSendHelper(InternalErrorReporterIF* reporter); TmSendHelper(MessageQueueIF* queue, InternalErrorReporterIF* reporter, MessageQueueId_t tmtcMsgDest);
TmSendHelper(MessageQueueIF* queue, InternalErrorReporterIF* reporter); TmSendHelper(MessageQueueIF* queue, InternalErrorReporterIF* reporter);
TmSendHelper(MessageQueueIF* queue, MessageQueueId_t tmtcMsgDest, explicit TmSendHelper(InternalErrorReporterIF* reporter);
InternalErrorReporterIF* reporter);
void setMsgQueue(MessageQueueIF* queue); void setMsgQueue(MessageQueueIF* queue);
void setMsgDestination(MessageQueueId_t msgDest); void setMsgDestination(MessageQueueId_t msgDest);
void setInternalErrorReporter(InternalErrorReporterIF* reporter); void setInternalErrorReporter(InternalErrorReporterIF* reporter);
ReturnValue_t sendPacket(MessageQueueId_t dest, const store_address_t& storeId);
ReturnValue_t sendPacket(const store_address_t& storeId); ReturnValue_t sendPacket(const store_address_t& storeId);
private: private:

View File

@ -5,4 +5,5 @@ target_sources(${FSFW_TEST_TGT} PRIVATE
CookieIFMock.cpp CookieIFMock.cpp
ComIFMock.cpp ComIFMock.cpp
MessageQueueMockBase.cpp MessageQueueMockBase.cpp
InternalErrorReporterMock.cpp
) )

View File

@ -0,0 +1,13 @@
#include "InternalErrorReporterMock.h"
InternalErrorReporterMock::InternalErrorReporterMock() = default;
void InternalErrorReporterMock::queueMessageNotSent() {
queueMsgNotSentCallCnt++;
}
void InternalErrorReporterMock::lostTm() {
lostTmCallCnt++;
}
void InternalErrorReporterMock::storeFull() {
storeFullCallCnt++;
}

View File

@ -5,7 +5,14 @@
class InternalErrorReporterMock: public InternalErrorReporterIF { class InternalErrorReporterMock: public InternalErrorReporterIF {
public: public:
unsigned int queueMsgNotSentCallCnt = 0;
unsigned int lostTmCallCnt = 0;
unsigned int storeFullCallCnt = 0;
InternalErrorReporterMock(); InternalErrorReporterMock();
void queueMessageNotSent() override;
void lostTm() override;
void storeFull() override;
private: private:
}; };
#endif // FSFW_TESTS_INTERNALERRORREPORTERMOCK_H #endif // FSFW_TESTS_INTERNALERRORREPORTERMOCK_H

View File

@ -43,10 +43,13 @@ ReturnValue_t MessageQueueMockBase::flush(uint32_t* count) {
ReturnValue_t MessageQueueMockBase::sendMessageFrom(MessageQueueId_t sendTo, ReturnValue_t MessageQueueMockBase::sendMessageFrom(MessageQueueId_t sendTo,
MessageQueueMessageIF* message, MessageQueueMessageIF* message,
MessageQueueId_t sentFrom, bool ignoreFault) { MessageQueueId_t sentFrom, bool ignoreFault) {
messageSent = true; auto iter = sendMap.find(sendTo);
messageSentCounter++; if (iter == sendMap.end()) {
MessageQueueMessage& messageRef = *(dynamic_cast<MessageQueueMessage*>(message)); sendMap.emplace(sendTo, SendInfo(message, 1));
messagesSentQueue.push(messageRef); } else {
iter->second.callCount += 1;
iter->second.msgs.push(message);
}
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
@ -55,6 +58,12 @@ ReturnValue_t MessageQueueMockBase::reply(MessageQueueMessageIF* message) {
} }
void MessageQueueMockBase::clearMessages(bool clearCommandMessages) { void MessageQueueMockBase::clearMessages(bool clearCommandMessages) {
for (const auto& destInfo: sendMap) {
if (clearCommandMessages) {
CommandMessage message;
std::memcpy(message.getBuffer(), destInfo.second.msgs.front()->getBuffer(),
message.getMessageSize());
}
while (not messagesSentQueue.empty()) { while (not messagesSentQueue.empty()) {
if (clearCommandMessages) { if (clearCommandMessages) {
CommandMessage message; CommandMessage message;

View File

@ -3,6 +3,7 @@
#include <cstring> #include <cstring>
#include <queue> #include <queue>
#include <map>
#include "CatchDefinitions.h" #include "CatchDefinitions.h"
#include "fsfw/ipc/CommandMessage.h" #include "fsfw/ipc/CommandMessage.h"
@ -10,14 +11,21 @@
#include "fsfw/ipc/MessageQueueIF.h" #include "fsfw/ipc/MessageQueueIF.h"
#include "fsfw/ipc/MessageQueueMessage.h" #include "fsfw/ipc/MessageQueueMessage.h"
struct SendInfo {
explicit SendInfo(MessageQueueMessageIF* initMsg, unsigned int initCallCnt = 1): callCount(initCallCnt) {
msgs.push(initMsg);
}
unsigned int callCount = 0;
std::queue<MessageQueueMessageIF*> msgs;
};
class MessageQueueMockBase : public MessageQueueBase { class MessageQueueMockBase : public MessageQueueBase {
public: public:
MessageQueueMockBase(); MessageQueueMockBase();
explicit MessageQueueMockBase(MessageQueueId_t queueId); explicit MessageQueueMockBase(MessageQueueId_t queueId);
uint8_t messageSentCounter = 0; std::map<MessageQueueId_t, SendInfo> sendMap;
bool messageSent = false;
bool wasMessageSent(uint8_t* messageSentCounter_ = nullptr, bool resetCounter = true); bool wasMessageSent(uint8_t* messageSentCounter_ = nullptr, bool resetCounter = true);
@ -37,7 +45,6 @@ class MessageQueueMockBase : public MessageQueueBase {
void clearMessages(bool clearCommandMessages = true); void clearMessages(bool clearCommandMessages = true);
private: private:
std::queue<MessageQueueMessage> messagesSentQueue;
}; };
#endif /* FSFW_UNITTEST_TESTS_MOCKS_MESSAGEQUEUEMOCKBASE_H_ */ #endif /* FSFW_UNITTEST_TESTS_MOCKS_MESSAGEQUEUEMOCKBASE_H_ */

View File

@ -1,7 +1,15 @@
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include "fsfw/tmtcservices/TmSendHelper.h" #include "fsfw/tmtcservices/TmSendHelper.h"
#include "mocks/InternalErrorReporterMock.h"
#include "mocks/MessageQueueMockBase.h"
TEST_CASE("TM Send Helper", "[tm-send-helper]") { TEST_CASE("TM Send Helper", "[tm-send-helper]") {
TmSendHelper sendHelper(nullptr); auto errReporter = InternalErrorReporterMock();
auto msgQueue = MessageQueueMockBase();
TmSendHelper sendHelper(&msgQueue, &errReporter);
SECTION("State") {
}
} }