• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 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_mainloop_unittest.h"
18 
19 #include <base/callback.h>
20 #include <base/functional/bind.h>
21 #include <base/location.h>
22 #include <base/threading/platform_thread.h>
23 #include <sys/syscall.h>
24 
25 #include <string>
26 
27 #include "gtest/gtest.h"
28 #include "module.h"
29 #include "os/handler.h"
30 #include "os/thread.h"
31 #include "stack/include/main_thread.h"
32 
33 using namespace bluetooth;
34 
35 namespace {
36 constexpr int sync_timeout_in_ms = 3000;
37 
38 std::promise<pid_t> external_function_promise;
39 std::promise<pid_t> private_impl_promise;
40 std::promise<pid_t> protected_method_promise;
41 
42 }  // namespace
43 
44 // Global function with C linkage
external_function_main(int,double,char)45 void external_function_main(int /* a */, double /* b */, char /* c */) {
46   external_function_promise.set_value(base::PlatformThread::CurrentId());
47 }
48 
49 // Module private implementation that is inaccessible externally
50 struct TestModule::PrivateImpl : public ModuleMainloop {
51   const int kMaxTestModuleRecurseDepth = 10;
52 
privateCallableMethodTestModule::PrivateImpl53   void privateCallableMethod(int /* a */, double /* b */, char /* c */) {
54     private_impl_promise.set_value(base::PlatformThread::CurrentId());
55   }
56 
repostMethodTestTestModule::PrivateImpl57   void repostMethodTest(int /* a */, double /* b */, char /* c */) {
58     private_impl_promise.set_value(base::PlatformThread::CurrentId());
59   }
60 
privateCallableRepostMethodTestModule::PrivateImpl61   void privateCallableRepostMethod(
62       std::shared_ptr<TestModule::PrivateImpl> ptr, int a, double b, char c) {
63     PostMethodOnMain(ptr, &PrivateImpl::repostMethodTest, a, b, c);
64   }
65 
privateCallableRecursiveMethodTestModule::PrivateImpl66   void privateCallableRecursiveMethod(
67       std::shared_ptr<TestModule::PrivateImpl> ptr, int depth, double b, char c) {
68     if (depth > kMaxTestModuleRecurseDepth) {
69       private_impl_promise.set_value(base::PlatformThread::CurrentId());
70       return;
71     }
72     PostMethodOnMain(ptr, &PrivateImpl::privateCallableRecursiveMethod, ptr, depth + 1, b, c);
73   }
74 };
75 
76 // Protected module method executed on handler
call_on_handler_protected_method(int loop_tid,int a,int b,int c)77 void TestModule::call_on_handler_protected_method(int loop_tid, int a, int b, int c) {
78   protected_method_promise = std::promise<pid_t>();
79   auto future = protected_method_promise.get_future();
80   CallOn(this, &TestModule::protected_method, a, b, c);
81   ASSERT_EQ(future.wait_for(std::chrono::seconds(3)), std::future_status::ready);
82   ASSERT_EQ(future.get(), loop_tid);
83 }
84 
85 // Global external function executed on main loop
call_on_main_external_function(int loop_tid,int a,double b,char c)86 void TestModule::call_on_main_external_function(int loop_tid, int a, double b, char c) {
87   external_function_promise = std::promise<pid_t>();
88   auto future = external_function_promise.get_future();
89   PostFunctionOnMain(&external_function_main, a, b, c);
90   ASSERT_EQ(future.wait_for(std::chrono::seconds(3)), std::future_status::ready);
91   ASSERT_EQ(future.get(), loop_tid);
92 }
93 
94 // Private implementation method executed on main loop
call_on_main(int loop_tid,int a,int b,int c)95 void TestModule::call_on_main(int loop_tid, int a, int b, int c) {
96   private_impl_promise = std::promise<pid_t>();
97   auto future = private_impl_promise.get_future();
98   PostMethodOnMain(pimpl_, &TestModule::PrivateImpl::privateCallableMethod, a, b, c);
99   ASSERT_EQ(future.wait_for(std::chrono::seconds(3)), std::future_status::ready);
100   ASSERT_EQ(future.get(), loop_tid);
101 }
102 
103 // Private implementation method executed on main loop and reposted
call_on_main_repost(int loop_tid,int a,int b,int c)104 void TestModule::call_on_main_repost(int loop_tid, int a, int b, int c) {
105   private_impl_promise = std::promise<pid_t>();
106   auto future = private_impl_promise.get_future();
107   PostMethodOnMain(pimpl_, &TestModule::PrivateImpl::privateCallableRepostMethod, pimpl_, a, b, c);
108   ASSERT_EQ(future.wait_for(std::chrono::seconds(3)), std::future_status::ready);
109   ASSERT_EQ(future.get(), loop_tid);
110 }
111 
112 // Private implementation method executed on main loop recursively
call_on_main_recurse(int loop_tid,int depth,int b,int c)113 void TestModule::call_on_main_recurse(int loop_tid, int depth, int b, int c) {
114   private_impl_promise = std::promise<pid_t>();
115   auto future = private_impl_promise.get_future();
116   PostMethodOnMain(
117       pimpl_, &TestModule::PrivateImpl::privateCallableRecursiveMethod, pimpl_, depth, b, c);
118   ASSERT_EQ(future.wait_for(std::chrono::seconds(3)), std::future_status::ready);
119   ASSERT_EQ(future.get(), loop_tid);
120 }
121 
protected_method(int,int,int)122 void TestModule::protected_method(int /* a */, int /* b */, int /* c */) {
123   protected_method_promise.set_value(base::PlatformThread::CurrentId());
124 }
125 
IsStarted() const126 bool TestModule::IsStarted() const {
127   return pimpl_ != nullptr;
128 }
129 
Start()130 void TestModule::Start() {
131   ASSERT_FALSE(IsStarted());
132   pimpl_ = std::make_shared<TestModule::PrivateImpl>();
133 }
134 
Stop()135 void TestModule::Stop() {
136   ASSERT_TRUE(IsStarted());
137   pimpl_.reset();
138 }
139 
ToString() const140 std::string TestModule::ToString() const {
141   return std::string(__func__);
142 }
143 
144 const bluetooth::ModuleFactory TestModule::Factory =
__anon87099e730202() 145     bluetooth::ModuleFactory([]() { return new TestModule(); });
146 
147 //
148 // Module GDx Testing Below
149 //
150 class ModuleMainGdxTest : public ::testing::Test {
151  protected:
SetUp()152   void SetUp() override {
153     test_framework_tid_ = base::PlatformThread::CurrentId();
154     module_ = new TestModule();
155     main_thread_start_up();
156     mainloop_tid_ = get_mainloop_tid();
157   }
158 
TearDown()159   void TearDown() override {
160     sync_main_handler();
161     main_thread_shut_down();
162     delete module_;
163   }
164 
sync_main_handler()165   void sync_main_handler() {
166     std::promise promise = std::promise<void>();
167     std::future future = promise.get_future();
168     post_on_bt_main([&promise]() { promise.set_value(); });
169     future.wait_for(std::chrono::milliseconds(sync_timeout_in_ms));
170   };
171 
get_mainloop_tid()172   static pid_t get_mainloop_tid() {
173     std::promise<pid_t> pid_promise = std::promise<pid_t>();
174     auto future = pid_promise.get_future();
175     post_on_bt_main([&pid_promise]() { pid_promise.set_value(base::PlatformThread::CurrentId()); });
176     return future.get();
177   }
178 
179   pid_t test_framework_tid_{-1};
180   pid_t mainloop_tid_{-1};
181   TestModuleRegistry module_registry_;
182   TestModule* module_;
183 };
184 
185 class ModuleMainGdxWithStackTest : public ModuleMainGdxTest {
186  protected:
SetUp()187   void SetUp() override {
188     ModuleMainGdxTest::SetUp();
189     module_registry_.InjectTestModule(&TestModule::Factory, module_ /* pass ownership */);
190     module_ = nullptr;  // ownership is passed
191     handler_tid_ = get_handler_tid(module_registry_.GetTestModuleHandler(&TestModule::Factory));
192   }
193 
get_handler_tid(os::Handler * handler)194   static pid_t get_handler_tid(os::Handler* handler) {
195     std::promise<pid_t> handler_tid_promise = std::promise<pid_t>();
196     std::future<pid_t> future = handler_tid_promise.get_future();
197     handler->Post(common::BindOnce(
198         [](std::promise<pid_t> promise) { promise.set_value(base::PlatformThread::CurrentId()); },
199         std::move(handler_tid_promise)));
200     return future.get();
201   }
202 
TearDown()203   void TearDown() override {
204     module_registry_.StopAll();
205     ModuleMainGdxTest::TearDown();
206   }
207 
Mod()208   TestModule* Mod() {
209     return module_registry_.GetModuleUnderTest<TestModule>();
210   }
211 
212   pid_t handler_tid_{-1};
213 };
214 
TEST_F(ModuleMainGdxTest,nop)215 TEST_F(ModuleMainGdxTest, nop) {}
216 
TEST_F(ModuleMainGdxTest,lifecycle)217 TEST_F(ModuleMainGdxTest, lifecycle) {
218   ::bluetooth::os::Thread* thread =
219       new bluetooth::os::Thread("Name", bluetooth::os::Thread::Priority::REAL_TIME);
220   ASSERT_FALSE(module_registry_.IsStarted<TestModule>());
221   module_registry_.Start<TestModule>(thread);
222   ASSERT_TRUE(module_registry_.IsStarted<TestModule>());
223   module_registry_.StopAll();
224   ASSERT_FALSE(module_registry_.IsStarted<TestModule>());
225   delete thread;
226 }
227 
TEST_F(ModuleMainGdxWithStackTest,call_on_handler_protected_method)228 TEST_F(ModuleMainGdxWithStackTest, call_on_handler_protected_method) {
229   Mod()->call_on_handler_protected_method(handler_tid_, 1, 2, 3);
230 }
231 
TEST_F(ModuleMainGdxWithStackTest,test_call_on_main)232 TEST_F(ModuleMainGdxWithStackTest, test_call_on_main) {
233   Mod()->call_on_main(mainloop_tid_, 1, 2, 3);
234 }
235 
TEST_F(ModuleMainGdxWithStackTest,test_call_external_function)236 TEST_F(ModuleMainGdxWithStackTest, test_call_external_function) {
237   Mod()->call_on_main_external_function(mainloop_tid_, 1, 2.3, 'c');
238 }
239 
TEST_F(ModuleMainGdxWithStackTest,test_call_on_main_repost)240 TEST_F(ModuleMainGdxWithStackTest, test_call_on_main_repost) {
241   Mod()->call_on_main_repost(mainloop_tid_, 1, 2, 3);
242 }
243 
TEST_F(ModuleMainGdxWithStackTest,test_call_on_main_recurse)244 TEST_F(ModuleMainGdxWithStackTest, test_call_on_main_recurse) {
245   Mod()->call_on_main_recurse(mainloop_tid_, 1, 2, 3);
246 }
247