Skip to content

Commit

Permalink
Fix the test for any_subscription_callback. (#13)
Browse files Browse the repository at this point in the history
In particular, change it so that the tests for TypeAdaptation
with any_subscription_callback always pass the custom type
to dispatch_intra_process().  That means that when using
TypeAdaptation, it is not possible to call dispatch_intra_process()
with the ROS message type.  But since this is a low-level
interface, this should be fine; the public-facing APIs handle
this case for us.

This also requires us to remove the test for inter-process
publishing with type adaptation.  That's because we didn't change
the signature of AnySubscriptionCallback::dispath() to take
in the custom type, so it always expects the ROS message type.

Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
clalancette authored Dec 10, 2021
1 parent a3c7e94 commit 1f9bc22
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 51 deletions.
14 changes: 7 additions & 7 deletions rclcpp/test/rclcpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ if(TARGET test_any_service_callback)
)
target_link_libraries(test_any_service_callback ${PROJECT_NAME})
endif()
# ament_add_gtest(test_any_subscription_callback test_any_subscription_callback.cpp)
# if(TARGET test_any_subscription_callback)
# ament_target_dependencies(test_any_subscription_callback
# "test_msgs"
# )
# target_link_libraries(test_any_subscription_callback ${PROJECT_NAME})
# endif()
ament_add_gtest(test_any_subscription_callback test_any_subscription_callback.cpp)
if(TARGET test_any_subscription_callback)
ament_target_dependencies(test_any_subscription_callback
"test_msgs"
)
target_link_libraries(test_any_subscription_callback ${PROJECT_NAME})
endif()
ament_add_gtest(test_client test_client.cpp)
if(TARGET test_client)
ament_target_dependencies(test_client
Expand Down
114 changes: 70 additions & 44 deletions rclcpp/test/rclcpp/test_any_subscription_callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@
#include "test_msgs/msg/empty.hpp"
#include "test_msgs/msg/empty.h"

// Type adapter to be used in tests.
struct MyEmpty {};

template<>
struct rclcpp::TypeAdapter<MyEmpty, test_msgs::msg::Empty>
{
using is_specialized = std::true_type;
using custom_type = MyEmpty;
using ros_message_type = test_msgs::msg::Empty;

static
void
convert_to_ros_message(const custom_type &, ros_message_type &)
{}

static
void
convert_to_custom(const ros_message_type &, custom_type &)
{}
};

using MyTA = rclcpp::TypeAdapter<MyEmpty, test_msgs::msg::Empty>;

class TestAnySubscriptionCallback : public ::testing::Test
{
public:
Expand All @@ -43,6 +66,24 @@ class TestAnySubscriptionCallback : public ::testing::Test
rclcpp::MessageInfo message_info_;
};

class TestAnySubscriptionCallbackTA : public ::testing::Test
{
public:
TestAnySubscriptionCallbackTA() {}

static
std::unique_ptr<MyEmpty>
get_unique_ptr_msg()
{
return std::make_unique<MyEmpty>();
}

protected:
rclcpp::AnySubscriptionCallback<MyEmpty> any_subscription_callback_;
std::shared_ptr<MyEmpty> msg_shared_ptr_{std::make_shared<MyEmpty>()};
rclcpp::MessageInfo message_info_;
};

void construct_with_null_allocator()
{
// suppress deprecated function warning
Expand Down Expand Up @@ -97,29 +138,6 @@ TEST_F(TestAnySubscriptionCallback, unset_dispatch_throw) {
// Parameterized test to test across all callback types and dispatch types.
//

// Type adapter to be used in tests.
struct MyEmpty {};

template<>
struct rclcpp::TypeAdapter<MyEmpty, test_msgs::msg::Empty>
{
using is_specialized = std::true_type;
using custom_type = MyEmpty;
using ros_message_type = test_msgs::msg::Empty;

static
void
convert_to_ros_message(const custom_type &, ros_message_type &)
{}

static
void
convert_to_custom(const ros_message_type &, custom_type &)
{}
};

using MyTA = rclcpp::TypeAdapter<MyEmpty, test_msgs::msg::Empty>;

template<typename MessageT>
class InstanceContextImpl
{
Expand Down Expand Up @@ -177,7 +195,7 @@ class DispatchTests
{};

class DispatchTestsWithTA
: public TestAnySubscriptionCallback,
: public TestAnySubscriptionCallbackTA,
public ::testing::WithParamInterface<InstanceContext<MyTA>>
{};

Expand All @@ -193,27 +211,35 @@ format_parameter_with_ta(const ::testing::TestParamInfo<DispatchTestsWithTA::Par
return info.param.name;
}

#define PARAMETERIZED_TESTS(DispatchTests_name) \
/* Testing dispatch with shared_ptr<MessageT> as input */ \
TEST_P(DispatchTests_name, test_inter_shared_dispatch) { \
auto any_subscription_callback_to_test = GetParam().get_any_subscription_callback_to_test(); \
any_subscription_callback_to_test.dispatch(msg_shared_ptr_, message_info_); \
} \
\
/* Testing dispatch with shared_ptr<const MessageT> as input */ \
TEST_P(DispatchTests_name, test_intra_shared_dispatch) { \
auto any_subscription_callback_to_test = GetParam().get_any_subscription_callback_to_test(); \
any_subscription_callback_to_test.dispatch_intra_process(msg_shared_ptr_, message_info_); \
} \
\
/* Testing dispatch with unique_ptr<MessageT> as input */ \
TEST_P(DispatchTests_name, test_intra_unique_dispatch) { \
auto any_subscription_callback_to_test = GetParam().get_any_subscription_callback_to_test(); \
any_subscription_callback_to_test.dispatch_intra_process(get_unique_ptr_msg(), message_info_); \
}
/* Testing dispatch with shared_ptr<MessageT> as input */
TEST_P(DispatchTests, test_inter_shared_dispatch) {
auto any_subscription_callback_to_test = GetParam().get_any_subscription_callback_to_test();
any_subscription_callback_to_test.dispatch(msg_shared_ptr_, message_info_);
}

/* Testing dispatch with shared_ptr<const MessageT> as input */
TEST_P(DispatchTests, test_intra_shared_dispatch) {
auto any_subscription_callback_to_test = GetParam().get_any_subscription_callback_to_test();
any_subscription_callback_to_test.dispatch_intra_process(msg_shared_ptr_, message_info_);
}

/* Testing dispatch with unique_ptr<MessageT> as input */
TEST_P(DispatchTests, test_intra_unique_dispatch) {
auto any_subscription_callback_to_test = GetParam().get_any_subscription_callback_to_test();
any_subscription_callback_to_test.dispatch_intra_process(get_unique_ptr_msg(), message_info_);
}

/* Testing dispatch with shared_ptr<const MessageT> as input */
TEST_P(DispatchTestsWithTA, test_intra_shared_dispatch) {
auto any_subscription_callback_to_test = GetParam().get_any_subscription_callback_to_test();
any_subscription_callback_to_test.dispatch_intra_process(msg_shared_ptr_, message_info_);
}

PARAMETERIZED_TESTS(DispatchTests)
PARAMETERIZED_TESTS(DispatchTestsWithTA)
/* Testing dispatch with unique_ptr<MessageT> as input */
TEST_P(DispatchTestsWithTA, test_intra_unique_dispatch) {
auto any_subscription_callback_to_test = GetParam().get_any_subscription_callback_to_test();
any_subscription_callback_to_test.dispatch_intra_process(get_unique_ptr_msg(), message_info_);
}

// Generic classes for testing callbacks using std::bind to class methods.
template<typename MessageT, typename ... CallbackArgs>
Expand Down

0 comments on commit 1f9bc22

Please sign in to comment.