Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(566)

Side by Side Diff: net/base/expiring_cache_unittest.cc

Issue 10556022: Consider the verification time as well as the expiration time when caching certificate verification… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comment fixes Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "base/stl_util.h" 7 #include "base/stl_util.h"
8 #include "base/stringprintf.h" 8 #include "base/stringprintf.h"
9 #include "testing/gmock/include/gmock/gmock.h" 9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 14 matching lines...) Expand all
25 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); 25 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
26 26
27 Cache cache(kMaxCacheEntries); 27 Cache cache(kMaxCacheEntries);
28 28
29 // Start at t=0. 29 // Start at t=0.
30 base::TimeTicks now; 30 base::TimeTicks now;
31 EXPECT_EQ(0U, cache.size()); 31 EXPECT_EQ(0U, cache.size());
32 32
33 // Add an entry at t=0 33 // Add an entry at t=0
34 EXPECT_FALSE(cache.Get("entry1", now)); 34 EXPECT_FALSE(cache.Get("entry1", now));
35 cache.Put("entry1", "test1", now, kTTL); 35 cache.Put("entry1", "test1", now, now + kTTL);
36 EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1"))); 36 EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1")));
37 EXPECT_EQ(1U, cache.size()); 37 EXPECT_EQ(1U, cache.size());
38 38
39 // Advance to t=5. 39 // Advance to t=5.
40 now += base::TimeDelta::FromSeconds(5); 40 now += base::TimeDelta::FromSeconds(5);
41 41
42 // Add an entry at t=5. 42 // Add an entry at t=5.
43 EXPECT_FALSE(cache.Get("entry2", now)); 43 EXPECT_FALSE(cache.Get("entry2", now));
44 cache.Put("entry2", "test2", now, kTTL); 44 cache.Put("entry2", "test2", now, now + kTTL);
45 EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2"))); 45 EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2")));
46 EXPECT_EQ(2U, cache.size()); 46 EXPECT_EQ(2U, cache.size());
47 47
48 // Advance to t=9. 48 // Advance to t=9.
49 now += base::TimeDelta::FromSeconds(4); 49 now += base::TimeDelta::FromSeconds(4);
50 50
51 // Verify that the entries added are still retrievable and usable. 51 // Verify that the entries added are still retrievable and usable.
52 EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1"))); 52 EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1")));
53 EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2"))); 53 EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2")));
54 54
55 // Advance to t=10; entry1 is now expired. 55 // Advance to t=10; entry1 is now expired.
56 now += base::TimeDelta::FromSeconds(1); 56 now += base::TimeDelta::FromSeconds(1);
57 57
58 EXPECT_FALSE(cache.Get("entry1", now)); 58 EXPECT_FALSE(cache.Get("entry1", now));
59 EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2"))); 59 EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2")));
60 60
61 // The expired element should no longer be in the cache. 61 // The expired element should no longer be in the cache.
62 EXPECT_EQ(1U, cache.size()); 62 EXPECT_EQ(1U, cache.size());
63 63
64 // Update entry1 so it is no longer expired. 64 // Update entry1 so it is no longer expired.
65 cache.Put("entry1", "test1", now, kTTL); 65 cache.Put("entry1", "test1", now, now + kTTL);
66 66
67 // Both entries should be retrievable and usable. 67 // Both entries should be retrievable and usable.
68 EXPECT_EQ(2U, cache.size()); 68 EXPECT_EQ(2U, cache.size());
69 EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1"))); 69 EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1")));
70 EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2"))); 70 EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2")));
71 71
72 // Advance to t=20; both entries are now expired. 72 // Advance to t=20; both entries are now expired.
73 now += base::TimeDelta::FromSeconds(10); 73 now += base::TimeDelta::FromSeconds(10);
74 74
75 EXPECT_FALSE(cache.Get("entry1", now)); 75 EXPECT_FALSE(cache.Get("entry1", now));
76 EXPECT_FALSE(cache.Get("entry2", now)); 76 EXPECT_FALSE(cache.Get("entry2", now));
77 } 77 }
78 78
79 TEST(ExpiringCacheTest, Compact) { 79 TEST(ExpiringCacheTest, Compact) {
80 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); 80 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
81 81
82 Cache cache(kMaxCacheEntries); 82 Cache cache(kMaxCacheEntries);
83 83
84 // Start at t=0. 84 // Start at t=0.
85 base::TimeTicks now; 85 base::TimeTicks now;
86 EXPECT_EQ(0U, cache.size()); 86 EXPECT_EQ(0U, cache.size());
87 87
88 // Add five valid entries at t=10. 88 // Add five valid entries at t=10 that expire at t=20.
89 base::TimeTicks t10 = now + kTTL; 89 base::TimeTicks t10 = now + kTTL;
90 for (int i = 0; i < 5; ++i) { 90 for (int i = 0; i < 5; ++i) {
91 std::string name = base::StringPrintf("valid%d", i); 91 std::string name = base::StringPrintf("valid%d", i);
92 cache.Put(name, "I'm valid!", t10, kTTL); // Expire at t=20. 92 cache.Put(name, "I'm valid!", t10, t10 + kTTL);
93 } 93 }
94 EXPECT_EQ(5U, cache.size()); 94 EXPECT_EQ(5U, cache.size());
95 95
96 // Add three expired entries at t=10. 96 // Add three entries at t=0 that expire at t=10.
97 for (int i = 0; i < 3; ++i) { 97 for (int i = 0; i < 3; ++i) {
98 std::string name = base::StringPrintf("expired%d", i); 98 std::string name = base::StringPrintf("expired%d", i);
99 cache.Put(name, "I'm expired.", now - kTTL, kTTL); // Expire at t=10. 99 cache.Put(name, "I'm expired.", now, t10);
100 } 100 }
101 EXPECT_EQ(8U, cache.size()); 101 EXPECT_EQ(8U, cache.size());
102 102
103 // Add two negative (instantly expired) entriies at t=10. 103 // Add two negative (instantly expired) entries at t=0 that expire at t=0.
104 for (int i = 0; i < 2; ++i) { 104 for (int i = 0; i < 2; ++i) {
105 std::string name = base::StringPrintf("negative%d", i); 105 std::string name = base::StringPrintf("negative%d", i);
106 cache.Put(name, "I was never valid.", now, base::TimeDelta::FromSeconds(0)); 106 cache.Put(name, "I was never valid.", now, now);
107 } 107 }
108 EXPECT_EQ(10U, cache.size()); 108 EXPECT_EQ(10U, cache.size());
109 109
110 EXPECT_TRUE(ContainsKey(cache.entries_, "valid0")); 110 EXPECT_TRUE(ContainsKey(cache.entries_, "valid0"));
111 EXPECT_TRUE(ContainsKey(cache.entries_, "valid1")); 111 EXPECT_TRUE(ContainsKey(cache.entries_, "valid1"));
112 EXPECT_TRUE(ContainsKey(cache.entries_, "valid2")); 112 EXPECT_TRUE(ContainsKey(cache.entries_, "valid2"));
113 EXPECT_TRUE(ContainsKey(cache.entries_, "valid3")); 113 EXPECT_TRUE(ContainsKey(cache.entries_, "valid3"));
114 EXPECT_TRUE(ContainsKey(cache.entries_, "valid4")); 114 EXPECT_TRUE(ContainsKey(cache.entries_, "valid4"));
115 EXPECT_TRUE(ContainsKey(cache.entries_, "expired0")); 115 EXPECT_TRUE(ContainsKey(cache.entries_, "expired0"));
116 EXPECT_TRUE(ContainsKey(cache.entries_, "expired1")); 116 EXPECT_TRUE(ContainsKey(cache.entries_, "expired1"));
(...skipping 19 matching lines...) Expand all
136 EXPECT_FALSE(ContainsKey(cache.entries_, "negative1")); 136 EXPECT_FALSE(ContainsKey(cache.entries_, "negative1"));
137 137
138 // Shrink further -- this time the compact will start dropping valid entries 138 // Shrink further -- this time the compact will start dropping valid entries
139 // to make space. 139 // to make space.
140 cache.max_entries_ = 4; 140 cache.max_entries_ = 4;
141 cache.Compact(now); 141 cache.Compact(now);
142 EXPECT_EQ(3U, cache.size()); 142 EXPECT_EQ(3U, cache.size());
143 } 143 }
144 144
145 // Add entries while the cache is at capacity, causing evictions. 145 // Add entries while the cache is at capacity, causing evictions.
146 TEST(ExpiringCacheTest, SetWithCompact) { 146 TEST(ExpiringCacheTest, SetWithCompact) {
cbentzel 2012/06/18 19:08:28 Should you add a test which provides a custom expi
147 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); 147 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
148 148
149 Cache cache(3); 149 Cache cache(3);
150 150
151 // t=10 151 // t=10
152 base::TimeTicks now = base::TimeTicks() + kTTL; 152 base::TimeTicks now = base::TimeTicks() + kTTL;
153 153
154 cache.Put("test1", "test1", now, kTTL); 154 cache.Put("test1", "test1", now, now + kTTL);
155 cache.Put("test2", "test2", now, kTTL); 155 cache.Put("test2", "test2", now, now + kTTL);
156 cache.Put("expired", "expired", now, base::TimeDelta::FromSeconds(0)); 156 cache.Put("expired", "expired", now, now);
157 157
158 EXPECT_EQ(3U, cache.size()); 158 EXPECT_EQ(3U, cache.size());
159 159
160 // Should all be retrievable except "expired". 160 // Should all be retrievable except "expired".
161 EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("test1"))); 161 EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("test1")));
162 EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("test2"))); 162 EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("test2")));
163 EXPECT_FALSE(cache.Get("expired", now)); 163 EXPECT_FALSE(cache.Get("expired", now));
164 164
165 // Adding the fourth entry will cause "expired" to be evicted. 165 // Adding the fourth entry will cause "expired" to be evicted.
166 cache.Put("test3", "test3", now, kTTL); 166 cache.Put("test3", "test3", now, now + kTTL);
167 EXPECT_EQ(3U, cache.size()); 167 EXPECT_EQ(3U, cache.size());
168 168
169 EXPECT_FALSE(cache.Get("expired", now)); 169 EXPECT_FALSE(cache.Get("expired", now));
170 EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("test1"))); 170 EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("test1")));
171 EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("test2"))); 171 EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("test2")));
172 EXPECT_THAT(cache.Get("test3", now), Pointee(StrEq("test3"))); 172 EXPECT_THAT(cache.Get("test3", now), Pointee(StrEq("test3")));
173 173
174 // Add two more entries. Something should be evicted, however "test5" 174 // Add two more entries. Something should be evicted, however "test5"
175 // should definitely be in there (since it was last inserted). 175 // should definitely be in there (since it was last inserted).
176 cache.Put("test4", "test4", now, kTTL); 176 cache.Put("test4", "test4", now, now + kTTL);
177 EXPECT_EQ(3U, cache.size()); 177 EXPECT_EQ(3U, cache.size());
178 cache.Put("test5", "test5", now, kTTL); 178 cache.Put("test5", "test5", now, now + kTTL);
179 EXPECT_EQ(3U, cache.size()); 179 EXPECT_EQ(3U, cache.size());
180 EXPECT_THAT(cache.Get("test5", now), Pointee(StrEq("test5"))); 180 EXPECT_THAT(cache.Get("test5", now), Pointee(StrEq("test5")));
181 } 181 }
182 182
183 TEST(ExpiringCacheTest, Clear) { 183 TEST(ExpiringCacheTest, Clear) {
184 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); 184 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
185 185
186 Cache cache(kMaxCacheEntries); 186 Cache cache(kMaxCacheEntries);
187 187
188 // Start at t=0. 188 // Start at t=0.
189 base::TimeTicks now; 189 base::TimeTicks now;
190 EXPECT_EQ(0U, cache.size()); 190 EXPECT_EQ(0U, cache.size());
191 191
192 // Add three entries. 192 // Add three entries.
193 cache.Put("test1", "foo", now, kTTL); 193 cache.Put("test1", "foo", now, now + kTTL);
194 cache.Put("test2", "foo", now, kTTL); 194 cache.Put("test2", "foo", now, now + kTTL);
195 cache.Put("test3", "foo", now, kTTL); 195 cache.Put("test3", "foo", now, now + kTTL);
196 EXPECT_EQ(3U, cache.size()); 196 EXPECT_EQ(3U, cache.size());
197 197
198 cache.Clear(); 198 cache.Clear();
199 199
200 EXPECT_EQ(0U, cache.size()); 200 EXPECT_EQ(0U, cache.size());
201 } 201 }
202 202
203 TEST(ExpiringCache, GetTruncatesExpiredEntries) { 203 TEST(ExpiringCache, GetTruncatesExpiredEntries) {
204 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); 204 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
205 205
206 Cache cache(kMaxCacheEntries); 206 Cache cache(kMaxCacheEntries);
207 207
208 // Start at t=0. 208 // Start at t=0.
209 base::TimeTicks now; 209 base::TimeTicks now;
210 EXPECT_EQ(0U, cache.size()); 210 EXPECT_EQ(0U, cache.size());
211 211
212 // Add three entries at t=0. 212 // Add three entries at t=0.
213 cache.Put("test1", "foo1", now, kTTL); 213 cache.Put("test1", "foo1", now, now + kTTL);
214 cache.Put("test2", "foo2", now, kTTL); 214 cache.Put("test2", "foo2", now, now + kTTL);
215 cache.Put("test3", "foo3", now, kTTL); 215 cache.Put("test3", "foo3", now, now + kTTL);
216 EXPECT_EQ(3U, cache.size()); 216 EXPECT_EQ(3U, cache.size());
217 217
218 // Ensure the entries were added. 218 // Ensure the entries were added.
219 EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("foo1"))); 219 EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("foo1")));
220 EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("foo2"))); 220 EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("foo2")));
221 EXPECT_THAT(cache.Get("test3", now), Pointee(StrEq("foo3"))); 221 EXPECT_THAT(cache.Get("test3", now), Pointee(StrEq("foo3")));
222 222
223 // Add five entries at t=10. 223 // Add five entries at t=10.
224 now += kTTL; 224 now += kTTL;
225 for (int i = 0; i < 5; ++i) { 225 for (int i = 0; i < 5; ++i) {
226 std::string name = base::StringPrintf("valid%d", i); 226 std::string name = base::StringPrintf("valid%d", i);
227 cache.Put(name, name, now, kTTL); // Expire at t=20. 227 cache.Put(name, name, now, now + kTTL); // Expire at t=20.
228 } 228 }
229 EXPECT_EQ(8U, cache.size()); 229 EXPECT_EQ(8U, cache.size());
230 230
231 // Now access two expired entries and ensure the cache size goes down. 231 // Now access two expired entries and ensure the cache size goes down.
232 EXPECT_FALSE(cache.Get("test1", now)); 232 EXPECT_FALSE(cache.Get("test1", now));
233 EXPECT_FALSE(cache.Get("test2", now)); 233 EXPECT_FALSE(cache.Get("test2", now));
234 EXPECT_EQ(6U, cache.size()); 234 EXPECT_EQ(6U, cache.size());
235 235
236 // Accessing non-expired entries should return entries and not adjust the 236 // Accessing non-expired entries should return entries and not adjust the
237 // cache size. 237 // cache size.
238 for (int i = 0; i < 5; ++i) { 238 for (int i = 0; i < 5; ++i) {
239 std::string name = base::StringPrintf("valid%d", i); 239 std::string name = base::StringPrintf("valid%d", i);
240 EXPECT_THAT(cache.Get(name, now), Pointee(StrEq(name))); 240 EXPECT_THAT(cache.Get(name, now), Pointee(StrEq(name)));
241 } 241 }
242 EXPECT_EQ(6U, cache.size()); 242 EXPECT_EQ(6U, cache.size());
243 } 243 }
244 244
245 } // namespace net 245 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698