diff options
| author | Chen Chen <cncn@google.com> | 2020-05-13 15:35:42 -0700 |
|---|---|---|
| committer | Chen Chen <cncn@google.com> | 2020-05-13 15:40:19 -0700 |
| commit | be4b6266147a36b3ea39af6fa924c50ceb0c1104 (patch) | |
| tree | 94834a14db51b536df806b6f3e511fba1d1fdce3 | |
| parent | da90ba697aa4ab28517a2444449b757509cd6179 (diff) | |
BluetoothMetrics: Fix LRU eviction callback bug
Test: atest bluetooth_test_common
Bug: 156504089
Tag: #stability
Change-Id: I594070a0dc1c33bd8f971bb8a7752fd317872a55
| -rw-r--r-- | system/common/lru.h | 4 | ||||
| -rw-r--r-- | system/common/lru_unittest.cc | 9 |
2 files changed, 8 insertions, 5 deletions
diff --git a/system/common/lru.h b/system/common/lru.h index 0904023ff5..68c227924f 100644 --- a/system/common/lru.h +++ b/system/common/lru.h @@ -119,8 +119,10 @@ class LruCache { // remove tail if (lru_map_.size() == capacity_) { lru_map_.erase(node_list_.back().first); + K key_evicted = node_list_.back().first; + V value_evicted = node_list_.back().second; node_list_.pop_back(); - lru_eviction_callback_(node_list_.back().first, node_list_.back().second); + lru_eviction_callback_(key_evicted, value_evicted); value_popped = true; } // insert to dummy next; diff --git a/system/common/lru_unittest.cc b/system/common/lru_unittest.cc index aae5827509..d8f998c20d 100644 --- a/system/common/lru_unittest.cc +++ b/system/common/lru_unittest.cc @@ -32,7 +32,7 @@ TEST(BluetoothLruCacheTest, LruCacheMainTest1) { int* value = new int(0); int dummy = 0; int* pointer = &dummy; - auto callback = [pointer](int a, int b) { (*pointer) = 1; }; + auto callback = [pointer](int a, int b) { (*pointer) = a * b; }; LruCache<int, int> cache(3, "testing", callback); // capacity = 3; cache.Put(1, 10); EXPECT_EQ(cache.Size(), 1); @@ -51,8 +51,8 @@ TEST(BluetoothLruCacheTest, LruCacheMainTest1) { EXPECT_EQ(cache.Size(), 3); cache.Put(4, 40); - EXPECT_EQ(dummy, 1); - // 2, 3, 4 should be in cache, 1 should not + EXPECT_EQ(dummy, 10); + // 2, 3, 4 should be in cache, 1 is evicted EXPECT_FALSE(cache.Get(1, value)); EXPECT_TRUE(cache.Get(4, value)); EXPECT_EQ(*value, 40); @@ -63,7 +63,8 @@ TEST(BluetoothLruCacheTest, LruCacheMainTest1) { cache.Put(5, 50); EXPECT_EQ(cache.Size(), 3); - // 2, 3, 5 should be in cache + EXPECT_EQ(dummy, 160); + // 2, 3, 5 should be in cache, 4 is evicted EXPECT_TRUE(cache.Remove(3)); cache.Put(6, 60); |
