|
| 1 | +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <gtest/gtest.h> |
| 16 | + |
| 17 | +#include <memory> |
| 18 | +#include <string> |
| 19 | + |
| 20 | +#include "rclcpp/rclcpp.hpp" |
| 21 | +#include "rmw/rmw.h" |
| 22 | +#include "test_msgs/msg/empty.hpp" |
| 23 | + |
| 24 | +class TestQosEvent : public ::testing::Test |
| 25 | +{ |
| 26 | +protected: |
| 27 | + static void SetUpTestCase() |
| 28 | + { |
| 29 | + rclcpp::init(0, nullptr); |
| 30 | + } |
| 31 | + |
| 32 | + void SetUp() |
| 33 | + { |
| 34 | + node = std::make_shared<rclcpp::Node>("test_qos_event", "/ns"); |
| 35 | + is_fastrtps = |
| 36 | + std::string(rmw_get_implementation_identifier()).find("rmw_fastrtps") != std::string::npos; |
| 37 | + } |
| 38 | + |
| 39 | + void TearDown() |
| 40 | + { |
| 41 | + node.reset(); |
| 42 | + } |
| 43 | + |
| 44 | + static constexpr char topic_name[] = "test_topic"; |
| 45 | + rclcpp::Node::SharedPtr node; |
| 46 | + bool is_fastrtps; |
| 47 | +}; |
| 48 | + |
| 49 | +constexpr char TestQosEvent::topic_name[]; |
| 50 | + |
| 51 | +/* |
| 52 | + Testing construction of a publishers with QoS event callback functions. |
| 53 | + */ |
| 54 | +TEST_F(TestQosEvent, test_publisher_constructor) |
| 55 | +{ |
| 56 | + rclcpp::PublisherOptions options; |
| 57 | + |
| 58 | + // options arg with no callbacks |
| 59 | + auto publisher = node->create_publisher<test_msgs::msg::Empty>( |
| 60 | + topic_name, 10, options); |
| 61 | + |
| 62 | + // options arg with one of the callbacks |
| 63 | + options.event_callbacks.deadline_callback = |
| 64 | + [node = node.get()](rclcpp::QOSDeadlineOfferedInfo & event) { |
| 65 | + RCLCPP_INFO( |
| 66 | + node->get_logger(), |
| 67 | + "Offered deadline missed - total %d (delta %d)", |
| 68 | + event.total_count, event.total_count_change); |
| 69 | + }; |
| 70 | + publisher = node->create_publisher<test_msgs::msg::Empty>( |
| 71 | + topic_name, 10, options); |
| 72 | + |
| 73 | + // options arg with two of the callbacks |
| 74 | + options.event_callbacks.liveliness_callback = |
| 75 | + [node = node.get()](rclcpp::QOSLivelinessLostInfo & event) { |
| 76 | + RCLCPP_INFO( |
| 77 | + node->get_logger(), |
| 78 | + "Liveliness lost - total %d (delta %d)", |
| 79 | + event.total_count, event.total_count_change); |
| 80 | + }; |
| 81 | + publisher = node->create_publisher<test_msgs::msg::Empty>( |
| 82 | + topic_name, 10, options); |
| 83 | + |
| 84 | + // options arg with three of the callbacks |
| 85 | + options.event_callbacks.incompatible_qos_callback = |
| 86 | + [node = node.get()](rclcpp::QOSOfferedIncompatibleQoSInfo & event) { |
| 87 | + RCLCPP_INFO( |
| 88 | + node->get_logger(), |
| 89 | + "Offered incompatible qos - total %d (delta %d), last_policy_kind: %d", |
| 90 | + event.total_count, event.total_count_change, event.last_policy_kind); |
| 91 | + }; |
| 92 | + try { |
| 93 | + publisher = node->create_publisher<test_msgs::msg::Empty>( |
| 94 | + topic_name, 10, options); |
| 95 | + } catch (const rclcpp::UnsupportedEventTypeException & /*exc*/) { |
| 96 | + EXPECT_TRUE(is_fastrtps); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/* |
| 101 | + Testing construction of a subscriptions with QoS event callback functions. |
| 102 | + */ |
| 103 | +TEST_F(TestQosEvent, test_subscription_constructor) |
| 104 | +{ |
| 105 | + rclcpp::SubscriptionOptions options; |
| 106 | + |
| 107 | + auto message_callback = [node = node.get()](const test_msgs::msg::Empty::SharedPtr /*msg*/) { |
| 108 | + RCLCPP_INFO(node->get_logger(), "Message received"); |
| 109 | + }; |
| 110 | + |
| 111 | + // options arg with no callbacks |
| 112 | + auto subscription = node->create_subscription<test_msgs::msg::Empty>( |
| 113 | + topic_name, 10, message_callback, options); |
| 114 | + |
| 115 | + // options arg with one of the callbacks |
| 116 | + options.event_callbacks.deadline_callback = |
| 117 | + [node = node.get()](rclcpp::QOSDeadlineRequestedInfo & event) { |
| 118 | + RCLCPP_INFO( |
| 119 | + node->get_logger(), |
| 120 | + "Requested deadline missed - total %d (delta %d)", |
| 121 | + event.total_count, event.total_count_change); |
| 122 | + }; |
| 123 | + subscription = node->create_subscription<test_msgs::msg::Empty>( |
| 124 | + topic_name, 10, message_callback, options); |
| 125 | + |
| 126 | + // options arg with two of the callbacks |
| 127 | + options.event_callbacks.liveliness_callback = |
| 128 | + [node = node.get()](rclcpp::QOSLivelinessChangedInfo & event) { |
| 129 | + RCLCPP_INFO( |
| 130 | + node->get_logger(), |
| 131 | + "Liveliness changed - alive %d (delta %d), not alive %d (delta %d)", |
| 132 | + event.alive_count, event.alive_count_change, |
| 133 | + event.not_alive_count, event.not_alive_count_change); |
| 134 | + }; |
| 135 | + subscription = node->create_subscription<test_msgs::msg::Empty>( |
| 136 | + topic_name, 10, message_callback, options); |
| 137 | + |
| 138 | + // options arg with three of the callbacks |
| 139 | + options.event_callbacks.incompatible_qos_callback = |
| 140 | + [node = node.get()](rclcpp::QOSRequestedIncompatibleQoSInfo & event) { |
| 141 | + RCLCPP_INFO( |
| 142 | + node->get_logger(), |
| 143 | + "Requested incompatible qos - total %d (delta %d), last_policy_kind: %d", |
| 144 | + event.total_count, event.total_count_change, event.last_policy_kind); |
| 145 | + }; |
| 146 | + try { |
| 147 | + subscription = node->create_subscription<test_msgs::msg::Empty>( |
| 148 | + topic_name, 10, message_callback, options); |
| 149 | + } catch (const rclcpp::UnsupportedEventTypeException & /*exc*/) { |
| 150 | + EXPECT_TRUE(is_fastrtps); |
| 151 | + } |
| 152 | +} |
0 commit comments