Improve:McuManager TestTool code.

This commit is contained in:
Fancy code 2024-02-08 14:18:42 -08:00
parent 92a62a1db6
commit 40310496e9
18 changed files with 183 additions and 10 deletions

View File

@ -17,6 +17,7 @@
#include "StatusCode.h"
#include <memory>
bool CreateMcuManager(void);
bool DestroyMcuManager(void);
enum class IpcMission
{
TEST = 0,
@ -84,5 +85,9 @@ public:
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
virtual const StatusCode FeedWatchDog(std::shared_ptr<VMcuAsk> &ask)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
};
#endif

View File

@ -206,7 +206,7 @@ void McuDevice::DeleteMcuAsk(std::shared_ptr<VMcuAsk> &ask)
std::lock_guard<std::mutex> locker(mMutex);
auto searchMcuAsk = [&ask](std::shared_ptr<VMcuAsk> &askList) -> bool {
if (ask->mSerialNumber == askList->mSerialNumber) {
// LogInfo("DeleteMcuAsk mSerialNumber = %d\n", askList->mSerialNumber);
LogInfo("DeleteMcuAsk mSerialNumber = %d\n", askList->mSerialNumber);
return REMOVE_THE_ASK;
}
return KEEP_THE_ASK;

View File

@ -29,12 +29,15 @@ const StatusCode McuManagerImpl::UnInit(void)
const StatusCode McuManagerImpl::GetIpcMission(std::shared_ptr<VMcuAsk> &ask)
{
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
McuProtocol::GetIpcMission(context);
return CreateStatusCode(STATUS_CODE_OK);
return McuProtocol::GetIpcMission(context);
}
const StatusCode McuManagerImpl::CutOffPowerSupply(std::shared_ptr<VMcuAsk> &ask)
{
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
McuProtocol::CutOffPowerSupply(context);
return CreateStatusCode(STATUS_CODE_OK);
return McuProtocol::CutOffPowerSupply(context);
}
const StatusCode McuManagerImpl::FeedWatchDog(std::shared_ptr<VMcuAsk> &ask)
{
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
return McuProtocol::FeedWatchDog(context);
}

View File

@ -27,5 +27,6 @@ public:
const StatusCode UnInit(void) override;
const StatusCode GetIpcMission(std::shared_ptr<VMcuAsk> &ask) override;
const StatusCode CutOffPowerSupply(std::shared_ptr<VMcuAsk> &ask) override;
const StatusCode FeedWatchDog(std::shared_ptr<VMcuAsk> &ask) override;
};
#endif

View File

@ -26,6 +26,12 @@ bool CreateMcuManager(void)
}
return false;
}
bool DestroyMcuManager(void)
{
auto instance = std::make_shared<IMcuManager>();
IMcuManager::GetInstance(&instance);
return true;
}
std::shared_ptr<McuManagerMakePtr> &McuManagerMakePtr::GetInstance(std::shared_ptr<McuManagerMakePtr> *impl)
{
static auto instance = std::make_shared<McuManagerMakePtr>();

View File

@ -9,7 +9,9 @@ include_directories(
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/UartDevice/include
${UTILS_SOURCE_PATH}/McuProtocol/include
${MIDDLEWARE_SOURCE_PATH}/McuManager/include
${MIDDLEWARE_SOURCE_PATH}/McuManager/src
${MIDDLEWARE_SOURCE_PATH}/McuAskBase/include
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${TEST_SOURCE_PATH}/utils/UartDevice/tool/include

View File

@ -17,7 +17,6 @@ public:
static void SetUpTestCase()
{
CreateLogModule();
CreateMcuManager();
ILogInit(LOG_INSTANCE_TYPE_END);
}
static void TearDownTestCase() { ILogUnInit(); }
@ -28,6 +27,7 @@ public:
LinuxApiMock::GetInstance(&test);
LinuxApiMock::GetInstance()->Init();
McuManagerTestTool::Init(mLinuxTest);
CreateMcuManager();
}
virtual void TearDown()
{
@ -36,6 +36,7 @@ public:
std::shared_ptr<LinuxApiMock> test = std::make_shared<LinuxApiMock>();
LinuxApiMock::GetInstance(&test);
McuManagerTestTool::UnInit();
DestroyMcuManager();
}
public:
@ -163,8 +164,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMission)
std::shared_ptr<McuAskBaseTestTool> testTool = std::dynamic_pointer_cast<McuAskBaseTestTool>(ask);
testTool->McuAskDefaultFeatures(testTool);
IMcuManager::GetInstance()->GetIpcMission(ask);
constexpr int NO_USER_USING_AFTER_ASK = 2; // ask and testTool
EXPECT_EQ(ask.use_count(), NO_USER_USING_AFTER_ASK);
constexpr int NO_USER_USING_AFTER_ASK = 2; // ask and testTool
EXPECT_EQ(ask.use_count(), NO_USER_USING_AFTER_ASK); // TODO: neet to improve.
IMcuManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/McuManagerTest

