Skip to content

Commit

Permalink
test publisher/subscription with the c/cpp typesupport for test_msgs:…
Browse files Browse the repository at this point in the history
…:msg::array (#1074)

* test publisher/subscription with the c/cpp typesupport for string

Signed-off-by: Chen Lihui <[email protected]>

* include the header file

Signed-off-by: Chen Lihui <[email protected]>

* add rosidl_typesupport_cpp

Signed-off-by: Chen Lihui <[email protected]>

* use rosidl_runtime_cpp package instead

Signed-off-by: Chen Lihui <[email protected]>

* fix the test_depend in package.xml

Signed-off-by: Chen Lihui <[email protected]>

* test with gtest param

Signed-off-by: Chen Lihui <[email protected]>

* to test arrays of test_msgs

Signed-off-by: Chen Lihui <[email protected]>

* address reviews

Co-authored-by: Chris Lalancette <[email protected]>
Signed-off-by: Chen Lihui <[email protected]>

---------

Signed-off-by: Chen Lihui <[email protected]>
Co-authored-by: Chris Lalancette <[email protected]>
  • Loading branch information
Chen Lihui and clalancette committed Jun 14, 2023
1 parent 0227978 commit 230ae2f
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 1 deletion.
1 change: 1 addition & 0 deletions rcl/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<test_depend>rcpputils</test_depend>
<test_depend>rmw</test_depend>
<test_depend>rmw_implementation_cmake</test_depend>
<test_depend>rosidl_runtime_cpp</test_depend>
<test_depend>test_msgs</test_depend>

<group_depend>rcl_logging_packages</group_depend>
Expand Down
6 changes: 5 additions & 1 deletion rcl/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ find_package(osrf_testing_tools_cpp REQUIRED)
find_package(rcpputils REQUIRED)
find_package(rcutils REQUIRED)
find_package(rmw_implementation_cmake REQUIRED)
find_package(rosidl_runtime_cpp REQUIRED)
find_package(test_msgs REQUIRED)

get_target_property(memory_tools_ld_preload_env_var
Expand Down Expand Up @@ -240,7 +241,10 @@ function(test_target_function)
ENV ${rmw_implementation_env_var}
APPEND_LIBRARY_DIRS ${extra_lib_dirs}
LIBRARIES ${PROJECT_NAME} mimick wait_for_entity_helpers
AMENT_DEPENDENCIES ${rmw_implementation} "osrf_testing_tools_cpp" "test_msgs"
AMENT_DEPENDENCIES ${rmw_implementation}
"osrf_testing_tools_cpp"
"rosidl_runtime_cpp"
"test_msgs"
TIMEOUT 120
)
# TODO(asorbini) Enable message timestamp tests for rmw_connextdds on Windows
Expand Down
133 changes: 133 additions & 0 deletions rcl/test/rcl/test_subscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@

#include "rcutils/strdup.h"
#include "rcutils/testing/fault_injection.h"
#include "test_msgs/msg/arrays.h"
#include "test_msgs/msg/basic_types.h"
#include "test_msgs/msg/strings.h"
#include "rosidl_runtime_c/string_functions.h"

#include "osrf_testing_tools_cpp/scope_exit.hpp"
#include "rosidl_typesupport_cpp/message_type_support.hpp"
#include "test_msgs/msg/arrays.hpp"
#include "rcl/error_handling.h"
#include "rcl/node.h"
#include "rcutils/env.h"
Expand All @@ -45,6 +48,19 @@
# define CLASSNAME(NAME, SUFFIX) NAME
#endif

#define EXPAND(x) x
#define TEST_FIXTURE_P_RMW(test_fixture_name) CLASSNAME( \
test_fixture_name, RMW_IMPLEMENTATION)
#define APPLY(macro, ...) EXPAND(macro(__VA_ARGS__))
#define TEST_P_RMW(test_case_name, test_name) \
APPLY( \
TEST_P, CLASSNAME(test_case_name, RMW_IMPLEMENTATION), test_name)
#define INSTANTIATE_TEST_SUITE_P_RMW(instance_name, test_case_name, ...) \
EXPAND( \
APPLY( \
INSTANTIATE_TEST_SUITE_P, instance_name, \
CLASSNAME(test_case_name, RMW_IMPLEMENTATION), __VA_ARGS__))

class CLASSNAME (TestSubscriptionFixture, RMW_IMPLEMENTATION) : public ::testing::Test
{
public:
Expand Down Expand Up @@ -1741,3 +1757,120 @@ TEST_F(CLASSNAME(TestSubscriptionFixture, RMW_IMPLEMENTATION), test_init_fini_ma
}
});
}

