Skip to content

Commit bbcd179

Browse files
authored
fix: unstable timed thread tests (#126)
1 parent 5e3cdaa commit bbcd179

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

tests/unit/general/test_retry_scheduler.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ namespace {
2121
constMethod() const { /* noop */ }
2222
};
2323

24+
// Some threads wake up a little earlier than expected, so we round the lower bound to
25+
// 99% of the expected value to stabilize the tests
26+
int
27+
roundTo99(int value) {
28+
return static_cast<int>(std::round(value * 0.99));
29+
}
30+
2431
// Test fixture(s) for this file
2532
class RetrySchedulerTest: public BaseTest {
2633
public:
@@ -157,9 +164,9 @@ TEST_F_S(Schedule, Execution, Immediate) {
157164
std::this_thread::sleep_for(1ms);
158165
}
159166

160-
EXPECT_GE(first_call_delay, 0);
167+
EXPECT_GE(first_call_delay, roundTo99(0));
161168
EXPECT_LT(first_call_delay, default_duration.count());
162-
EXPECT_GE(second_call_delay, default_duration.count() * 2);
169+
EXPECT_GE(second_call_delay, roundTo99(default_duration.count() * 2));
163170
EXPECT_LT(second_call_delay, default_duration.count() * 3);
164171

165172
EXPECT_TRUE(first_call_scheduler_thread_id);
@@ -199,9 +206,9 @@ TEST_F_S(Schedule, Execution, ImmediateWithSleep) {
199206
std::this_thread::sleep_for(1ms);
200207
}
201208

202-
EXPECT_GE(first_call_delay, default_duration.count() * 2);
209+
EXPECT_GE(first_call_delay, roundTo99(default_duration.count() * 2));
203210
EXPECT_LT(first_call_delay, default_duration.count() * 3);
204-
EXPECT_GE(second_call_delay, default_duration.count());
211+
EXPECT_GE(second_call_delay, roundTo99(default_duration.count()));
205212
EXPECT_LT(second_call_delay, default_duration.count() * 2);
206213

207214
EXPECT_TRUE(first_call_scheduler_thread_id);
@@ -241,9 +248,9 @@ TEST_F_S(Schedule, Execution, ScheduledOnly) {
241248
std::this_thread::sleep_for(1ms);
242249
}
243250

244-
EXPECT_GE(first_call_delay, default_duration.count() * 2);
251+
EXPECT_GE(first_call_delay, roundTo99(default_duration.count() * 2));
245252
EXPECT_LT(first_call_delay, default_duration.count() * 3);
246-
EXPECT_GE(second_call_delay, default_duration.count());
253+
EXPECT_GE(second_call_delay, roundTo99(default_duration.count()));
247254
EXPECT_LT(second_call_delay, default_duration.count() * 2);
248255

249256
EXPECT_TRUE(first_call_scheduler_thread_id);

0 commit comments

Comments
 (0)