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

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

Issue 9197009: Adds custom ttl argument to HostCache::Set. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed incomplete refactor. Created 8 years, 11 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/host_cache.h" 5 #include "net/base/host_cache.h"
6 6
7 #include "base/format_macros.h" 7 #include "base/format_macros.h"
8 #include "base/stl_util.h" 8 #include "base/stl_util.h"
9 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "base/stringprintf.h" 10 #include "base/stringprintf.h"
11 #include "net/base/net_errors.h" 11 #include "net/base/net_errors.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 namespace net { 14 namespace net {
15 15
16 namespace { 16 namespace {
17
17 const int kMaxCacheEntries = 10; 18 const int kMaxCacheEntries = 10;
18 19
19 const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10);
20 const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(0);
21
22 // Builds a key for |hostname|, defaulting the address family to unspecified. 20 // Builds a key for |hostname|, defaulting the address family to unspecified.
23 HostCache::Key Key(const std::string& hostname) { 21 HostCache::Key Key(const std::string& hostname) {
24 return HostCache::Key(hostname, ADDRESS_FAMILY_UNSPECIFIED, 0); 22 return HostCache::Key(hostname, ADDRESS_FAMILY_UNSPECIFIED, 0);
25 } 23 }
26 24
27 } // namespace 25 } // namespace
28 26
29 TEST(HostCacheTest, Basic) { 27 TEST(HostCacheTest, Basic) {
30 HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); 28 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
29
30 HostCache cache(kMaxCacheEntries);
31 31
32 // Start at t=0. 32 // Start at t=0.
33 base::TimeTicks now; 33 base::TimeTicks now;
34 34
35 const HostCache::Entry* entry1 = NULL; // Entry for foobar.com. 35 const HostCache::Entry* entry1 = NULL; // Entry for foobar.com.
36 const HostCache::Entry* entry2 = NULL; // Entry for foobar2.com. 36 const HostCache::Entry* entry2 = NULL; // Entry for foobar2.com.
37 37
38 EXPECT_EQ(0U, cache.size()); 38 EXPECT_EQ(0U, cache.size());
39 39
40 // Add an entry for "foobar.com" at t=0. 40 // Add an entry for "foobar.com" at t=0.
41 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); 41 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL);
42 cache.Set(Key("foobar.com"), OK, AddressList(), now); 42 cache.Set(Key("foobar.com"), OK, AddressList(), now, kTTL);
43 entry1 = cache.Lookup(Key("foobar.com"), base::TimeTicks()); 43 entry1 = cache.Lookup(Key("foobar.com"), base::TimeTicks());
44 EXPECT_FALSE(entry1 == NULL); 44 EXPECT_FALSE(entry1 == NULL);
45 EXPECT_EQ(1U, cache.size()); 45 EXPECT_EQ(1U, cache.size());
46 46
47 // Advance to t=5. 47 // Advance to t=5.
48 now += base::TimeDelta::FromSeconds(5); 48 now += base::TimeDelta::FromSeconds(5);
49 49
50 // Add an entry for "foobar2.com" at t=5. 50 // Add an entry for "foobar2.com" at t=5.
51 EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), base::TimeTicks()) == NULL); 51 EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), base::TimeTicks()) == NULL);
52 cache.Set(Key("foobar2.com"), OK, AddressList(), now); 52 cache.Set(Key("foobar2.com"), OK, AddressList(), now, kTTL);
53 entry2 = cache.Lookup(Key("foobar2.com"), base::TimeTicks()); 53 entry2 = cache.Lookup(Key("foobar2.com"), base::TimeTicks());
54 EXPECT_FALSE(NULL == entry1); 54 EXPECT_FALSE(NULL == entry1);
55 EXPECT_EQ(2U, cache.size()); 55 EXPECT_EQ(2U, cache.size());
56 56
57 // Advance to t=9 57 // Advance to t=9
58 now += base::TimeDelta::FromSeconds(4); 58 now += base::TimeDelta::FromSeconds(4);
59 59
60 // Verify that the entries we added are still retrievable, and usable. 60 // Verify that the entries we added are still retrievable, and usable.
61 EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now)); 61 EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now));
62 EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now)); 62 EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now));
63 63
64 // Advance to t=10; entry1 is now expired. 64 // Advance to t=10; entry1 is now expired.
65 now += base::TimeDelta::FromSeconds(1); 65 now += base::TimeDelta::FromSeconds(1);
66 66
67 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL); 67 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL);
68 EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now)); 68 EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now));
69 69
70 // Update entry1, so it is no longer expired. 70 // Update entry1, so it is no longer expired.
71 cache.Set(Key("foobar.com"), OK, AddressList(), now); 71 cache.Set(Key("foobar.com"), OK, AddressList(), now, kTTL);
72 // Re-uses existing entry storage. 72 // Re-uses existing entry storage.
73 EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now)); 73 EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now));
74 EXPECT_EQ(2U, cache.size()); 74 EXPECT_EQ(2U, cache.size());
75 75
76 // Both entries should still be retrievable and usable. 76 // Both entries should still be retrievable and usable.
77 EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now)); 77 EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now));
78 EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now)); 78 EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now));
79 79
80 // Advance to t=20; both entries are now expired. 80 // Advance to t=20; both entries are now expired.
81 now += base::TimeDelta::FromSeconds(10); 81 now += base::TimeDelta::FromSeconds(10);
82 82
83 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL); 83 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL);
84 EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), now) == NULL); 84 EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), now) == NULL);
85 } 85 }
86 86
87 // Try caching entries for a failed resolve attempt -- since we set 87 // Try caching entries for a failed resolve attempt -- since we set
88 // the TTL of such entries to 0 it won't work. 88 // the TTL of such entries to 0 it won't work.
89 TEST(HostCacheTest, NoCacheNegative) { 89 TEST(HostCacheTest, NoCacheNegative) {
90 HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); 90 const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10);
91 const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(0);
92
93 HostCache cache(kMaxCacheEntries);
91 94
92 // Set t=0. 95 // Set t=0.
93 base::TimeTicks now; 96 base::TimeTicks now;
94 97
95 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); 98 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL);
96 cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now); 99 cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(),
100 now, kFailureEntryTTL);
97 EXPECT_EQ(1U, cache.size()); 101 EXPECT_EQ(1U, cache.size());
98 102
99 // We disallow use of negative entries. 103 // We disallow use of negative entries.
cbentzel 2012/01/20 11:04:53 This test isn't so much what happens with negative
szym 2012/01/20 16:31:46 Not really. Zero-TTL entries can never be served f
100 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL); 104 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL);
101 105
102 // Now overwrite with a valid entry, and then overwrite with negative entry 106 // Now overwrite with a valid entry, and then overwrite with negative entry
103 // again -- the valid entry should be kicked out. 107 // again -- the valid entry should be kicked out.
104 cache.Set(Key("foobar.com"), OK, AddressList(), now); 108 cache.Set(Key("foobar.com"), OK, AddressList(), now, kSuccessEntryTTL);
105 EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now) == NULL); 109 EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now) == NULL);
106 cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now); 110 cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(),
111 now, kFailureEntryTTL);
107 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL); 112 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL);
108 } 113 }
109 114
110 // Try caching entries for a failed resolves for 10 seconds. 115 // Try caching entries for a failed resolves for 10 seconds.
111 TEST(HostCacheTest, CacheNegativeEntry) { 116 TEST(HostCacheTest, CacheNegativeEntry) {
112 HostCache cache(kMaxCacheEntries, 117 const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(10);
113 base::TimeDelta::FromSeconds(0), // success entry TTL. 118
114 base::TimeDelta::FromSeconds(10)); // failure entry TTL. 119 HostCache cache(kMaxCacheEntries);
115 120
116 // Start at t=0. 121 // Start at t=0.
117 base::TimeTicks now; 122 base::TimeTicks now;
118 123
119 const HostCache::Entry* entry1 = NULL; // Entry for foobar.com. 124 const HostCache::Entry* entry1 = NULL; // Entry for foobar.com.
120 const HostCache::Entry* entry2 = NULL; // Entry for foobar2.com. 125 const HostCache::Entry* entry2 = NULL; // Entry for foobar2.com.
121 126
122 EXPECT_EQ(0U, cache.size()); 127 EXPECT_EQ(0U, cache.size());
123 128
124 // Add an entry for "foobar.com" at t=0. 129 // Add an entry for "foobar.com" at t=0.
125 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); 130 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL);
126 cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now); 131 cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(),
132 now, kFailureEntryTTL);
127 entry1 = cache.Lookup(Key("foobar.com"), base::TimeTicks()); 133 entry1 = cache.Lookup(Key("foobar.com"), base::TimeTicks());
128 EXPECT_FALSE(entry1 == NULL); 134 EXPECT_FALSE(entry1 == NULL);
129 EXPECT_EQ(1U, cache.size()); 135 EXPECT_EQ(1U, cache.size());
130 136
131 // Advance to t=5. 137 // Advance to t=5.
132 now += base::TimeDelta::FromSeconds(5); 138 now += base::TimeDelta::FromSeconds(5);
133 139
134 // Add an entry for "foobar2.com" at t=5. 140 // Add an entry for "foobar2.com" at t=5.
135 EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), base::TimeTicks()) == NULL); 141 EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), base::TimeTicks()) == NULL);
136 cache.Set(Key("foobar2.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now); 142 cache.Set(Key("foobar2.com"), ERR_NAME_NOT_RESOLVED, AddressList(),
143 now, kFailureEntryTTL);
137 entry2 = cache.Lookup(Key("foobar2.com"), base::TimeTicks()); 144 entry2 = cache.Lookup(Key("foobar2.com"), base::TimeTicks());
138 EXPECT_FALSE(NULL == entry1); 145 EXPECT_FALSE(NULL == entry1);
139 EXPECT_EQ(2U, cache.size()); 146 EXPECT_EQ(2U, cache.size());
140 147
141 // Advance to t=9 148 // Advance to t=9
142 now += base::TimeDelta::FromSeconds(4); 149 now += base::TimeDelta::FromSeconds(4);
143 150
144 // Verify that the entries we added are still retrievable, and usable. 151 // Verify that the entries we added are still retrievable, and usable.
145 EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now)); 152 EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now));
146 EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now)); 153 EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now));
147 154
148 // Advance to t=10; entry1 is now expired. 155 // Advance to t=10; entry1 is now expired.
149 now += base::TimeDelta::FromSeconds(1); 156 now += base::TimeDelta::FromSeconds(1);
150 157
151 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL); 158 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL);
152 EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now)); 159 EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now));
153 160
154 // Update entry1, so it is no longer expired. 161 // Update entry1, so it is no longer expired.
155 cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now); 162 cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(),
163 now, kFailureEntryTTL);
156 // Re-uses existing entry storage. 164 // Re-uses existing entry storage.
157 EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now)); 165 EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now));
158 EXPECT_EQ(2U, cache.size()); 166 EXPECT_EQ(2U, cache.size());
159 167
160 // Both entries should still be retrievable and usable. 168 // Both entries should still be retrievable and usable.
161 EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now)); 169 EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now));
162 EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now)); 170 EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now));
163 171
164 // Advance to t=20; both entries are now expired. 172 // Advance to t=20; both entries are now expired.
165 now += base::TimeDelta::FromSeconds(10); 173 now += base::TimeDelta::FromSeconds(10);
166 174
167 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL); 175 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL);
168 EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), now) == NULL); 176 EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), now) == NULL);
169 } 177 }
170 178
171 TEST(HostCacheTest, Compact) { 179 TEST(HostCacheTest, Compact) {
172 // Initial entries limit is big enough to accomadate everything we add. 180 // Initial entries limit is big enough to accomadate everything we add.
173 HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); 181 const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10);
182 const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(0);
183 HostCache cache(kMaxCacheEntries);
174 184
175 EXPECT_EQ(0U, cache.size()); 185 EXPECT_EQ(0U, cache.size());
176 186
177 // t=10 187 // t=10
178 base::TimeTicks now = base::TimeTicks() + base::TimeDelta::FromSeconds(10); 188 base::TimeTicks now = base::TimeTicks() + base::TimeDelta::FromSeconds(10);
179 189
180 // Add five valid entries at t=10. 190 // Add five valid entries at t=10.
181 for (int i = 0; i < 5; ++i) { 191 for (int i = 0; i < 5; ++i) {
182 std::string hostname = base::StringPrintf("valid%d", i); 192 std::string hostname = base::StringPrintf("valid%d", i);
183 cache.Set(Key(hostname), OK, AddressList(), now); 193 cache.Set(Key(hostname), OK, AddressList(), now, kSuccessEntryTTL);
184 } 194 }
185 EXPECT_EQ(5U, cache.size()); 195 EXPECT_EQ(5U, cache.size());
186 196
187 // Add 3 expired entries at t=0. 197 // Add 3 expired entries at t=0.
188 for (int i = 0; i < 3; ++i) { 198 for (int i = 0; i < 3; ++i) {
189 std::string hostname = base::StringPrintf("expired%d", i); 199 std::string hostname = base::StringPrintf("expired%d", i);
190 base::TimeTicks t = now - base::TimeDelta::FromSeconds(10); 200 base::TimeTicks t = now - base::TimeDelta::FromSeconds(10);
191 cache.Set(Key(hostname), OK, AddressList(), t); 201 cache.Set(Key(hostname), OK, AddressList(), t, kSuccessEntryTTL);
192 } 202 }
193 EXPECT_EQ(8U, cache.size()); 203 EXPECT_EQ(8U, cache.size());
194 204
195 // Add 2 negative entries at t=10 205 // Add 2 negative entries at t=10
196 for (int i = 0; i < 2; ++i) { 206 for (int i = 0; i < 2; ++i) {
197 std::string hostname = base::StringPrintf("negative%d", i); 207 std::string hostname = base::StringPrintf("negative%d", i);
198 cache.Set(Key(hostname), ERR_NAME_NOT_RESOLVED, AddressList(), now); 208 cache.Set(Key(hostname), ERR_NAME_NOT_RESOLVED, AddressList(),
209 now, kFailureEntryTTL);
199 } 210 }
200 EXPECT_EQ(10U, cache.size()); 211 EXPECT_EQ(10U, cache.size());
201 212
202 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid0"))); 213 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid0")));
203 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid1"))); 214 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid1")));
204 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid2"))); 215 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid2")));
205 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid3"))); 216 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid3")));
206 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid4"))); 217 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid4")));
207 EXPECT_TRUE(ContainsKey(cache.entries_, Key("expired0"))); 218 EXPECT_TRUE(ContainsKey(cache.entries_, Key("expired0")));
208 EXPECT_TRUE(ContainsKey(cache.entries_, Key("expired1"))); 219 EXPECT_TRUE(ContainsKey(cache.entries_, Key("expired1")));
(...skipping 20 matching lines...) Expand all
229 240
230 // Shrink further -- this time the compact will start dropping valid entries 241 // Shrink further -- this time the compact will start dropping valid entries
231 // to make space. 242 // to make space.
232 cache.max_entries_ = 3; 243 cache.max_entries_ = 3;
233 cache.Compact(now, NULL); 244 cache.Compact(now, NULL);
234 EXPECT_EQ(3U, cache.size()); 245 EXPECT_EQ(3U, cache.size());
235 } 246 }
236 247
237 // Add entries while the cache is at capacity, causing evictions. 248 // Add entries while the cache is at capacity, causing evictions.
238 TEST(HostCacheTest, SetWithCompact) { 249 TEST(HostCacheTest, SetWithCompact) {
239 HostCache cache(3, kSuccessEntryTTL, kFailureEntryTTL); 250 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
251
252 HostCache cache(3);
240 253
241 // t=10 254 // t=10
242 base::TimeTicks now = base::TimeTicks() + kSuccessEntryTTL; 255 base::TimeTicks now = base::TimeTicks() + kTTL;
243 256
244 cache.Set(Key("host1"), OK, AddressList(), now); 257 cache.Set(Key("host1"), OK, AddressList(), now, kTTL);
245 cache.Set(Key("host2"), OK, AddressList(), now); 258 cache.Set(Key("host2"), OK, AddressList(), now, kTTL);
246 cache.Set(Key("expired"), OK, AddressList(), now - kSuccessEntryTTL); 259 cache.Set(Key("expired"), OK, AddressList(), now, kTTL - kTTL);
247 260
248 EXPECT_EQ(3U, cache.size()); 261 EXPECT_EQ(3U, cache.size());
249 262
250 // Should all be retrievable except "expired". 263 // Should all be retrievable except "expired".
251 EXPECT_FALSE(NULL == cache.Lookup(Key("host1"), now)); 264 EXPECT_FALSE(NULL == cache.Lookup(Key("host1"), now));
252 EXPECT_FALSE(NULL == cache.Lookup(Key("host2"), now)); 265 EXPECT_FALSE(NULL == cache.Lookup(Key("host2"), now));
253 EXPECT_TRUE(NULL == cache.Lookup(Key("expired"), now)); 266 EXPECT_TRUE(NULL == cache.Lookup(Key("expired"), now));
254 267
255 // Adding the fourth entry will cause "expired" to be evicted. 268 // Adding the fourth entry will cause "expired" to be evicted.
256 cache.Set(Key("host3"), OK, AddressList(), now); 269 cache.Set(Key("host3"), OK, AddressList(), now, kTTL);
257 EXPECT_EQ(3U, cache.size()); 270 EXPECT_EQ(3U, cache.size());
258 EXPECT_TRUE(cache.Lookup(Key("expired"), now) == NULL); 271 EXPECT_TRUE(cache.Lookup(Key("expired"), now) == NULL);
259 EXPECT_FALSE(cache.Lookup(Key("host1"), now) == NULL); 272 EXPECT_FALSE(cache.Lookup(Key("host1"), now) == NULL);
260 EXPECT_FALSE(cache.Lookup(Key("host2"), now) == NULL); 273 EXPECT_FALSE(cache.Lookup(Key("host2"), now) == NULL);
261 EXPECT_FALSE(cache.Lookup(Key("host3"), now) == NULL); 274 EXPECT_FALSE(cache.Lookup(Key("host3"), now) == NULL);
262 275
263 // Add two more entries. Something should be evicted, however "host5" 276 // Add two more entries. Something should be evicted, however "host5"
264 // should definitely be in there (since it was last inserted). 277 // should definitely be in there (since it was last inserted).
265 cache.Set(Key("host4"), OK, AddressList(), now); 278 cache.Set(Key("host4"), OK, AddressList(), now, kTTL);
266 EXPECT_EQ(3U, cache.size()); 279 EXPECT_EQ(3U, cache.size());
267 cache.Set(Key("host5"), OK, AddressList(), now); 280 cache.Set(Key("host5"), OK, AddressList(), now, kTTL);
268 EXPECT_EQ(3U, cache.size()); 281 EXPECT_EQ(3U, cache.size());
269 EXPECT_FALSE(cache.Lookup(Key("host5"), now) == NULL); 282 EXPECT_FALSE(cache.Lookup(Key("host5"), now) == NULL);
270 } 283 }
271 284
272 // Tests that the same hostname can be duplicated in the cache, so long as 285 // Tests that the same hostname can be duplicated in the cache, so long as
273 // the address family differs. 286 // the address family differs.
274 TEST(HostCacheTest, AddressFamilyIsPartOfKey) { 287 TEST(HostCacheTest, AddressFamilyIsPartOfKey) {
275 HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); 288 const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10);
289
290 HostCache cache(kMaxCacheEntries);
276 291
277 // t=0. 292 // t=0.
278 base::TimeTicks now; 293 base::TimeTicks now;
279 294
280 HostCache::Key key1("foobar.com", ADDRESS_FAMILY_UNSPECIFIED, 0); 295 HostCache::Key key1("foobar.com", ADDRESS_FAMILY_UNSPECIFIED, 0);
281 HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4, 0); 296 HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4, 0);
282 297
283 const HostCache::Entry* entry1 = NULL; // Entry for key1 298 const HostCache::Entry* entry1 = NULL; // Entry for key1
284 const HostCache::Entry* entry2 = NULL; // Entry for key2 299 const HostCache::Entry* entry2 = NULL; // Entry for key2
285 300
286 EXPECT_EQ(0U, cache.size()); 301 EXPECT_EQ(0U, cache.size());
287 302
288 // Add an entry for ("foobar.com", UNSPECIFIED) at t=0. 303 // Add an entry for ("foobar.com", UNSPECIFIED) at t=0.
289 EXPECT_TRUE(cache.Lookup(key1, base::TimeTicks()) == NULL); 304 EXPECT_TRUE(cache.Lookup(key1, base::TimeTicks()) == NULL);
290 cache.Set(key1, OK, AddressList(), now); 305 cache.Set(key1, OK, AddressList(), now, kSuccessEntryTTL);
291 entry1 = cache.Lookup(key1, base::TimeTicks()); 306 entry1 = cache.Lookup(key1, base::TimeTicks());
292 EXPECT_FALSE(entry1 == NULL); 307 EXPECT_FALSE(entry1 == NULL);
293 EXPECT_EQ(1U, cache.size()); 308 EXPECT_EQ(1U, cache.size());
294 309
295 // Add an entry for ("foobar.com", IPV4_ONLY) at t=0. 310 // Add an entry for ("foobar.com", IPV4_ONLY) at t=0.
296 EXPECT_TRUE(cache.Lookup(key2, base::TimeTicks()) == NULL); 311 EXPECT_TRUE(cache.Lookup(key2, base::TimeTicks()) == NULL);
297 cache.Set(key2, OK, AddressList(), now); 312 cache.Set(key2, OK, AddressList(), now, kSuccessEntryTTL);
298 entry2 = cache.Lookup(key2, base::TimeTicks()); 313 entry2 = cache.Lookup(key2, base::TimeTicks());
299 EXPECT_FALSE(entry2 == NULL); 314 EXPECT_FALSE(entry2 == NULL);
300 EXPECT_EQ(2U, cache.size()); 315 EXPECT_EQ(2U, cache.size());
301 316
302 // Even though the hostnames were the same, we should have two unique 317 // Even though the hostnames were the same, we should have two unique
303 // entries (because the address families differ). 318 // entries (because the address families differ).
304 EXPECT_NE(entry1, entry2); 319 EXPECT_NE(entry1, entry2);
305 } 320 }
306 321
307 // Tests that the same hostname can be duplicated in the cache, so long as 322 // Tests that the same hostname can be duplicated in the cache, so long as
308 // the HostResolverFlags differ. 323 // the HostResolverFlags differ.
309 TEST(HostCacheTest, HostResolverFlagsArePartOfKey) { 324 TEST(HostCacheTest, HostResolverFlagsArePartOfKey) {
310 HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); 325 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
326
327 HostCache cache(kMaxCacheEntries);
311 328
312 // t=0. 329 // t=0.
313 base::TimeTicks now; 330 base::TimeTicks now;
314 331
315 HostCache::Key key1("foobar.com", ADDRESS_FAMILY_IPV4, 0); 332 HostCache::Key key1("foobar.com", ADDRESS_FAMILY_IPV4, 0);
316 HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4, 333 HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4,
317 HOST_RESOLVER_CANONNAME); 334 HOST_RESOLVER_CANONNAME);
318 HostCache::Key key3("foobar.com", ADDRESS_FAMILY_IPV4, 335 HostCache::Key key3("foobar.com", ADDRESS_FAMILY_IPV4,
319 HOST_RESOLVER_LOOPBACK_ONLY); 336 HOST_RESOLVER_LOOPBACK_ONLY);
320 337
321 const HostCache::Entry* entry1 = NULL; // Entry for key1 338 const HostCache::Entry* entry1 = NULL; // Entry for key1
322 const HostCache::Entry* entry2 = NULL; // Entry for key2 339 const HostCache::Entry* entry2 = NULL; // Entry for key2
323 const HostCache::Entry* entry3 = NULL; // Entry for key3 340 const HostCache::Entry* entry3 = NULL; // Entry for key3
324 341
325 EXPECT_EQ(0U, cache.size()); 342 EXPECT_EQ(0U, cache.size());
326 343
327 // Add an entry for ("foobar.com", IPV4, NONE) at t=0. 344 // Add an entry for ("foobar.com", IPV4, NONE) at t=0.
328 EXPECT_TRUE(cache.Lookup(key1, base::TimeTicks()) == NULL); 345 EXPECT_TRUE(cache.Lookup(key1, base::TimeTicks()) == NULL);
329 cache.Set(key1, OK, AddressList(), now); 346 cache.Set(key1, OK, AddressList(), now, kTTL);
330 entry1 = cache.Lookup(key1, base::TimeTicks()); 347 entry1 = cache.Lookup(key1, base::TimeTicks());
331 EXPECT_FALSE(entry1 == NULL); 348 EXPECT_FALSE(entry1 == NULL);
332 EXPECT_EQ(1U, cache.size()); 349 EXPECT_EQ(1U, cache.size());
333 350
334 // Add an entry for ("foobar.com", IPV4, CANONNAME) at t=0. 351 // Add an entry for ("foobar.com", IPV4, CANONNAME) at t=0.
335 EXPECT_TRUE(cache.Lookup(key2, base::TimeTicks()) == NULL); 352 EXPECT_TRUE(cache.Lookup(key2, base::TimeTicks()) == NULL);
336 cache.Set(key2, OK, AddressList(), now); 353 cache.Set(key2, OK, AddressList(), now, kTTL);
337 entry2 = cache.Lookup(key2, base::TimeTicks()); 354 entry2 = cache.Lookup(key2, base::TimeTicks());
338 EXPECT_FALSE(entry2 == NULL); 355 EXPECT_FALSE(entry2 == NULL);
339 EXPECT_EQ(2U, cache.size()); 356 EXPECT_EQ(2U, cache.size());
340 357
341 // Add an entry for ("foobar.com", IPV4, LOOPBACK_ONLY) at t=0. 358 // Add an entry for ("foobar.com", IPV4, LOOPBACK_ONLY) at t=0.
342 EXPECT_TRUE(cache.Lookup(key3, base::TimeTicks()) == NULL); 359 EXPECT_TRUE(cache.Lookup(key3, base::TimeTicks()) == NULL);
343 cache.Set(key3, OK, AddressList(), now); 360 cache.Set(key3, OK, AddressList(), now, kTTL);
344 entry3 = cache.Lookup(key3, base::TimeTicks()); 361 entry3 = cache.Lookup(key3, base::TimeTicks());
345 EXPECT_FALSE(entry3 == NULL); 362 EXPECT_FALSE(entry3 == NULL);
346 EXPECT_EQ(3U, cache.size()); 363 EXPECT_EQ(3U, cache.size());
347 364
348 // Even though the hostnames were the same, we should have two unique 365 // Even though the hostnames were the same, we should have two unique
349 // entries (because the HostResolverFlags differ). 366 // entries (because the HostResolverFlags differ).
350 EXPECT_NE(entry1, entry2); 367 EXPECT_NE(entry1, entry2);
351 EXPECT_NE(entry1, entry3); 368 EXPECT_NE(entry1, entry3);
352 EXPECT_NE(entry2, entry3); 369 EXPECT_NE(entry2, entry3);
353 } 370 }
354 371
355 TEST(HostCacheTest, NoCache) { 372 TEST(HostCacheTest, NoCache) {
356 // Disable caching. 373 // Disable caching.
357 HostCache cache(0, kSuccessEntryTTL, kFailureEntryTTL); 374 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
375
376 HostCache cache(0);
358 EXPECT_TRUE(cache.caching_is_disabled()); 377 EXPECT_TRUE(cache.caching_is_disabled());
359 378
360 // Set t=0. 379 // Set t=0.
361 base::TimeTicks now; 380 base::TimeTicks now;
362 381
363 // Lookup and Set should have no effect. 382 // Lookup and Set should have no effect.
364 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); 383 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL);
365 cache.Set(Key("foobar.com"), OK, AddressList(), now); 384 cache.Set(Key("foobar.com"), OK, AddressList(), now, kTTL);
366 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); 385 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL);
367 386
368 EXPECT_EQ(0U, cache.size()); 387 EXPECT_EQ(0U, cache.size());
369 } 388 }
370 389
371 TEST(HostCacheTest, Clear) { 390 TEST(HostCacheTest, Clear) {
372 HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); 391 const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
392
393 HostCache cache(kMaxCacheEntries);
373 394
374 // Set t=0. 395 // Set t=0.
375 base::TimeTicks now; 396 base::TimeTicks now;
376 397
377 EXPECT_EQ(0u, cache.size()); 398 EXPECT_EQ(0u, cache.size());
378 399
379 // Add three entries. 400 // Add three entries.
380 cache.Set(Key("foobar1.com"), OK, AddressList(), now); 401 cache.Set(Key("foobar1.com"), OK, AddressList(), now, kTTL);
381 cache.Set(Key("foobar2.com"), OK, AddressList(), now); 402 cache.Set(Key("foobar2.com"), OK, AddressList(), now, kTTL);
382 cache.Set(Key("foobar3.com"), OK, AddressList(), now); 403 cache.Set(Key("foobar3.com"), OK, AddressList(), now, kTTL);
383 404
384 EXPECT_EQ(3u, cache.size()); 405 EXPECT_EQ(3u, cache.size());
385 406
386 cache.clear(); 407 cache.clear();
387 408
388 EXPECT_EQ(0u, cache.size()); 409 EXPECT_EQ(0u, cache.size());
389 } 410 }
390 411
391 // Tests the less than and equal operators for HostCache::Key work. 412 // Tests the less than and equal operators for HostCache::Key work.
392 TEST(HostCacheTest, KeyComparators) { 413 TEST(HostCacheTest, KeyComparators) {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 EXPECT_TRUE(key2 < key1); 495 EXPECT_TRUE(key2 < key1);
475 EXPECT_FALSE(key2 == key1); 496 EXPECT_FALSE(key2 == key1);
476 break; 497 break;
477 default: 498 default:
478 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; 499 FAIL() << "Invalid expectation. Can be only -1, 0, 1";
479 } 500 }
480 } 501 }
481 } 502 }
482 503
483 } // namespace net 504 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698