init: ROS2 learning suite
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
// =============================================================================
|
||||
// test_pub_sub.cpp —— C++ 节点端到端集成测试 (gtest)。
|
||||
//
|
||||
// 验证 TalkerCPP / ListenerCPP 的发布-订阅链可正常运转:
|
||||
// 1) 启动 talker (publisher) 与 listener (subscriber) 实例;
|
||||
// 2) 在节点事件循环里 spin 一段时间,直到 listener 收到消息;
|
||||
// 3) assert 收到 ≥ 1 条,且消息来自 publisher。
|
||||
//
|
||||
// 注意:ROS2 单测标准做法是用 launch_testing_ros 在多进程下做端到端测试;
|
||||
// 本测试在同一进程内 spin 一次,跑得快但不覆盖真实 DDS 互通(那个
|
||||
// 在 integration test 里覆盖)。此处更偏"单元"性质。
|
||||
//
|
||||
// 运行:本测试由 colcon test --packages-select cpp_pubsub 自动调用,
|
||||
// 其依赖在 CMakeLists.txt 的 if(BUILD_TESTING) ... 块内拉起。
|
||||
// =============================================================================
|
||||
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "rclcpp/rclcpp.hpp"
|
||||
#include "std_msgs/msg/string.hpp"
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
// 测试夹具:继承 rclcpp::Node + 携带一个 listener 订阅,做"收到 N 条"断言。
|
||||
class PubsubFixture : public rclcpp::Node
|
||||
{
|
||||
public:
|
||||
PubsubFixture()
|
||||
: rclcpp::Node("pubsub_test_node"), received_(0)
|
||||
{
|
||||
subscription_ = this->create_subscription<std_msgs::msg::String>(
|
||||
"chatter_unit_test", 10,
|
||||
[this](const std_msgs::msg::String::SharedPtr msg) {
|
||||
(void)msg;
|
||||
received_++;
|
||||
});
|
||||
}
|
||||
|
||||
int received_count() const { return received_; }
|
||||
|
||||
private:
|
||||
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
|
||||
int received_;
|
||||
};
|
||||
|
||||
// Test 1:验证 publisher 在 1s 内能发出至少 1 条消息,
|
||||
// 通过 ros2 topic hz 等价手段(同进程订阅计数)做侧面验证。
|
||||
TEST(PubsubTest, TalkerPublishesAtLeastOnce)
|
||||
{
|
||||
auto node = std::make_shared<PubsubFixture>();
|
||||
auto publisher = node->create_publisher<std_msgs::msg::String>("chatter_unit_test", 10);
|
||||
|
||||
// 用 wall timer 触发一次 publish,然后 spin 200ms 让回调跑完。
|
||||
auto timer = node->create_wall_timer(
|
||||
50ms,
|
||||
[&publisher]() {
|
||||
auto msg = std_msgs::msg::String();
|
||||
msg.data = "unit-test-msg";
|
||||
publisher->publish(msg);
|
||||
});
|
||||
|
||||
// Humble 的 rclcpp::spin_some 只接 1 个参数,
|
||||
// 用 SingleThreadedExecutor 显式给每次 spin 一段时间。
|
||||
auto exec = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
|
||||
exec->add_node(node);
|
||||
auto end = std::chrono::steady_clock::now() + 500ms;
|
||||
while (std::chrono::steady_clock::now() < end) {
|
||||
exec->spin_some(50ms);
|
||||
}
|
||||
|
||||
ASSERT_GE(node->received_count(), 1) <<
|
||||
"subscriber should have received at least one message within 500ms";
|
||||
}
|
||||
|
||||
// Test 2:验证 std_msgs/String 字段正确填充(数据类型契约)。
|
||||
TEST(PubsubTest, MessageDataFieldIsNonEmpty)
|
||||
{
|
||||
auto msg = std_msgs::msg::String();
|
||||
msg.data = "non-empty";
|
||||
|
||||
EXPECT_FALSE(msg.data.empty());
|
||||
EXPECT_EQ(msg.data, "non-empty");
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
rclcpp::init(argc, argv);
|
||||
int rc = RUN_ALL_TESTS();
|
||||
rclcpp::shutdown();
|
||||
return rc;
|
||||
}
|
||||
Reference in New Issue
Block a user