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