From e3dc20d7a4668e9c25d3b81de4a3e2c0b87d7f8f Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Sat, 10 Feb 2024 03:24:07 -0800 Subject: [PATCH] Improve:McuManager test code. --- middleware/McuManager/src/McuDevice.cpp | 1 + .../McuManager/src_mock/McuManager_Mock_Test.cpp | 4 ++-- .../McuProtocol/tool/include/McuProtocolTestTool.h | 3 ++- .../McuProtocol/tool/src/McuProtocolTestTool.cpp | 14 ++++++-------- utils/McuProtocol/src/McuProtocol.cpp | 2 ++ utils/McuProtocol/src/ProtocolHandle.cpp | 11 +++++++++++ utils/McuProtocol/src/ProtocolHandle.h | 2 ++ 7 files changed, 26 insertions(+), 11 deletions(-) diff --git a/middleware/McuManager/src/McuDevice.cpp b/middleware/McuManager/src/McuDevice.cpp index c7c250d0..dd2b6c9c 100644 --- a/middleware/McuManager/src/McuDevice.cpp +++ b/middleware/McuManager/src/McuDevice.cpp @@ -203,6 +203,7 @@ void McuDevice::AddMcuAsk(std::shared_ptr &ask) } bool McuDevice::SearchMcuAsk(const unsigned int &serialNumber, std::shared_ptr &ask) { + // LogInfo("SearchMcuAsk serialNumber = %d\n", serialNumber); std::lock_guard locker(mMutex); for (auto iter = mAllAsk.begin(); iter != mAllAsk.end(); ++iter) { std::shared_ptr listData = *iter; diff --git a/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp b/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp index 6060b1ff..54f66fef 100644 --- a/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp +++ b/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp @@ -313,9 +313,9 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDo std::shared_ptr testTool = std::dynamic_pointer_cast(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 diff --git a/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h b/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h index f063f681..704f4e7b 100644 --- a/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h +++ b/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h @@ -16,6 +16,7 @@ #define MCU_PROTOCOL_TEST_TOOL_H #include "LinuxApiMock.h" #include "UartDeviceTestTool.h" +#include #include #include constexpr int READ_PRINT = 0; @@ -52,6 +53,6 @@ private: bool mThreadRuning; std::thread mLockThread; std::thread mUnLockThread; - unsigned int mSerialNumberCheck; + std::list mSerialNumberList; }; #endif \ No newline at end of file diff --git a/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp b/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp index 47e7eb9e..ccb9d85e 100644 --- a/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp +++ b/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp @@ -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 &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) diff --git a/utils/McuProtocol/src/McuProtocol.cpp b/utils/McuProtocol/src/McuProtocol.cpp index 5877b3fa..4842225e 100644 --- a/utils/McuProtocol/src/McuProtocol.cpp +++ b/utils/McuProtocol/src/McuProtocol.cpp @@ -26,6 +26,7 @@ std::shared_ptr &VProtocolRecv::GetInstance(std::shared_ptr ProtocolHandle::CreateProtocolData(const std::shared_ptr ¶m) { std::shared_ptr handle; diff --git a/utils/McuProtocol/src/ProtocolHandle.h b/utils/McuProtocol/src/ProtocolHandle.h index 07c7bf66..b7da52fc 100644 --- a/utils/McuProtocol/src/ProtocolHandle.h +++ b/utils/McuProtocol/src/ProtocolHandle.h @@ -142,6 +142,8 @@ protected: static std::mutex mMutex; public: + static void Init(void); + static void UnInit(void); static std::shared_ptr CreateProtocolData(const std::shared_ptr ¶m); static void ProtocolAnalysis(const void *data, const size_t &length); static size_t GetKeyHeadLength(void);