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

Commit 66c78ec9 authored by Hansong Zhang's avatar Hansong Zhang Committed by Myles Watson
Browse files

Allow Handler synchronization in test code

SynchronizeModuleHandler() blocks until all previous closures are done.

Test: atest bluetooth_test_gd --host
Change-Id: I2526f4bc1c5ce485ceaedbd0cb744a98b928518b
parent f00d53f7
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -94,4 +94,12 @@ void ModuleRegistry::StopAll() {
  ASSERT(started_modules_.empty());
  start_order_.clear();
}

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
+13 −3
Original line number Diff line number Diff line
@@ -17,8 +17,9 @@
#pragma once

#include <functional>
#include <vector>
#include <future>
#include <map>
#include <vector>

#include "os/log.h"
#include "os/handler.h"
@@ -118,6 +119,8 @@ class ModuleRegistry {
 protected:
  Module* Get(const ModuleFactory* module) const;

  os::Handler* GetModuleHandler(const ModuleFactory* module) const;

  std::map<const ModuleFactory*, Module*> started_modules_;
  std::vector<const ModuleFactory*> start_order_;
};
@@ -133,8 +136,8 @@ class TestModuleRegistry : public ModuleRegistry {
    return Get(module);
  }

  os::Handler* GetTestModuleHandler() {
    return new os::Handler(&test_thread);
  os::Handler* GetTestModuleHandler(const ModuleFactory* module) const {
    return GetModuleHandler(module);
  }

  os::Thread& GetTestThread() {
@@ -146,6 +149,13 @@ class TestModuleRegistry : public ModuleRegistry {
    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};
};