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