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