Skip to content

Commit

Permalink
Make test_two_timers_ready_before_timeout less flaky (#911)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Santiago Paunovic <ivanpauno@ekumenlabs.com>
  • Loading branch information
ivanpauno authored Apr 7, 2021
1 parent 0bed83b commit 29388f3
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions rcl/test/rcl/test_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,24 @@ TEST_F(TestTimerFixture, test_two_timers) {
ret = rcl_wait_set_fini(&wait_set);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
});
ret = rcl_wait(&wait_set, RCL_MS_TO_NS(100));
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
// The loop is needed because the rcl_wait_set might suffer spurious
// awakes when timers are involved.
// The loop can be removed if spurious awakes are fixed in the future.
// This issue particularly happens on Windows.
uint8_t nonnull_timers = 0;
for (uint8_t i = 0; i < wait_set.size_of_timers; i++) {
if (wait_set.timers[i] != NULL) {
nonnull_timers++;
auto start = std::chrono::system_clock::now();
do {
ret = rcl_wait(&wait_set, RCL_MS_TO_NS(100));
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
for (uint8_t i = 0; i < wait_set.size_of_timers; i++) {
if (wait_set.timers[i] != NULL) {
nonnull_timers++;
}
}
}
} while (
nonnull_timers == 0u ||
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now() - start).count() > 100u);
bool is_ready = false;
ret = rcl_timer_is_ready(&timer, &is_ready);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
Expand Down Expand Up @@ -304,14 +314,24 @@ TEST_F(TestTimerFixture, test_two_timers_ready_before_timeout) {
ret = rcl_wait_set_fini(&wait_set);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
});
ret = rcl_wait(&wait_set, RCL_MS_TO_NS(100));
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
uint8_t nonnull_timers = 0;
for (uint8_t i = 0; i < wait_set.size_of_timers; i++) {
if (wait_set.timers[i] != NULL) {
nonnull_timers++;
// The loop is needed because the rcl_wait_set might suffer spurious
// awakes when timers are involved.
// The loop can be removed if spurious awakes are fixed in the future.
// This issue particularly happens on Windows.
uint8_t nonnull_timers = 0u;
auto start = std::chrono::system_clock::now();
do {
ret = rcl_wait(&wait_set, RCL_MS_TO_NS(100));
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
for (uint8_t i = 0; i < wait_set.size_of_timers; i++) {
if (wait_set.timers[i] != NULL) {
nonnull_timers++;
}
}
}
} while (
nonnull_timers == 0u ||
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now() - start).count() > 100u);
bool is_ready = false;
ret = rcl_timer_is_ready(&timer, &is_ready);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
Expand Down

0 comments on commit 29388f3

Please sign in to comment.