Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions rclcpp_lifecycle/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ if(BUILD_TESTING)
rclcpp::rclcpp
test_msgs::test_msgs)
endif()
ament_add_gtest(
test_lifecycle_generic_publisher
test/test_lifecycle_generic_publisher.cpp)
if(TARGET test_lifecycle_generic_publisher)
target_link_libraries(
test_lifecycle_generic_publisher
${PROJECT_NAME}
rclcpp::rclcpp
test_msgs::test_msgs)
endif()
ament_add_ros_isolated_gtest(
test_lifecycle_wait_for_message
test/test_lifecycle_wait_for_message.cpp
Expand Down
112 changes: 112 additions & 0 deletions rclcpp_lifecycle/test/test_lifecycle_generic_publisher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2026 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>

#include <memory>
#include <string>

#include "rclcpp/callback_group.hpp"
#include "rclcpp/exceptions.hpp"
#include "rclcpp/generic_publisher.hpp"
#include "rclcpp/publisher_options.hpp"
#include "rclcpp/qos.hpp"
#include "rclcpp/serialized_message.hpp"
#include "rclcpp/utilities.hpp"
#include "rclcpp_lifecycle/lifecycle_node.hpp"

class TestLifecycleGenericPublisher : public ::testing::Test
{
public:
static void SetUpTestSuite()
{
rclcpp::init(0, nullptr);
}

static void TearDownTestSuite()
{
rclcpp::shutdown();
}

protected:
void TearDown() override
{
node_->shutdown();
node_.reset();
}

rclcpp_lifecycle::LifecycleNode::SharedPtr node_ =
std::make_shared<rclcpp_lifecycle::LifecycleNode>("test_lifecycle_generic_publisher");
};

TEST_F(TestLifecycleGenericPublisher, creation_and_destruction)
{
constexpr char topic_name[] = "test_lifecycle_generic_pub_topic";
constexpr char topic_type[] = "test_msgs/msg/Strings";
const auto qos = rclcpp::QoS(10);

auto publisher = node_->create_generic_publisher(topic_name, topic_type, qos);
ASSERT_NE(nullptr, publisher);
EXPECT_STREQ("/test_lifecycle_generic_pub_topic", publisher->get_topic_name());
}

TEST_F(TestLifecycleGenericPublisher, invalid_topic_name)
{
constexpr char invalid_topic[] = "invalid_topic?";
constexpr char topic_type[] = "test_msgs/msg/Strings";
const auto qos = rclcpp::QoS(10);

EXPECT_THROW(
node_->create_generic_publisher(invalid_topic, topic_type, qos),
rclcpp::exceptions::InvalidTopicNameError);
}

TEST_F(TestLifecycleGenericPublisher, invalid_topic_type)
{
constexpr char topic_name[] = "valid_topic_name";
constexpr char invalid_type[] = "invalid_package/msg/InvalidType";
const auto qos = rclcpp::QoS(10);

EXPECT_THROW(
node_->create_generic_publisher(topic_name, invalid_type, qos),
std::runtime_error);
}

TEST_F(TestLifecycleGenericPublisher, custom_publisher_options)
{
constexpr char topic_name[] = "test_options_topic";
constexpr char topic_type[] = "test_msgs/msg/Strings";
const auto qos = rclcpp::QoS(10);

rclcpp::PublisherOptions options;
auto group = node_->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
options.callback_group = group;

auto publisher = node_->create_generic_publisher(topic_name, topic_type, qos, options);
ASSERT_NE(nullptr, publisher);
EXPECT_STREQ("/test_options_topic", publisher->get_topic_name());
}

TEST_F(TestLifecycleGenericPublisher, publish_serialized_message)
{
constexpr char topic_name[] = "test_lifecycle_pub_message_topic";
constexpr char topic_type[] = "test_msgs/msg/Strings";
const auto qos = rclcpp::QoS(10);

auto publisher = node_->create_generic_publisher(topic_name, topic_type, qos);
ASSERT_NE(nullptr, publisher);

rclcpp::SerializedMessage serialized_msg;
EXPECT_NO_THROW(publisher->publish(serialized_msg));
}
Loading