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 "module.h"
18
19 #include <unistd.h>
20
21 #include <functional>
22 #include <sstream>
23 #include <string>
24
25 #include "gtest/gtest.h"
26 #include "os/handler.h"
27 #include "os/thread.h"
28
29 using ::bluetooth::os::Thread;
30
31 namespace bluetooth {
32 namespace {
33
34 class ModuleTest : public ::testing::Test {
35 protected:
SetUp()36 void SetUp() override {
37 thread_ = new Thread("test_thread", Thread::Priority::NORMAL);
38 registry_ = new ModuleRegistry();
39 }
40
TearDown()41 void TearDown() override {
42 delete registry_;
43 delete thread_;
44 }
45
46 ModuleRegistry* registry_;
47 Thread* thread_;
48 };
49
50 os::Handler* test_module_no_dependency_handler = nullptr;
51
52 class TestModuleNoDependency : public Module {
53 public:
54 static const ModuleFactory Factory;
55
56 protected:
ListDependencies(ModuleList *) const57 void ListDependencies(ModuleList* /* list */) const {}
58
Start()59 void Start() override {
60 // A module is not considered started until Start() finishes
61 EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
62 test_module_no_dependency_handler = GetHandler();
63 }
64
Stop()65 void Stop() override {
66 // A module is not considered stopped until after Stop() finishes
67 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
68 }
69
ToString() const70 std::string ToString() const override { return std::string("TestModuleNoDependency"); }
71 };
72
73 const ModuleFactory TestModuleNoDependency::Factory =
__anona53e19f70202() 74 ModuleFactory([]() { return new TestModuleNoDependency(); });
75
76 os::Handler* test_module_one_dependency_handler = nullptr;
77
78 class TestModuleOneDependency : public Module {
79 public:
80 static const ModuleFactory Factory;
81
82 protected:
ListDependencies(ModuleList * list) const83 void ListDependencies(ModuleList* list) const { list->add<TestModuleNoDependency>(); }
84
Start()85 void Start() override {
86 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
87
88 // A module is not considered started until Start() finishes
89 EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
90 test_module_one_dependency_handler = GetHandler();
91 }
92
Stop()93 void Stop() override {
94 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
95
96 // A module is not considered stopped until after Stop() finishes
97 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
98 }
99
ToString() const100 std::string ToString() const override { return std::string("TestModuleOneDependency"); }
101 };
102
103 const ModuleFactory TestModuleOneDependency::Factory =
__anona53e19f70302() 104 ModuleFactory([]() { return new TestModuleOneDependency(); });
105
106 class TestModuleNoDependencyTwo : public Module {
107 public:
108 static const ModuleFactory Factory;
109
110 protected:
ListDependencies(ModuleList *) const111 void ListDependencies(ModuleList* /* list */) const {}
112
Start()113 void Start() override {
114 // A module is not considered started until Start() finishes
115 EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
116 }
117
Stop()118 void Stop() override {
119 // A module is not considered stopped until after Stop() finishes
120 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
121 }
122
ToString() const123 std::string ToString() const override { return std::string("TestModuleNoDependencyTwo"); }
124 };
125
126 const ModuleFactory TestModuleNoDependencyTwo::Factory =
__anona53e19f70402() 127 ModuleFactory([]() { return new TestModuleNoDependencyTwo(); });
128
129 class TestModuleTwoDependencies : public Module {
130 public:
131 static const ModuleFactory Factory;
132
133 protected:
ListDependencies(ModuleList * list) const134 void ListDependencies(ModuleList* list) const {
135 list->add<TestModuleOneDependency>();
136 list->add<TestModuleNoDependencyTwo>();
137 }
138
Start()139 void Start() override {
140 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
141 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
142
143 // A module is not considered started until Start() finishes
144 EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleTwoDependencies>());
145 }
146
Stop()147 void Stop() override {
148 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
149 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
150
151 // A module is not considered stopped until after Stop() finishes
152 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleTwoDependencies>());
153 }
154
ToString() const155 std::string ToString() const override { return std::string("TestModuleTwoDependencies"); }
156 };
157
158 const ModuleFactory TestModuleTwoDependencies::Factory =
__anona53e19f70502() 159 ModuleFactory([]() { return new TestModuleTwoDependencies(); });
160
TEST_F(ModuleTest,no_dependency)161 TEST_F(ModuleTest, no_dependency) {
162 ModuleList list;
163 list.add<TestModuleNoDependency>();
164 registry_->Start(&list, thread_);
165
166 EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependency>());
167 EXPECT_FALSE(registry_->IsStarted<TestModuleOneDependency>());
168 EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>());
169 EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>());
170
171 registry_->StopAll();
172
173 EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependency>());
174 EXPECT_FALSE(registry_->IsStarted<TestModuleOneDependency>());
175 EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>());
176 EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>());
177 }
178
TEST_F(ModuleTest,one_dependency)179 TEST_F(ModuleTest, one_dependency) {
180 ModuleList list;
181 list.add<TestModuleOneDependency>();
182 registry_->Start(&list, thread_);
183
184 EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependency>());
185 EXPECT_TRUE(registry_->IsStarted<TestModuleOneDependency>());
186 EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>());
187 EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>());
188
189 registry_->StopAll();
190
191 EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependency>());
192 EXPECT_FALSE(registry_->IsStarted<TestModuleOneDependency>());
193 EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>());
194 EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>());
195 }
196
TEST_F(ModuleTest,two_dependencies)197 TEST_F(ModuleTest, two_dependencies) {
198 ModuleList list;
199 list.add<TestModuleTwoDependencies>();
200 registry_->Start(&list, thread_);
201
202 EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependency>());
203 EXPECT_TRUE(registry_->IsStarted<TestModuleOneDependency>());
204 EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependencyTwo>());
205 EXPECT_TRUE(registry_->IsStarted<TestModuleTwoDependencies>());
206
207 registry_->StopAll();
208
209 EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependency>());
210 EXPECT_FALSE(registry_->IsStarted<TestModuleOneDependency>());
211 EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>());
212 EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>());
213 }
214
post_to_module_one_handler()215 void post_to_module_one_handler() {
216 std::this_thread::sleep_for(std::chrono::milliseconds(100));
217 test_module_one_dependency_handler->Post(common::BindOnce([] { FAIL(); }));
218 }
219
TEST_F(ModuleTest,shutdown_with_unhandled_callback)220 TEST_F(ModuleTest, shutdown_with_unhandled_callback) {
221 ModuleList list;
222 list.add<TestModuleOneDependency>();
223 registry_->Start(&list, thread_);
224 test_module_no_dependency_handler->Post(common::BindOnce(&post_to_module_one_handler));
225 registry_->StopAll();
226 }
227
228 } // namespace
229 } // namespace bluetooth
230