From bcce051eb243b523ca1dff2ab0d200d218aa4dd3 Mon Sep 17 00:00:00 2001 From: Chris Lalancette Date: Wed, 30 Sep 2020 15:12:04 +0000 Subject: [PATCH] Add in additional tests for parameter_client.cpp coverage. This gets us to 96% line coverage. Signed-off-by: Chris Lalancette --- rclcpp/test/rclcpp/test_parameter_client.cpp | 111 ++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) diff --git a/rclcpp/test/rclcpp/test_parameter_client.cpp b/rclcpp/test/rclcpp/test_parameter_client.cpp index 523cd3f..9e5bcdd 100644 --- a/rclcpp/test/rclcpp/test_parameter_client.cpp +++ b/rclcpp/test/rclcpp/test_parameter_client.cpp @@ -14,8 +14,13 @@ #include -#include +#include +#include +#include #include +#include +#include +#include #include "rclcpp/rclcpp.hpp" @@ -182,3 +187,107 @@ TEST_F(TestParameterClient, sync_parameter_is_ready) { EXPECT_TRUE(client.wait_for_service()); EXPECT_TRUE(client.service_is_ready()); } + +/* + Coverage for async get_parameter_types + */ +TEST_F(TestParameterClient, async_parameter_get_parameter_types) { + auto asynchronous_client = std::make_shared(node); + bool callback_called = false; + auto callback = [&callback_called](std::shared_future> result) + { + // We expect the result to be empty since we tried to get a parameter that didn't exist. + if (result.valid() && result.get().size() == 0) { + callback_called = true; + } + }; + std::vector names{"foo"}; + std::shared_future> future = + asynchronous_client->get_parameter_types(names, callback); + auto return_code = rclcpp::spin_until_future_complete( + node, future, std::chrono::milliseconds(100)); + ASSERT_EQ(rclcpp::FutureReturnCode::SUCCESS, return_code); + ASSERT_TRUE(callback_called); +} + +/* + Coverage for async get_parameters + */ +TEST_F(TestParameterClient, async_parameter_get_parameters) { + auto asynchronous_client = std::make_shared(node); + bool callback_called = false; + auto callback = [&callback_called](std::shared_future> result) + { + if (result.valid() && result.get().size() == 1 && result.get()[0].get_name() == "foo") { + callback_called = true; + } + }; + std::vector names{"foo"}; + std::shared_future> future = asynchronous_client->get_parameters( + names, callback); + auto return_code = rclcpp::spin_until_future_complete( + node, future, std::chrono::milliseconds(100)); + ASSERT_EQ(rclcpp::FutureReturnCode::SUCCESS, return_code); + ASSERT_TRUE(callback_called); +} + +/* + Coverage for async set_parameters_atomically + */ +TEST_F(TestParameterClient, async_parameter_set_parameters_atomically) { + auto asynchronous_client = std::make_shared(node); + bool callback_called = false; + auto callback = + [&callback_called](std::shared_future result) + { + // We expect this to fail since we didn't declare the parameter first. + if (result.valid() && !result.get().successful && + result.get().reason == "One or more parameters were not declared before setting") + { + callback_called = true; + } + }; + std::vector parameters; + parameters.emplace_back(rclcpp::Parameter("foo")); + std::shared_future future = + asynchronous_client->set_parameters_atomically(parameters, callback); + auto return_code = rclcpp::spin_until_future_complete( + node, future, std::chrono::milliseconds(100)); + ASSERT_EQ(rclcpp::FutureReturnCode::SUCCESS, return_code); + ASSERT_TRUE(callback_called); +} + +/* + Coverage for async list_parameters + */ +TEST_F(TestParameterClient, async_parameter_list_parameters) { + auto asynchronous_client = std::make_shared(node); + bool callback_called = false; + auto callback = + [&callback_called](std::shared_future result) + { + if (result.valid() && result.get().names.size() == 0 && result.get().prefixes.size() == 0) { + callback_called = true; + } + }; + std::vector prefixes{"foo"}; + std::shared_future future = + asynchronous_client->list_parameters(prefixes, 0, callback); + auto return_code = rclcpp::spin_until_future_complete( + node, future, std::chrono::milliseconds(100)); + ASSERT_EQ(rclcpp::FutureReturnCode::SUCCESS, return_code); + ASSERT_TRUE(callback_called); +} + +/* + Coverage for sync get_parameter_types + */ +TEST_F(TestParameterClient, sync_parameter_get_parameter_types) { + node->declare_parameter("foo", 4); + auto synchronous_client = std::make_shared(node); + std::vector names{"foo"}; + std::vector parameter_types = + synchronous_client->get_parameter_types(names); + ASSERT_EQ(1u, parameter_types.size()); + ASSERT_EQ(rclcpp::ParameterType::PARAMETER_INTEGER, parameter_types[0]); +}