Skip to content

Commit

Permalink
Fix memory leak in test_subscription_nominal (#469) (#562)
Browse files Browse the repository at this point in the history
Narrow scope of subscription to prevent leak and separate nominal test from init/fini test.

Signed-off-by: y-okumura-isp <y-okumura@isp.co.jp>
  • Loading branch information
y-okumura-isp authored Feb 24, 2020
1 parent 2563332 commit 25f39c3
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions rcl/test/rcl/test_subscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,33 +116,26 @@ wait_for_subscription_to_be_ready(
success = false;
}

/* Basic nominal test of a subscription.
/* Test subscription init, fini and is_valid functions
*/
TEST_F(CLASSNAME(TestSubscriptionFixture, RMW_IMPLEMENTATION), test_subscription_nominal) {
TEST_F(
CLASSNAME(TestSubscriptionFixture, RMW_IMPLEMENTATION),
test_subscription_init_fini_and_is_valid)
{
rcl_ret_t ret;
rcl_publisher_t publisher = rcl_get_zero_initialized_publisher();

const rosidl_message_type_support_t * ts =
ROSIDL_GET_MSG_TYPE_SUPPORT(test_msgs, msg, BasicTypes);
const char * topic = "chatter";
const char * expected_topic = "/chatter";
rcl_publisher_options_t publisher_options = rcl_publisher_get_default_options();
ret = rcl_publisher_init(&publisher, this->node_ptr, ts, topic, &publisher_options);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
rcl_ret_t ret = rcl_publisher_fini(&publisher, this->node_ptr);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
});
rcl_subscription_t subscription = rcl_get_zero_initialized_subscription();

rcl_subscription_options_t subscription_options = rcl_subscription_get_default_options();
rcl_subscription_t subscription = rcl_get_zero_initialized_subscription();
ret = rcl_subscription_init(&subscription, this->node_ptr, ts, topic, &subscription_options);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
rcl_ret_t ret = rcl_subscription_fini(&subscription, this->node_ptr);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
});
EXPECT_EQ(strcmp(rcl_subscription_get_topic_name(&subscription), expected_topic), 0);
ret = rcl_subscription_fini(&subscription, this->node_ptr);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;

// Test is_valid for subscription with nullptr
EXPECT_FALSE(rcl_subscription_is_valid(nullptr));
Expand All @@ -152,12 +145,34 @@ TEST_F(CLASSNAME(TestSubscriptionFixture, RMW_IMPLEMENTATION), test_subscription
subscription = rcl_get_zero_initialized_subscription();
EXPECT_FALSE(rcl_subscription_is_valid(&subscription));
rcl_reset_error();
}

// Check that valid subscriber is valid
subscription = rcl_get_zero_initialized_subscription();
/* Basic nominal test of a subscription
*/
TEST_F(CLASSNAME(TestSubscriptionFixture, RMW_IMPLEMENTATION), test_subscription_nominal) {
rcl_ret_t ret;
rcl_publisher_t publisher = rcl_get_zero_initialized_publisher();
const rosidl_message_type_support_t * ts =
ROSIDL_GET_MSG_TYPE_SUPPORT(test_msgs, msg, BasicTypes);
const char * topic = "/chatter";
rcl_publisher_options_t publisher_options = rcl_publisher_get_default_options();
ret = rcl_publisher_init(&publisher, this->node_ptr, ts, topic, &publisher_options);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
rcl_ret_t ret = rcl_publisher_fini(&publisher, this->node_ptr);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
});

rcl_subscription_options_t subscription_options = rcl_subscription_get_default_options();
rcl_subscription_t subscription = rcl_get_zero_initialized_subscription();
ret = rcl_subscription_init(&subscription, this->node_ptr, ts, topic, &subscription_options);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
EXPECT_TRUE(rcl_subscription_is_valid(&subscription));
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
ret = rcl_subscription_fini(&subscription, this->node_ptr);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
});
rcl_reset_error();

// TODO(wjwwood): add logic to wait for the connection to be established
Expand Down

0 comments on commit 25f39c3

Please sign in to comment.