@@ -3263,6 +3263,135 @@ TEST_F(TestNode, get_publishers_subscriptions_info_by_topic) {
32633263 }, rclcpp::exceptions::InvalidTopicNameError);
32643264}
32653265
3266+ // test that calling get_clients_info_by_service and get_servers_info_by_service
3267+ TEST_F (TestNode, get_clients_servers_info_by_service) {
3268+ auto node = std::make_shared<rclcpp::Node>(" my_node" , " /ns" );
3269+ std::string service_name = " test_service_info" ;
3270+ std::string fq_service_name = rclcpp::expand_topic_or_service_name (
3271+ service_name, node->get_name (), node->get_namespace (), true );
3272+
3273+ // Lists should be empty
3274+ EXPECT_TRUE (node->get_clients_info_by_service (fq_service_name).empty ());
3275+ EXPECT_TRUE (node->get_servers_info_by_service (fq_service_name).empty ());
3276+
3277+ // Add a publisher
3278+ rclcpp::QoSInitialization qos_initialization =
3279+ {
3280+ RMW_QOS_POLICY_HISTORY_KEEP_ALL,
3281+ 10
3282+ };
3283+ rmw_qos_profile_t rmw_qos_profile_default =
3284+ {
3285+ RMW_QOS_POLICY_HISTORY_KEEP_ALL,
3286+ 10 ,
3287+ RMW_QOS_POLICY_RELIABILITY_BEST_EFFORT,
3288+ RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL,
3289+ {1 , 12345 },
3290+ {20 , 9887665 },
3291+ RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_TOPIC,
3292+ {5 , 23456 },
3293+ false
3294+ };
3295+ rclcpp::QoS qos = rclcpp::QoS (qos_initialization, rmw_qos_profile_default);
3296+ auto client = node->create_client <test_msgs::srv::Empty>(service_name, qos);
3297+ // Wait for the underlying RMW implementation to catch up with graph changes
3298+ auto client_is_generated =
3299+ [&]() {return node->get_clients_info_by_service (fq_service_name).size () > 0u ;};
3300+ ASSERT_TRUE (wait_for_event (node, client_is_generated));
3301+ // List should have at least one item
3302+ auto client_list = node->get_clients_info_by_service (fq_service_name);
3303+ ASSERT_GE (client_list.size (), (size_t )1 );
3304+ // Server list should be empty
3305+ EXPECT_TRUE (node->get_servers_info_by_service (fq_service_name).empty ());
3306+ // Verify client list has the right data.
3307+ for (auto client_info : client_list) {
3308+ EXPECT_EQ (node->get_name (), client_info.node_name ());
3309+ EXPECT_EQ (node->get_namespace (), client_info.node_namespace ());
3310+ ASSERT_NE (client_info.topic_type ().find (" test_msgs/srv/Empty" ), std::string::npos);
3311+ if (client_info.topic_type () == " test_msgs/srv/Empty_Request" ) {
3312+ EXPECT_EQ (rclcpp::EndpointType::Publisher, client_info.endpoint_type ());
3313+ auto actual_qos_profile = client_info.qos_profile ().get_rmw_qos_profile ();
3314+ {
3315+ SCOPED_TRACE (" Publisher QOS 1" );
3316+ expect_qos_profile_eq (qos.get_rmw_qos_profile (), actual_qos_profile, true );
3317+ }
3318+ } else if (client_info.topic_type () == " test_msgs/srv/Empty_Response" ) {
3319+ EXPECT_EQ (rclcpp::EndpointType::Subscription, client_info.endpoint_type ());
3320+ auto actual_qos_profile = client_info.qos_profile ().get_rmw_qos_profile ();
3321+ {
3322+ SCOPED_TRACE (" Publisher QOS 1" );
3323+ expect_qos_profile_eq (qos.get_rmw_qos_profile (), actual_qos_profile, false );
3324+ }
3325+ }
3326+ }
3327+
3328+ // Add a subscription
3329+ rclcpp::QoSInitialization qos_initialization2 =
3330+ {
3331+ RMW_QOS_POLICY_HISTORY_KEEP_LAST,
3332+ 0
3333+ };
3334+ rmw_qos_profile_t rmw_qos_profile_default2 =
3335+ {
3336+ RMW_QOS_POLICY_HISTORY_KEEP_LAST,
3337+ 0 ,
3338+ RMW_QOS_POLICY_RELIABILITY_RELIABLE,
3339+ RMW_QOS_POLICY_DURABILITY_VOLATILE,
3340+ {15 , 1678 },
3341+ {29 , 2345 },
3342+ RMW_QOS_POLICY_LIVELINESS_AUTOMATIC,
3343+ {5 , 23456 },
3344+ false
3345+ };
3346+ rclcpp::QoS qos2 = rclcpp::QoS (qos_initialization2, rmw_qos_profile_default2);
3347+ auto callback = [](test_msgs::srv::Empty_Request::ConstSharedPtr req,
3348+ test_msgs::srv::Empty_Response::ConstSharedPtr resp) {
3349+ (void )req;
3350+ (void )resp;
3351+ };
3352+ auto server = node->create_service <test_msgs::srv::Empty>(service_name, callback, qos2);
3353+ // Wait for the underlying RMW implementation to catch up with graph changes
3354+ auto server_is_generated =
3355+ [&]() {return node->get_servers_info_by_service (fq_service_name).size () > 0u ;};
3356+ ASSERT_TRUE (wait_for_event (node, server_is_generated));
3357+ // Both lists should have at least one item
3358+ client_list = node->get_clients_info_by_service (fq_service_name);
3359+ auto server_list = node->get_servers_info_by_service (fq_service_name);
3360+ ASSERT_GE (client_list.size (), (size_t )1 );
3361+ ASSERT_GE (server_list.size (), (size_t )1 );
3362+ // Verify server list has the right data.
3363+ for (auto server_info : server_list) {
3364+ EXPECT_EQ (node->get_name (), server_info.node_name ());
3365+ EXPECT_EQ (node->get_namespace (), server_info.node_namespace ());
3366+ ASSERT_NE (server_info.topic_type ().find (" test_msgs/srv/Empty" ), std::string::npos);
3367+ if (server_info.topic_type () == " test_msgs/srv/Empty_Request" ) {
3368+ EXPECT_EQ (rclcpp::EndpointType::Subscription, server_info.endpoint_type ());
3369+ auto actual_qos_profile = server_info.qos_profile ().get_rmw_qos_profile ();
3370+ {
3371+ SCOPED_TRACE (" Publisher QOS 1" );
3372+ expect_qos_profile_eq (qos2.get_rmw_qos_profile (), actual_qos_profile, false );
3373+ }
3374+ } else if (server_info.topic_type () == " test_msgs/srv/Empty_Response" ) {
3375+ EXPECT_EQ (rclcpp::EndpointType::Publisher, server_info.endpoint_type ());
3376+ auto actual_qos_profile = server_info.qos_profile ().get_rmw_qos_profile ();
3377+ {
3378+ SCOPED_TRACE (" Publisher QOS 1" );
3379+ expect_qos_profile_eq (qos2.get_rmw_qos_profile (), actual_qos_profile, true );
3380+ }
3381+ }
3382+ }
3383+
3384+ // Error cases
3385+ EXPECT_THROW (
3386+ {
3387+ client_list = node->get_clients_info_by_service (" 13" );
3388+ }, rclcpp::exceptions::InvalidServiceNameError);
3389+ EXPECT_THROW (
3390+ {
3391+ server_list = node->get_servers_info_by_service (" 13" );
3392+ }, rclcpp::exceptions::InvalidServiceNameError);
3393+ }
3394+
32663395TEST_F (TestNode, callback_groups) {
32673396 auto node = std::make_shared<rclcpp::Node>(" node" , " ns" );
32683397 size_t num_callback_groups_in_basic_node = 0 ;
0 commit comments