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