• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 "com_android_bluetooth_flags.h"
26 #include "gtest/gtest.h"
27 #include "os/handler.h"
28 #include "os/thread.h"
29 
30 using ::bluetooth::os::Thread;
31 
32 namespace bluetooth {
33 namespace {
34 
35 class ModuleTest : public ::testing::Test {
36 protected:
SetUp()37   void SetUp() override {
38     thread_ = new Thread("test_thread", Thread::Priority::NORMAL);
39     handler_ = new os::Handler(thread_);
40     registry_ = new ModuleRegistry();
41   }
42 
TearDown()43   void TearDown() override {
44     handler_->Clear();
45     if (com::android::bluetooth::flags::same_handler_for_all_modules()) {
46       handler_->WaitUntilStopped(kHandlerStopTimeout);
47     }
48     delete registry_;
49     delete handler_;
50     delete thread_;
51   }
52 
53   ModuleRegistry* registry_;
54   Thread* thread_;
55   os::Handler* handler_;
56 };
57 
58 os::Handler* test_module_no_dependency_handler = nullptr;
59 
60 class TestModuleNoDependency : public Module {
61 public:
62   static const ModuleFactory Factory;
63 
64 protected:
ListDependencies(ModuleList *) const65   void ListDependencies(ModuleList* /* list */) const {}
66 
Start()67   void Start() override {
68     // A module is not considered started until Start() finishes
69     EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
70     test_module_no_dependency_handler = GetHandler();
71   }
72 
Stop()73   void Stop() override {
74     // A module is not considered stopped until after Stop() finishes
75     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
76   }
77 
ToString() const78   std::string ToString() const override { return std::string("TestModuleNoDependency"); }
79 };
80 
81 const ModuleFactory TestModuleNoDependency::Factory =
__anon2fc164770202() 82         ModuleFactory([]() { return new TestModuleNoDependency(); });
83 
84 os::Handler* test_module_one_dependency_handler = nullptr;
85 
86 class TestModuleOneDependency : public Module {
87 public:
88   static const ModuleFactory Factory;
89 
90 protected:
ListDependencies(ModuleList * list) const91   void ListDependencies(ModuleList* list) const { list->add<TestModuleNoDependency>(); }
92 
Start()93   void Start() override {
94     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
95 
96     // A module is not considered started until Start() finishes
97     EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
98     test_module_one_dependency_handler = GetHandler();
99   }
100 
Stop()101   void Stop() override {
102     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
103 
104     // A module is not considered stopped until after Stop() finishes
105     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
106   }
107 
ToString() const108   std::string ToString() const override { return std::string("TestModuleOneDependency"); }
109 };
110 
111 const ModuleFactory TestModuleOneDependency::Factory =
__anon2fc164770302() 112         ModuleFactory([]() { return new TestModuleOneDependency(); });
113 
114 class TestModuleNoDependencyTwo : public Module {
115 public:
116   static const ModuleFactory Factory;
117 
118 protected:
ListDependencies(ModuleList *) const119   void ListDependencies(ModuleList* /* list */) const {}
120 
Start()121   void Start() override {
122     // A module is not considered started until Start() finishes
123     EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
124   }
125 
Stop()126   void Stop() override {
127     // A module is not considered stopped until after Stop() finishes
128     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
129   }
130 
ToString() const131   std::string ToString() const override { return std::string("TestModuleNoDependencyTwo"); }
132 };
133 
134 const ModuleFactory TestModuleNoDependencyTwo::Factory =
__anon2fc164770402() 135         ModuleFactory([]() { return new TestModuleNoDependencyTwo(); });
136 
137 class TestModuleTwoDependencies : public Module {
138 public:
139   static const ModuleFactory Factory;
140 
141 protected:
ListDependencies(ModuleList * list) const142   void ListDependencies(ModuleList* list) const {
143     list->add<TestModuleOneDependency>();
144     list->add<TestModuleNoDependencyTwo>();
145   }
146 
Start()147   void Start() override {
148     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
149     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
150 
151     // A module is not considered started until Start() finishes
152     EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleTwoDependencies>());
153   }
154 
Stop()155   void Stop() override {
156     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
157     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
158 
159     // A module is not considered stopped until after Stop() finishes
160     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleTwoDependencies>());
161   }
162 
ToString() const163   std::string ToString() const override { return std::string("TestModuleTwoDependencies"); }
164 };
165 
166 const ModuleFactory TestModuleTwoDependencies::Factory =
__anon2fc164770502() 167         ModuleFactory([]() { return new TestModuleTwoDependencies(); });
168 
TEST_F(ModuleTest,no_dependency)169 TEST_F(ModuleTest, no_dependency) {
170   ModuleList list;
171   list.add<TestModuleNoDependency>();
172   registry_->Start(&list, thread_, handler_);
173 
174   EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependency>());
175   EXPECT_FALSE(registry_->IsStarted<TestModuleOneDependency>());
176   EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>());
177   EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>());
178 
179   registry_->StopAll();
180 
181   EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependency>());
182   EXPECT_FALSE(registry_->IsStarted<TestModuleOneDependency>());
183   EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>());
184   EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>());
185 }
186 
TEST_F(ModuleTest,one_dependency)187 TEST_F(ModuleTest, one_dependency) {
188   ModuleList list;
189   list.add<TestModuleOneDependency>();
190   registry_->Start(&list, thread_, handler_);
191 
192   EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependency>());
193   EXPECT_TRUE(registry_->IsStarted<TestModuleOneDependency>());
194   EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>());
195   EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>());
196 
197   registry_->StopAll();
198 
199   EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependency>());
200   EXPECT_FALSE(registry_->IsStarted<TestModuleOneDependency>());
201   EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>());
202   EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>());
203 }
204 
TEST_F(ModuleTest,two_dependencies)205 TEST_F(ModuleTest, two_dependencies) {
206   ModuleList list;
207   list.add<TestModuleTwoDependencies>();
208   registry_->Start(&list, thread_, handler_);
209 
210   EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependency>());
211   EXPECT_TRUE(registry_->IsStarted<TestModuleOneDependency>());
212   EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependencyTwo>());
213   EXPECT_TRUE(registry_->IsStarted<TestModuleTwoDependencies>());
214 
215   registry_->StopAll();
216 
217   EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependency>());
218   EXPECT_FALSE(registry_->IsStarted<TestModuleOneDependency>());
219   EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>());
220   EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>());
221 }
222 
post_to_module_one_handler()223 void post_to_module_one_handler() {
224   std::this_thread::sleep_for(std::chrono::milliseconds(100));
225   test_module_one_dependency_handler->Post(common::BindOnce([] { FAIL(); }));
226 }
227 
TEST_F(ModuleTest,shutdown_with_unhandled_callback)228 TEST_F(ModuleTest, shutdown_with_unhandled_callback) {
229   ModuleList list;
230   list.add<TestModuleOneDependency>();
231   registry_->Start(&list, thread_, handler_);
232   test_module_no_dependency_handler->Post(common::BindOnce(&post_to_module_one_handler));
233   registry_->StopAll();
234 }
235 
236 }  // namespace
237 }  // namespace bluetooth
238