• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 AlarmTest : 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 Alarm(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 
fake_timer_advance(uint64_t ms)49   void fake_timer_advance(uint64_t ms) {
50     handler_->Post(common::BindOnce(fake_timerfd_advance, ms));
51   }
52   Alarm* alarm_;
53 
54  private:
55   Handler* handler_;
56   Thread* thread_;
57 };
58 
TEST_F(AlarmTest,cancel_while_not_armed)59 TEST_F(AlarmTest, cancel_while_not_armed) {
60   alarm_->Cancel();
61 }
62 
TEST_F(AlarmTest,schedule)63 TEST_F(AlarmTest, schedule) {
64   std::promise<void> promise;
65   auto future = promise.get_future();
66   int delay_ms = 10;
67   alarm_->Schedule(
68       BindOnce(&std::promise<void>::set_value, common::Unretained(&promise)), std::chrono::milliseconds(delay_ms));
69   fake_timer_advance(10);
70   future.get();
71   ASSERT_FALSE(future.valid());
72 }
73 
TEST_F(AlarmTest,cancel_alarm)74 TEST_F(AlarmTest, cancel_alarm) {
75   alarm_->Schedule(BindOnce([]() { ASSERT_TRUE(false) << "Should not happen"; }), std::chrono::milliseconds(3));
76   alarm_->Cancel();
77   std::this_thread::sleep_for(std::chrono::milliseconds(5));
78 }
79 
TEST_F(AlarmTest,cancel_alarm_from_callback)80 TEST_F(AlarmTest, cancel_alarm_from_callback) {
81   alarm_->Schedule(BindOnce(&Alarm::Cancel, common::Unretained(alarm_)), std::chrono::milliseconds(1));
82   std::this_thread::sleep_for(std::chrono::milliseconds(5));
83 }
84 
TEST_F(AlarmTest,schedule_while_alarm_armed)85 TEST_F(AlarmTest, schedule_while_alarm_armed) {
86   alarm_->Schedule(BindOnce([]() { ASSERT_TRUE(false) << "Should not happen"; }), std::chrono::milliseconds(1));
87   std::promise<void> promise;
88   auto future = promise.get_future();
89   alarm_->Schedule(
90       BindOnce(&std::promise<void>::set_value, common::Unretained(&promise)), std::chrono::milliseconds(10));
91   fake_timer_advance(10);
92   future.get();
93 }
94 
TEST_F(AlarmTest,delete_while_alarm_armed)95 TEST_F(AlarmTest, delete_while_alarm_armed) {
96   alarm_->Schedule(BindOnce([]() { ASSERT_TRUE(false) << "Should not happen"; }), std::chrono::milliseconds(1));
97   delete alarm_;
98   alarm_ = nullptr;
99   std::this_thread::sleep_for(std::chrono::milliseconds(10));
100 }
101 
102 }  // namespace
103 }  // namespace os
104 }  // namespace bluetooth
105