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"
TmSendHelper::TmSendHelper(MessageQueueIF* queue, MessageQueueId_t tmtcMsgDest,
InternalErrorReporterIF *reporter)
TmSendHelper::TmSendHelper(MessageQueueIF* queue, InternalErrorReporterIF *reporter, MessageQueueId_t tmtcMsgDest)
: tmtcMsgDest(tmtcMsgDest), queue(queue), errReporter(reporter) {}
TmSendHelper::TmSendHelper(MessageQueueIF *queue, InternalErrorReporterIF *reporter)
@ -32,3 +31,8 @@ void TmSendHelper::setInternalErrorReporter(InternalErrorReporterIF *reporter) {
errReporter = reporter;
}
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 {
public:
explicit TmSendHelper(InternalErrorReporterIF* reporter);
TmSendHelper(MessageQueueIF* queue, InternalErrorReporterIF* reporter, MessageQueueId_t tmtcMsgDest);
TmSendHelper(MessageQueueIF* queue, InternalErrorReporterIF* reporter);
TmSendHelper(MessageQueueIF* queue, MessageQueueId_t tmtcMsgDest,
InternalErrorReporterIF* reporter);
explicit TmSendHelper(InternalErrorReporterIF* reporter);
void setMsgQueue(MessageQueueIF* queue);
void setMsgDestination(MessageQueueId_t msgDest);
void setInternalErrorReporter(InternalErrorReporterIF* reporter);
ReturnValue_t sendPacket(MessageQueueId_t dest, const store_address_t& storeId);
ReturnValue_t sendPacket(const store_address_t& storeId);
private:

View File

@ -5,4 +5,5 @@ target_sources(${FSFW_TEST_TGT} PRIVATE
CookieIFMock.cpp
ComIFMock.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 {
public:
unsigned int queueMsgNotSentCallCnt = 0;
unsigned int lostTmCallCnt = 0;
unsigned int storeFullCallCnt = 0;
InternalErrorReporterMock();
void queueMessageNotSent() override;
void lostTm() override;
void storeFull() override;
private:
};
#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,
MessageQueueMessageIF* message,
MessageQueueId_t sentFrom, bool ignoreFault) {
messageSent = true;
messageSentCounter++;
MessageQueueMessage& messageRef = *(dynamic_cast<MessageQueueMessage*>(message));
messagesSentQueue.push(messageRef);
auto iter = sendMap.find(sendTo);
if (iter == sendMap.end()) {
sendMap.emplace(sendTo, SendInfo(message, 1));
} else {
iter->second.callCount += 1;
iter->second.msgs.push(message);
}
return HasReturnvaluesIF::RETURN_OK;
}
@ -55,6 +58,12 @@ ReturnValue_t MessageQueueMockBase::reply(MessageQueueMessageIF* message) {
}
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()) {
if (clearCommandMessages) {
CommandMessage message;

View File

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

View File

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