-
Notifications
You must be signed in to change notification settings - Fork 427
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
fix flakiness in TestTimersManager unit-test #2468
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -367,22 +367,23 @@ TEST_F(TestTimersManager, check_one_timer_cancel_doesnt_affect_other_timers) | |
auto timers_manager = std::make_shared<TimersManager>( | ||
rclcpp::contexts::get_global_default_context()); | ||
|
||
size_t t1_runs = 0; | ||
std::atomic<size_t> t1_runs = 0; | ||
const size_t cancel_iter = 5; | ||
std::shared_ptr<TimerT> t1; | ||
// After a while cancel t1. Don't remove it though. | ||
// Simulates typical usage in a Node where a timer is cancelled but not removed, | ||
// since typical users aren't going to mess around with the timer manager. | ||
t1 = TimerT::make_shared( | ||
1ms, | ||
[&t1_runs, &t1]() { | ||
[&t1_runs, &t1, cancel_iter]() { | ||
t1_runs++; | ||
if (t1_runs == 5) { | ||
if (t1_runs == cancel_iter) { | ||
t1->cancel(); | ||
} | ||
}, | ||
rclcpp::contexts::get_global_default_context()); | ||
|
||
size_t t2_runs = 0; | ||
std::atomic<size_t> t2_runs = 0; | ||
auto t2 = TimerT::make_shared( | ||
1ms, | ||
[&t2_runs]() { | ||
|
@@ -397,11 +398,30 @@ TEST_F(TestTimersManager, check_one_timer_cancel_doesnt_affect_other_timers) | |
// Start timers thread | ||
timers_manager->start(); | ||
|
||
std::this_thread::sleep_for(15ms); | ||
// Wait for t1 to be canceled | ||
while (!t1->is_canceled()) { | ||
std::this_thread::sleep_for(3ms); | ||
} | ||
|
||
EXPECT_TRUE(t1->is_canceled()); | ||
EXPECT_FALSE(t2->is_canceled()); | ||
EXPECT_EQ(t1_runs, cancel_iter); | ||
|
||
// Verify that t2 is still being invoked | ||
const size_t start_t2_runs = t2_runs; | ||
const size_t num_t2_extra_runs = 6; | ||
while (t2_runs < start_t2_runs + num_t2_extra_runs) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
std::this_thread::sleep_for(3ms); | ||
} | ||
|
||
EXPECT_TRUE(t1->is_canceled()); | ||
EXPECT_FALSE(t2->is_canceled()); | ||
// t1 hasn't run since before | ||
EXPECT_EQ(t1_runs, cancel_iter); | ||
// t2 has run the expected additional number of times | ||
EXPECT_GE(t2_runs, start_t2_runs + num_t2_extra_runs); | ||
// the t2 runs are strictly more than the t1 runs | ||
EXPECT_GT(t2_runs, t1_runs); | ||
|
||
// t1 has stopped running | ||
EXPECT_NE(t1_runs, t2_runs); | ||
// Check that t2 has significantly more calls | ||
EXPECT_LT(t1_runs + 5, t2_runs); | ||
timers_manager->stop(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we put an overall timeout on this, something like 30 seconds? It just ensures that if there is a bug, this test won't hang our CI forever.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in e06bd91