From 7ef258fff7a3c44c63c7a5cdea585b0a99ffa9c3 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Wed, 10 Jun 2020 13:39:38 -0300 Subject: [PATCH] Revert "Revert "Allow spin_until_future_complete to accept std::future (#1113)" (#1159)" (#1160) This reverts commit bba9dce253d98a31a3069218504d26c4be9fb8b8. Signed-off-by: Ivan Santiago Paunovic --- rclcpp/include/rclcpp/executor.hpp | 4 +- rclcpp/include/rclcpp/executors.hpp | 12 ++--- .../static_single_threaded_executor.hpp | 4 +- rclcpp/test/test_executor.cpp | 48 +++++++++++++++++++ 4 files changed, 58 insertions(+), 10 deletions(-) diff --git a/rclcpp/include/rclcpp/executor.hpp b/rclcpp/include/rclcpp/executor.hpp index 39acba3ec4..77fb35ca2e 100644 --- a/rclcpp/include/rclcpp/executor.hpp +++ b/rclcpp/include/rclcpp/executor.hpp @@ -188,10 +188,10 @@ class Executor * code. * \return The return code, one of `SUCCESS`, `INTERRUPTED`, or `TIMEOUT`. */ - template + template FutureReturnCode spin_until_future_complete( - const std::shared_future & future, + const FutureT & future, std::chrono::duration timeout = std::chrono::duration(-1)) { // TODO(wjwwood): does not work recursively; can't call spin_node_until_future_complete diff --git a/rclcpp/include/rclcpp/executors.hpp b/rclcpp/include/rclcpp/executors.hpp index ec55d1309e..36fb0d63cf 100644 --- a/rclcpp/include/rclcpp/executors.hpp +++ b/rclcpp/include/rclcpp/executors.hpp @@ -66,12 +66,12 @@ using rclcpp::executors::SingleThreadedExecutor; * If the time spent inside the blocking loop exceeds this timeout, return a `TIMEOUT` return code. * \return The return code, one of `SUCCESS`, `INTERRUPTED`, or `TIMEOUT`. */ -template +template rclcpp::FutureReturnCode spin_node_until_future_complete( rclcpp::Executor & executor, rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, - const std::shared_future & future, + const FutureT & future, std::chrono::duration timeout = std::chrono::duration(-1)) { // TODO(wjwwood): does not work recursively; can't call spin_node_until_future_complete @@ -82,13 +82,13 @@ spin_node_until_future_complete( return retcode; } -template rclcpp::FutureReturnCode spin_node_until_future_complete( rclcpp::Executor & executor, std::shared_ptr node_ptr, - const std::shared_future & future, + const FutureT & future, std::chrono::duration timeout = std::chrono::duration(-1)) { return rclcpp::executors::spin_node_until_future_complete( @@ -104,7 +104,7 @@ template & future, + const FutureT & future, std::chrono::duration timeout = std::chrono::duration(-1)) { rclcpp::executors::SingleThreadedExecutor executor; @@ -116,7 +116,7 @@ template node_ptr, - const std::shared_future & future, + const FutureT & future, std::chrono::duration timeout = std::chrono::duration(-1)) { return rclcpp::spin_until_future_complete(node_ptr->get_node_base_interface(), future, timeout); diff --git a/rclcpp/include/rclcpp/executors/static_single_threaded_executor.hpp b/rclcpp/include/rclcpp/executors/static_single_threaded_executor.hpp index 1e1668bcb3..25645e23f2 100644 --- a/rclcpp/include/rclcpp/executors/static_single_threaded_executor.hpp +++ b/rclcpp/include/rclcpp/executors/static_single_threaded_executor.hpp @@ -143,10 +143,10 @@ class StaticSingleThreadedExecutor : public rclcpp::Executor * exec.add_node(node); * exec.spin_until_future_complete(future); */ - template + template rclcpp::FutureReturnCode spin_until_future_complete( - std::shared_future & future, + FutureT & future, std::chrono::duration timeout = std::chrono::duration(-1)) { std::future_status status = future.wait_for(std::chrono::seconds(0)); diff --git a/rclcpp/test/test_executor.cpp b/rclcpp/test/test_executor.cpp index 95371415fc..cdefec71e1 100644 --- a/rclcpp/test/test_executor.cpp +++ b/rclcpp/test/test_executor.cpp @@ -67,3 +67,51 @@ TEST_F(TestExecutors, addTemporaryNode) { executor.add_node(std::make_shared("temporary_node")); EXPECT_NO_THROW(executor.spin_some()); } + +// Make sure that the spin_until_future_complete works correctly with std::future +TEST_F(TestExecutors, testSpinUntilFutureComplete) { + rclcpp::executors::SingleThreadedExecutor executor; + std::future future; + rclcpp::FutureReturnCode ret; + + // test success + future = std::async( + []() { + return; + }); + ret = executor.spin_until_future_complete(future, 1s); + EXPECT_EQ(rclcpp::FutureReturnCode::SUCCESS, ret); + + // test timeout + future = std::async( + []() { + std::this_thread::sleep_for(1s); + return; + }); + ret = executor.spin_until_future_complete(future, 100ms); + EXPECT_EQ(rclcpp::FutureReturnCode::TIMEOUT, ret); +} + +// Make sure that the spin_until_future_complete works correctly with std::shared_future +TEST_F(TestExecutors, testSpinUntilFutureCompleteSharedFuture) { + rclcpp::executors::SingleThreadedExecutor executor; + std::future future; + rclcpp::FutureReturnCode ret; + + // test success + future = std::async( + []() { + return; + }); + ret = executor.spin_until_future_complete(future.share(), 1s); + EXPECT_EQ(rclcpp::FutureReturnCode::SUCCESS, ret); + + // test timeout + future = std::async( + []() { + std::this_thread::sleep_for(1s); + return; + }); + ret = executor.spin_until_future_complete(future.share(), 100ms); + EXPECT_EQ(rclcpp::FutureReturnCode::TIMEOUT, ret); +}