|
| 1 | +// Copyright 2021 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 "rcutils/strdup.h" |
| 18 | + |
| 19 | +#include "rmw/rmw.h" |
| 20 | +#include "rmw/error_handling.h" |
| 21 | + |
| 22 | +#include "test_msgs/msg/basic_types.h" |
| 23 | + |
| 24 | +#include "./config.hpp" |
| 25 | +#include "./testing_macros.hpp" |
| 26 | + |
| 27 | +#ifdef RMW_IMPLEMENTATION |
| 28 | +# define CLASSNAME_(NAME, SUFFIX) NAME ## __ ## SUFFIX |
| 29 | +# define CLASSNAME(NAME, SUFFIX) CLASSNAME_(NAME, SUFFIX) |
| 30 | +#else |
| 31 | +# define CLASSNAME(NAME, SUFFIX) NAME |
| 32 | +#endif |
| 33 | + |
| 34 | +class CLASSNAME (TestDurationInfinite, RMW_IMPLEMENTATION) : public ::testing::Test |
| 35 | +{ |
| 36 | +protected: |
| 37 | + void SetUp() override |
| 38 | + { |
| 39 | + init_options = rmw_get_zero_initialized_init_options(); |
| 40 | + rmw_ret_t ret = rmw_init_options_init(&init_options, rcutils_get_default_allocator()); |
| 41 | + ASSERT_EQ(RMW_RET_OK, ret) << rcutils_get_error_string().str; |
| 42 | + init_options.enclave = rcutils_strdup("/", rcutils_get_default_allocator()); |
| 43 | + ASSERT_STREQ("/", init_options.enclave); |
| 44 | + context = rmw_get_zero_initialized_context(); |
| 45 | + ret = rmw_init(&init_options, &context); |
| 46 | + ASSERT_EQ(RMW_RET_OK, ret) << rcutils_get_error_string().str; |
| 47 | + constexpr char node_name[] = "infinite_duration_test_node"; |
| 48 | + constexpr char node_namespace[] = "/inifinite_duration_test_ns"; |
| 49 | + node = rmw_create_node(&context, node_name, node_namespace); |
| 50 | + ASSERT_NE(nullptr, node) << rcutils_get_error_string().str; |
| 51 | + } |
| 52 | + |
| 53 | + void TearDown() override |
| 54 | + { |
| 55 | + rmw_ret_t ret = rmw_destroy_node(node); |
| 56 | + EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str; |
| 57 | + ret = rmw_shutdown(&context); |
| 58 | + EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str; |
| 59 | + ret = rmw_context_fini(&context); |
| 60 | + EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str; |
| 61 | + ret = rmw_init_options_fini(&init_options); |
| 62 | + EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str; |
| 63 | + } |
| 64 | + |
| 65 | + rmw_init_options_t init_options; |
| 66 | + rmw_context_t context; |
| 67 | + rmw_node_t * node; |
| 68 | +}; |
| 69 | + |
| 70 | +TEST_F(CLASSNAME(TestDurationInfinite, RMW_IMPLEMENTATION), create_publisher) |
| 71 | +{ |
| 72 | + rmw_ret_t ret = RMW_RET_ERROR; |
| 73 | + size_t match_count = 0; |
| 74 | + const rosidl_message_type_support_t * ts = |
| 75 | + ROSIDL_GET_MSG_TYPE_SUPPORT(test_msgs, msg, BasicTypes); |
| 76 | + constexpr char topic_name[] = "/test"; |
| 77 | + |
| 78 | + rmw_publisher_options_t pub_options = rmw_get_default_publisher_options(); |
| 79 | + rmw_qos_profile_t offer_qos = rmw_qos_profile_default; |
| 80 | + offer_qos.deadline = RMW_DURATION_INFINITE; |
| 81 | + offer_qos.lifespan = RMW_DURATION_INFINITE; |
| 82 | + offer_qos.liveliness_lease_duration = RMW_DURATION_INFINITE; |
| 83 | + |
| 84 | + rmw_subscription_options_t sub_options = rmw_get_default_subscription_options(); |
| 85 | + rmw_qos_profile_t request_qos = rmw_qos_profile_default; |
| 86 | + request_qos.deadline = RMW_DURATION_INFINITE; |
| 87 | + request_qos.lifespan = RMW_DURATION_INFINITE; |
| 88 | + request_qos.liveliness_lease_duration = RMW_DURATION_INFINITE; |
| 89 | + |
| 90 | + // Must be able to successfully create a publisher with these offered values |
| 91 | + rmw_publisher_t * pub = |
| 92 | + rmw_create_publisher(node, ts, topic_name, &offer_qos, &pub_options); |
| 93 | + ASSERT_NE(nullptr, pub) << rmw_get_error_string().str; |
| 94 | + |
| 95 | + // Must be able to match with a subscription requesting infinite durations |
| 96 | + rmw_subscription_t * sub = |
| 97 | + rmw_create_subscription(node, ts, topic_name, &request_qos, &sub_options); |
| 98 | + ASSERT_NE(nullptr, sub) << rmw_get_error_string().str; |
| 99 | + |
| 100 | + SLEEP_AND_RETRY_UNTIL(rmw_intraprocess_discovery_delay, rmw_intraprocess_discovery_delay * 10) { |
| 101 | + ret = rmw_publisher_count_matched_subscriptions(pub, &match_count); |
| 102 | + if (RMW_RET_OK == ret && 1u == match_count) { |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + ret = rmw_publisher_count_matched_subscriptions(pub, &match_count); |
| 107 | + EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str; |
| 108 | + EXPECT_EQ(1u, match_count); |
| 109 | + |
| 110 | + ret = rmw_destroy_subscription(node, sub); |
| 111 | + EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str; |
| 112 | + |
| 113 | + ret = rmw_destroy_publisher(node, pub); |
| 114 | + EXPECT_EQ(RMW_RET_OK, ret) << rmw_get_error_string().str; |
| 115 | +} |
0 commit comments