fix(canbus): remove obsolete SocketCAN raw test

This commit is contained in:
lgv 2026-11-19 18:59:59 +08:00
parent 55736f58ff
commit 0ebce4d4bb
2 changed files with 0 additions and 111 deletions

View File

@ -17,22 +17,6 @@ install(TARGETS canbus LIBRARY DESTINATION lib)
# --------------------------------------------------------
# Unit test
# --------------------------------------------------------
add_executable(socket_can_client_raw_test
${CMAKE_CURRENT_SOURCE_DIR}/can_client/socket/socket_can_client_raw_test.cc
)
target_link_libraries(socket_can_client_raw_test
PRIVATE
canbus
gtest
gtest_main
pthread
glog
cmvr_es::proto
)
add_executable(protocol_data_test
${CMAKE_CURRENT_SOURCE_DIR}/can_comm/protocol_data_test.cc
)

View File

@ -1,95 +0,0 @@
#include "canbus/can_client/socket/socket_can_client_raw.h"
#include "canbus/can_comm/can_receiver.h"
#include "cmvr/msgs/robot_detail.pb.h"
#include "gtest/gtest.h"
#include <chrono>
#include <thread>
namespace cmvr::device {
// Test the production receive/timeout/stop paths without a CAN adapter,
// motor commands, or privileged vcan configuration.
class SocketCanClientRawTestPeer {
public:
static bool attach(SocketCanClientRaw& client, int fd) {
if (!client.configureReceiveTimeout(fd)) return false;
client.dev_handler_ = fd;
client.is_started_ = true;
return true;
}
};
class SocketCanReceiveTest : public testing::Test {
protected:
config::SocketCanConfig config;
SocketCanClientRaw client{config};
int peer = -1;
void SetUp() override {
int sockets[2];
ASSERT_EQ(::socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets), 0);
peer = sockets[1];
ASSERT_TRUE(SocketCanClientRawTestPeer::attach(client, sockets[0]));
}
void TearDown() override {
client.stop();
if (peer >= 0) ::close(peer);
}
};
TEST_F(SocketCanReceiveTest, IdleReceiveReturnsZeroWithoutAnError) {
std::vector<CanFrame> frames(1);
int32_t count = MAX_CAN_RECV_FRAME_LEN;
const auto start = std::chrono::steady_clock::now();
EXPECT_EQ(client.receive(&frames, &count), msgs::ErrorCode::OK);
EXPECT_EQ(count, 0);
EXPECT_TRUE(frames.empty());
EXPECT_LT(std::chrono::steady_clock::now() - start, std::chrono::milliseconds(500));
}
TEST_F(SocketCanReceiveTest, ReceivedFrameIsReturnedWithActualCount) {
can_frame input{};
input.can_id = 0x141;
input.can_dlc = 8;
input.data[0] = 0x9A;
ASSERT_EQ(::write(peer, &input, sizeof(input)), sizeof(input));
std::vector<CanFrame> frames;
int32_t count = MAX_CAN_RECV_FRAME_LEN;
ASSERT_EQ(client.receive(&frames, &count), msgs::ErrorCode::OK);
ASSERT_EQ(count, 1);
ASSERT_EQ(frames.size(), 1U);
EXPECT_EQ(frames[0].id, 0x141);
EXPECT_EQ(frames[0].len, 8);
EXPECT_EQ(frames[0].data[0], 0x9A);
}
TEST_F(SocketCanReceiveTest, ReceiverStopsOnAnIdleBusAndCanRestart) {
MessageManager<msgs::RobotDetail> manager;
CanReceiver<msgs::RobotDetail> receiver;
ASSERT_EQ(receiver.Init(&client, &manager, false), msgs::ErrorCode::OK);
for (int attempt = 0; attempt < 3; ++attempt) {
ASSERT_EQ(receiver.Start(), msgs::ErrorCode::OK);
std::this_thread::sleep_for(std::chrono::milliseconds(5));
const auto start = std::chrono::steady_clock::now();
receiver.Stop();
EXPECT_LT(std::chrono::steady_clock::now() - start, std::chrono::milliseconds(500));
EXPECT_FALSE(receiver.IsRunning());
receiver.Stop();
}
}
TEST_F(SocketCanReceiveTest, ShortFrameIsRejectedAndStopIsIdempotent) {
const char byte = 0;
ASSERT_EQ(::write(peer, &byte, 1), 1);
std::vector<CanFrame> frames;
int32_t count = 1;
EXPECT_EQ(client.receive(&frames, &count), msgs::ErrorCode::CAN_CLIENT_ERROR_RECV_FAILED);
EXPECT_EQ(count, 0);
EXPECT_TRUE(client.stop());
EXPECT_TRUE(client.stop());
count = 1;
EXPECT_EQ(client.receive(&frames, &count), msgs::ErrorCode::CAN_CLIENT_ERROR_RECV_FAILED);
EXPECT_EQ(count, 0);
}
} // namespace cmvr::device