View File

@ -7,6 +7,8 @@ include_directories(
./include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/McuProtocol/include
${MIDDLEWARE_SOURCE_PATH}/McuManager/src
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include
)

View File

@ -15,7 +15,46 @@
#ifndef MCU_MANAGER_TEST_TOOL_H
#define MCU_MANAGER_TEST_TOOL_H
#include "LinuxApiMock.h"
#include "McuManagerImpl.h"
#include "McuProtocolTestTool.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using ::testing::_;
using ::testing::Action;
using ::testing::ActionInterface;
using ::testing::AnyNumber;
using ::testing::Assign;
using ::testing::AtLeast;
using ::testing::ByMove;
using ::testing::ByRef;
using ::testing::DefaultValue;
using ::testing::DoAll;
using ::testing::DoDefault;
using ::testing::IgnoreResult;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::MakePolymorphicAction;
using ::testing::PolymorphicAction;
using ::testing::Return;
using ::testing::ReturnNew;
using ::testing::ReturnNull;
using ::testing::ReturnPointee;
using ::testing::ReturnRef;
using ::testing::ReturnRefOfCopy;
using ::testing::ReturnRoundRobin;
using ::testing::SaveArg;
using ::testing::SetArgPointee;
using ::testing::SetArgumentPointee;
using ::testing::Unused;
using ::testing::WithArgs;
using ::testing::internal::BuiltInDefaultValue;
class McuManagerImplTest : public McuManagerImpl
{
public:
McuManagerImplTest() = default;
virtual ~McuManagerImplTest() = default;
MOCK_METHOD1(DeleteMcuAsk, void(std::shared_ptr<VMcuAsk> &));
};
class McuManagerTestTool : virtual public McuProtocolTestTool
{
public:
@ -23,5 +62,8 @@ public:
virtual ~McuManagerTestTool() = default;
void Init(std::shared_ptr<LinuxTest> &mock);
void UnInit(void);
private:
std::shared_ptr<McuManagerImplTest> mMcuManagerMock;
};
#endif

View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "McuManagerMakePtrTest.h"
#include "ILog.h"
#include "McuManagerTestTool.h"
void OverrideMcuManagerMakePtrObject(void)
{
std::shared_ptr<McuManagerMakePtr> impl = std::make_shared<McuManagerMakePtrTest>();
McuManagerMakePtr::GetInstance(&impl);
}
McuManagerMakePtrTest::McuManagerMakePtrTest()
{
//
}
McuManagerMakePtrTest::~McuManagerMakePtrTest()
{
//
mMcuManagerMock.reset();
}
const StatusCode McuManagerMakePtrTest::CreateMcuManager(std::shared_ptr<IMcuManager> &impl)
{
if (mMcuManagerMock) {
impl = mMcuManagerMock;
}
else {
LogWarning("CreateMcuManager failed:mMcuManagerMock is nullptr.\n");
}
return CreateStatusCode(STATUS_CODE_OK);
}

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MCU_MANAGER_MAKE_PTR_TEST_H
#define MCU_MANAGER_MAKE_PTR_TEST_H
#include "McuManagerMakePtr.h"
#include "McuManagerTestTool.h"
void OverrideMcuManagerMakePtrObject(void);
class McuManagerMakePtrTest : public McuManagerMakePtr
{
public:
McuManagerMakePtrTest();
virtual ~McuManagerMakePtrTest();
const StatusCode CreateMcuManager(std::shared_ptr<IMcuManager> &impl) override;
public:
public:
std::shared_ptr<McuManagerImplTest> mMcuManagerMock;
};
#endif

