Skip to content

Commit

Permalink
pw_rpc: Adjust alarm_timer_test
Browse files Browse the repository at this point in the history
Tweaks AlarmTimerTest.Restart to be more robust and less prone to flake.

Bug: b/317990451
Change-Id: I8bd95106994c4ab217fd92d6afe74b1c380dfe81
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/186274
Commit-Queue: Auto-Submit <[email protected]>
Presubmit-Verified: CQ Bot Account <[email protected]>
Pigweed-Auto-Submit: Armando Montanez <[email protected]>
Reviewed-by: Erik Gilling <[email protected]>
  • Loading branch information
armandomontanez authored and CQ Bot Account committed Dec 28, 2023
1 parent 5d0eaa7 commit 355d707
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions pw_rpc/fuzz/alarm_timer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,45 @@ TEST(AlarmTimerTest, Start) {
}

TEST(AlarmTimerTest, Restart) {
sync::BinarySemaphore sem;
AlarmTimer timer([&sem](chrono::SystemClock::time_point) { sem.release(); });
timer.Start(50ms);
for (size_t i = 0; i < 10; ++i) {
sync::BinarySemaphore final_sem;
sync::BinarySemaphore kick_sem;
constexpr auto kTimerDuration = 200ms;
constexpr auto kTimerKickDuration = 10ms;
constexpr size_t kNumRestarts = 10;
static_assert(kTimerKickDuration < kTimerDuration);

AlarmTimer timer(
[&final_sem](chrono::SystemClock::time_point) { final_sem.release(); });
AlarmTimer timer_kicker(
[&kick_sem](chrono::SystemClock::time_point) { kick_sem.release(); });

timer.Start(chrono::SystemClock::for_at_least(kTimerDuration));

bool acquired = false;
const auto start = chrono::SystemClock::now();
for (size_t i = 0; i < kNumRestarts; ++i) {
// Be overly aggressive with restarting the timer, the point is to ensure
// that it doesn't time out when restareted. Since this tests timings, it
// inherrently is very prone to flake in some environments (e.g. heavy load
// on a Windows machine).
timer.Restart();
EXPECT_FALSE(sem.try_acquire_for(chrono::SystemClock::for_at_least(10us)));
timer_kicker.Start(chrono::SystemClock::for_at_least(kTimerKickDuration));
timer.Restart();
kick_sem.acquire();
timer.Restart();

acquired = final_sem.try_acquire();
EXPECT_FALSE(acquired);
if (acquired) {
break;
}
}
sem.acquire();

if (!acquired) {
final_sem.acquire();
}
auto end = chrono::SystemClock::now();
EXPECT_GT(end - start, kTimerKickDuration * kNumRestarts + kTimerDuration);
}

TEST(AlarmTimerTest, Cancel) {
Expand Down

0 comments on commit 355d707

Please sign in to comment.