Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for LoanedMessage with mocked loaned message publisher #1366

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rclcpp/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ ament_add_gtest(test_loaned_message rclcpp/test_loaned_message.cpp)
ament_target_dependencies(test_loaned_message
"test_msgs"
)
target_link_libraries(test_loaned_message ${PROJECT_NAME})
target_link_libraries(test_loaned_message ${PROJECT_NAME} mimick)

ament_add_gtest(test_memory_strategy rclcpp/test_memory_strategy.cpp)
ament_target_dependencies(test_memory_strategy
Expand Down
50 changes: 50 additions & 0 deletions rclcpp/test/rclcpp/test_loaned_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include "test_msgs/msg/basic_types.hpp"

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

using MessageT = test_msgs::msg::BasicTypes;
using LoanedMessageT = rclcpp::LoanedMessage<MessageT>;

Expand Down Expand Up @@ -81,3 +83,51 @@ TEST_F(TestLoanedMessage, release) {

SUCCEED();
}

TEST_F(TestLoanedMessage, construct_with_loaned_message_publisher) {
auto node = std::make_shared<rclcpp::Node>("loaned_message_test_node");
auto publisher = node->create_publisher<MessageT>("topic", 10);
std::allocator<MessageT> allocator;

auto mock_can_loan = mocking_utils::patch_and_return(
"lib:rclcpp", rcl_publisher_can_loan_messages, true);

{
auto mock_borrow_loaned = mocking_utils::patch_and_return(
"self", rcl_borrow_loaned_message, RCL_RET_ERROR);

EXPECT_THROW(
std::make_shared<LoanedMessageT>(*publisher.get(), allocator).reset(),
rclcpp::exceptions::RCLError);
}

MessageT message;
auto borrow_loaned_message_callback =
[&message](
const rcl_publisher_t *, const rosidl_message_type_support_t *, void ** ros_message) {
*ros_message = &message;
return RCL_RET_OK;
};
auto mock_borrow_loaned = mocking_utils::patch(
"self", rcl_borrow_loaned_message, borrow_loaned_message_callback);

{
auto mock_return_loaned = mocking_utils::patch_and_return(
"self", rcl_return_loaned_message_from_publisher, RCL_RET_OK);

auto loaned_message = std::make_shared<LoanedMessageT>(*publisher.get(), allocator);
EXPECT_TRUE(loaned_message->is_valid());
EXPECT_NO_THROW(loaned_message.reset());
}

{
auto loaned_message = std::make_shared<LoanedMessageT>(*publisher.get(), allocator);
EXPECT_TRUE(loaned_message->is_valid());

auto mock_return_loaned = mocking_utils::patch_and_return(
"self", rcl_return_loaned_message_from_publisher, RCL_RET_ERROR);

// No exception, it just logs an error
EXPECT_NO_THROW(loaned_message.reset());
}
}