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

Multi-threaded Executor starvation fix #2702

Open
wants to merge 2 commits into
base: rolling
Choose a base branch
from
Open
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
63 changes: 63 additions & 0 deletions rclcpp/test/rclcpp/executors/test_multi_threaded_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,66 @@ TEST_F(TestMultiThreadedExecutor, timer_over_take) {
executor.add_node(node);
executor.spin();
}

/*
Test that no tasks are starved
*/
TEST_F(TestMultiThreadedExecutor, starvation) {
HarunTeper marked this conversation as resolved.
Show resolved Hide resolved
rclcpp::executors::MultiThreadedExecutor executor(rclcpp::ExecutorOptions(),
2u);

std::shared_ptr<rclcpp::Node> node =
std::make_shared<rclcpp::Node>("test_multi_threaded_executor_starvation");

std::atomic_int timer_one_count{0};
std::atomic_int timer_two_count{0};

rclcpp::TimerBase::SharedPtr timer_one;
rclcpp::TimerBase::SharedPtr timer_two;

auto timer_one_callback = [&timer_one_count, &timer_two_count]() {
std::cout << "Timer one callback executed. Count: "
<< timer_one_count.load() << std::endl;

auto start_time = std::chrono::steady_clock::now();
while (std::chrono::steady_clock::now() - start_time < 100ms) {
}
HarunTeper marked this conversation as resolved.
Show resolved Hide resolved

timer_one_count++;

auto diff = std::abs(timer_one_count - timer_two_count);

std::cout << "Difference in counts: " << diff << std::endl;

if (diff > 1) {
rclcpp::shutdown();
ASSERT_LE(diff, 1);
}
};

auto timer_two_callback = [&timer_one_count, &timer_two_count]() {
std::cout << "Timer two callback executed. Count: "
<< timer_two_count.load() << std::endl;

auto start_time = std::chrono::steady_clock::now();
while (std::chrono::steady_clock::now() - start_time < 100ms) {
}
HarunTeper marked this conversation as resolved.
Show resolved Hide resolved

timer_two_count++;

auto diff = std::abs(timer_one_count - timer_two_count);

std::cout << "Difference in counts: " << diff << std::endl;

if (diff > 1) {
rclcpp::shutdown();
ASSERT_LE(diff, 1);
}
};

timer_one = node->create_wall_timer(0ms, timer_one_callback);
timer_two = node->create_wall_timer(0ms, timer_two_callback);

executor.add_node(node);
executor.spin();
HarunTeper marked this conversation as resolved.
Show resolved Hide resolved
}