Skip to content

Commit

Permalink
fix: unstable timed thread tests (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrogTheFrog authored Nov 16, 2024
1 parent 5e3cdaa commit bbcd179
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions tests/unit/general/test_retry_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ namespace {
constMethod() const { /* noop */ }
};

// Some threads wake up a little earlier than expected, so we round the lower bound to
// 99% of the expected value to stabilize the tests
int
roundTo99(int value) {
return static_cast<int>(std::round(value * 0.99));
}

// Test fixture(s) for this file
class RetrySchedulerTest: public BaseTest {
public:
Expand Down Expand Up @@ -157,9 +164,9 @@ TEST_F_S(Schedule, Execution, Immediate) {
std::this_thread::sleep_for(1ms);
}

EXPECT_GE(first_call_delay, 0);
EXPECT_GE(first_call_delay, roundTo99(0));
EXPECT_LT(first_call_delay, default_duration.count());
EXPECT_GE(second_call_delay, default_duration.count() * 2);
EXPECT_GE(second_call_delay, roundTo99(default_duration.count() * 2));
EXPECT_LT(second_call_delay, default_duration.count() * 3);

EXPECT_TRUE(first_call_scheduler_thread_id);
Expand Down Expand Up @@ -199,9 +206,9 @@ TEST_F_S(Schedule, Execution, ImmediateWithSleep) {
std::this_thread::sleep_for(1ms);
}

EXPECT_GE(first_call_delay, default_duration.count() * 2);
EXPECT_GE(first_call_delay, roundTo99(default_duration.count() * 2));
EXPECT_LT(first_call_delay, default_duration.count() * 3);
EXPECT_GE(second_call_delay, default_duration.count());
EXPECT_GE(second_call_delay, roundTo99(default_duration.count()));
EXPECT_LT(second_call_delay, default_duration.count() * 2);

EXPECT_TRUE(first_call_scheduler_thread_id);
Expand Down Expand Up @@ -241,9 +248,9 @@ TEST_F_S(Schedule, Execution, ScheduledOnly) {
std::this_thread::sleep_for(1ms);
}

EXPECT_GE(first_call_delay, default_duration.count() * 2);
EXPECT_GE(first_call_delay, roundTo99(default_duration.count() * 2));
EXPECT_LT(first_call_delay, default_duration.count() * 3);
EXPECT_GE(second_call_delay, default_duration.count());
EXPECT_GE(second_call_delay, roundTo99(default_duration.count()));
EXPECT_LT(second_call_delay, default_duration.count() * 2);

EXPECT_TRUE(first_call_scheduler_thread_id);
Expand Down

0 comments on commit bbcd179

Please sign in to comment.