Skip to content

Release ownership of entities after spinning cancelled #2556

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

Merged
Merged
Changes from 1 commit
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
48 changes: 43 additions & 5 deletions rclcpp/test/rclcpp/test_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "rclcpp/executor.hpp"
#include "rclcpp/memory_strategy.hpp"
#include "rclcpp/executors/multi_threaded_executor.hpp"
#include "rclcpp/executors/single_threaded_executor.hpp"
#include "rclcpp/strategies/allocator_memory_strategy.hpp"
#include "test_msgs/srv/empty.hpp"
Expand Down Expand Up @@ -510,24 +511,61 @@ TEST_F(TestExecutor, is_spinning) {
ASSERT_TRUE(timer_called);
}

TEST_F(TestExecutor, release_ownership_entity_after_spinning_cancel) {
class TestAllThreadedExecutor
: public ::testing::Test, public ::testing::WithParamInterface<std::string>
{
public:
void SetUp() override
{
rclcpp::init(0, nullptr);
}

void TearDown() override
{
rclcpp::shutdown();
}
};

TEST_P(TestAllThreadedExecutor, release_ownership_entity_after_spinning_cancel) {
using namespace std::chrono_literals;
const std::string single_threaded_executor(
typeid(rclcpp::executors::SingleThreadedExecutor).name());
const std::string multi_threaded_executor(
typeid(rclcpp::executors::MultiThreadedExecutor).name());

// Create an Executor
rclcpp::executors::SingleThreadedExecutor executor;
auto type_name = GetParam();
std::shared_ptr<rclcpp::Executor> executor;
if (single_threaded_executor == type_name) {
executor = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
} else if (multi_threaded_executor == type_name) {
executor = std::make_shared<rclcpp::executors::MultiThreadedExecutor>();
} else {
FAIL() << "Unsupported Executor Type !";
}

auto future = std::async(std::launch::async, [&executor] {executor.spin();});
auto future = std::async(std::launch::async, [&executor] {executor->spin();});

auto node = std::make_shared<rclcpp::Node>("test_node");
auto callback = [](
const test_msgs::srv::Empty::Request::SharedPtr, test_msgs::srv::Empty::Response::SharedPtr) {
};
auto server = node->create_service<test_msgs::srv::Empty>("test_service", callback);
executor.add_node(node);
while (!executor->is_spinning()) {
std::this_thread::sleep_for(50ms);
}
executor->add_node(node);
std::this_thread::sleep_for(50ms);
executor.cancel();
executor->cancel();
std::future_status future_status = future.wait_for(1s);
EXPECT_EQ(future_status, std::future_status::ready);

EXPECT_EQ(server.use_count(), 1);
}

INSTANTIATE_TEST_SUITE_P(
TestAllThreadedExecutorWithParam,
TestAllThreadedExecutor,
::testing::Values(
std::string(typeid(rclcpp::executors::SingleThreadedExecutor).name()),
std::string(typeid(rclcpp::executors::MultiThreadedExecutor).name())));