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
24 namespace bluetooth {
25 namespace os {
26 namespace {
27
28 using common::BindOnce;
29
30 class AlarmTest : public ::testing::Test {
31 protected:
SetUp()32 void SetUp() override {
33 thread_ = new Thread("test_thread", Thread::Priority::NORMAL);
34 handler_ = new Handler(thread_);
35 alarm_ = new Alarm(handler_);
36 }
37
TearDown()38 void TearDown() override {
39 delete alarm_;
40 handler_->Clear();
41 delete handler_;
42 delete thread_;
43 }
44 Alarm* alarm_;
45
46 private:
47 Handler* handler_;
48 Thread* thread_;
49 };
50
TEST_F(AlarmTest,cancel_while_not_armed)51 TEST_F(AlarmTest, cancel_while_not_armed) {
52 alarm_->Cancel();
53 }
54
TEST_F(AlarmTest,schedule)55 TEST_F(AlarmTest, schedule) {
56 std::promise<void> promise;
57 auto future = promise.get_future();
58 auto before = std::chrono::steady_clock::now();
59 int delay_ms = 10;
60 int delay_error_ms = 3;
61 alarm_->Schedule(
62 BindOnce(&std::promise<void>::set_value, common::Unretained(&promise)), std::chrono::milliseconds(delay_ms));
63 future.get();
64 auto after = std::chrono::steady_clock::now();
65 auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(after - before);
66 ASSERT_NEAR(duration_ms.count(), delay_ms, delay_error_ms);
67 }
68
TEST_F(AlarmTest,cancel_alarm)69 TEST_F(AlarmTest, cancel_alarm) {
70 alarm_->Schedule(BindOnce([]() { ASSERT_TRUE(false) << "Should not happen"; }), std::chrono::milliseconds(3));
71 alarm_->Cancel();
72 std::this_thread::sleep_for(std::chrono::milliseconds(5));
73 }
74
TEST_F(AlarmTest,cancel_alarm_from_callback)75 TEST_F(AlarmTest, cancel_alarm_from_callback) {
76 alarm_->Schedule(BindOnce(&Alarm::Cancel, common::Unretained(alarm_)), std::chrono::milliseconds(1));
77 std::this_thread::sleep_for(std::chrono::milliseconds(5));
78 }
79
TEST_F(AlarmTest,schedule_while_alarm_armed)80 TEST_F(AlarmTest, schedule_while_alarm_armed) {
81 alarm_->Schedule(BindOnce([]() { ASSERT_TRUE(false) << "Should not happen"; }), std::chrono::milliseconds(1));
82 std::promise<void> promise;
83 auto future = promise.get_future();
84 alarm_->Schedule(
85 BindOnce(&std::promise<void>::set_value, common::Unretained(&promise)), std::chrono::milliseconds(10));
86 future.get();
87 }
88
TEST_F(AlarmTest,delete_while_alarm_armed)89 TEST_F(AlarmTest, delete_while_alarm_armed) {
90 alarm_->Schedule(BindOnce([]() { ASSERT_TRUE(false) << "Should not happen"; }), std::chrono::milliseconds(1));
91 delete alarm_;
92 alarm_ = nullptr;
93 std::this_thread::sleep_for(std::chrono::milliseconds(10));
94 }
95
96 } // namespace
97 } // namespace os
98 } // namespace bluetooth
99