Loading system/gd/hci/hci_layer_test.cc +5 −8 Original line number Diff line number Diff line Loading @@ -173,18 +173,16 @@ class HciTest : public ::testing::Test { counting_bytes.push_back(i); counting_down_bytes.push_back(~i); } thread_ = new Thread("test_thread", Thread::Priority::NORMAL); hal = new TestHciHal(); fake_registry_.inject_test_module(&hal::HciHal::Factory, hal, thread_); fake_registry_.Start<DependsOnHci>(thread_); hci = fake_registry_.get_module_under_test<HciLayer>(); upper = fake_registry_.get_module_under_test<DependsOnHci>(); fake_registry_.InjectTestModule(&hal::HciHal::Factory, hal); fake_registry_.StartTestModule<DependsOnHci>(); hci = static_cast<HciLayer*>(fake_registry_.GetModuleUnderTest(&HciLayer::Factory)); upper = static_cast<DependsOnHci*>(fake_registry_.GetModuleUnderTest(&DependsOnHci::Factory)); ASSERT(fake_registry_.IsStarted<HciLayer>()); } void TearDown() override { fake_registry_.StopAll(); delete thread_; } std::vector<uint8_t> GetPacketBytes(std::unique_ptr<packet::BasePacketBuilder> packet) { Loading @@ -198,8 +196,7 @@ class HciTest : public ::testing::Test { DependsOnHci* upper = nullptr; TestHciHal* hal = nullptr; HciLayer* hci = nullptr; ModuleRegistry fake_registry_; Thread* thread_ = nullptr; TestModuleRegistry fake_registry_; }; TEST_F(HciTest, initAndClose) {} Loading system/gd/module.cc +6 −7 Original line number Diff line number Diff line Loading @@ -95,12 +95,11 @@ void ModuleRegistry::StopAll() { start_order_.clear(); } void ModuleRegistry::inject_test_module(const ModuleFactory* module, Module* instance, os::Thread* thread) { instance->registry_ = this; instance->handler_ = new Handler(thread); start_order_.push_back(module); started_modules_[module] = instance; instance->Start(); os::Handler* ModuleRegistry::GetModuleHandler(const ModuleFactory* module) const { auto started_instance = started_modules_.find(module); if (started_instance != started_modules_.end()) { return started_instance->second->GetHandler(); } return nullptr; } } // namespace bluetooth system/gd/module.h +39 −11 Original line number Diff line number Diff line Loading @@ -17,8 +17,9 @@ #pragma once #include <functional> #include <vector> #include <future> #include <map> #include <vector> #include "os/log.h" #include "os/handler.h" Loading Loading @@ -115,20 +116,47 @@ class ModuleRegistry { // Stop all running modules in reverse order of start void StopAll(); // Helper for dependency injection in test code. DO NOT USE in prod code! // Ownership of |instance| is transferred to the registry. void inject_test_module(const ModuleFactory* module, Module* instance, os::Thread* thread); protected: Module* Get(const ModuleFactory* module) const; // Helper for dependency injection in test code. DO NOT USE in prod code! template <class T> T* get_module_under_test() const { return static_cast<T*>(Get(&T::Factory)); } os::Handler* GetModuleHandler(const ModuleFactory* module) const; private: Module* Get(const ModuleFactory* module) const; std::map<const ModuleFactory*, Module*> started_modules_; std::vector<const ModuleFactory*> start_order_; }; class TestModuleRegistry : public ModuleRegistry { public: void InjectTestModule(const ModuleFactory* module, Module* instance) { start_order_.push_back(module); started_modules_[module] = instance; } Module* GetModuleUnderTest(const ModuleFactory* module) const { return Get(module); } os::Handler* GetTestModuleHandler(const ModuleFactory* module) const { return GetModuleHandler(module); } os::Thread& GetTestThread() { return test_thread; } template <class T> T* StartTestModule() { return Start<T>(&test_thread); } bool SynchronizeModuleHandler(const ModuleFactory* module, std::chrono::milliseconds timeout) const { std::promise<void> promise; os::Handler* handler = GetTestModuleHandler(module); handler->Post([&promise] { promise.set_value(); }); return promise.get_future().wait_for(timeout) == std::future_status::ready; } os::Thread test_thread{"test_thread", os::Thread::Priority::NORMAL}; }; } // namespace bluetooth Loading
system/gd/hci/hci_layer_test.cc +5 −8 Original line number Diff line number Diff line Loading @@ -173,18 +173,16 @@ class HciTest : public ::testing::Test { counting_bytes.push_back(i); counting_down_bytes.push_back(~i); } thread_ = new Thread("test_thread", Thread::Priority::NORMAL); hal = new TestHciHal(); fake_registry_.inject_test_module(&hal::HciHal::Factory, hal, thread_); fake_registry_.Start<DependsOnHci>(thread_); hci = fake_registry_.get_module_under_test<HciLayer>(); upper = fake_registry_.get_module_under_test<DependsOnHci>(); fake_registry_.InjectTestModule(&hal::HciHal::Factory, hal); fake_registry_.StartTestModule<DependsOnHci>(); hci = static_cast<HciLayer*>(fake_registry_.GetModuleUnderTest(&HciLayer::Factory)); upper = static_cast<DependsOnHci*>(fake_registry_.GetModuleUnderTest(&DependsOnHci::Factory)); ASSERT(fake_registry_.IsStarted<HciLayer>()); } void TearDown() override { fake_registry_.StopAll(); delete thread_; } std::vector<uint8_t> GetPacketBytes(std::unique_ptr<packet::BasePacketBuilder> packet) { Loading @@ -198,8 +196,7 @@ class HciTest : public ::testing::Test { DependsOnHci* upper = nullptr; TestHciHal* hal = nullptr; HciLayer* hci = nullptr; ModuleRegistry fake_registry_; Thread* thread_ = nullptr; TestModuleRegistry fake_registry_; }; TEST_F(HciTest, initAndClose) {} Loading
system/gd/module.cc +6 −7 Original line number Diff line number Diff line Loading @@ -95,12 +95,11 @@ void ModuleRegistry::StopAll() { start_order_.clear(); } void ModuleRegistry::inject_test_module(const ModuleFactory* module, Module* instance, os::Thread* thread) { instance->registry_ = this; instance->handler_ = new Handler(thread); start_order_.push_back(module); started_modules_[module] = instance; instance->Start(); os::Handler* ModuleRegistry::GetModuleHandler(const ModuleFactory* module) const { auto started_instance = started_modules_.find(module); if (started_instance != started_modules_.end()) { return started_instance->second->GetHandler(); } return nullptr; } } // namespace bluetooth
system/gd/module.h +39 −11 Original line number Diff line number Diff line Loading @@ -17,8 +17,9 @@ #pragma once #include <functional> #include <vector> #include <future> #include <map> #include <vector> #include "os/log.h" #include "os/handler.h" Loading Loading @@ -115,20 +116,47 @@ class ModuleRegistry { // Stop all running modules in reverse order of start void StopAll(); // Helper for dependency injection in test code. DO NOT USE in prod code! // Ownership of |instance| is transferred to the registry. void inject_test_module(const ModuleFactory* module, Module* instance, os::Thread* thread); protected: Module* Get(const ModuleFactory* module) const; // Helper for dependency injection in test code. DO NOT USE in prod code! template <class T> T* get_module_under_test() const { return static_cast<T*>(Get(&T::Factory)); } os::Handler* GetModuleHandler(const ModuleFactory* module) const; private: Module* Get(const ModuleFactory* module) const; std::map<const ModuleFactory*, Module*> started_modules_; std::vector<const ModuleFactory*> start_order_; }; class TestModuleRegistry : public ModuleRegistry { public: void InjectTestModule(const ModuleFactory* module, Module* instance) { start_order_.push_back(module); started_modules_[module] = instance; } Module* GetModuleUnderTest(const ModuleFactory* module) const { return Get(module); } os::Handler* GetTestModuleHandler(const ModuleFactory* module) const { return GetModuleHandler(module); } os::Thread& GetTestThread() { return test_thread; } template <class T> T* StartTestModule() { return Start<T>(&test_thread); } bool SynchronizeModuleHandler(const ModuleFactory* module, std::chrono::milliseconds timeout) const { std::promise<void> promise; os::Handler* handler = GetTestModuleHandler(module); handler->Post([&promise] { promise.set_value(); }); return promise.get_future().wait_for(timeout) == std::future_status::ready; } os::Thread test_thread{"test_thread", os::Thread::Priority::NORMAL}; }; } // namespace bluetooth