OSDN Git Service

BluetoothMetrics: Return true if SaveDevice is called twice. Add more
authorChen Chen <cncn@google.com>
Tue, 24 Mar 2020 21:52:52 +0000 (14:52 -0700)
committerChen Chen <cncn@google.com>
Tue, 24 Mar 2020 22:06:43 +0000 (15:06 -0700)
logging

Test: atest BluetoothMetricIdAllocatorTest
Bug:152251473

Change-Id: I3d5ee8d086a4e589e84a766d3d0923d3ae2278ba

common/metric_id_allocator.cc
common/metric_id_allocator_unittest.cc

index 652803b..005ef3a 100644 (file)
@@ -146,24 +146,45 @@ int MetricIdAllocator::AllocateId(const RawAddress& mac_address) {
 bool MetricIdAllocator::SaveDevice(const RawAddress& mac_address) {
   std::lock_guard<std::mutex> lock(id_allocator_mutex_);
   int id = 0;
-  bool success = temporary_device_cache_.Get(mac_address, &id);
-  success &= temporary_device_cache_.Remove(mac_address);
-  if (success) {
-    paired_device_cache_.Put(mac_address, id);
-    success = save_id_callback_(mac_address, id);
+  if (paired_device_cache_.Get(mac_address, &id)) {
+    return true;
+  }
+  if (!temporary_device_cache_.Get(mac_address, &id)) {
+    LOG(ERROR) << LOGGING_TAG
+               << "Failed to save device because device is not in "
+               << "temporary_device_cache_";
+    return false;
+  }
+  if (!temporary_device_cache_.Remove(mac_address)) {
+    LOG(ERROR) << LOGGING_TAG
+               << "Failed to remove device from temporary_device_cache_";
+    return false;
   }
-  return success;
+  paired_device_cache_.Put(mac_address, id);
+  if (!save_id_callback_(mac_address, id)) {
+    LOG(ERROR) << LOGGING_TAG
+               << "Callback returned false after saving the device";
+    return false;
+  }
+  return true;
 }
 
 // call this function when a device is forgotten
 void MetricIdAllocator::ForgetDevice(const RawAddress& mac_address) {
   std::lock_guard<std::mutex> lock(id_allocator_mutex_);
   int id = 0;
-  bool success = paired_device_cache_.Get(mac_address, &id);
-  success &= paired_device_cache_.Remove(mac_address);
-  if (success) {
-    ForgetDevicePostprocess(mac_address, id);
+  if (!paired_device_cache_.Get(mac_address, &id)) {
+    LOG(ERROR) << LOGGING_TAG
+               << "Failed to forget device because device is not in "
+               << "paired_device_cache_";
+    return;
+  }
+  if (!paired_device_cache_.Remove(mac_address)) {
+    LOG(ERROR) << LOGGING_TAG
+               << "Failed to remove device from paired_device_cache_";
+    return;
   }
+  ForgetDevicePostprocess(mac_address, id);
 }
 
 bool MetricIdAllocator::IsValidId(const int id) {
index eae2ffe..ccc1576 100644 (file)
@@ -165,8 +165,8 @@ TEST(BluetoothMetricIdAllocatorTest, MetricIdAllocatorMainTest1) {
   EXPECT_TRUE(allocator.SaveDevice(RawAddress({0, 0, 0, 0, 0, 3})));
   EXPECT_EQ(dummy, 176);
 
-  // should fail, since id had been saved
-  EXPECT_FALSE(allocator.SaveDevice(RawAddress({0, 0, 0, 0, 0, 0})));
+  // should be true but callback won't be called, since id had been saved
+  EXPECT_TRUE(allocator.SaveDevice(RawAddress({0, 0, 0, 0, 0, 0})));
   EXPECT_EQ(dummy, 176);
 
   // forget
@@ -262,8 +262,8 @@ TEST(BluetoothMetricIdAllocatorTest, MetricIdAllocatorFullPairedMap) {
   // paired: 4 ... 199, 200, 201, 202, 203
   // scanned: 0, 1
 
-  // should fail, since id had been saved
-  EXPECT_FALSE(allocator.SaveDevice(kthAddress(key + 2)));
+  // should be true but callback won't be called, since id had been saved
+  EXPECT_TRUE(allocator.SaveDevice(kthAddress(key + 2)));
   EXPECT_EQ(dummy, 4);
 
   dummy = 27;
@@ -284,7 +284,9 @@ TEST(BluetoothMetricIdAllocatorTest, MetricIdAllocatorFullPairedMap) {
 
   EXPECT_TRUE(allocator.SaveDevice(kthAddress(key + 2)));
   EXPECT_EQ(dummy, 18);  // no key is evicted, a key is saved so *2,
-  EXPECT_FALSE(allocator.SaveDevice(kthAddress(key + 3)));
+
+  // should be true but callback won't be called, since id had been saved
+  EXPECT_TRUE(allocator.SaveDevice(kthAddress(key + 3)));
   EXPECT_EQ(dummy, 18);  // no such a key in scanned
   EXPECT_TRUE(allocator.SaveDevice(kthAddress(key + 4)));
   EXPECT_EQ(dummy, 12);  // one key is evicted, another key is saved so *2/3,