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

Commit f9635e31 authored by Chris Manton's avatar Chris Manton
Browse files

gdx_unittest: Rename mainloop class

And a few more naming and protection cleanups

Bug: 316698184
Test: atest bluetooth_test_gdx_unit
Flag: EXEMPT, test infra

Change-Id: I3524e52a4186ed10e868f3917e2e3d969e7d4a0e
parent 5f28d62f
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -25,12 +25,11 @@
namespace bluetooth {

class ModuleMainloop {
 public:
 protected:
  ModuleMainloop() noexcept = default;
  virtual ~ModuleMainloop() = default;
  ModuleMainloop(const ModuleMainloop& mod) = delete;

 protected:
  // Threadsafe post onto mainloop a function with copyable arguments
  template <typename Functor, typename... Args>
  void PostFunctionOnMain(Functor&& functor, Args&&... args) const {
@@ -38,7 +37,7 @@ class ModuleMainloop {
        FROM_HERE, base::BindOnce(std::forward<Functor>(functor), std::forward<Args>(args)...));
  }

  // Threadsafe post onto mainloop a method and contex with copyable arguments
  // Threadsafe post onto mainloop a method and context with copyable arguments
  template <typename T, typename Functor, typename... Args>
  void PostMethodOnMain(std::shared_ptr<T> ref, Functor&& functor, Args... args) const {
    do_in_main_thread(
+23 −24
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@
#include "gtest/gtest.h"
#include "module.h"
#include "os/handler.h"
#include "os/log.h"
#include "os/thread.h"
#include "stack/include/main_thread.h"

@@ -43,7 +42,7 @@ std::promise<pid_t> protected_method_promise;
}  // namespace

// Global function with C linkage
void external_function(int /* a */, double /* b */, char /* c */) {
void external_function_main(int /* a */, double /* b */, char /* c */) {
  external_function_promise.set_value(base::PlatformThread::CurrentId());
}

@@ -75,49 +74,49 @@ struct TestModule::PrivateImpl : public ModuleMainloop {
};

// Protected module method executed on handler
void TestModule::call_on_handler_protected_method(int handler_tid, int a, int b, int c) {
void TestModule::call_on_handler_protected_method(int loop_tid, int a, int b, int c) {
  protected_method_promise = std::promise<pid_t>();
  auto future = protected_method_promise.get_future();
  CallOn(this, &TestModule::protected_method, a, b, c);
  ASSERT_EQ(future.wait_for(std::chrono::seconds(3)), std::future_status::ready);
  ASSERT_EQ(future.get(), handler_tid);
  ASSERT_EQ(future.get(), loop_tid);
}

// Global external function executed on main loop
void TestModule::call_on_main_external_function(int mainloop_tid, int a, double b, char c) {
void TestModule::call_on_main_external_function(int loop_tid, int a, double b, char c) {
  external_function_promise = std::promise<pid_t>();
  auto future = external_function_promise.get_future();
  PostFunctionOnMain(&external_function, a, b, c);
  PostFunctionOnMain(&external_function_main, a, b, c);
  ASSERT_EQ(future.wait_for(std::chrono::seconds(3)), std::future_status::ready);
  ASSERT_EQ(future.get(), mainloop_tid);
  ASSERT_EQ(future.get(), loop_tid);
}

// Private implementation method executed on main loop
void TestModule::call_on_main(int mainloop_tid, int a, int b, int c) {
void TestModule::call_on_main(int loop_tid, int a, int b, int c) {
  private_impl_promise = std::promise<pid_t>();
  auto future = private_impl_promise.get_future();
  PostMethodOnMain(pimpl_, &TestModule::PrivateImpl::privateCallableMethod, a, b, c);
  ASSERT_EQ(future.wait_for(std::chrono::seconds(3)), std::future_status::ready);
  ASSERT_EQ(future.get(), mainloop_tid);
  ASSERT_EQ(future.get(), loop_tid);
}

// Private implementation method executed on main loop and reposted
void TestModule::call_on_main_repost(int mainloop_tid, int a, int b, int c) {
void TestModule::call_on_main_repost(int loop_tid, int a, int b, int c) {
  private_impl_promise = std::promise<pid_t>();
  auto future = private_impl_promise.get_future();
  PostMethodOnMain(pimpl_, &TestModule::PrivateImpl::privateCallableRepostMethod, pimpl_, a, b, c);
  ASSERT_EQ(future.wait_for(std::chrono::seconds(3)), std::future_status::ready);
  ASSERT_EQ(future.get(), mainloop_tid);
  ASSERT_EQ(future.get(), loop_tid);
}

// Private implementation method executed on main loop recursively
void TestModule::call_on_main_recurse(int mainloop_tid, int depth, int b, int c) {
void TestModule::call_on_main_recurse(int loop_tid, int depth, int b, int c) {
  private_impl_promise = std::promise<pid_t>();
  auto future = private_impl_promise.get_future();
  PostMethodOnMain(
      pimpl_, &TestModule::PrivateImpl::privateCallableRecursiveMethod, pimpl_, depth, b, c);
  ASSERT_EQ(future.wait_for(std::chrono::seconds(3)), std::future_status::ready);
  ASSERT_EQ(future.get(), mainloop_tid);
  ASSERT_EQ(future.get(), loop_tid);
}

void TestModule::protected_method(int /* a */, int /* b */, int /* c */) {
@@ -148,7 +147,7 @@ const bluetooth::ModuleFactory TestModule::Factory =
//
// Module GDx Testing Below
//
class ModuleGdxTest : public ::testing::Test {
class ModuleMainGdxTest : public ::testing::Test {
 protected:
  void SetUp() override {
    test_framework_tid_ = base::PlatformThread::CurrentId();
@@ -183,10 +182,10 @@ class ModuleGdxTest : public ::testing::Test {
  TestModule* module_;
};

class ModuleGdxWithStackTest : public ModuleGdxTest {
class ModuleMainGdxWithStackTest : public ModuleMainGdxTest {
 protected:
  void SetUp() override {
    ModuleGdxTest::SetUp();
    ModuleMainGdxTest::SetUp();
    module_registry_.InjectTestModule(&TestModule::Factory, module_ /* pass ownership */);
    module_ = nullptr;  // ownership is passed
    handler_tid_ = get_handler_tid(module_registry_.GetTestModuleHandler(&TestModule::Factory));
@@ -203,7 +202,7 @@ class ModuleGdxWithStackTest : public ModuleGdxTest {

  void TearDown() override {
    module_registry_.StopAll();
    ModuleGdxTest::TearDown();
    ModuleMainGdxTest::TearDown();
  }

  TestModule* Mod() {
@@ -213,9 +212,9 @@ class ModuleGdxWithStackTest : public ModuleGdxTest {
  pid_t handler_tid_{-1};
};

TEST_F(ModuleGdxTest, nop) {}
TEST_F(ModuleMainGdxTest, nop) {}

TEST_F(ModuleGdxTest, lifecycle) {
TEST_F(ModuleMainGdxTest, lifecycle) {
  ::bluetooth::os::Thread* thread =
      new bluetooth::os::Thread("Name", bluetooth::os::Thread::Priority::REAL_TIME);
  ASSERT_FALSE(module_registry_.IsStarted<TestModule>());
@@ -226,22 +225,22 @@ TEST_F(ModuleGdxTest, lifecycle) {
  delete thread;
}

TEST_F(ModuleGdxWithStackTest, call_on_handler_protected_method) {
TEST_F(ModuleMainGdxWithStackTest, call_on_handler_protected_method) {
  Mod()->call_on_handler_protected_method(handler_tid_, 1, 2, 3);
}

TEST_F(ModuleGdxWithStackTest, test_call_on_main) {
TEST_F(ModuleMainGdxWithStackTest, test_call_on_main) {
  Mod()->call_on_main(mainloop_tid_, 1, 2, 3);
}

TEST_F(ModuleGdxWithStackTest, test_call_external_function) {
TEST_F(ModuleMainGdxWithStackTest, test_call_external_function) {
  Mod()->call_on_main_external_function(mainloop_tid_, 1, 2.3, 'c');
}

TEST_F(ModuleGdxWithStackTest, test_call_on_main_repost) {
TEST_F(ModuleMainGdxWithStackTest, test_call_on_main_repost) {
  Mod()->call_on_main_repost(mainloop_tid_, 1, 2, 3);
}

TEST_F(ModuleGdxWithStackTest, test_call_on_main_recurse) {
TEST_F(ModuleMainGdxWithStackTest, test_call_on_main_recurse) {
  Mod()->call_on_main_recurse(mainloop_tid_, 1, 2, 3);
}