• 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/handler.h"
18 
19 #include <future>
20 #include <thread>
21 
22 #include "common/bind.h"
23 #include "common/callback.h"
24 #include "gtest/gtest.h"
25 #include "os/log.h"
26 
27 namespace bluetooth {
28 namespace os {
29 namespace {
30 
31 class HandlerTest : public ::testing::Test {
32  protected:
SetUp()33   void SetUp() override {
34     thread_ = new Thread("test_thread", Thread::Priority::NORMAL);
35     handler_ = new Handler(thread_);
36   }
TearDown()37   void TearDown() override {
38     delete handler_;
39     delete thread_;
40   }
41 
42   Handler* handler_;
43   Thread* thread_;
44 };
45 
TEST_F(HandlerTest,empty)46 TEST_F(HandlerTest, empty) {
47   handler_->Clear();
48 }
49 
TEST_F(HandlerTest,post_task_invoked)50 TEST_F(HandlerTest, post_task_invoked) {
51   int val = 0;
52   std::promise<void> closure_ran;
53   auto future = closure_ran.get_future();
54   common::OnceClosure closure = common::BindOnce(
55       [](int* val, std::promise<void> closure_ran) {
56         *val = *val + 1;
57         closure_ran.set_value();
58       },
59       common::Unretained(&val),
60       std::move(closure_ran));
61   handler_->Post(std::move(closure));
62   future.wait();
63   ASSERT_EQ(val, 1);
64   handler_->Clear();
65 }
66 
TEST_F(HandlerTest,post_task_cleared)67 TEST_F(HandlerTest, post_task_cleared) {
68   int val = 0;
69   std::promise<void> closure_started;
70   auto closure_started_future = closure_started.get_future();
71   std::promise<void> closure_can_continue;
72   auto can_continue_future = closure_can_continue.get_future();
73   std::promise<void> closure_finished;
74   auto closure_finished_future = closure_finished.get_future();
75   handler_->Post(common::BindOnce(
76       [](int* val,
77          std::promise<void> closure_started,
78          std::future<void> can_continue_future,
79          std::promise<void> closure_finished) {
80         closure_started.set_value();
81         *val = *val + 1;
82         can_continue_future.wait();
83         closure_finished.set_value();
84       },
85       common::Unretained(&val),
86       std::move(closure_started),
87       std::move(can_continue_future),
88       std::move(closure_finished)));
89   handler_->Post(common::BindOnce([]() { ASSERT_TRUE(false); }));
90   closure_started_future.wait();
91   handler_->Clear();
92   closure_can_continue.set_value();
93   closure_finished_future.wait();
94   ASSERT_EQ(val, 1);
95 }
96 
check_int(std::unique_ptr<int> number,std::shared_ptr<int> to_change)97 void check_int(std::unique_ptr<int> number, std::shared_ptr<int> to_change) {
98   *to_change = *number;
99 }
100 
TEST_F(HandlerTest,once_callback)101 TEST_F(HandlerTest, once_callback) {
102   auto number = std::make_unique<int>(1);
103   auto to_change = std::make_shared<int>(0);
104   auto once_callback = common::BindOnce(&check_int, std::move(number), to_change);
105   std::move(once_callback).Run();
106   EXPECT_EQ(*to_change, 1);
107   handler_->Clear();
108 }
109 
TEST_F(HandlerTest,callback_with_promise)110 TEST_F(HandlerTest, callback_with_promise) {
111   std::promise<void> promise;
112   auto future = promise.get_future();
113   auto once_callback = common::BindOnce(&std::promise<void>::set_value, common::Unretained(&promise));
114   std::move(once_callback).Run();
115   future.wait();
116   handler_->Clear();
117 }
118 
119 // For Death tests, all the threading needs to be done in the ASSERT_DEATH call
120 class HandlerDeathTest : public ::testing::Test {
121  protected:
ThreadSetUp()122   void ThreadSetUp() {
123     thread_ = new Thread("test_thread", Thread::Priority::NORMAL);
124     handler_ = new Handler(thread_);
125   }
126 
ThreadTearDown()127   void ThreadTearDown() {
128     delete handler_;
129     delete thread_;
130   }
131 
ClearTwice()132   void ClearTwice() {
133     ThreadSetUp();
134     handler_->Clear();
135     handler_->Clear();
136     ThreadTearDown();
137   }
138 
NotCleared()139   void NotCleared() {
140     ThreadSetUp();
141     ThreadTearDown();
142   }
143 
144   Handler* handler_;
145   Thread* thread_;
146 };
147 
TEST_F(HandlerDeathTest,clear_after_handler_cleared)148 TEST_F(HandlerDeathTest, clear_after_handler_cleared) {
149   ASSERT_DEATH(ClearTwice(), "Handlers must only be cleared once");
150 }
151 
TEST_F(HandlerDeathTest,not_cleared_before_destruction)152 TEST_F(HandlerDeathTest, not_cleared_before_destruction) {
153   ASSERT_DEATH(NotCleared(), "Handlers must be cleared");
154 }
155 
156 }  // namespace
157 }  // namespace os
158 }  // namespace bluetooth
159