Skip to content

Commit

Permalink
Refactor starvation test in multi-threaded executor to use sleep inst…
Browse files Browse the repository at this point in the history
…ead of busy wait

Signed-off-by: HarunTeper <[email protected]>
  • Loading branch information
HarunTeper committed Jan 6, 2025
1 parent f6f72c8 commit 330c484
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions rclcpp/test/rclcpp/executors/test_multi_threaded_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,47 +120,35 @@ TEST_F(TestMultiThreadedExecutor, starvation) {
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) {
}
std::this_thread::sleep_for(100ms);

timer_one_count++;

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

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

if (diff > 1) {
if (timer_one_count > 10 || timer_two_count > 10) {
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) {
}
std::this_thread::sleep_for(100ms);

timer_two_count++;

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

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

if (diff > 1) {
if (timer_one_count > 10 || timer_two_count > 10) {
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);
timer_one = node->create_wall_timer(10ms, timer_one_callback);
timer_two = node->create_wall_timer(10ms, timer_two_callback);

executor.add_node(node);
executor.spin();
Expand Down

0 comments on commit 330c484

Please sign in to comment.