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);
testTool->McuAskDefaultFeatures(testTool);
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.
};
IMcuManager::GetInstance()->Init();

View File

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

View File

@ -18,6 +18,10 @@
#include <netinet/in.h>
#include <string.h>
#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_COMMAND_LENGTH = 6;
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};
unsigned char REPLY_SET_FEEDING_CYCLE[] = {
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)
{
if (pipe(mPipeFdMockSelect) == 0) {
mPipeFdMockSelectInit = true;
}
else {
LogWarning("pipe failed, mPipeFdMockSelect willn,t work.\n");
}
int uartFd = GetDeviceMockFd(uart);
static size_t WRITE_COUNT = -1;
auto api_write = [=, &mock](int fd, const void *buf, size_t count) {
@ -57,6 +73,13 @@ void McuProtocolTestTool::UnInit(void)
mUnLockThread.join();
}
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)
{
@ -89,13 +112,37 @@ void McuProtocolTestTool::ReplySelectSucceed(std::shared_ptr<LinuxTest> &mock, c
};
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));
if (false == mPipeFdMockSelectInit) {
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, _, _, _, _))
.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)));
PipeSelectTimeoutForProtocolHandleImmediately();
}
void McuProtocolTestTool::IpcMissionProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf,
size_t count)
@ -232,6 +279,20 @@ void McuProtocolTestTool::UnlockThread(void)
std::this_thread::sleep_for(std::chrono::milliseconds(5));
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)
{
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;
ssize_t readLength = 0;
int fs_sel = -1;
fd_set fs_read;
int selectResult = -1;
fd_set fdsRead;
struct timeval time;
if (nullptr == buff) {
@ -84,12 +84,12 @@ const ssize_t UartDeviceImpl::UartRecv(void *buff, const size_t &buffLength, con
return RECV_FAILED;
}
FD_ZERO(&fs_read);
FD_SET(mFd, &fs_read);
FD_ZERO(&fdsRead);
FD_SET(mFd, &fdsRead);
time.tv_sec = timeOutMs / 1000;
time.tv_usec = (timeOutMs % 1000) * 1000;
fs_sel = fx_select(mFd + 1, &fs_read, NULL, NULL, &time);
if (fs_sel) {
selectResult = fx_select(mFd + 1, &fdsRead, NULL, NULL, &time);
if (selectResult) {
std::this_thread::sleep_for(std::chrono::milliseconds(delayReadMs));
readLength = fx_read(mFd, buff, buffLength);
return readLength;