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

Commit 1860f94d authored by Chienyuan's avatar Chienyuan
Browse files

Add test about post/clear task from callback for handler

Test: sudo ./bluetooth_test_common
Change-Id: Ibbf8622a705d3523638ef6cc9e6a62130b877637
parent 88a942ec
Loading
Loading
Loading
Loading
+26 −1
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ TEST_F(HandlerTest, post_task_cleared) {
    std::this_thread::sleep_for(std::chrono::milliseconds(5));
  };
  handler_->Post(std::move(closure));
  closure = []() { LOG(FATAL) << "Should not happen"; };
  closure = []() { EXPECT_EQ(0, 1) << "Should not happen"; };
  std::this_thread::sleep_for(std::chrono::milliseconds(5));
  handler_->Post(std::move(closure));
  handler_->Clear();
@@ -66,6 +66,31 @@ TEST_F(HandlerTest, post_task_cleared) {
  EXPECT_EQ(val, 1);
}

TEST_F(HandlerTest, post_task_from_callback) {
  int val = 0;
  Closure closure = [&val, this] {
    Closure inner_closure = [&val] { val++; };
    handler_->Post(inner_closure);
  };
  handler_->Post(closure);
  std::this_thread::sleep_for(std::chrono::milliseconds(10));
  EXPECT_EQ(val, 1);
}

TEST_F(HandlerTest, clear_task_from_callback) {
  int val = 0;
  Closure closure = [&val, this] {
    val++;
    handler_->Clear();
    std::this_thread::sleep_for(std::chrono::milliseconds(5));
  };
  handler_->Post(std::move(closure));
  closure = []() { EXPECT_EQ(0, 1) << "Should not happen"; };
  handler_->Post(std::move(closure));
  std::this_thread::sleep_for(std::chrono::milliseconds(10));
  EXPECT_EQ(val, 1);
}

}  // namespace
}  // namespace common
}  // namespace bluetooth