| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/expiring_cache.h" | 5 #include "net/base/expiring_cache.h" |
| 6 | 6 |
| 7 #include <functional> | 7 #include <functional> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 } | 112 } |
| 113 EXPECT_EQ(8U, cache.size()); | 113 EXPECT_EQ(8U, cache.size()); |
| 114 | 114 |
| 115 // Add two negative (instantly expired) entries at t=0 that expire at t=0. | 115 // Add two negative (instantly expired) entries at t=0 that expire at t=0. |
| 116 for (int i = 0; i < 2; ++i) { | 116 for (int i = 0; i < 2; ++i) { |
| 117 std::string name = base::StringPrintf("negative%d", i); | 117 std::string name = base::StringPrintf("negative%d", i); |
| 118 cache.Put(name, "I was never valid.", now, now); | 118 cache.Put(name, "I was never valid.", now, now); |
| 119 } | 119 } |
| 120 EXPECT_EQ(10U, cache.size()); | 120 EXPECT_EQ(10U, cache.size()); |
| 121 | 121 |
| 122 EXPECT_TRUE(ContainsKey(cache.entries_, "valid0")); | 122 EXPECT_TRUE(base::ContainsKey(cache.entries_, "valid0")); |
| 123 EXPECT_TRUE(ContainsKey(cache.entries_, "valid1")); | 123 EXPECT_TRUE(base::ContainsKey(cache.entries_, "valid1")); |
| 124 EXPECT_TRUE(ContainsKey(cache.entries_, "valid2")); | 124 EXPECT_TRUE(base::ContainsKey(cache.entries_, "valid2")); |
| 125 EXPECT_TRUE(ContainsKey(cache.entries_, "valid3")); | 125 EXPECT_TRUE(base::ContainsKey(cache.entries_, "valid3")); |
| 126 EXPECT_TRUE(ContainsKey(cache.entries_, "valid4")); | 126 EXPECT_TRUE(base::ContainsKey(cache.entries_, "valid4")); |
| 127 EXPECT_TRUE(ContainsKey(cache.entries_, "expired0")); | 127 EXPECT_TRUE(base::ContainsKey(cache.entries_, "expired0")); |
| 128 EXPECT_TRUE(ContainsKey(cache.entries_, "expired1")); | 128 EXPECT_TRUE(base::ContainsKey(cache.entries_, "expired1")); |
| 129 EXPECT_TRUE(ContainsKey(cache.entries_, "expired2")); | 129 EXPECT_TRUE(base::ContainsKey(cache.entries_, "expired2")); |
| 130 EXPECT_TRUE(ContainsKey(cache.entries_, "negative0")); | 130 EXPECT_TRUE(base::ContainsKey(cache.entries_, "negative0")); |
| 131 EXPECT_TRUE(ContainsKey(cache.entries_, "negative1")); | 131 EXPECT_TRUE(base::ContainsKey(cache.entries_, "negative1")); |
| 132 | 132 |
| 133 // Shrink the new max constraints bound and compact. The "negative" and | 133 // Shrink the new max constraints bound and compact. The "negative" and |
| 134 // "expired" entries should be dropped. | 134 // "expired" entries should be dropped. |
| 135 cache.max_entries_ = 6; | 135 cache.max_entries_ = 6; |
| 136 cache.Compact(now); | 136 cache.Compact(now); |
| 137 EXPECT_EQ(5U, cache.size()); | 137 EXPECT_EQ(5U, cache.size()); |
| 138 | 138 |
| 139 EXPECT_TRUE(ContainsKey(cache.entries_, "valid0")); | 139 EXPECT_TRUE(base::ContainsKey(cache.entries_, "valid0")); |
| 140 EXPECT_TRUE(ContainsKey(cache.entries_, "valid1")); | 140 EXPECT_TRUE(base::ContainsKey(cache.entries_, "valid1")); |
| 141 EXPECT_TRUE(ContainsKey(cache.entries_, "valid2")); | 141 EXPECT_TRUE(base::ContainsKey(cache.entries_, "valid2")); |
| 142 EXPECT_TRUE(ContainsKey(cache.entries_, "valid3")); | 142 EXPECT_TRUE(base::ContainsKey(cache.entries_, "valid3")); |
| 143 EXPECT_TRUE(ContainsKey(cache.entries_, "valid4")); | 143 EXPECT_TRUE(base::ContainsKey(cache.entries_, "valid4")); |
| 144 EXPECT_FALSE(ContainsKey(cache.entries_, "expired0")); | 144 EXPECT_FALSE(base::ContainsKey(cache.entries_, "expired0")); |
| 145 EXPECT_FALSE(ContainsKey(cache.entries_, "expired1")); | 145 EXPECT_FALSE(base::ContainsKey(cache.entries_, "expired1")); |
| 146 EXPECT_FALSE(ContainsKey(cache.entries_, "expired2")); | 146 EXPECT_FALSE(base::ContainsKey(cache.entries_, "expired2")); |
| 147 EXPECT_FALSE(ContainsKey(cache.entries_, "negative0")); | 147 EXPECT_FALSE(base::ContainsKey(cache.entries_, "negative0")); |
| 148 EXPECT_FALSE(ContainsKey(cache.entries_, "negative1")); | 148 EXPECT_FALSE(base::ContainsKey(cache.entries_, "negative1")); |
| 149 | 149 |
| 150 // Shrink further -- this time the compact will start dropping valid entries | 150 // Shrink further -- this time the compact will start dropping valid entries |
| 151 // to make space. | 151 // to make space. |
| 152 cache.max_entries_ = 4; | 152 cache.max_entries_ = 4; |
| 153 cache.Compact(now); | 153 cache.Compact(now); |
| 154 EXPECT_EQ(3U, cache.size()); | 154 EXPECT_EQ(3U, cache.size()); |
| 155 } | 155 } |
| 156 | 156 |
| 157 // Add entries while the cache is at capacity, causing evictions. | 157 // Add entries while the cache is at capacity, causing evictions. |
| 158 TEST(ExpiringCacheTest, SetWithCompact) { | 158 TEST(ExpiringCacheTest, SetWithCompact) { |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 // should be able to add something at kHeatDeath that expires at kMuchLater. | 302 // should be able to add something at kHeatDeath that expires at kMuchLater. |
| 303 cache.Put("test7", "foo7", kHeatDeath, kMuchLater); | 303 cache.Put("test7", "foo7", kHeatDeath, kMuchLater); |
| 304 EXPECT_EQ(1U, cache.size()); | 304 EXPECT_EQ(1U, cache.size()); |
| 305 EXPECT_THAT(cache.Get("test7", kNow), Pointee(StrEq("foo7"))); | 305 EXPECT_THAT(cache.Get("test7", kNow), Pointee(StrEq("foo7"))); |
| 306 EXPECT_THAT(cache.Get("test7", kLater), Pointee(StrEq("foo7"))); | 306 EXPECT_THAT(cache.Get("test7", kLater), Pointee(StrEq("foo7"))); |
| 307 EXPECT_THAT(cache.Get("test7", kHeatDeath), Pointee(StrEq("foo7"))); | 307 EXPECT_THAT(cache.Get("test7", kHeatDeath), Pointee(StrEq("foo7"))); |
| 308 EXPECT_FALSE(cache.Get("test7", kMuchLater)); | 308 EXPECT_FALSE(cache.Get("test7", kMuchLater)); |
| 309 } | 309 } |
| 310 | 310 |
| 311 } // namespace net | 311 } // namespace net |
| OLD | NEW |