Improve:McuManager select mock.

This commit is contained in:
Fancy code 2024-02-10 07:15:29 -08:00
parent e3dc20d7a4
commit 4627d47f50
4 changed files with 74 additions and 9 deletions

View File

@ -409,6 +409,7 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_STRESS_MultiThreadWrite)
std::shared_ptr<McuAskBaseTestTool> testTool = std::dynamic_pointer_cast<McuAskBaseTestTool>(ask); std::shared_ptr<McuAskBaseTestTool> testTool = std::dynamic_pointer_cast<McuAskBaseTestTool>(ask);
testTool->McuAskDefaultFeatures(testTool); testTool->McuAskDefaultFeatures(testTool);
IMcuManager::GetInstance()->GetIpcMission(ask); IMcuManager::GetInstance()->GetIpcMission(ask);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
EXPECT_EQ(test->CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. EXPECT_EQ(test->CheckAskExist(ask), false); // Ensure that the request has been processed and deleted.
}; };
IMcuManager::GetInstance()->Init(); IMcuManager::GetInstance()->Init();

View File

@ -44,6 +44,7 @@ private:
void LockProtocolHandle(void); void LockProtocolHandle(void);
void UnlockProtocolHandle(void); void UnlockProtocolHandle(void);
void UnlockThread(void); void UnlockThread(void);
void PipeSelectTimeoutForProtocolHandleImmediately(void);
private: private:
static void PrintHexadecimalData(const void *buf, const size_t &bufLength, const int event); static void PrintHexadecimalData(const void *buf, const size_t &bufLength, const int event);
@ -54,5 +55,7 @@ private:
std::thread mLockThread; std::thread mLockThread;
std::thread mUnLockThread; std::thread mUnLockThread;
std::list<unsigned int> mSerialNumberList; std::list<unsigned int> mSerialNumberList;
bool mPipeFdMockSelectInit;
int mPipeFdMockSelect[2];
}; };
#endif #endif

View File