struct TestParameters
{
enum class TYPESUPPORT {C, CPP};

explicit TestParameters(
TYPESUPPORT pub_ts = TYPESUPPORT::C,
TYPESUPPORT sub_ts = TYPESUPPORT::CPP)
: pub_ts_(pub_ts), sub_ts_(sub_ts) {}

TYPESUPPORT pub_ts_;
TYPESUPPORT sub_ts_;
};

class TEST_FIXTURE_P_RMW (TestSubscriptionFixtureParam)
: public TEST_FIXTURE_P_RMW(TestSubscriptionFixture),
public ::testing::WithParamInterface<TestParameters>
{
protected:
void SetUp()
{
param_ = GetParam();
TEST_FIXTURE_P_RMW(TestSubscriptionFixture) ::SetUp();
}

TestParameters param_;
};


/* Test subscription to receive complex message from a publisher with typesupport settings.
*/
TEST_P_RMW(TestSubscriptionFixtureParam, test_subscription_complex_message) {
rcl_ret_t ret;
const rosidl_message_type_support_t * ts_pub;
const rosidl_message_type_support_t * ts_sub;
if (param_.pub_ts_ == TestParameters::TYPESUPPORT::C) {
ts_pub = ROSIDL_GET_MSG_TYPE_SUPPORT(test_msgs, msg, Arrays);
} else {
ts_pub = rosidl_typesupport_cpp::get_message_type_support_handle<test_msgs::msg::Arrays>();
}
if (param_.sub_ts_ == TestParameters::TYPESUPPORT::C) {
ts_sub = ROSIDL_GET_MSG_TYPE_SUPPORT(test_msgs, msg, Arrays);
} else {
ts_sub = rosidl_typesupport_cpp::get_message_type_support_handle<test_msgs::msg::Arrays>();
}
constexpr char topic[] = "rcl_test_subscription_nominal_string_chatter";
rcl_publisher_options_t publisher_options = rcl_publisher_get_default_options();
rcl_publisher_t publisher = rcl_get_zero_initialized_publisher();
ret = rcl_publisher_init(&publisher, this->node_ptr, ts_pub, 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();
ret = rcl_subscription_init(&subscription, this->node_ptr, ts_sub, 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;
});
ASSERT_TRUE(wait_for_established_subscription(&publisher, 10, 100));
constexpr char test_string[] = "testing";
constexpr bool bool_values[3] = {true, false, true};
if (param_.pub_ts_ == TestParameters::TYPESUPPORT::C) {
test_msgs__msg__Arrays msg;
test_msgs__msg__Arrays__init(&msg);
std::copy(std::begin(bool_values), std::end(bool_values), msg.bool_values);
ASSERT_TRUE(rosidl_runtime_c__String__assign(&msg.string_values[1], test_string));
ret = rcl_publish(&publisher, &msg, nullptr);
test_msgs__msg__Arrays__fini(&msg);
} else {
test_msgs::msg::Arrays msg;
std::copy(std::begin(bool_values), std::end(bool_values), msg.bool_values.begin());
msg.string_values[1] = test_string;
ret = rcl_publish(&publisher, &msg, nullptr);
}
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
ASSERT_TRUE(wait_for_subscription_to_be_ready(&subscription, context_ptr, 10, 100));
if (param_.sub_ts_ == TestParameters::TYPESUPPORT::C) {
test_msgs__msg__Arrays msg;
test_msgs__msg__Arrays__init(&msg);
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
test_msgs__msg__Arrays__fini(&msg);
});
ret = rcl_take(&subscription, &msg, nullptr, nullptr);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
for (size_t i = 0; i < 3; ++i) {
ASSERT_EQ(bool_values[i], msg.bool_values[i]);
}
ASSERT_EQ(
std::string(test_string),
std::string(msg.string_values[1].data, msg.string_values[1].size));
} else {
test_msgs::msg::Arrays msg;
ret = rcl_take(&subscription, &msg, nullptr, nullptr);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
for (size_t i = 0; i < msg.bool_values.size(); ++i) {
ASSERT_EQ(bool_values[i], msg.bool_values[i]);
}
ASSERT_EQ(std::string(test_string), msg.string_values[1]);
}
}

INSTANTIATE_TEST_SUITE_P_RMW(
TestSubscriptionFixtureParamWithDifferentSettings,
TestSubscriptionFixtureParam,
::testing::Values(
TestParameters(TestParameters::TYPESUPPORT::C, TestParameters::TYPESUPPORT::C),
TestParameters(TestParameters::TYPESUPPORT::C, TestParameters::TYPESUPPORT::CPP),
TestParameters(TestParameters::TYPESUPPORT::CPP, TestParameters::TYPESUPPORT::C),
TestParameters(TestParameters::TYPESUPPORT::CPP, TestParameters::TYPESUPPORT::CPP)
));

0 comments on commit 230ae2f

Please sign in to comment.