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/repeating_alarm.h"
18 
19 #include <future>
20 
21 #include "common/bind.h"
22 #include "gtest/gtest.h"
23 #include "os/fake_timer/fake_timerfd.h"
24 
25 namespace bluetooth {
26 namespace os {
27 namespace {
28 
29 using common::BindOnce;
30 using fake_timer::fake_timerfd_advance;
31 using fake_timer::fake_timerfd_reset;
32 
33 class RepeatingAlarmTest : public ::testing::Test {
34 protected:
SetUp()35   void SetUp() override {
36     thread_ = new Thread("test_thread", Thread::Priority::NORMAL);
37     handler_ = new Handler(thread_);
38     alarm_ = new RepeatingAlarm(handler_);
39   }
40 
TearDown()41   void TearDown() override {
42     delete alarm_;
43     handler_->Clear();
44     delete handler_;
45     delete thread_;
46     fake_timerfd_reset();
47   }
48 
VerifyMultipleDelayedTasks(int scheduled_tasks,int task_length_ms,int interval_between_tasks_ms)49   void VerifyMultipleDelayedTasks(int scheduled_tasks, int task_length_ms,
50                                   int interval_between_tasks_ms) {
51     std::promise<void> promise;
52     auto future = promise.get_future();
53     auto start_time = std::chrono::steady_clock::now();
54     int counter = 0;
55     alarm_->Schedule(
56             common::Bind(&RepeatingAlarmTest::verify_delayed_tasks, common::Unretained(this),
57                          common::Unretained(&counter), start_time, scheduled_tasks,
58                          common::Unretained(&promise), task_length_ms, interval_between_tasks_ms),
59             std::chrono::milliseconds(interval_between_tasks_ms));
60     fake_timer_advance(interval_between_tasks_ms * scheduled_tasks);
61     future.get();
62     alarm_->Cancel();
63   }
64 
verify_delayed_tasks(int * counter,std::chrono::steady_clock::time_point,int scheduled_tasks,std::promise<void> * promise,int,int)65   void verify_delayed_tasks(int* counter, std::chrono::steady_clock::time_point /* start_time */,
66                             int scheduled_tasks, std::promise<void>* promise,
67                             int /* task_length_ms */, int /* interval_between_tasks_ms */) {
68     *counter = *counter + 1;
69     if (*counter == scheduled_tasks) {
70       promise->set_value();
71     }
72   }
73 
fake_timer_advance(uint64_t ms)74   void fake_timer_advance(uint64_t ms) {
75     handler_->Post(common::BindOnce(fake_timerfd_advance, ms));
76   }
77 
78   RepeatingAlarm* alarm_;
79 
__anon2811a4680202() 80   common::Closure should_not_happen_ = common::Bind([]() { FAIL(); });
81 
82 private:
83   Thread* thread_;
84   Handler* handler_;
85 };
86 
TEST_F(RepeatingAlarmTest,cancel_while_not_armed)87 TEST_F(RepeatingAlarmTest, cancel_while_not_armed) { alarm_->Cancel(); }
88 
TEST_F(RepeatingAlarmTest,schedule)89 TEST_F(RepeatingAlarmTest, schedule) {
90   std::promise<void> promise;
91   auto future = promise.get_future();
92   int period_ms = 10;
93   alarm_->Schedule(common::Bind(&std::promise<void>::set_value, common::Unretained(&promise)),
94                    std::chrono::milliseconds(period_ms));
95   fake_timer_advance(period_ms);
96   future.get();
97   alarm_->Cancel();
98   ASSERT_FALSE(future.valid());
99 }
100 
TEST_F(RepeatingAlarmTest,cancel_alarm)101 TEST_F(RepeatingAlarmTest, cancel_alarm) {
102   alarm_->Schedule(should_not_happen_, std::chrono::milliseconds(10));
103   alarm_->Cancel();
104   fake_timer_advance(10);
105 }
106 
TEST_F(RepeatingAlarmTest,cancel_alarm_from_callback)107 TEST_F(RepeatingAlarmTest, cancel_alarm_from_callback) {
108   std::promise<void> promise;
109   auto future = promise.get_future();
110   alarm_->Schedule(common::Bind(
111                            [](RepeatingAlarm* alarm, std::promise<void>* promise) {
112                              alarm->Cancel();
113                              promise->set_value();
114                            },
115                            common::Unretained(this->alarm_), common::Unretained(&promise)),
116                    std::chrono::milliseconds(1));
117   fake_timer_advance(1);
118   ASSERT_EQ(std::future_status::ready, future.wait_for(std::chrono::seconds(1)));
119 }
120 
TEST_F(RepeatingAlarmTest,schedule_while_alarm_armed)121 TEST_F(RepeatingAlarmTest, schedule_while_alarm_armed) {
122   alarm_->Schedule(should_not_happen_, std::chrono::milliseconds(1));
123   std::promise<void> promise;
124   auto future = promise.get_future();
125   alarm_->Schedule(common::Bind(&std::promise<void>::set_value, common::Unretained(&promise)),
126                    std::chrono::milliseconds(10));
127   fake_timer_advance(10);
128   ASSERT_EQ(std::future_status::ready, future.wait_for(std::chrono::seconds(1)));
129   alarm_->Cancel();
130 }
131 
TEST_F(RepeatingAlarmTest,delete_while_alarm_armed)132 TEST_F(RepeatingAlarmTest, delete_while_alarm_armed) {
133   alarm_->Schedule(should_not_happen_, std::chrono::milliseconds(1));
134   delete alarm_;
135   alarm_ = nullptr;
136   fake_timer_advance(10);
137 }
138 
TEST_F(RepeatingAlarmTest,verify_small)139 TEST_F(RepeatingAlarmTest, verify_small) { VerifyMultipleDelayedTasks(100, 1, 10); }
140 
TEST_F(RepeatingAlarmTest,verify_large)141 TEST_F(RepeatingAlarmTest, verify_large) { VerifyMultipleDelayedTasks(100, 3, 10); }
142 
143 }  // namespace
144 }  // namespace os
145 }  // namespace bluetooth
146