Skip to content

Commit

Permalink
Add test to reproduce bug with timers lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Pojomovsky committed May 10, 2024
1 parent 7aef192 commit 2c9859d
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
8 changes: 8 additions & 0 deletions rclcpp/test/rclcpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,14 @@ if(TARGET test_timer)
target_link_libraries(test_timer ${PROJECT_NAME} mimick)
endif()

ament_add_gtest(test_reinitialized_timers test_reinitialized_timers.cpp
APPEND_LIBRARY_DIRS "${append_library_dirs}")
if(TARGET test_reinitialized_timers)
ament_target_dependencies(test_reinitialized_timers
"rcl")
target_link_libraries(test_reinitialized_timers ${PROJECT_NAME} mimick)
endif()

ament_add_gtest(test_timers_manager test_timers_manager.cpp
APPEND_LIBRARY_DIRS "${append_library_dirs}")
if(TARGET test_timers_manager)
Expand Down
107 changes: 107 additions & 0 deletions rclcpp/test/rclcpp/test_reinitialized_timers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2024 iRobot Corporation. All Rights Reserved.

#include <gtest/gtest.h>

#include <rclcpp/rclcpp.hpp>

#include "rclcpp/experimental/executors/events_executor/events_executor.hpp"
#include "rclcpp/experimental/executors/events_executor/simple_events_queue.hpp"

using rclcpp::experimental::executors::EventsExecutor;

class TimersTest
: public testing::Test
{
public:
void SetUp() override
{
rclcpp::init(0, nullptr);
}

void TearDown() override
{
rclcpp::shutdown();
}
};

TEST_F(TimersTest, TimersWithSamePeriod)
{
auto timers_period = std::chrono::milliseconds(50);
auto node = std::make_shared<rclcpp::Node>("test_node");
auto events_queue = std::make_unique<rclcpp::experimental::executors::SimpleEventsQueue>();
auto executor = std::make_unique<EventsExecutor>(std::move(events_queue), true, rclcpp::ExecutorOptions());

executor->add_node(node);

size_t count_1 = 0;
auto timer_1 = rclcpp::create_timer(
node,
node->get_clock(),
rclcpp::Duration(timers_period),
[&count_1]() {
count_1++;
});

size_t count_2 = 0;
auto timer_2 = rclcpp::create_timer(
node,
node->get_clock(),
rclcpp::Duration(timers_period),
[&count_2]() {
count_2++;
});

{
std::thread executor_thread([&executor](){
executor->spin();
});

while (count_2 < 10u) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
executor->cancel();
executor_thread.join();

EXPECT_GE(count_2, 10u);
EXPECT_LE(count_2 - count_1, 1u);
}

count_1 = 0;
timer_1 = rclcpp::create_timer(
node,
node->get_clock(),
rclcpp::Duration(timers_period),
[&count_1]() {
count_1++;
});

count_2 = 0;
timer_2 = rclcpp::create_timer(
node,
node->get_clock(),
rclcpp::Duration(timers_period),
[&count_2]() {
count_2++;
});

{
std::thread executor_thread([&executor](){
executor->spin();
});

while (count_2 < 10u) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
executor->cancel();
executor_thread.join();

EXPECT_GE(count_2, 10u);
EXPECT_LE(count_2 - count_1, 1u);
}
}

int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit 2c9859d

Please sign in to comment.