OSDN Git Service

Add test about post/clear task from callback for handler
authorChienyuan <chienyuanhuang@google.com>
Sat, 23 Mar 2019 00:12:52 +0000 (17:12 -0700)
committerChienyuan <chienyuanhuang@google.com>
Sat, 23 Mar 2019 02:51:21 +0000 (19:51 -0700)
Test: sudo ./bluetooth_test_common
Change-Id: Ibbf8622a705d3523638ef6cc9e6a62130b877637

common/handler_unittest.cc

index c51e51b..1850140 100644 (file)
@@ -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