Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,9 @@ bool EventsCBGExecutor::collect_and_execute_ready_events(
}

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;
Expand All @@ -522,7 +524,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;
}
Expand Down
62 changes: 62 additions & 0 deletions rclcpp/test/rclcpp/executors/test_executors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,68 @@ 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<std::shared_ptr<TestWaitable>> waitables;
for (size_t i = 0; i < number_of_waitables; ++i) {
waitables.push_back(std::make_shared<TestWaitable>());
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 deprecated EventsExecutor rejects a max_duration of 0 in spin_all()
RCPPUTILS_DEPRECATION_WARNING_OFF_START
if (std::is_same<ExecutorType, rclcpp::experimental::executors::EventsExecutor>()) {
return;
}
RCPPUTILS_DEPRECATION_WARNING_OFF_STOP

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
TYPED_TEST(TestExecutorsStable, spinSomeMaxDuration)
Expand Down
10 changes: 6 additions & 4 deletions rclcpp/test/rclcpp/executors/test_executors_warmup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,13 @@ 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
RCPPUTILS_DEPRECATION_WARNING_OFF_START
if (
!std::is_same<ExecutorType, rclcpp::experimental::executors::EventsExecutor>())
!std::is_same<ExecutorType, rclcpp::experimental::executors::EventsExecutor>() &&
!std::is_same<ExecutorType, rclcpp::executors::EventsCBGExecutor>())
{
GTEST_SKIP();
}
Expand Down Expand Up @@ -199,12 +200,13 @@ 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
RCPPUTILS_DEPRECATION_WARNING_OFF_START
if (
!std::is_same<ExecutorType, rclcpp::experimental::executors::EventsExecutor>())
!std::is_same<ExecutorType, rclcpp::experimental::executors::EventsExecutor>() &&
!std::is_same<ExecutorType, rclcpp::executors::EventsCBGExecutor>())
{
GTEST_SKIP();
}
Expand Down
Loading