diff --git a/rclcpp/src/rclcpp/executors/events_cbg_executor/events_cbg_executor.cpp b/rclcpp/src/rclcpp/executors/events_cbg_executor/events_cbg_executor.cpp index 329f233125..92d72f2a9b 100644 --- a/rclcpp/src/rclcpp/executors/events_cbg_executor/events_cbg_executor.cpp +++ b/rclcpp/src/rclcpp/executors/events_cbg_executor/events_cbg_executor.cpp @@ -461,7 +461,9 @@ bool EventsCBGExecutor::collect_and_execute_ready_events( RCPPUTILS_SCOPE_EXIT(this->spinning.store(false); ); const auto start = std::chrono::steady_clock::now(); - const auto end_time = start + max_duration; + // a max_duration of 0 means that there is no time limit + const auto end_time = max_duration == std::chrono::nanoseconds::zero() ? + std::chrono::steady_clock::time_point::max() : start + max_duration; auto cur_time = start; bool had_work = false; @@ -475,7 +477,13 @@ bool EventsCBGExecutor::collect_and_execute_ready_events( had_work = true; - if (!recollect_if_no_work_available) { + // If entities were added, the collected work contained the request to resync + // the callback groups, and not yet the events of the new entities. In this + // case we need to resync and collect once more, even if we shall only collect + // once. Otherwise the new entities would not be served before the next call. + if (!recollect_if_no_work_available && + !needs_callback_group_resync.load(std::memory_order_acquire)) + { // we are done return had_work; } diff --git a/rclcpp/test/rclcpp/executors/test_executors.cpp b/rclcpp/test/rclcpp/executors/test_executors.cpp index e33482f538..f32976c89e 100644 --- a/rclcpp/test/rclcpp/executors/test_executors.cpp +++ b/rclcpp/test/rclcpp/executors/test_executors.cpp @@ -478,6 +478,66 @@ TYPED_TEST(TestExecutors, spinSome) } } +// The purpose of this test is to check that a max_duration of 0 means "no limit": +// - spin_some() executes all the work that was ready when it was called +// - spin_all(0) executes work until none is left +TYPED_TEST(TestExecutors, spinSomeAndSpinAllWithoutMaxDuration) +{ + using ExecutorType = TypeParam; + + // Use an isolated callback group to avoid interference from any housekeeping + // items that may be in the default callback group of the node. + constexpr bool automatically_add_to_executor_with_node = false; + auto isolated_callback_group = this->node->create_callback_group( + rclcpp::CallbackGroupType::MutuallyExclusive, + automatically_add_to_executor_with_node); + + // The callbacks take some time, so that the clock has always advanced + // after the first one was executed. + auto slow_callback = []() { + std::this_thread::sleep_for(1ms); + }; + + constexpr size_t number_of_waitables = 5u; + auto waitable_interfaces = this->node->get_node_waitables_interface(); + std::vector> waitables; + for (size_t i = 0; i < number_of_waitables; ++i) { + waitables.push_back(std::make_shared()); + waitables.back()->set_on_execute_callback(slow_callback); + waitable_interfaces->add_waitable(waitables.back(), isolated_callback_group); + } + + auto number_of_waitables_executed = [&waitables]() { + size_t count = 0; + for (const auto & waitable : waitables) { + count += waitable->get_count(); + } + return count; + }; + + ExecutorType executor; + executor.add_callback_group(isolated_callback_group, this->node->get_node_base_interface()); + + for (const auto & waitable : waitables) { + waitable->trigger(); + } + executor.spin_some(); + EXPECT_EQ(number_of_waitables_executed(), number_of_waitables) + << "spin_some() without a max_duration did not execute all the ready waitables"; + + // The EventsExecutor rejects a max_duration of 0 in spin_all() + if (std::is_same()) { + return; + } + + for (const auto & waitable : waitables) { + waitable->trigger(); + } + executor.spin_all(0ns); + EXPECT_EQ(number_of_waitables_executed(), 2u * number_of_waitables) + << "spin_all() with a max_duration of 0 did not execute all the ready waitables"; +} + // The purpose of this test is to check that the ExecutorT.spin_some() method: // - does not continue executing after max_duration has elapsed // TODO(wjwwood): The `StaticSingleThreadedExecutor` diff --git a/rclcpp/test/rclcpp/executors/test_executors_warmup.cpp b/rclcpp/test/rclcpp/executors/test_executors_warmup.cpp index ab5a362f17..ea555431ae 100644 --- a/rclcpp/test/rclcpp/executors/test_executors_warmup.cpp +++ b/rclcpp/test/rclcpp/executors/test_executors_warmup.cpp @@ -143,11 +143,12 @@ TYPED_TEST(TestExecutorsWarmup, spin_some_doesnt_require_warmup) { using ExecutorType = TypeParam; - // TODO(alsora): currently only the events-executor passes this test. + // TODO(alsora): currently only the events based executors pass this test. // Enable single-threaded and multi-threaded executors // when https://github.com/ros2/rclcpp/pull/2595 gets merged if ( - !std::is_same()) + !std::is_same() && + !std::is_same()) { GTEST_SKIP(); } @@ -188,11 +189,12 @@ TYPED_TEST(TestExecutorsWarmup, spin_some_doesnt_require_warmup_with_cbgroup) { using ExecutorType = TypeParam; - // TODO(alsora): currently only the events-executor passes this test. + // TODO(alsora): currently only the events based executors pass this test. // Enable single-threaded and multi-threaded executors // when https://github.com/ros2/rclcpp/pull/2595 gets merged if ( - !std::is_same()) + !std::is_same() && + !std::is_same()) { GTEST_SKIP(); }