Improve:test code.
This commit is contained in:
parent
980cd6a4ff
commit
b299854b90
|
@ -28,7 +28,6 @@ std::shared_ptr<MissionStateMachine> &MissionStateMachine::GetInstance(std::shar
|
|||
}
|
||||
MissionStateMachine::MissionStateMachine()
|
||||
{
|
||||
//
|
||||
mStartMission = IpcMission::END;
|
||||
}
|
||||
void MissionStateMachine::Init(void)
|
||||
|
|
|
@ -20,7 +20,6 @@ StatusCode HuntingUpgradeImpl::CheckFileHeader(const UpgradeFileHeader &head)
|
|||
}
|
||||
StatusCode HuntingUpgradeImpl::CheckUpgradeFile(void)
|
||||
{
|
||||
LogError("sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\n");
|
||||
UpgradeFileHeader header;
|
||||
StatusCode code = UpgradeBase::CheckUpgradeFile(SD_CARD_MOUNT_PATH APPLICATION_CHECK_PATH, header);
|
||||
if (!IsCodeOK(code)) {
|
||||
|
|
|
@ -40,12 +40,10 @@ ASK_RESULT McuAskBase::Blocking(void)
|
|||
// }
|
||||
bool McuAskBase::NeedReply(void)
|
||||
{
|
||||
//
|
||||
return mNeedReply == McuAskReply::NEED_REPLY ? true : false;
|
||||
}
|
||||
void McuAskBase::ReplyFinished(const bool result)
|
||||
{
|
||||
//
|
||||
mReplyOK = result;
|
||||
sem_post(&mSem);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* 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 "AppManagerTestTool.h"
|
||||
#include "GtestUsing.h"
|
||||
#include "HalTestTool.h"
|
||||
#include "HuntingCameraTest.h"
|
||||
#include "ILog.h"
|
||||
#include "MainThread.h"
|
||||
#include "McuManagerTestTool.h"
|
||||
#include "MissionManagerTestTool.h"
|
||||
#include "TestManager.h"
|
||||
#include <thread>
|
||||
namespace McuManager_Mock_Test
|
||||
{
|
||||
// ../output_files/test/bin/HuntingCameraTest
|
||||
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_EXAMPLE_UartOpenSuccessButReadNothing
|
||||
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_EXAMPLE_UartOpenSuccessButReadNothing)
|
||||
{
|
||||
// SetAllCamerasResult(mAllCamerasMock);
|
||||
// MockReportCameraEvent("/tmp/test.MP4", CameraType::MAIN_CAMERA);
|
||||
// CreateUpgradeFile();
|
||||
McuManagerTestTool::MockMcuDeviceOpenSuccessButReadNothing(mLinuxTest);
|
||||
// std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
MainThread::GetInstance()->Init();
|
||||
TestManager::ResetTimeOut(1000 * 6);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
MainThread::GetInstance()->Runing();
|
||||
RemoveUpgradeFile();
|
||||
}
|
||||
} // namespace McuManager_Mock_Test
|
|
@ -399,6 +399,43 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMission)
|
|||
EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted.
|
||||
IMcuManager::GetInstance()->UnInit();
|
||||
}
|
||||
/**
|
||||
* @brief This use case demonstrates that only the serial port was opened and written successfully, and no data was
|
||||
* read.
|
||||
* Run Test:
|
||||
* ../output_files/test/bin/McuManagerTest --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_GetIpcMission_1
|
||||
*/
|
||||
TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMission_1)
|
||||
{
|
||||
class McuAskTest : public McuAsk<IpcMission>, public McuAskBaseTestTool
|
||||
{
|
||||
public:
|
||||
McuAskTest() : McuAskBaseTestTool(McuAskBlock::BLOCK, McuAskReply::NEED_REPLY)
|
||||
{
|
||||
}
|
||||
virtual ~McuAskTest() = default;
|
||||
void ReplyFinished(const bool result) override
|
||||
{
|
||||
McuAskBaseTestTool::ReplyFinished(result);
|
||||
if (result) {
|
||||
LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast<int>(mDataReply));
|
||||
// Do something here.
|
||||
}
|
||||
else {
|
||||
LogError("Ask data falied.\n");
|
||||
}
|
||||
}
|
||||
};
|
||||
McuManagerTestTool::MockMcuDeviceOpenSuccessButReadNothing(mLinuxTest);
|
||||
IMcuManager::GetInstance()->Init();
|
||||
std::shared_ptr<VMcuAsk> ask = std::make_shared<McuAskTest>();
|
||||
std::shared_ptr<McuAskBaseTestTool> testTool = std::dynamic_pointer_cast<McuAskBaseTestTool>(ask);
|
||||
testTool->McuAskDefaultFeatures(testTool);
|
||||
IMcuManager::GetInstance()->GetIpcMission(ask);
|
||||
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
|
||||
// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_GetIpcMission
|
||||
TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMission2)
|
||||
|
|
|
@ -36,6 +36,7 @@ public:
|
|||
bool CheckAskExist(const std::shared_ptr<VMcuAsk> &ask);
|
||||
void OtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
|
||||
void MockMcuDeviceOpenFailed(std::shared_ptr<LinuxTest> &mock);
|
||||
void MockMcuDeviceOpenSuccessButReadNothing(std::shared_ptr<LinuxTest> &mock);
|
||||
|
||||
private:
|
||||
std::shared_ptr<McuManagerImplTest> mMcuManagerMock;
|
||||
|
|
|
@ -51,4 +51,8 @@ void McuManagerTestTool::OtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock
|
|||
void McuManagerTestTool::MockMcuDeviceOpenFailed(std::shared_ptr<LinuxTest> &mock)
|
||||
{
|
||||
UartDeviceTestTool::SetUartDeviceOpenFailed(mock, gUartDevice);
|
||||
}
|
||||
void McuManagerTestTool::MockMcuDeviceOpenSuccessButReadNothing(std::shared_ptr<LinuxTest> &mock)
|
||||
{
|
||||
McuProtocolTestTool::ReadNothingAnyTime(mock);
|
||||
}
|
|
@ -32,12 +32,14 @@ public:
|
|||
void Init(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart);
|
||||
void UnInit(void);
|
||||
void OtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
|
||||
void ReadNothingAnyTime(std::shared_ptr<LinuxTest> &mock);
|
||||
|
||||
private:
|
||||
void CheckSerialNumber(const void *buf, const size_t &count);
|
||||
void ChecCRC16Code(const void *buf, const size_t &count);
|
||||
void ResetCheckCode(const void *buf, const size_t &count);
|
||||
void ReplySelectSucceed(std::shared_ptr<LinuxTest> &mock, const int &uartFd);
|
||||
void ReplySelectTimeOut(std::shared_ptr<LinuxTest> &mock, const int &uartFd);
|
||||
bool MonitorProtocolPacket(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
|
||||
bool IpcMissionProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
|
||||
void IpcMissionProtocolInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
|
||||
|
@ -72,14 +74,6 @@ private:
|
|||
private:
|
||||
static void PrintHexadecimalData(const void *buf, const size_t &bufLength, const char *log);
|
||||
|
||||
private:
|
||||
// virtual void MonitorWriteProtocolData(const short &head, const unsigned int
|
||||
// &serialNumber, const short &command,
|
||||
// const void *data, const short
|
||||
// &packetLength)
|
||||
// {
|
||||
// }
|
||||
|
||||
private:
|
||||
std::mutex mMutex;
|
||||
bool mThreadRuning;
|
||||
|
@ -89,6 +83,7 @@ private:
|
|||
bool mPipeFdMockSelectInit;
|
||||
int mPipeFdMockSelect[2];
|
||||
std::list<ProtocolHandleFunc> mProtocolHandle;
|
||||
std::shared_ptr<UartInfo> mUart;
|
||||
int mUartFd;
|
||||
};
|
||||
#endif
|
|
@ -95,6 +95,7 @@ void McuProtocolTestTool::Init(std::shared_ptr<LinuxTest> &mock, const UartInfo
|
|||
LogWarning("pipe failed, mPipeFdMockSelect willn,t work.\n");
|
||||
}
|
||||
int uartFd = GetDeviceMockFd(uart);
|
||||
mUart = std::make_shared<UartInfo>(uart);
|
||||
mUartFd = uartFd;
|
||||
static size_t WRITE_COUNT = -1;
|
||||
auto api_write = [=, &mock](int fd, const void *buf, size_t count) {
|
||||
|
@ -136,6 +137,18 @@ void McuProtocolTestTool::OtherSideAskIpcMission(std::shared_ptr<LinuxTest> &moc
|
|||
{
|
||||
OtherSideAskIpcMissionHandle(mock, mUartFd, serialNumber);
|
||||
}
|
||||
void McuProtocolTestTool::ReadNothingAnyTime(std::shared_ptr<LinuxTest> &mock)
|
||||
{
|
||||
static size_t WRITE_COUNT = -1;
|
||||
EXPECT_CALL(*mock.get(), fx_write(mUartFd, _, _))
|
||||
.WillRepeatedly(DoAll(SaveArg<2>(&WRITE_COUNT), ReturnPointee(&WRITE_COUNT)));
|
||||
if (mUart) {
|
||||
UartDeviceTestTool::SetUartDeviceReadNothing(mock, *mUart.get());
|
||||
}
|
||||
else {
|
||||
LogWarning("mUart is null.\n");
|
||||
}
|
||||
}
|
||||
void McuProtocolTestTool::CheckSerialNumber(const void *buf, const size_t &count)
|
||||
{
|
||||
unsigned int serialNumber = 0;
|
||||
|
@ -203,6 +216,15 @@ void McuProtocolTestTool::ReplySelectSucceed(std::shared_ptr<LinuxTest> &mock, c
|
|||
.WillRepeatedly(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectTimeOut)), Return(MOCK_SELECT_TIME_OUT)));
|
||||
PipeSelectTimeoutForProtocolHandleImmediately();
|
||||
}
|
||||
void McuProtocolTestTool::ReplySelectTimeOut(std::shared_ptr<LinuxTest> &mock, const int &uartFd)
|
||||
{
|
||||
if (mUart) {
|
||||
UartDeviceTestTool::SetUartDeviceTimeOut(mock, *mUart.get());
|
||||
}
|
||||
else {
|
||||
LogWarning("mUart is null.\n");
|
||||
}
|
||||
}
|
||||
bool McuProtocolTestTool::MonitorProtocolPacket(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf,
|
||||
size_t count)
|
||||
{
|
||||
|
|
|
@ -68,6 +68,20 @@ public:
|
|||
* @param uart
|
||||
*/
|
||||
void SetUartDeviceOpenFailed(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart);
|
||||
/**
|
||||
* @brief Set the Uart Device Read Nothing object
|
||||
*
|
||||
* @param mock
|
||||
* @param uart
|
||||
*/
|
||||
void SetUartDeviceReadNothing(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart);
|
||||
/**
|
||||
* @brief Set the Uart Device Time Out object
|
||||
*
|
||||
* @param mock
|
||||
* @param uart
|
||||
*/
|
||||
void SetUartDeviceTimeOut(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart);
|
||||
|
||||
private:
|
||||
std::map<const char *, int> mDeviceMap;
|
||||
|
|
|
@ -110,4 +110,21 @@ void UartDeviceTestTool::SetUartDeviceOpenFailed(std::shared_ptr<LinuxTest> &moc
|
|||
{
|
||||
LogInfo("SetUartDeviceOpenFailed[%s], will return -1\n", uart.mDevice);
|
||||
EXPECT_CALL(*mock.get(), fx_open(uart.mDevice, _)).WillRepeatedly(DoAll((Return(-1))));
|
||||
}
|
||||
void UartDeviceTestTool::SetUartDeviceReadNothing(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart)
|
||||
{
|
||||
SetUartDeviceTimeOut(mock, uart);
|
||||
int uartFd = mDeviceMap[uart.mDevice];
|
||||
EXPECT_CALL(*mock.get(), fx_read(uartFd, _, _)).WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING)));
|
||||
}
|
||||
void UartDeviceTestTool::SetUartDeviceTimeOut(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart)
|
||||
{
|
||||
int uartFd = mDeviceMap[uart.mDevice];
|
||||
auto selectTimeOut =
|
||||
[=, &mock](int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) {
|
||||
long long timeMs = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(timeMs));
|
||||
};
|
||||
EXPECT_CALL(*mock.get(), fx_select(uartFd + 1, _, _, _, _))
|
||||
.WillRepeatedly(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectTimeOut)), Return(MOCK_SELECT_TIME_OUT)));
|
||||
}
|
Loading…
Reference in New Issue
Block a user