Skip to content

Commit

Permalink
Add in more tests for the utilities. (#1349)
Browse files Browse the repository at this point in the history
* Add in more tests for the utilities.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
  • Loading branch information
clalancette authored Sep 29, 2020
1 parent 1b65284 commit 0b54476
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
2 changes: 2 additions & 0 deletions rclcpp/src/rclcpp/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "rclcpp/utilities.hpp"

#include <chrono>
#include <functional>
#include <string>
#include <vector>

Expand Down
89 changes: 86 additions & 3 deletions rclcpp/test/rclcpp/test_utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

#include <gtest/gtest.h>

#include <string>
#include <memory>
#include <string>
#include <vector>

#include "rcl/init.h"
#include "rcl/logging.h"
Expand All @@ -26,6 +27,7 @@
#include "rclcpp/scope_exit.hpp"

#include "../mocking_utils/patch.hpp"
#include "../utils/rclcpp_gtest_macros.hpp"

TEST(TestUtilities, remove_ros_arguments) {
const char * const argv[] = {
Expand All @@ -46,12 +48,14 @@ TEST(TestUtilities, remove_ros_arguments) {
}

TEST(TestUtilities, init_with_args) {
EXPECT_FALSE(rclcpp::signal_handlers_installed());
const char * const argv[] = {"process_name"};
int argc = sizeof(argv) / sizeof(const char *);
auto other_args = rclcpp::init_and_remove_ros_arguments(argc, argv);

ASSERT_EQ(1u, other_args.size());
ASSERT_EQ(std::string{"process_name"}, other_args[0]);
EXPECT_TRUE(rclcpp::signal_handlers_installed());

EXPECT_TRUE(rclcpp::ok());
rclcpp::shutdown();
Expand Down Expand Up @@ -97,7 +101,6 @@ TEST(TestUtilities, test_context_basic_access_const_methods) {

EXPECT_NE(nullptr, context1->get_init_options().get_rcl_init_options());
EXPECT_EQ(0u, context1->get_on_shutdown_callbacks().size());
// EXPECT_EQ(std::string{""}, context1->shutdown_reason()); not available for const
}

MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcl_guard_condition_options_t, ==)
Expand Down Expand Up @@ -158,7 +161,6 @@ TEST(TestUtilities, test_context_release_interrupt_guard_condition) {
EXPECT_NO_THROW(context1->release_interrupt_guard_condition(&wait_set));
}


TEST(TestUtilities, test_context_init_shutdown_fails) {
{
auto context = std::make_shared<rclcpp::contexts::DefaultContext>();
Expand Down Expand Up @@ -209,3 +211,84 @@ TEST(TestUtilities, test_context_init_shutdown_fails) {
EXPECT_NO_THROW({context_to_destroy.reset();});
}
}

// Required for mocking_utils below
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, ==)
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, !=)
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, <)
MOCKING_UTILS_BOOL_OPERATOR_RETURNS_FALSE(rcutils_allocator_t, >)

TEST(TestUtilities, remove_ros_arguments_rcl_parse_arguments_failed) {
const char * const argv[] = {
"process_name",
"-d", "--ros-args",
"-r", "__ns:=/foo/bar",
"-r", "__ns:=/fiz/buz",
"--", "--foo=bar", "--baz"
};
int argc = sizeof(argv) / sizeof(const char *);

auto mock = mocking_utils::inject_on_return(
"lib:rclcpp", rcl_parse_arguments, RCL_RET_ERROR);
RCLCPP_EXPECT_THROW_EQ(
rclcpp::remove_ros_arguments(argc, argv),
rclcpp::exceptions::RCLError(
RCL_RET_ERROR, rcl_get_error_state(), "failed to parse arguments"));
}

TEST(TestUtilities, remove_ros_arguments_rcl_remove_ros_arguments_failed) {
const char * const argv[] = {
"process_name",
"-d", "--ros-args",
"-r", "__ns:=/foo/bar",
"-r", "__ns:=/fiz/buz",
"--", "--foo=bar", "--baz"
};
int argc = sizeof(argv) / sizeof(const char *);

auto mock = mocking_utils::inject_on_return(
"lib:rclcpp", rcl_remove_ros_arguments, RCL_RET_ERROR);
RCLCPP_EXPECT_THROW_EQ(
rclcpp::remove_ros_arguments(argc, argv),
rclcpp::exceptions::RCLError(
RCL_RET_ERROR, rcl_get_error_state(), ""));
}

TEST(TestUtilities, remove_ros_arguments_rcl_remove_ros_arguments_failed_and_fini) {
const char * const argv[] = {
"process_name",
"-d", "--ros-args",
"-r", "__ns:=/foo/bar",
"-r", "__ns:=/fiz/buz",
"--", "--foo=bar", "--baz"
};
int argc = sizeof(argv) / sizeof(const char *);

auto mock = mocking_utils::inject_on_return(
"lib:rclcpp", rcl_remove_ros_arguments, RCL_RET_ERROR);
auto mock2 = mocking_utils::inject_on_return(
"lib:rclcpp", rcl_arguments_fini, RCL_RET_ERROR);
RCLCPP_EXPECT_THROW_EQ(
rclcpp::remove_ros_arguments(argc, argv),
rclcpp::exceptions::RCLError(
RCL_RET_ERROR, rcl_get_error_state(),
", failed also to cleanup parsed arguments, leaking memory: "));
}

TEST(TestUtilities, remove_ros_arguments_rcl_arguments_fini_failed) {
const char * const argv[] = {
"process_name",
"-d", "--ros-args",
"-r", "__ns:=/foo/bar",
"-r", "__ns:=/fiz/buz",
"--", "--foo=bar", "--baz"
};
int argc = sizeof(argv) / sizeof(const char *);

auto mock = mocking_utils::inject_on_return(
"lib:rclcpp", rcl_arguments_fini, RCL_RET_ERROR);
RCLCPP_EXPECT_THROW_EQ(
rclcpp::remove_ros_arguments(argc, argv),
rclcpp::exceptions::RCLError(
RCL_RET_ERROR, rcl_get_error_state(), "failed to cleanup parsed arguments, leaking memory"));
}

0 comments on commit 0b54476

Please sign in to comment.