View File

@ -14,6 +14,7 @@
*/
#include "McuManagerTestTool.h"
#include "ILog.h"
#include "McuManagerMakePtrTest.h"
extern const char *MCU_UART_DEVICE_PTR;
static UartInfo gUartDevice = {
MCU_UART_DEVICE_PTR,
@ -26,11 +27,26 @@ static UartInfo gUartDevice = {
void McuManagerTestTool::Init(std::shared_ptr<LinuxTest> &mock)
{
LogInfo("McuManagerTestTool::Init\n");
mMcuManagerMock = std::make_shared<McuManagerImplTest>();
EXPECT_CALL(*mMcuManagerMock.get(), DeleteMcuAsk(_)).Times(500);
OverrideMcuManagerMakePtrObject();
std::shared_ptr<McuManagerMakePtr> tmp = McuManagerMakePtr::GetInstance();
std::shared_ptr<McuManagerMakePtrTest> test = std::dynamic_pointer_cast<McuManagerMakePtrTest>(tmp);
if (test) {
test->mMcuManagerMock = mMcuManagerMock;
}
UartDeviceTestTool::RegisterUartDevice(mock, gUartDevice);
McuProtocolTestTool::Init(mock, gUartDevice);
}
void McuManagerTestTool::UnInit(void)
{
//
mMcuManagerMock.reset();
std::shared_ptr<McuManagerMakePtr> tmp = McuManagerMakePtr::GetInstance();
std::shared_ptr<McuManagerMakePtrTest> test = std::dynamic_pointer_cast<McuManagerMakePtrTest>(tmp);
if (test) {
test->mMcuManagerMock.reset();
}
std::shared_ptr<McuManagerMakePtr> impl = std::make_shared<McuManagerMakePtr>();
McuManagerMakePtr::GetInstance(&impl);
UartDeviceTestTool::UnregisterUartDevice(gUartDevice);
}

View File

@ -92,6 +92,7 @@ public:
const StatusCode UnInit(void);
const StatusCode GetIpcMission(std::shared_ptr<VProtocolContext> &context);
const StatusCode CutOffPowerSupply(std::shared_ptr<VProtocolContext> &context);
const StatusCode FeedWatchDog(std::shared_ptr<VProtocolContext> &context);
void DataHandleThread(void);
protected:

View File

@ -33,6 +33,7 @@ void LittleEndianHandle::BigEndianConversion(ProtocolPacket &packet)
packet.mCheckCode = htons(packet.mCheckCode);
packet.mSerialNumber = htonl(packet.mSerialNumber);
}
short LittleEndianHandle::BigEndianConversion(const short &number) { return htons(number); }
void LittleEndianHandle::HostByteOrderConversion(ProtocolPacket &packet)
{
packet.mHead = ntohs(packet.mHead);

View File

@ -24,6 +24,7 @@ public:
private:
void BigEndianConversion(ProtocolPacket &packet) override;
short BigEndianConversion(const short &number) override;
void HostByteOrderConversion(ProtocolPacket &packet) override;
bool CheckoutTheCheckCode(const ProtocolPacket &packet) override;
};

View File

@ -48,6 +48,8 @@ const StatusCode McuProtocol::UnInit(void)
if (mDataHandleThread.joinable()) {
mDataHandleThread.join();
}
std::shared_ptr<VProtocolRecv> reply = std::make_shared<VProtocolRecv>();
VProtocolRecv::GetInstance(&reply);
sem_destroy(&mSem);
return CreateStatusCode(STATUS_CODE_OK);
}
@ -66,6 +68,13 @@ const StatusCode McuProtocol::CutOffPowerSupply(std::shared_ptr<VProtocolContext
return WriteProtocolData(
handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), context, handle->GetSerialNumber());
}
const StatusCode McuProtocol::FeedWatchDog(std::shared_ptr<VProtocolContext> &context)
{
std::shared_ptr<VProtocolParam> param = std::make_shared<VProtocolParam>(PROTOCOL_COMMAND::ASK_FEED_WATCH_DOG);
std::shared_ptr<ProtocolHandle> handle = ProtocolHandle::CreateProtocolData(param);
return WriteProtocolData(
handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), context, handle->GetSerialNumber());
}
void McuProtocol::DataHandleThread(void)
{
mThreadRuning = true;

View File

@ -81,7 +81,7 @@ void ProtocolHandle::MakeNoUserDataPacket(const std::shared_ptr<VProtocolParam>
BigEndianConversion(packet);
memcpy(mProtocolData, &packet, KEY_HEAD_LENGTH);
packet.mCheckCode = calculate_check_sum(mProtocolData, dataLength - sizeof(short));
packet.mCheckCode = htons(packet.mCheckCode);
packet.mCheckCode = BigEndianConversion(packet.mCheckCode);
memcpy(mProtocolData + KEY_HEAD_LENGTH, &packet.mCheckCode, sizeof(short));
mProtocolDataLength = dataLength;
}
@ -93,6 +93,10 @@ void ProtocolHandle::MakeAskCutOffPowerSupplyPacket(const std::shared_ptr<VProto
{
MakeNoUserDataPacket(param);
}
void ProtocolHandle::MakeAskFeedWatchDogPacket(const std::shared_ptr<VProtocolParam> &param)
{
MakeNoUserDataPacket(param);
}
void ProtocolHandle::AnalyzeProtocolPacket(void)
{
ProtocolPacket packet = {0};

View File

@ -36,6 +36,7 @@ enum PROTOCOL_COMMAND
ASK_IPC_MISSION = 0x8101,
REPLY_IPC_MISSION = 0x0101,
ASK_CUT_OFF_PWOER_SUPPLY = 0x8102,
ASK_FEED_WATCH_DOG = 0x8103,
PROTOCOL_COMMAND_END
};
class VProtocolParam
@ -62,6 +63,10 @@ public:
};
using MakePacketFunc = std::function<void(const std::shared_ptr<VProtocolParam> &)>;
using AnalyzePacketFunc = std::function<void(const ProtocolPacket &)>;
/**
* @brief Protocol processing classes need to focus on data size issues, and ProtocolHandle defaults to handling large
* end data.
*/
class ProtocolHandle
{
public:
@ -81,6 +86,7 @@ private:
void MakeNoUserDataPacket(const std::shared_ptr<VProtocolParam> &param);
void MakeAskIpcMissionPacket(const std::shared_ptr<VProtocolParam> &param);
void MakeAskCutOffPowerSupplyPacket(const std::shared_ptr<VProtocolParam> &param);
void MakeAskFeedWatchDogPacket(const std::shared_ptr<VProtocolParam> &param);
/**
* @brief These function implementations parse the received frame by frame continuous data into the data required by
@ -92,6 +98,7 @@ private:
private:
virtual void BigEndianConversion(ProtocolPacket &packet) {}
virtual short BigEndianConversion(const short &number) { return number; }
virtual void HostByteOrderConversion(ProtocolPacket &packet) {}
virtual bool CheckoutTheCheckCode(const ProtocolPacket &packet);