OSDN Git Service

Fix failling ClosureTest
authorJakub Pawlowski <jpawlowski@google.com>
Tue, 3 Jan 2017 21:53:34 +0000 (13:53 -0800)
committerJakub Pawlowski <jpawlowski@google.com>
Tue, 3 Jan 2017 21:59:50 +0000 (13:59 -0800)
BTA closure doesn't keep the HDR messages any more, test have keep track
of HDR pointers for itself.

Bug: 33947176
Test: test/run_unit_tests.sh  net_test_bta
Change-Id: I32994f248dc2e082bf8bed426a32acf677c58ed5

bta/test/bta_closure_test.cc

index daa096f..9e1e4bb 100644 (file)
@@ -17,6 +17,7 @@
  ******************************************************************************/
 
 #include <gtest/gtest.h>
+#include <queue>
 
 #include "bta/closure/bta_closure_int.h"
 #include "bta/include/bta_closure_api.h"
@@ -29,24 +30,24 @@ namespace {
  */
 
 int test_counter = 0;
-int msg_send_counter = 0;
 tBTA_SYS_EVT_HDLR* closure_handler = NULL;
-void* msg = NULL;
+std::queue<BT_HDR*> msgs;
 
 void test_plus_one_task() { test_counter++; }
 
 void test_plus_two_task() { test_counter += 2; }
 
-void fake_bta_sys_sendmsg(void* p_msg) {
-  msg_send_counter++;
-  msg = p_msg;
-}
+void fake_bta_sys_sendmsg(void* p_msg) { msgs.push((BT_HDR*)p_msg); }
 
 void fake_bta_sys_register(uint8_t id, const tBTA_SYS_REG* p_reg) {
   closure_handler = p_reg->evt_hdlr;
 }
 
-bool fake_bta_sys_sendmsg_execute() { return closure_handler((BT_HDR*)msg); }
+bool fake_bta_sys_sendmsg_execute() {
+  BT_HDR* p_msg = msgs.front();
+  msgs.pop();
+  return closure_handler(p_msg);
+}
 
 }  // namespace
 
@@ -55,27 +56,18 @@ bool fake_bta_sys_sendmsg_execute() { return closure_handler((BT_HDR*)msg); }
 void LogMsg(uint32_t trace_set_mask, const char* fmt_str, ...) {}
 
 TEST(ClosureTest, test_post_task) {
-  msg_send_counter = 0;
   test_counter = 0;
 
   bta_closure_init(fake_bta_sys_register, fake_bta_sys_sendmsg);
 
-  msg_send_counter = 0;
   do_in_bta_thread(FROM_HERE, base::Bind(&test_plus_one_task));
-  EXPECT_EQ(1, msg_send_counter);
-  EXPECT_TRUE(msg != NULL) << "Message should not be NULL";
+  EXPECT_EQ(1U, msgs.size()) << "Message should not be NULL";
 
   EXPECT_TRUE(fake_bta_sys_sendmsg_execute());
   EXPECT_EQ(1, test_counter);
-
-  // We sent only one task for execution, attempt to execute non-exiting
-  // task should fail, counter shouldn't be updated
-  EXPECT_FALSE(fake_bta_sys_sendmsg_execute());
-  EXPECT_EQ(1, test_counter);
 }
 
 TEST(ClosureTest, test_post_multiple_tasks) {
-  msg_send_counter = 0;
   test_counter = 0;
 
   bta_closure_init(fake_bta_sys_register, fake_bta_sys_sendmsg);
@@ -87,8 +79,7 @@ TEST(ClosureTest, test_post_multiple_tasks) {
   do_in_bta_thread(FROM_HERE, base::Bind(&test_plus_one_task));
   do_in_bta_thread(FROM_HERE, base::Bind(&test_plus_two_task));
 
-  EXPECT_EQ(6, msg_send_counter);
-  EXPECT_TRUE(msg != NULL) << "Message should not be NULL";
+  EXPECT_EQ(6U, msgs.size());
 
   EXPECT_TRUE(fake_bta_sys_sendmsg_execute());
   EXPECT_EQ(1, test_counter);
@@ -107,9 +98,4 @@ TEST(ClosureTest, test_post_multiple_tasks) {
 
   EXPECT_TRUE(fake_bta_sys_sendmsg_execute());
   EXPECT_EQ(9, test_counter);
-
-  // We sent only one task for execution, attempt to execute non-exiting
-  // task should fail, counter shouldn't be updated
-  EXPECT_FALSE(fake_bta_sys_sendmsg_execute());
-  EXPECT_EQ(9, test_counter);
 }