diff options
author | 2024-04-22 18:02:37 +0000 | |
---|---|---|
committer | 2024-04-22 18:02:37 +0000 | |
commit | 04f646b8775f401beb49b1db6882ef722e3ee3a9 (patch) | |
tree | 37d04aff119b37dd12ece3bb4d0c94af1daf0caf | |
parent | 9380fb660c931b9330600e75116306b5ee884902 (diff) | |
parent | f86f053e23a5030e2f42eb160faa917ea8b49dc5 (diff) |
Merge "AlarmTest: Synchronize the callback with promises" into main
-rw-r--r-- | system/gd/os/linux_generic/alarm_unittest.cc | 18 | ||||
-rw-r--r-- | system/gd/os/linux_generic/repeating_alarm_unittest.cc | 23 |
2 files changed, 32 insertions, 9 deletions
diff --git a/system/gd/os/linux_generic/alarm_unittest.cc b/system/gd/os/linux_generic/alarm_unittest.cc index 81d16d5c62..d3b07beb89 100644 --- a/system/gd/os/linux_generic/alarm_unittest.cc +++ b/system/gd/os/linux_generic/alarm_unittest.cc @@ -16,7 +16,6 @@ #include "os/alarm.h" -#include <cstddef> #include <future> #include <memory> @@ -85,8 +84,21 @@ TEST_F(AlarmTest, cancel_alarm) { } TEST_F(AlarmTest, cancel_alarm_from_callback) { - alarm_->Schedule(BindOnce(&Alarm::Cancel, alarm_), std::chrono::milliseconds(1)); - std::this_thread::sleep_for(std::chrono::milliseconds(5)); + std::promise<void> promise; + auto future = promise.get_future(); + alarm_->Schedule( + BindOnce( + [](std::shared_ptr<Alarm> alarm, std::promise<void> promise) { + alarm->Cancel(); + alarm.reset(); // Allow alarm to be freed by Teardown + promise.set_value(); + }, + alarm_, + std::move(promise)), + std::chrono::milliseconds(1)); + fake_timer_advance(10); + ASSERT_EQ(std::future_status::ready, future.wait_for(std::chrono::seconds(1))); + ASSERT_EQ(alarm_.use_count(), 1); } TEST_F(AlarmTest, schedule_while_alarm_armed) { diff --git a/system/gd/os/linux_generic/repeating_alarm_unittest.cc b/system/gd/os/linux_generic/repeating_alarm_unittest.cc index 2e2bcdf4ed..a1329845f5 100644 --- a/system/gd/os/linux_generic/repeating_alarm_unittest.cc +++ b/system/gd/os/linux_generic/repeating_alarm_unittest.cc @@ -26,6 +26,7 @@ namespace bluetooth { namespace os { namespace { +using common::BindOnce; using fake_timer::fake_timerfd_advance; using fake_timer::fake_timerfd_reset; @@ -85,7 +86,7 @@ class RepeatingAlarmTest : public ::testing::Test { RepeatingAlarm* alarm_; - common::Closure should_not_happen_ = common::Bind([] { ASSERT_TRUE(false); }); + common::Closure should_not_happen_ = common::Bind([]() { FAIL(); }); private: Thread* thread_; @@ -111,13 +112,23 @@ TEST_F(RepeatingAlarmTest, schedule) { TEST_F(RepeatingAlarmTest, cancel_alarm) { alarm_->Schedule(should_not_happen_, std::chrono::milliseconds(10)); alarm_->Cancel(); - std::this_thread::sleep_for(std::chrono::milliseconds(50)); + fake_timer_advance(10); } TEST_F(RepeatingAlarmTest, cancel_alarm_from_callback) { + std::promise<void> promise; + auto future = promise.get_future(); alarm_->Schedule( - common::Bind(&RepeatingAlarm::Cancel, common::Unretained(this->alarm_)), std::chrono::milliseconds(1)); - std::this_thread::sleep_for(std::chrono::milliseconds(5)); + common::Bind( + [](RepeatingAlarm* alarm, std::promise<void>* promise) { + alarm->Cancel(); + promise->set_value(); + }, + common::Unretained(this->alarm_), + common::Unretained(&promise)), + std::chrono::milliseconds(1)); + fake_timer_advance(1); + ASSERT_EQ(std::future_status::ready, future.wait_for(std::chrono::seconds(1))); } TEST_F(RepeatingAlarmTest, schedule_while_alarm_armed) { @@ -127,7 +138,7 @@ TEST_F(RepeatingAlarmTest, schedule_while_alarm_armed) { alarm_->Schedule( common::Bind(&std::promise<void>::set_value, common::Unretained(&promise)), std::chrono::milliseconds(10)); fake_timer_advance(10); - future.get(); + ASSERT_EQ(std::future_status::ready, future.wait_for(std::chrono::seconds(1))); alarm_->Cancel(); } @@ -135,7 +146,7 @@ TEST_F(RepeatingAlarmTest, delete_while_alarm_armed) { alarm_->Schedule(should_not_happen_, std::chrono::milliseconds(1)); delete alarm_; alarm_ = nullptr; - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + fake_timer_advance(10); } TEST_F(RepeatingAlarmTest, verify_small) { |