@ -18,6 +18,10 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <string.h> #include <string.h>
#include <thread> #include <thread>
#include <unistd.h>
constexpr int PIPE_READ_FD_INDEX = 0;
constexpr int PIPE_WRITE_FD_INDEX = 1;
const char *gPipeBuf = "nothing";
constexpr size_t PROTOCOL_DATA_KEY_HEAD_LENGTH = 10; constexpr size_t PROTOCOL_DATA_KEY_HEAD_LENGTH = 10;
constexpr size_t PROTOCOL_COMMAND_LENGTH = 6; constexpr size_t PROTOCOL_COMMAND_LENGTH = 6;
constexpr size_t PROTOCOL_CHECK_CODE_LENGTH = sizeof(short); constexpr size_t PROTOCOL_CHECK_CODE_LENGTH = sizeof(short);
@ -30,9 +34,21 @@ unsigned char ASK_SET_FEEDING_CYCLE[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x04, 0x00, 0x0F, 0x01, 0x01, 0x01, 0xA7, 0x9A}; 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x04, 0x00, 0x0F, 0x01, 0x01, 0x01, 0xA7, 0x9A};
unsigned char REPLY_SET_FEEDING_CYCLE[] = { unsigned char REPLY_SET_FEEDING_CYCLE[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x0D, 0x01, 0x52, 0x26}; 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x0D, 0x01, 0x52, 0x26};
McuProtocolTestTool::McuProtocolTestTool() { mThreadRuning = false; } McuProtocolTestTool::McuProtocolTestTool()
{
mThreadRuning = false;
mPipeFdMockSelectInit = false;
mPipeFdMockSelect[PIPE_READ_FD_INDEX] = -1;
mPipeFdMockSelect[PIPE_WRITE_FD_INDEX] = -1;
}
void McuProtocolTestTool::Init(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart) void McuProtocolTestTool::Init(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart)
{ {
if (pipe(mPipeFdMockSelect) == 0) {
mPipeFdMockSelectInit = true;
}
else {
LogWarning("pipe failed, mPipeFdMockSelect willn,t work.\n");
}
int uartFd = GetDeviceMockFd(uart); int uartFd = GetDeviceMockFd(uart);
static size_t WRITE_COUNT = -1; static size_t WRITE_COUNT = -1;
auto api_write = [=, &mock](int fd, const void *buf, size_t count) { auto api_write = [=, &mock](int fd, const void *buf, size_t count) {
@ -57,6 +73,13 @@ void McuProtocolTestTool::UnInit(void)
mUnLockThread.join(); mUnLockThread.join();
} }
mSerialNumberList.clear(); mSerialNumberList.clear();
if (mPipeFdMockSelectInit) {
close(mPipeFdMockSelect[PIPE_READ_FD_INDEX]);
close(mPipeFdMockSelect[PIPE_WRITE_FD_INDEX]);
mPipeFdMockSelect[PIPE_READ_FD_INDEX] = -1;
mPipeFdMockSelect[PIPE_WRITE_FD_INDEX] = -1;
}
mPipeFdMockSelectInit = false;
} }
void McuProtocolTestTool::CheckSerialNumber(const void *buf, const size_t &count) void McuProtocolTestTool::CheckSerialNumber(const void *buf, const size_t &count)
{ {
@ -89,13 +112,37 @@ void McuProtocolTestTool::ReplySelectSucceed(std::shared_ptr<LinuxTest> &mock, c
}; };
auto selectTimeOut = auto selectTimeOut =
[=, &mock](int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) { [=, &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; if (false == mPipeFdMockSelectInit) {
std::this_thread::sleep_for(std::chrono::milliseconds(timeMs)); long long timeMs = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
std::this_thread::sleep_for(std::chrono::milliseconds(timeMs));
}
else {
constexpr int READ_BUF_LENGTH = 256;
constexpr int READ_FAILD = -1;
char buf[READ_BUF_LENGTH] = {0};
int selectResult = -1;
fd_set fdsRead;
FD_ZERO(&fdsRead);
FD_SET(mPipeFdMockSelect[PIPE_READ_FD_INDEX], &fdsRead);
selectResult = select(mPipeFdMockSelect[PIPE_READ_FD_INDEX] + 1, &fdsRead, NULL, NULL, timeout);
if (selectResult) {
// Do nothing here.
ssize_t length = read(mPipeFdMockSelect[PIPE_READ_FD_INDEX], buf, READ_BUF_LENGTH);
if (READ_FAILD == length) {
LogError("mPipeFdMockSelect failed.\n");
return;
}
if ((size_t)length != strlen(gPipeBuf)) {
LogWarning("Something wrong happened.\n");
}
}
}
}; };
EXPECT_CALL(*mock.get(), fx_select(uartFd + 1, _, _, _, _)) EXPECT_CALL(*mock.get(), fx_select(uartFd + 1, _, _, _, _))
.WillOnce(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectReadable)), Return(1))) .WillOnce(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectReadable)), Return(1)))
.WillOnce(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectReadable)), Return(1))) .WillOnce(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectReadable)), Return(1)))
.WillRepeatedly(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectTimeOut)), Return(MOCK_SELECT_TIME_OUT))); .WillRepeatedly(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectTimeOut)), Return(MOCK_SELECT_TIME_OUT)));
PipeSelectTimeoutForProtocolHandleImmediately();
} }
void McuProtocolTestTool::IpcMissionProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, void McuProtocolTestTool::IpcMissionProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf,
size_t count) size_t count)
@ -232,6 +279,20 @@ void McuProtocolTestTool::UnlockThread(void)
std::this_thread::sleep_for(std::chrono::milliseconds(5)); std::this_thread::sleep_for(std::chrono::milliseconds(5));
mMutex.unlock(); mMutex.unlock();
} }
void McuProtocolTestTool::PipeSelectTimeoutForProtocolHandleImmediately(void)
{
constexpr int WRITE_FAILD = -1;
if (true == mPipeFdMockSelectInit) {
ssize_t length = write(mPipeFdMockSelect[PIPE_WRITE_FD_INDEX], gPipeBuf, strlen(gPipeBuf));
if (WRITE_FAILD == length) {
LogError("mPipeFdMockSelect failed.\n");
return;
}
if ((size_t)length != strlen(gPipeBuf)) {
LogWarning("Something wrong happened.\n");
}
}
}
void McuProtocolTestTool::PrintHexadecimalData(const void *buf, const size_t &bufLength, const int event) void McuProtocolTestTool::PrintHexadecimalData(const void *buf, const size_t &bufLength, const int event)
{ {
if (WRITE_PRINT == event) { if (WRITE_PRINT == event) {

View File

@ -71,8 +71,8 @@ const ssize_t UartDeviceImpl::UartRecv(void *buff, const size_t &buffLength, con
{ {
constexpr size_t RECV_FAILED = -1; constexpr size_t RECV_FAILED = -1;
ssize_t readLength = 0; ssize_t readLength = 0;
int fs_sel = -1; int selectResult = -1;
fd_set fs_read; fd_set fdsRead;
struct timeval time; struct timeval time;
if (nullptr == buff) { if (nullptr == buff) {
@ -84,12 +84,12 @@ const ssize_t UartDeviceImpl::UartRecv(void *buff, const size_t &buffLength, con
return RECV_FAILED; return RECV_FAILED;
} }
FD_ZERO(&fs_read); FD_ZERO(&fdsRead);
FD_SET(mFd, &fs_read); FD_SET(mFd, &fdsRead);
time.tv_sec = timeOutMs / 1000; time.tv_sec = timeOutMs / 1000;
time.tv_usec = (timeOutMs % 1000) * 1000; time.tv_usec = (timeOutMs % 1000) * 1000;
fs_sel = fx_select(mFd + 1, &fs_read, NULL, NULL, &time); selectResult = fx_select(mFd + 1, &fdsRead, NULL, NULL, &time);
if (fs_sel) { if (selectResult) {
std::this_thread::sleep_for(std::chrono::milliseconds(delayReadMs)); std::this_thread::sleep_for(std::chrono::milliseconds(delayReadMs));
readLength = fx_read(mFd, buff, buffLength); readLength = fx_read(mFd, buff, buffLength);
return readLength; return readLength;