Improve:McuManager test code.

This commit is contained in:
Fancy code 2024-02-10 03:24:07 -08:00
parent 524fb9db30
commit e3dc20d7a4
7 changed files with 26 additions and 11 deletions

View File

@ -203,6 +203,7 @@ void McuDevice::AddMcuAsk(std::shared_ptr<VMcuAsk> &ask)
}
bool McuDevice::SearchMcuAsk(const unsigned int &serialNumber, std::shared_ptr<VMcuAsk> &ask)
{
// LogInfo("SearchMcuAsk serialNumber = %d\n", serialNumber);
std::lock_guard<std::mutex> locker(mMutex);
for (auto iter = mAllAsk.begin(); iter != mAllAsk.end(); ++iter) {
std::shared_ptr<VMcuAsk> listData = *iter;

View File

@ -313,9 +313,9 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDo
std::shared_ptr<McuAskBaseTestTool> testTool = std::dynamic_pointer_cast<McuAskBaseTestTool>(ask);
testTool->McuAskDefaultFeatures(testTool);
StatusCode code = IMcuManager::GetInstance()->SetFeedingCycleForWatchDog(ask, 1, 1, 1);
EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted.
EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed.
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted.
IMcuManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/McuManagerTest

View File

@ -16,6 +16,7 @@
#define MCU_PROTOCOL_TEST_TOOL_H
#include "LinuxApiMock.h"
#include "UartDeviceTestTool.h"
#include <list>
#include <mutex>
#include <thread>
constexpr int READ_PRINT = 0;
@ -52,6 +53,6 @@ private:
bool mThreadRuning;
std::thread mLockThread;
std::thread mUnLockThread;
unsigned int mSerialNumberCheck;
std::list<unsigned int> mSerialNumberList;
};
#endif

View File

@ -30,11 +30,7 @@ unsigned char ASK_SET_FEEDING_CYCLE[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x04, 0x00, 0x0F, 0x01, 0x01, 0x01, 0xA7, 0x9A};
unsigned char REPLY_SET_FEEDING_CYCLE[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x0D, 0x01, 0x52, 0x26};
McuProtocolTestTool::McuProtocolTestTool()
{
mSerialNumberCheck = 0;
mThreadRuning = false;
}
McuProtocolTestTool::McuProtocolTestTool() { mThreadRuning = false; }
void McuProtocolTestTool::Init(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart)
{
int uartFd = GetDeviceMockFd(uart);
@ -60,15 +56,17 @@ void McuProtocolTestTool::UnInit(void)
if (mUnLockThread.joinable()) {
mUnLockThread.join();
}
mSerialNumberList.clear();
}
void McuProtocolTestTool::CheckSerialNumber(const void *buf, const size_t &count)
{
unsigned int serialNumber = 0;
if (count > PROTOCOL_COMMAND_LENGTH + PROTOCOL_CHECK_CODE_LENGTH) {
memcpy(&serialNumber, (unsigned char *)buf + 2, 4);
serialNumber = ntohl(serialNumber);
// EXPECT_EQ(serialNumber, mSerialNumberCheck); // TODO:
mSerialNumberCheck++;
for (auto iter = mSerialNumberList.begin(); iter != mSerialNumberList.end(); ++iter) {
EXPECT_NE(*iter, serialNumber);
}
mSerialNumberList.push_back(serialNumber);
}
}
void McuProtocolTestTool::ChecCRC16Code(const void *buf, const size_t &count)

View File

@ -26,6 +26,7 @@ std::shared_ptr<VProtocolRecv> &VProtocolRecv::GetInstance(std::shared_ptr<VProt
}
const StatusCode McuProtocol::Init(void)
{
ProtocolHandle::Init();
constexpr int THREAD_SHARING = 0;
constexpr int INITIAL_VALUE_OF_SEMAPHORE = 0;
sem_init(&mSem, THREAD_SHARING, INITIAL_VALUE_OF_SEMAPHORE);
@ -43,6 +44,7 @@ const StatusCode McuProtocol::Init(void)
}
const StatusCode McuProtocol::UnInit(void)
{
ProtocolHandle::UnInit();
mThreadRuning = false;
sem_post(&mSem);
if (mDataHandleThread.joinable()) {

View File

@ -201,6 +201,17 @@ bool ProtocolHandle::CheckoutTheCheckCode(const ProtocolPacket &packet)
}
return false;
}
void ProtocolHandle::Init(void)
{
//
mSerialNumber = 1;
}
void ProtocolHandle::UnInit(void)
{
//
mSerialNumber = 1;
}
std::shared_ptr<ProtocolHandle> ProtocolHandle::CreateProtocolData(const std::shared_ptr<VProtocolParam> &param)
{
std::shared_ptr<ProtocolHandle> handle;

View File

@ -142,6 +142,8 @@ protected:
static std::mutex mMutex;
public:
static void Init(void);
static void UnInit(void);
static std::shared_ptr<ProtocolHandle> CreateProtocolData(const std::shared_ptr<VProtocolParam> &param);
static void ProtocolAnalysis(const void *data, const size_t &length);
static size_t GetKeyHeadLength(void);