Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/gtest' into parameter_handler
Browse files Browse the repository at this point in the history
  • Loading branch information
christianrauch committed Nov 24, 2024
2 parents 4ff30bf + 4831621 commit 64879db
Show file tree
Hide file tree
Showing 8 changed files with 489 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ jobs:
- name: build and test
uses: ros-tooling/action-ros-ci@v0.3
id: action_ros_ci
env:
ASAN_OPTIONS: new_delete_type_mismatch=0
with:
package-name: camera_ros
target-ros2-distro: ${{ matrix.distribution }}
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ if(BUILD_TESTING)
# uncomment the line when this package is not in a git repo
#set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()

add_subdirectory(test)
endif()

ament_package()
2 changes: 2 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
<test_depend>ament_cmake_pep257</test_depend>
<test_depend>ament_cmake_pyflakes</test_depend>
<test_depend>ament_cmake_xmllint</test_depend>
<test_depend>ament_cmake_gtest</test_depend>
<test_depend>rcl_interfaces</test_depend>

<export>
<build_type>ament_cmake</build_type>
Expand Down
17 changes: 17 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
find_package(ament_cmake_gtest REQUIRED)

find_package(rcl_interfaces REQUIRED)

ament_add_gtest(${PROJECT_NAME}_test_param test_param.cpp)
target_include_directories(${PROJECT_NAME}_test_param PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
ament_target_dependencies(${PROJECT_NAME}_test_param
rclcpp
rclcpp_components
rcl_interfaces
)

# link the 'camera_component' via '--no-as-needed' to find all plugins automatically via the ClassLoader
target_link_libraries(${PROJECT_NAME}_test_param "-Wl,--no-as-needed" camera_component)
40 changes: 40 additions & 0 deletions test/instantiate_component.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once
#include <class_loader/class_loader.hpp>
#include <rclcpp/rclcpp.hpp>
#include <rclcpp_components/node_factory.hpp>
#include <rclcpp_components/node_factory_template.hpp>


/**
* @brief instantiate a component as node
*
* Search all linked libraries for the factory of the given component and
* instantiate a node with optional options.
*
* Base on the `linktime_composition` example of the `composition` package:
* https://github.com/ros2/demos/blob/rolling/composition/src/linktime_composition.cpp
*
* @throws class_loader::CreateClassException if component does not exist.
* @throws rclcpp::exceptions::RCLInvalidArgument if `rclcpp::init()` has not been called before
*
* @param component name of the component
* @param options node options
* @return instance of the component as node
*/
rclcpp_components::NodeInstanceWrapper
instantiate_component(const std::string &component,
const rclcpp::NodeOptions &options = rclcpp::NodeOptions {})
{
const std::string node_factory_class_name = "rclcpp_components::NodeFactoryTemplate<" + component + ">";

// load all classes from statically or dynamically linked libraries
std::unique_ptr<class_loader::ClassLoader> loader =
std::make_unique<class_loader::ClassLoader>(std::string {});

// instantiate the node factory for the component
class_loader::ClassLoader::UniquePtr<rclcpp_components::NodeFactory> node_factory =
loader->createUniqueInstance<rclcpp_components::NodeFactory>(node_factory_class_name);

// instantiate the node
return node_factory->create_node_instance(options);
}
47 changes: 47 additions & 0 deletions test/log_client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <rcl_interfaces/msg/log.hpp>
#include <rclcpp/rclcpp.hpp>
#include <regex>


class LogClient : public rclcpp::Node
{
public:
LogClient(const rclcpp::NodeOptions &options,
const std::string &camera_node_name)
: Node("log_client", options),
camera_node_name(camera_node_name)
{
sub_log = this->create_subscription<rcl_interfaces::msg::Log>(
"/rosout", rclcpp::RosoutQoS {},
std::bind(&LogClient::on_log, this, std::placeholders::_1));
}

void
reset()
{
msgs.clear();
}

bool
regex_search(const std::string &pattern)
{
std::regex re {pattern};
for (const std::string &msg : msgs) {
if (std::regex_search(msg, re))
return true;
}
return false;
}

private:
rclcpp::Subscription<rcl_interfaces::msg::Log>::SharedPtr sub_log;
const std::string camera_node_name;
std::list<std::string> msgs;

void
on_log(const rcl_interfaces::msg::Log::SharedPtr msg)
{
if (msg->name == camera_node_name)
msgs.push_back(msg->msg);
}
};
66 changes: 66 additions & 0 deletions test/param_client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <rclcpp/node.hpp>
#include <rclcpp/parameter_client.hpp>


class ParamClient
{
public:
ParamClient(const rclcpp::NodeOptions &options,
const std::string &camera_node_name,
rclcpp::Executor::SharedPtr executor)
: node {rclcpp::Node::make_shared("param_client", options)},
client {executor, node, camera_node_name}
{
//
}

rclcpp::node_interfaces::NodeBaseInterface::SharedPtr
get_node_base_interface()
{
return node->get_node_base_interface();
}

std::vector<std::string>
list_parameters()
{
return client.list_parameters({}, {}).names;
}

bool
has_parameter(const std::string &parameter_name)
{
return client.has_parameter(parameter_name);
}

bool
is_set_parameter(const std::string &parameter_name)
{
const std::vector<rclcpp::Parameter> parameters =
client.get_parameters({parameter_name});
if (parameters.empty())
return false;
return parameters.front().get_type() != rclcpp::PARAMETER_NOT_SET;
}

std::vector<rclcpp::Parameter>
get_parameters(const std::vector<std::string> &parameter_names)
{
return client.get_parameters(parameter_names);
}

std::vector<rcl_interfaces::msg::SetParametersResult>
set_parameters(const std::vector<rclcpp::Parameter> &parameters)
{
return client.set_parameters(parameters);
}

rcl_interfaces::msg::SetParametersResult
set_parameters_atomically(const std::vector<rclcpp::Parameter> &parameters)
{
return client.set_parameters_atomically(parameters);
}

private:
rclcpp::Node::SharedPtr node;
rclcpp::SyncParametersClient client;
};
Loading

0 comments on commit 64879db

Please sign in to comment.