1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "os/alarm.h"
18 
19 #include <future>
20 #include <memory>
21 
22 #include "common/bind.h"
23 #include "gtest/gtest.h"
24 #include "os/fake_timer/fake_timerfd.h"
25 
26 namespace bluetooth {
27 namespace os {
28 namespace {
29 
30 using common::BindOnce;
31 using fake_timer::fake_timerfd_advance;
32 using fake_timer::fake_timerfd_reset;
33 
34 class AlarmTest : public ::testing::TestWithParam<bool> {
35 protected:
SetUp()36   void SetUp() override {
37     bool isWakeAlarm = GetParam();
38     thread_ = new Thread("test_thread", Thread::Priority::NORMAL);
39     handler_ = new Handler(thread_);
40     alarm_ = std::make_shared<Alarm>(handler_, isWakeAlarm);
41   }
42 
TearDown()43   void TearDown() override {
44     alarm_.reset();
45     handler_->Clear();
46     delete handler_;
47     delete thread_;
48     fake_timerfd_reset();
49   }
50 
fake_timer_advance(uint64_t ms)51   void fake_timer_advance(uint64_t ms) {
52     handler_->Post(common::BindOnce(fake_timerfd_advance, ms));
53   }
54 
get_new_alarm()55   std::shared_ptr<Alarm> get_new_alarm() { return std::make_shared<Alarm>(handler_, GetParam()); }
56 
57   std::shared_ptr<Alarm> alarm_;
58 
59 private:
60   Handler* handler_;
61   Thread* thread_;
62 };
63 
TEST_P(AlarmTest,cancel_while_not_armed)64 TEST_P(AlarmTest, cancel_while_not_armed) { alarm_->Cancel(); }
65 
TEST_P(AlarmTest,schedule)66 TEST_P(AlarmTest, schedule) {
67   std::promise<void> promise;
68   auto future = promise.get_future();
69   int delay_ms = 10;
70   alarm_->Schedule(BindOnce(&std::promise<void>::set_value, common::Unretained(&promise)),
71                    std::chrono::milliseconds(delay_ms));
72   fake_timer_advance(10);
73   future.get();
74   ASSERT_FALSE(future.valid());
75 }
76 
TEST_P(AlarmTest,cancel_alarm)77 TEST_P(AlarmTest, cancel_alarm) {
78   alarm_->Schedule(BindOnce([]() { FAIL() << "Should not happen"; }), std::chrono::milliseconds(3));
79   alarm_->Cancel();
80   std::this_thread::sleep_for(std::chrono::milliseconds(5));
81 }
82 
TEST_P(AlarmTest,cancel_alarm_from_callback)83 TEST_P(AlarmTest, cancel_alarm_from_callback) {
84   std::promise<void> promise;
85   auto future = promise.get_future();
86   alarm_->Schedule(BindOnce(
87                            [](std::shared_ptr<Alarm> alarm, std::promise<void> promise) {
88                              alarm->Cancel();
89                              alarm.reset();  // Allow alarm to be freed by Teardown
90                              promise.set_value();
91                            },
92                            alarm_, std::move(promise)),
93                    std::chrono::milliseconds(1));
94   fake_timer_advance(10);
95   ASSERT_EQ(std::future_status::ready, future.wait_for(std::chrono::seconds(1)));
96   ASSERT_EQ(alarm_.use_count(), 1);
97 }
98 
TEST_P(AlarmTest,schedule_while_alarm_armed)99 TEST_P(AlarmTest, schedule_while_alarm_armed) {
100   alarm_->Schedule(BindOnce([]() { FAIL() << "Should not happen"; }), std::chrono::milliseconds(1));
101   std::promise<void> promise;
102   auto future = promise.get_future();
103   alarm_->Schedule(BindOnce(&std::promise<void>::set_value, common::Unretained(&promise)),
104                    std::chrono::milliseconds(10));
105   fake_timer_advance(10);
106   future.get();
107 }
108 
TEST_P(AlarmTest,delete_while_alarm_armed)109 TEST_P(AlarmTest, delete_while_alarm_armed) {
110   alarm_->Schedule(BindOnce([]() { FAIL() << "Should not happen"; }), std::chrono::milliseconds(1));
111   alarm_.reset();
112   std::this_thread::sleep_for(std::chrono::milliseconds(10));
113 }
114 
115 class TwoAlarmTest : public AlarmTest {
116 protected:
SetUp()117   void SetUp() override {
118     AlarmTest::SetUp();
119     alarm2 = get_new_alarm();
120   }
121 
TearDown()122   void TearDown() override {
123     alarm2.reset();
124     AlarmTest::TearDown();
125   }
126 
127   std::shared_ptr<Alarm> alarm2;
128 };
129 
TEST_P(TwoAlarmTest,schedule_from_alarm_long)130 TEST_P(TwoAlarmTest, schedule_from_alarm_long) {
131   auto promise = std::make_unique<std::promise<void>>();
132   auto future = promise->get_future();
133   auto promise2 = std::make_unique<std::promise<void>>();
134   auto future2 = promise2->get_future();
135   alarm_->Schedule(
136           BindOnce(
137                   [](std::shared_ptr<Alarm> alarm2, std::unique_ptr<std::promise<void>> promise,
138                      std::unique_ptr<std::promise<void>> promise2) {
139                     promise->set_value();
140                     alarm2->Schedule(BindOnce(&std::promise<void>::set_value, std::move(promise2)),
141                                      std::chrono::milliseconds(10));
142                   },
143                   alarm2, std::move(promise), std::move(promise2)),
144           std::chrono::milliseconds(1));
145   fake_timer_advance(10);
146   EXPECT_EQ(std::future_status::ready, future.wait_for(std::chrono::milliseconds(20)));
147   fake_timer_advance(10);
148   EXPECT_EQ(std::future_status::ready, future2.wait_for(std::chrono::milliseconds(20)));
149 }
150 
151 INSTANTIATE_TEST_SUITE_P(
152         /* no label */, AlarmTest, ::testing::Bool());
153 
154 INSTANTIATE_TEST_SUITE_P(
155         /* no label */, TwoAlarmTest, ::testing::Bool());
156 
157 }  // namespace
158 }  // namespace os
159 }  // namespace bluetooth
160