Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 10a670e7 authored by Hansong Zhang's avatar Hansong Zhang Committed by android-build-merger
Browse files

Stack manager: Clear handler during ShutDown()

am: d1b2a0bc

Change-Id: I2b332437d132adc3b08975ad30186978b0611811
parents 1564cff8 d1b2a0bc
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -249,6 +249,7 @@ cc_test {
    },
    srcs: [
        "module_unittest.cc",
        "stack_manager_unittest.cc",
        ":BluetoothCommonTestSources",
        ":BluetoothCryptoToolboxTestSources",
        ":BluetoothHciTestSources",
+13 −13
Original line number Diff line number Diff line
@@ -36,39 +36,39 @@ void StackManager::StartUp(ModuleList* modules, Thread* stack_thread) {
  management_thread_ = new Thread("management_thread", Thread::Priority::NORMAL);
  handler_ = new Handler(management_thread_);

  std::promise<void>* promise = new std::promise<void>();
  handler_->Post(
      common::BindOnce(&StackManager::handle_start_up, common::Unretained(this), modules, stack_thread, promise));
  std::promise<void> promise;
  auto future = promise.get_future();
  handler_->Post(common::BindOnce(&StackManager::handle_start_up, common::Unretained(this), modules, stack_thread,
                                  std::move(promise)));

  auto future = promise->get_future();
  auto init_status = future.wait_for(std::chrono::seconds(3));
  ASSERT_LOG(init_status == std::future_status::ready, "Can't start stack");
  delete promise;

  LOG_INFO("init complete");
}

void StackManager::handle_start_up(ModuleList* modules, Thread* stack_thread, std::promise<void>* promise) {
void StackManager::handle_start_up(ModuleList* modules, Thread* stack_thread, std::promise<void> promise) {
  registry_.Start(modules, stack_thread);
  promise->set_value();
  promise.set_value();
}

void StackManager::ShutDown() {
  std::promise<void>* promise = new std::promise<void>();
  handler_->Post(common::BindOnce(&StackManager::handle_shut_down, common::Unretained(this), promise));
  std::promise<void> promise;
  auto future = promise.get_future();
  handler_->Post(common::BindOnce(&StackManager::handle_shut_down, common::Unretained(this), std::move(promise)));

  auto future = promise->get_future();
  auto stop_status = future.wait_for(std::chrono::seconds(3));
  ASSERT_LOG(stop_status == std::future_status::ready, "Can't stop stack");

  delete promise;
  handler_->Clear();
  handler_->WaitUntilStopped(std::chrono::milliseconds(20));
  delete handler_;
  delete management_thread_;
}

void StackManager::handle_shut_down(std::promise<void>* promise) {
void StackManager::handle_shut_down(std::promise<void> promise) {
  registry_.StopAll();
  promise->set_value();
  promise.set_value();
}

}  // namespace bluetooth
+2 −2
Original line number Diff line number Diff line
@@ -37,8 +37,8 @@ class StackManager {
  os::Handler* handler_ = nullptr;
  ModuleRegistry registry_;

  void handle_start_up(ModuleList* modules, os::Thread* stack_thread, std::promise<void>* promise);
  void handle_shut_down(std::promise<void>* promise);
  void handle_start_up(ModuleList* modules, os::Thread* stack_thread, std::promise<void> promise);
  void handle_shut_down(std::promise<void> promise);
};

}  // namespace bluetooth
+56 −0
Original line number Diff line number Diff line
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "stack_manager.h"

#include "gtest/gtest.h"
#include "os/thread.h"

namespace bluetooth {
namespace {

TEST(StackManagerTest, start_and_shutdown_no_module) {
  StackManager stack_manager;
  ModuleList module_list;
  os::Thread thread{"test_thread", os::Thread::Priority::NORMAL};
  stack_manager.StartUp(&module_list, &thread);
  stack_manager.ShutDown();
}

class TestModuleNoDependency : public Module {
 public:
  static const ModuleFactory Factory;

 protected:
  void ListDependencies(ModuleList* list) override {}
  void Start() override {}
  void Stop() override {}
};

const ModuleFactory TestModuleNoDependency::Factory = ModuleFactory([]() { return new TestModuleNoDependency(); });

TEST(StackManagerTest, get_module_instance) {
  StackManager stack_manager;
  ModuleList module_list;
  module_list.add<TestModuleNoDependency>();
  os::Thread thread{"test_thread", os::Thread::Priority::NORMAL};
  stack_manager.StartUp(&module_list, &thread);
  EXPECT_NE(stack_manager.GetInstance<TestModuleNoDependency>(), nullptr);
  stack_manager.ShutDown();
}

}  // namespace
}  // namespace bluetooth