Stack manager: Clear handler during ShutDown()

* Clear StackManager.handler_ before it is deleted
* Use local variable promise in StartUp and ShutDown
* Add unit test

Test: bluetooth_test_gd
Change-Id: I0a6de64c5f159fdf4c97c123aa3fa2f37b2b7e34
diff --git a/gd/stack_manager.cc b/gd/stack_manager.cc
index 7cfaade..9c01d56 100644
--- a/gd/stack_manager.cc
+++ b/gd/stack_manager.cc
@@ -36,39 +36,39 @@
   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