60 lines
1.6 KiB
C++
60 lines
1.6 KiB
C++
# test/test_pub_sub.cpp - cpp_pubsub 单元测试
|
|
#
|
|
# 设计思想:
|
|
# - SetUpTestSuite / TearDownTestSuite 共享 rclcpp::init / shutdown
|
|
# - 用 spin_some(50ms) 代替 spin() 控制超时
|
|
# - 不依赖 launch_testing(避免环境耦合)
|
|
|
|
#include <chrono>
|
|
#include <memory>
|
|
|
|
#include "gtest/gtest.h"
|
|
#include "rclcpp/rclcpp.hpp"
|
|
|
|
#include "cpp_pubsub/chatter_publisher.hpp"
|
|
#include "cpp_pubsub/chatter_subscriber.hpp"
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
class PubsubTest : public ::testing::Test
|
|
{
|
|
protected:
|
|
static void SetUpTestSuite()
|
|
{
|
|
rclcpp::init(0, nullptr);
|
|
}
|
|
|
|
static void TearDownTestSuite()
|
|
{
|
|
rclcpp::shutdown();
|
|
}
|
|
};
|
|
|
|
TEST_F(PubsubTest, PublisherConstructsWithDefaults)
|
|
{
|
|
auto node = std::make_shared<cpp_pubsub::ChatterPublisher>();
|
|
EXPECT_EQ(node->get_name(), std::string("chatter_publisher"));
|
|
EXPECT_EQ(node->get_parameter("publish_rate_hz").as_int(), 2);
|
|
EXPECT_EQ(node->get_parameter("topic_name").as_string(), std::string("chatter"));
|
|
}
|
|
|
|
TEST_F(PubsubTest, SubscriberConstructsWithDefaults)
|
|
{
|
|
auto node = std::make_shared<cpp_pubsub::ChatterSubscriber>();
|
|
EXPECT_EQ(node->get_name(), std::string("chatter_subscriber"));
|
|
EXPECT_EQ(node->get_parameter("topic_name").as_string(), std::string("chatter"));
|
|
}
|
|
|
|
TEST_F(PubsubTest, PublisherSpinSomeWorks)
|
|
{
|
|
auto node = std::make_shared<cpp_pubsub::ChatterPublisher>();
|
|
auto exec = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
|
|
exec->add_node(node);
|
|
|
|
const auto end = std::chrono::steady_clock::now() + 1s;
|
|
while (std::chrono::steady_clock::now() < end) {
|
|
exec->spin_some(50ms);
|
|
}
|
|
|
|
EXPECT_GE(node->count_publishers("chatter"), 0u);
|
|
} |