From 2c8668bf35207e4584431acf4f898d2bbef5ba93 Mon Sep 17 00:00:00 2001 From: Berkay Karaman Date: Wed, 13 Mar 2024 14:24:21 +0300 Subject: [PATCH 1/6] feat(published_time_publisher): add unit test Signed-off-by: Berkay Karaman --- .../src/ros/test_published_time_publisher.cpp | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp diff --git a/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp b/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp new file mode 100644 index 0000000000000..751c5f12060a9 --- /dev/null +++ b/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp @@ -0,0 +1,81 @@ +// Copyright 2024 The Autoware Contributors +// +// 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 +#include + +#include +#include + +#include + +class PublishedTimePublisherTest : public ::testing::Test +{ +protected: + std::shared_ptr node_{nullptr}; + std::shared_ptr published_time_publisher_{nullptr}; + std::shared_ptr> test_publisher_{nullptr}; + std::shared_ptr> + test_subscriber_{nullptr}; + autoware_internal_msgs::msg::PublishedTime::ConstSharedPtr published_time_{nullptr}; + + void SetUp() override + { + ASSERT_TRUE(rclcpp::ok()); + + // Create a node + node_ = std::make_shared("PublishedTimePublisher_test_node"); + // Create a publisher + test_publisher_ = + node_->create_publisher("PublishedTimePublisher_test_topic", 1); + // Create a PublishedTimePublisher + published_time_publisher_ = + std::make_shared(node_.get()); + // Create a subscriber + test_subscriber_ = node_->create_subscription( + "PublishedTimePublisher_test_topic/debug/published_time", 1, + [this](autoware_internal_msgs::msg::PublishedTime::SharedPtr msg) { + this->published_time_ = msg; + }); + rclcpp::spin_some(node_); + } + + void TearDown() override {} +}; + +TEST_F(PublishedTimePublisherTest, PublishMsgWithHeader) +{ + std_msgs::msg::Header header; + header.stamp = rclcpp::Time(1234); + + // Use Published Time Publisher with a timestamp + published_time_publisher_->publish(test_publisher_, header); + rclcpp::spin_some(node_); + + // Check if the published time is the same as the header + EXPECT_EQ(published_time_->header.stamp, header.stamp); +} + +TEST_F(PublishedTimePublisherTest, PublishMsgWithTimestamp) +{ + std_msgs::msg::Header header; + header.stamp = rclcpp::Time(4321); + + // Use Published Time Publisher with a timestamp + published_time_publisher_->publish(test_publisher_, header.stamp); + rclcpp::spin_some(node_); + + // Check if the published time is the same as the header + EXPECT_EQ(published_time_->header.stamp, header.stamp); +} From 64a0c71290270966d58c025884cd2d0950bfde77 Mon Sep 17 00:00:00 2001 From: Berkay Karaman Date: Wed, 13 Mar 2024 15:02:17 +0300 Subject: [PATCH 2/6] feat: add nullpointer test Signed-off-by: Berkay Karaman --- .../test/src/ros/test_published_time_publisher.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp b/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp index 751c5f12060a9..96965114630c2 100644 --- a/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp +++ b/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp @@ -62,6 +62,7 @@ TEST_F(PublishedTimePublisherTest, PublishMsgWithHeader) // Use Published Time Publisher with a timestamp published_time_publisher_->publish(test_publisher_, header); rclcpp::spin_some(node_); + ASSERT_TRUE(published_time_ != nullptr); // Check if the published time is the same as the header EXPECT_EQ(published_time_->header.stamp, header.stamp); @@ -75,6 +76,7 @@ TEST_F(PublishedTimePublisherTest, PublishMsgWithTimestamp) // Use Published Time Publisher with a timestamp published_time_publisher_->publish(test_publisher_, header.stamp); rclcpp::spin_some(node_); + ASSERT_TRUE(published_time_ != nullptr); // Check if the published time is the same as the header EXPECT_EQ(published_time_->header.stamp, header.stamp); From 5882726aeedf932b4b54445d903ec891011ea44b Mon Sep 17 00:00:00 2001 From: Berkay Karaman Date: Wed, 13 Mar 2024 15:59:41 +0300 Subject: [PATCH 3/6] feat: simplify node and topic names Signed-off-by: Berkay Karaman --- .../src/ros/test_published_time_publisher.cpp | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp b/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp index 96965114630c2..3b55a87fd47bb 100644 --- a/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp +++ b/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp @@ -34,19 +34,27 @@ class PublishedTimePublisherTest : public ::testing::Test { ASSERT_TRUE(rclcpp::ok()); + // Simplify node and topic names for brevity and uniqueness + const std::string test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name(); + const std::string base_name = + "published_time_publisher_" + test_name; // Base name for node and topics + const std::string suffix = "/debug/published_time"; + // Create a node - node_ = std::make_shared("PublishedTimePublisher_test_node"); + node_ = std::make_shared(base_name + "_node"); + // Create a publisher - test_publisher_ = - node_->create_publisher("PublishedTimePublisher_test_topic", 1); + test_publisher_ = node_->create_publisher(base_name, 1); + // Create a PublishedTimePublisher published_time_publisher_ = std::make_shared(node_.get()); + // Create a subscriber test_subscriber_ = node_->create_subscription( - "PublishedTimePublisher_test_topic/debug/published_time", 1, - [this](autoware_internal_msgs::msg::PublishedTime::SharedPtr msg) { - this->published_time_ = msg; + base_name + suffix, 1, + [this](autoware_internal_msgs::msg::PublishedTime::ConstSharedPtr msg) { + this->published_time_ = std::move(msg); }); rclcpp::spin_some(node_); } @@ -56,12 +64,17 @@ class PublishedTimePublisherTest : public ::testing::Test TEST_F(PublishedTimePublisherTest, PublishMsgWithHeader) { + // Check if the PublishedTimePublisher is created + ASSERT_TRUE(published_time_publisher_ != nullptr); + std_msgs::msg::Header header; header.stamp = rclcpp::Time(1234); // Use Published Time Publisher with a timestamp published_time_publisher_->publish(test_publisher_, header); rclcpp::spin_some(node_); + + // Check if the published_time_ is created ASSERT_TRUE(published_time_ != nullptr); // Check if the published time is the same as the header @@ -70,12 +83,17 @@ TEST_F(PublishedTimePublisherTest, PublishMsgWithHeader) TEST_F(PublishedTimePublisherTest, PublishMsgWithTimestamp) { + // Check if the PublishedTimePublisher is created + ASSERT_TRUE(published_time_publisher_ != nullptr); + std_msgs::msg::Header header; header.stamp = rclcpp::Time(4321); // Use Published Time Publisher with a timestamp published_time_publisher_->publish(test_publisher_, header.stamp); rclcpp::spin_some(node_); + + // Check if the published_time_ is created ASSERT_TRUE(published_time_ != nullptr); // Check if the published time is the same as the header From 3b1beb63891f95d8f9fb2b237aae90215a453f1b Mon Sep 17 00:00:00 2001 From: Berkay Karaman Date: Thu, 14 Mar 2024 11:27:01 +0300 Subject: [PATCH 4/6] feat: modify to test multiple PublishedTime publishers Signed-off-by: Berkay Karaman --- .../src/ros/test_published_time_publisher.cpp | 103 ++++++++++++++---- 1 file changed, 84 insertions(+), 19 deletions(-) diff --git a/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp b/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp index 3b55a87fd47bb..23d4f194f15c7 100644 --- a/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp +++ b/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp @@ -25,10 +25,17 @@ class PublishedTimePublisherTest : public ::testing::Test protected: std::shared_ptr node_{nullptr}; std::shared_ptr published_time_publisher_{nullptr}; - std::shared_ptr> test_publisher_{nullptr}; + + std::shared_ptr> first_test_publisher_{nullptr}; + std::shared_ptr> second_test_publisher_{nullptr}; + + std::shared_ptr> + first_test_subscriber_{nullptr}; std::shared_ptr> - test_subscriber_{nullptr}; - autoware_internal_msgs::msg::PublishedTime::ConstSharedPtr published_time_{nullptr}; + second_test_subscriber_{nullptr}; + + autoware_internal_msgs::msg::PublishedTime::ConstSharedPtr first_published_time_{nullptr}; + autoware_internal_msgs::msg::PublishedTime::ConstSharedPtr second_published_time_{nullptr}; void SetUp() override { @@ -37,25 +44,39 @@ class PublishedTimePublisherTest : public ::testing::Test // Simplify node and topic names for brevity and uniqueness const std::string test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name(); const std::string base_name = - "published_time_publisher_" + test_name; // Base name for node and topics - const std::string suffix = "/debug/published_time"; + "published_time_publisher_" + test_name; // Base name for node and topics + const std::string suffix = "/debug/published_time"; // Suffix for published time topics // Create a node node_ = std::make_shared(base_name + "_node"); - // Create a publisher - test_publisher_ = node_->create_publisher(base_name, 1); + // Create the first publisher + first_test_publisher_ = + node_->create_publisher(base_name + "_topic1", 1); + + // Create the second publisher + second_test_publisher_ = + node_->create_publisher(base_name + "_topic2", 1); // Create a PublishedTimePublisher published_time_publisher_ = std::make_shared(node_.get()); - // Create a subscriber - test_subscriber_ = node_->create_subscription( - base_name + suffix, 1, + // Create the first subscriber + first_test_subscriber_ = node_->create_subscription( + base_name + "_topic1" + suffix, 1, [this](autoware_internal_msgs::msg::PublishedTime::ConstSharedPtr msg) { - this->published_time_ = std::move(msg); + this->first_published_time_ = std::move(msg); }); + + // Create the second subscriber + second_test_subscriber_ = + node_->create_subscription( + base_name + "_topic2" + suffix, 1, + [this](autoware_internal_msgs::msg::PublishedTime::ConstSharedPtr msg) { + this->second_published_time_ = std::move(msg); + }); + rclcpp::spin_some(node_); } @@ -70,15 +91,15 @@ TEST_F(PublishedTimePublisherTest, PublishMsgWithHeader) std_msgs::msg::Header header; header.stamp = rclcpp::Time(1234); - // Use Published Time Publisher with a timestamp - published_time_publisher_->publish(test_publisher_, header); + // Use Published Time Publisher .publish() with a header + published_time_publisher_->publish(first_test_publisher_, header); rclcpp::spin_some(node_); // Check if the published_time_ is created - ASSERT_TRUE(published_time_ != nullptr); + ASSERT_TRUE(first_published_time_ != nullptr); // Check if the published time is the same as the header - EXPECT_EQ(published_time_->header.stamp, header.stamp); + EXPECT_EQ(first_published_time_->header.stamp, header.stamp); } TEST_F(PublishedTimePublisherTest, PublishMsgWithTimestamp) @@ -89,13 +110,57 @@ TEST_F(PublishedTimePublisherTest, PublishMsgWithTimestamp) std_msgs::msg::Header header; header.stamp = rclcpp::Time(4321); - // Use Published Time Publisher with a timestamp - published_time_publisher_->publish(test_publisher_, header.stamp); + // Use Published Time Publisher .publish() with a timestamp + published_time_publisher_->publish(first_test_publisher_, header.stamp); + rclcpp::spin_some(node_); + + // Check if the published_time_ is created + ASSERT_TRUE(first_published_time_ != nullptr); + + // Check if the published time is the same as the header + EXPECT_EQ(first_published_time_->header.stamp, header.stamp); +} + +TEST_F(PublishedTimePublisherTest, MultiplePublishMsgWithHeader) +{ + // Check if the PublishedTimePublisher is created + ASSERT_TRUE(published_time_publisher_ != nullptr); + + std_msgs::msg::Header header; + header.stamp = rclcpp::Time(12345); + + // Use Published Time Publisher .publish() with a header for multiple publishers + published_time_publisher_->publish(first_test_publisher_, header); + published_time_publisher_->publish(second_test_publisher_, header); + rclcpp::spin_some(node_); + + // Check if the published_time_ is created + ASSERT_TRUE(first_published_time_ != nullptr); + ASSERT_TRUE(second_published_time_ != nullptr); + + // Check if the published time is the same as the header + EXPECT_EQ(first_published_time_->header.stamp, header.stamp); + EXPECT_EQ(second_published_time_->header.stamp, header.stamp); +} + +TEST_F(PublishedTimePublisherTest, MultiplePublishMsgWithTimestamp) +{ + // Check if the PublishedTimePublisher is created + ASSERT_TRUE(published_time_publisher_ != nullptr); + + std_msgs::msg::Header header; + header.stamp = rclcpp::Time(12345); + + // Use Published Time Publisher .publish() with a timestamp for multiple publishers + published_time_publisher_->publish(first_test_publisher_, header.stamp); + published_time_publisher_->publish(second_test_publisher_, header.stamp); rclcpp::spin_some(node_); // Check if the published_time_ is created - ASSERT_TRUE(published_time_ != nullptr); + ASSERT_TRUE(first_published_time_ != nullptr); + ASSERT_TRUE(second_published_time_ != nullptr); // Check if the published time is the same as the header - EXPECT_EQ(published_time_->header.stamp, header.stamp); + EXPECT_EQ(first_published_time_->header.stamp, header.stamp); + EXPECT_EQ(second_published_time_->header.stamp, header.stamp); } From 287695c262c7b6dc536f5da2e8429a1004f08cdf Mon Sep 17 00:00:00 2001 From: Berkay Karaman Date: Thu, 14 Mar 2024 13:24:38 +0300 Subject: [PATCH 5/6] feat: change function name publish() -> publish_if_subscribed() Signed-off-by: Berkay Karaman --- .../src/ros/test_published_time_publisher.cpp | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp b/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp index 23d4f194f15c7..6bf3a6af7c699 100644 --- a/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp +++ b/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp @@ -91,8 +91,8 @@ TEST_F(PublishedTimePublisherTest, PublishMsgWithHeader) std_msgs::msg::Header header; header.stamp = rclcpp::Time(1234); - // Use Published Time Publisher .publish() with a header - published_time_publisher_->publish(first_test_publisher_, header); + // Use Published Time Publisher .publish_if_subscribed() with a header + published_time_publisher_->publish_if_subscribed(first_test_publisher_, header); rclcpp::spin_some(node_); // Check if the published_time_ is created @@ -110,8 +110,8 @@ TEST_F(PublishedTimePublisherTest, PublishMsgWithTimestamp) std_msgs::msg::Header header; header.stamp = rclcpp::Time(4321); - // Use Published Time Publisher .publish() with a timestamp - published_time_publisher_->publish(first_test_publisher_, header.stamp); + // Use Published Time Publisher .publish_if_subscribed() with a timestamp + published_time_publisher_->publish_if_subscribed(first_test_publisher_, header.stamp); rclcpp::spin_some(node_); // Check if the published_time_ is created @@ -129,9 +129,9 @@ TEST_F(PublishedTimePublisherTest, MultiplePublishMsgWithHeader) std_msgs::msg::Header header; header.stamp = rclcpp::Time(12345); - // Use Published Time Publisher .publish() with a header for multiple publishers - published_time_publisher_->publish(first_test_publisher_, header); - published_time_publisher_->publish(second_test_publisher_, header); + // Use Published Time Publisher .publish_if_subscribed() with a header for multiple publishers + published_time_publisher_->publish_if_subscribed(first_test_publisher_, header); + published_time_publisher_->publish_if_subscribed(second_test_publisher_, header); rclcpp::spin_some(node_); // Check if the published_time_ is created @@ -151,9 +151,9 @@ TEST_F(PublishedTimePublisherTest, MultiplePublishMsgWithTimestamp) std_msgs::msg::Header header; header.stamp = rclcpp::Time(12345); - // Use Published Time Publisher .publish() with a timestamp for multiple publishers - published_time_publisher_->publish(first_test_publisher_, header.stamp); - published_time_publisher_->publish(second_test_publisher_, header.stamp); + // Use Published Time Publisher .publish_if_subscribed() with a timestamp for multiple publishers + published_time_publisher_->publish_if_subscribed(first_test_publisher_, header.stamp); + published_time_publisher_->publish_if_subscribed(second_test_publisher_, header.stamp); rclcpp::spin_some(node_); // Check if the published_time_ is created From 7c775fadda6f1948876031feb3fa4287d3a1dcb3 Mon Sep 17 00:00:00 2001 From: Berkay Karaman Date: Sat, 16 Mar 2024 01:59:21 +0300 Subject: [PATCH 6/6] feat: update Signed-off-by: Berkay Karaman --- .../src/ros/test_published_time_publisher.cpp | 224 +++++++++++++----- 1 file changed, 165 insertions(+), 59 deletions(-) diff --git a/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp b/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp index 6bf3a6af7c699..133cb01f9b348 100644 --- a/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp +++ b/common/tier4_autoware_utils/test/src/ros/test_published_time_publisher.cpp @@ -20,61 +20,72 @@ #include -class PublishedTimePublisherTest : public ::testing::Test +class PublishedTimePublisherWithSubscriptionTest : public ::testing::Test { protected: std::shared_ptr node_{nullptr}; - std::shared_ptr published_time_publisher_{nullptr}; + std::shared_ptr published_time_publisher_ptr_{ + nullptr}; - std::shared_ptr> first_test_publisher_{nullptr}; - std::shared_ptr> second_test_publisher_{nullptr}; + std::shared_ptr> first_test_publisher_ptr_{nullptr}; + std::shared_ptr> second_test_publisher_ptr_{nullptr}; std::shared_ptr> - first_test_subscriber_{nullptr}; + first_test_subscriber_ptr_{nullptr}; std::shared_ptr> - second_test_subscriber_{nullptr}; + second_test_subscriber_ptr_{nullptr}; - autoware_internal_msgs::msg::PublishedTime::ConstSharedPtr first_published_time_{nullptr}; - autoware_internal_msgs::msg::PublishedTime::ConstSharedPtr second_published_time_{nullptr}; + autoware_internal_msgs::msg::PublishedTime::ConstSharedPtr first_published_time_ptr_{nullptr}; + autoware_internal_msgs::msg::PublishedTime::ConstSharedPtr second_published_time_ptr_{nullptr}; + + std_msgs::msg::Header first_header_; + std_msgs::msg::Header second_header_; void SetUp() override { - ASSERT_TRUE(rclcpp::ok()); - // Simplify node and topic names for brevity and uniqueness const std::string test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name(); const std::string base_name = "published_time_publisher_" + test_name; // Base name for node and topics const std::string suffix = "/debug/published_time"; // Suffix for published time topics - // Create a node + // Initialize ROS node node_ = std::make_shared(base_name + "_node"); + ASSERT_TRUE(rclcpp::ok()); + + // init headers which will be used to publish + first_header_.stamp = rclcpp::Time(0); + first_header_.frame_id = "frame_id_1"; + second_header_.stamp = rclcpp::Time(1); + second_header_.frame_id = "frame_id_2"; + // Create the first publisher - first_test_publisher_ = + first_test_publisher_ptr_ = node_->create_publisher(base_name + "_topic1", 1); // Create the second publisher - second_test_publisher_ = + second_test_publisher_ptr_ = node_->create_publisher(base_name + "_topic2", 1); // Create a PublishedTimePublisher - published_time_publisher_ = + published_time_publisher_ptr_ = std::make_shared(node_.get()); // Create the first subscriber - first_test_subscriber_ = node_->create_subscription( - base_name + "_topic1" + suffix, 1, - [this](autoware_internal_msgs::msg::PublishedTime::ConstSharedPtr msg) { - this->first_published_time_ = std::move(msg); - }); + first_test_subscriber_ptr_ = + node_->create_subscription( + base_name + "_topic1" + suffix, 1, + [this](autoware_internal_msgs::msg::PublishedTime::ConstSharedPtr msg) { + this->first_published_time_ptr_ = std::move(msg); + }); // Create the second subscriber - second_test_subscriber_ = + second_test_subscriber_ptr_ = node_->create_subscription( base_name + "_topic2" + suffix, 1, [this](autoware_internal_msgs::msg::PublishedTime::ConstSharedPtr msg) { - this->second_published_time_ = std::move(msg); + this->second_published_time_ptr_ = std::move(msg); }); rclcpp::spin_some(node_); @@ -83,84 +94,179 @@ class PublishedTimePublisherTest : public ::testing::Test void TearDown() override {} }; -TEST_F(PublishedTimePublisherTest, PublishMsgWithHeader) +class PublishedTimePublisherWithoutSubscriptionTest : public ::testing::Test { - // Check if the PublishedTimePublisher is created - ASSERT_TRUE(published_time_publisher_ != nullptr); +protected: + std::shared_ptr node_{nullptr}; + std::shared_ptr published_time_publisher_ptr_{ + nullptr}; + + std::shared_ptr> first_test_publisher_ptr_{nullptr}; + std::shared_ptr> second_test_publisher_ptr_{nullptr}; - std_msgs::msg::Header header; - header.stamp = rclcpp::Time(1234); + std_msgs::msg::Header first_header_; + std_msgs::msg::Header second_header_; + + void SetUp() override + { + // Simplify node and topic names for brevity and uniqueness + const std::string test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name(); + const std::string base_name = + "published_time_publisher_" + test_name; // Base name for node and topics + + // Initialize ROS node + node_ = std::make_shared(base_name + "_node"); + + ASSERT_TRUE(rclcpp::ok()); + + // init headers which will be used to publish + first_header_.stamp = rclcpp::Time(0); + first_header_.frame_id = "frame_id_1"; + second_header_.stamp = rclcpp::Time(1); + second_header_.frame_id = "frame_id_2"; + + // Create the first publisher + first_test_publisher_ptr_ = + node_->create_publisher(base_name + "_topic1", 1); + + // Create the second publisher + second_test_publisher_ptr_ = + node_->create_publisher(base_name + "_topic2", 1); + + // Create a PublishedTimePublisher + published_time_publisher_ptr_ = + std::make_shared(node_.get()); + + rclcpp::spin_some(node_); + } + + void TearDown() override {} +}; + +TEST_F(PublishedTimePublisherWithSubscriptionTest, PublishMsgWithHeader) +{ + // Check if the PublishedTimePublisher is created + ASSERT_TRUE(published_time_publisher_ptr_ != nullptr); // Use Published Time Publisher .publish_if_subscribed() with a header - published_time_publisher_->publish_if_subscribed(first_test_publisher_, header); + published_time_publisher_ptr_->publish_if_subscribed(first_test_publisher_ptr_, first_header_); rclcpp::spin_some(node_); // Check if the published_time_ is created - ASSERT_TRUE(first_published_time_ != nullptr); + ASSERT_TRUE(first_published_time_ptr_ != nullptr); // Check if the published time is the same as the header - EXPECT_EQ(first_published_time_->header.stamp, header.stamp); + EXPECT_EQ(first_published_time_ptr_->header.stamp, first_header_.stamp); + EXPECT_EQ(first_published_time_ptr_->header.frame_id, first_header_.frame_id); } -TEST_F(PublishedTimePublisherTest, PublishMsgWithTimestamp) +TEST_F(PublishedTimePublisherWithSubscriptionTest, PublishMsgWithTimestamp) { // Check if the PublishedTimePublisher is created - ASSERT_TRUE(published_time_publisher_ != nullptr); - - std_msgs::msg::Header header; - header.stamp = rclcpp::Time(4321); + ASSERT_TRUE(published_time_publisher_ptr_ != nullptr); // Use Published Time Publisher .publish_if_subscribed() with a timestamp - published_time_publisher_->publish_if_subscribed(first_test_publisher_, header.stamp); + published_time_publisher_ptr_->publish_if_subscribed( + first_test_publisher_ptr_, first_header_.stamp); rclcpp::spin_some(node_); // Check if the published_time_ is created - ASSERT_TRUE(first_published_time_ != nullptr); + ASSERT_TRUE(first_published_time_ptr_ != nullptr); // Check if the published time is the same as the header - EXPECT_EQ(first_published_time_->header.stamp, header.stamp); + EXPECT_EQ(first_published_time_ptr_->header.stamp, first_header_.stamp); + EXPECT_EQ(first_published_time_ptr_->header.frame_id, std::string("")); } -TEST_F(PublishedTimePublisherTest, MultiplePublishMsgWithHeader) +TEST_F(PublishedTimePublisherWithSubscriptionTest, MultiplePublishMsgWithHeader) { // Check if the PublishedTimePublisher is created - ASSERT_TRUE(published_time_publisher_ != nullptr); - - std_msgs::msg::Header header; - header.stamp = rclcpp::Time(12345); + ASSERT_TRUE(published_time_publisher_ptr_ != nullptr); // Use Published Time Publisher .publish_if_subscribed() with a header for multiple publishers - published_time_publisher_->publish_if_subscribed(first_test_publisher_, header); - published_time_publisher_->publish_if_subscribed(second_test_publisher_, header); + published_time_publisher_ptr_->publish_if_subscribed(first_test_publisher_ptr_, first_header_); + published_time_publisher_ptr_->publish_if_subscribed(second_test_publisher_ptr_, second_header_); rclcpp::spin_some(node_); // Check if the published_time_ is created - ASSERT_TRUE(first_published_time_ != nullptr); - ASSERT_TRUE(second_published_time_ != nullptr); + ASSERT_TRUE(first_published_time_ptr_ != nullptr); + ASSERT_TRUE(second_published_time_ptr_ != nullptr); // Check if the published time is the same as the header - EXPECT_EQ(first_published_time_->header.stamp, header.stamp); - EXPECT_EQ(second_published_time_->header.stamp, header.stamp); + EXPECT_EQ(first_published_time_ptr_->header.stamp, first_header_.stamp); + EXPECT_EQ(second_published_time_ptr_->header.stamp, second_header_.stamp); + EXPECT_EQ(first_published_time_ptr_->header.frame_id, first_header_.frame_id); + EXPECT_EQ(second_published_time_ptr_->header.frame_id, second_header_.frame_id); } -TEST_F(PublishedTimePublisherTest, MultiplePublishMsgWithTimestamp) +TEST_F(PublishedTimePublisherWithSubscriptionTest, MultiplePublishMsgWithTimestamp) { // Check if the PublishedTimePublisher is created - ASSERT_TRUE(published_time_publisher_ != nullptr); - - std_msgs::msg::Header header; - header.stamp = rclcpp::Time(12345); + ASSERT_TRUE(published_time_publisher_ptr_ != nullptr); // Use Published Time Publisher .publish_if_subscribed() with a timestamp for multiple publishers - published_time_publisher_->publish_if_subscribed(first_test_publisher_, header.stamp); - published_time_publisher_->publish_if_subscribed(second_test_publisher_, header.stamp); + published_time_publisher_ptr_->publish_if_subscribed( + first_test_publisher_ptr_, first_header_.stamp); + published_time_publisher_ptr_->publish_if_subscribed( + second_test_publisher_ptr_, second_header_.stamp); rclcpp::spin_some(node_); // Check if the published_time_ is created - ASSERT_TRUE(first_published_time_ != nullptr); - ASSERT_TRUE(second_published_time_ != nullptr); + ASSERT_TRUE(first_published_time_ptr_ != nullptr); + ASSERT_TRUE(second_published_time_ptr_ != nullptr); // Check if the published time is the same as the header - EXPECT_EQ(first_published_time_->header.stamp, header.stamp); - EXPECT_EQ(second_published_time_->header.stamp, header.stamp); + EXPECT_EQ(first_published_time_ptr_->header.stamp, first_header_.stamp); + EXPECT_EQ(second_published_time_ptr_->header.stamp, second_header_.stamp); + EXPECT_EQ(first_published_time_ptr_->header.frame_id, std::string("")); + EXPECT_EQ(second_published_time_ptr_->header.frame_id, std::string("")); +} + +TEST_F(PublishedTimePublisherWithoutSubscriptionTest, PublishMsgWithHeader) +{ + // Check if the PublishedTimePublisher is created + ASSERT_TRUE(published_time_publisher_ptr_ != nullptr); + + // Use Published Time Publisher .publish_if_subscribed() with a header + published_time_publisher_ptr_->publish_if_subscribed(first_test_publisher_ptr_, first_header_); + rclcpp::spin_some(node_); + ASSERT_TRUE(rclcpp::ok()); +} + +TEST_F(PublishedTimePublisherWithoutSubscriptionTest, PublishMsgWithTimestamp) +{ + // Check if the PublishedTimePublisher is created + ASSERT_TRUE(published_time_publisher_ptr_ != nullptr); + + // Use Published Time Publisher .publish_if_subscribed() with a timestamp + published_time_publisher_ptr_->publish_if_subscribed( + first_test_publisher_ptr_, first_header_.stamp); + rclcpp::spin_some(node_); + ASSERT_TRUE(rclcpp::ok()); +} + +TEST_F(PublishedTimePublisherWithoutSubscriptionTest, MultiplePublishMsgWithHeader) +{ + // Check if the PublishedTimePublisher is created + ASSERT_TRUE(published_time_publisher_ptr_ != nullptr); + + // Use Published Time Publisher .publish_if_subscribed() with a header for multiple publishers + published_time_publisher_ptr_->publish_if_subscribed(first_test_publisher_ptr_, first_header_); + published_time_publisher_ptr_->publish_if_subscribed(second_test_publisher_ptr_, second_header_); + rclcpp::spin_some(node_); + ASSERT_TRUE(rclcpp::ok()); +} + +TEST_F(PublishedTimePublisherWithoutSubscriptionTest, MultiplePublishMsgWithTimestamp) +{ + // Check if the PublishedTimePublisher is created + ASSERT_TRUE(published_time_publisher_ptr_ != nullptr); + + // Use Published Time Publisher .publish_if_subscribed() with a timestamp for multiple publishers + published_time_publisher_ptr_->publish_if_subscribed( + first_test_publisher_ptr_, first_header_.stamp); + published_time_publisher_ptr_->publish_if_subscribed( + second_test_publisher_ptr_, second_header_.stamp); + rclcpp::spin_some(node_); + ASSERT_TRUE(rclcpp::ok()); }