| 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-inl.h" | 8 #include "base/stl_util-inl.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/stringprintf.h" |
| 10 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 namespace net { | 14 namespace net { |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 const int kMaxCacheEntries = 10; | 17 const int kMaxCacheEntries = 10; |
| 17 | 18 |
| 18 const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10); | 19 const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10); |
| 19 const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(0); | 20 const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(0); |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 // Initial entries limit is big enough to accomadate everything we add. | 172 // Initial entries limit is big enough to accomadate everything we add. |
| 172 HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); | 173 HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); |
| 173 | 174 |
| 174 EXPECT_EQ(0U, cache.size()); | 175 EXPECT_EQ(0U, cache.size()); |
| 175 | 176 |
| 176 // t=10 | 177 // t=10 |
| 177 base::TimeTicks now = base::TimeTicks() + base::TimeDelta::FromSeconds(10); | 178 base::TimeTicks now = base::TimeTicks() + base::TimeDelta::FromSeconds(10); |
| 178 | 179 |
| 179 // Add five valid entries at t=10. | 180 // Add five valid entries at t=10. |
| 180 for (int i = 0; i < 5; ++i) { | 181 for (int i = 0; i < 5; ++i) { |
| 181 std::string hostname = StringPrintf("valid%d", i); | 182 std::string hostname = base::StringPrintf("valid%d", i); |
| 182 cache.Set(Key(hostname), OK, AddressList(), now); | 183 cache.Set(Key(hostname), OK, AddressList(), now); |
| 183 } | 184 } |
| 184 EXPECT_EQ(5U, cache.size()); | 185 EXPECT_EQ(5U, cache.size()); |
| 185 | 186 |
| 186 // Add 3 expired entries at t=0. | 187 // Add 3 expired entries at t=0. |
| 187 for (int i = 0; i < 3; ++i) { | 188 for (int i = 0; i < 3; ++i) { |
| 188 std::string hostname = StringPrintf("expired%d", i); | 189 std::string hostname = base::StringPrintf("expired%d", i); |
| 189 base::TimeTicks t = now - base::TimeDelta::FromSeconds(10); | 190 base::TimeTicks t = now - base::TimeDelta::FromSeconds(10); |
| 190 cache.Set(Key(hostname), OK, AddressList(), t); | 191 cache.Set(Key(hostname), OK, AddressList(), t); |
| 191 } | 192 } |
| 192 EXPECT_EQ(8U, cache.size()); | 193 EXPECT_EQ(8U, cache.size()); |
| 193 | 194 |
| 194 // Add 2 negative entries at t=10 | 195 // Add 2 negative entries at t=10 |
| 195 for (int i = 0; i < 2; ++i) { | 196 for (int i = 0; i < 2; ++i) { |
| 196 std::string hostname = StringPrintf("negative%d", i); | 197 std::string hostname = base::StringPrintf("negative%d", i); |
| 197 cache.Set(Key(hostname), ERR_NAME_NOT_RESOLVED, AddressList(), now); | 198 cache.Set(Key(hostname), ERR_NAME_NOT_RESOLVED, AddressList(), now); |
| 198 } | 199 } |
| 199 EXPECT_EQ(10U, cache.size()); | 200 EXPECT_EQ(10U, cache.size()); |
| 200 | 201 |
| 201 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid0"))); | 202 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid0"))); |
| 202 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid1"))); | 203 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid1"))); |
| 203 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid2"))); | 204 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid2"))); |
| 204 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid3"))); | 205 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid3"))); |
| 205 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid4"))); | 206 EXPECT_TRUE(ContainsKey(cache.entries_, Key("valid4"))); |
| 206 EXPECT_TRUE(ContainsKey(cache.entries_, Key("expired0"))); | 207 EXPECT_TRUE(ContainsKey(cache.entries_, Key("expired0"))); |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 { | 446 { |
| 446 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, | 447 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, |
| 447 HOST_RESOLVER_CANONNAME), | 448 HOST_RESOLVER_CANONNAME), |
| 448 HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED, | 449 HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED, |
| 449 HOST_RESOLVER_CANONNAME), | 450 HOST_RESOLVER_CANONNAME), |
| 450 -1 | 451 -1 |
| 451 }, | 452 }, |
| 452 }; | 453 }; |
| 453 | 454 |
| 454 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 455 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 455 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "]", i)); | 456 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]", i)); |
| 456 | 457 |
| 457 const HostCache::Key& key1 = tests[i].key1; | 458 const HostCache::Key& key1 = tests[i].key1; |
| 458 const HostCache::Key& key2 = tests[i].key2; | 459 const HostCache::Key& key2 = tests[i].key2; |
| 459 | 460 |
| 460 switch (tests[i].expected_comparison) { | 461 switch (tests[i].expected_comparison) { |
| 461 case -1: | 462 case -1: |
| 462 EXPECT_TRUE(key1 < key2); | 463 EXPECT_TRUE(key1 < key2); |
| 463 EXPECT_FALSE(key2 < key1); | 464 EXPECT_FALSE(key2 < key1); |
| 464 EXPECT_FALSE(key2 == key1); | 465 EXPECT_FALSE(key2 == key1); |
| 465 break; | 466 break; |
| 466 case 0: | 467 case 0: |
| 467 EXPECT_FALSE(key1 < key2); | 468 EXPECT_FALSE(key1 < key2); |
| 468 EXPECT_FALSE(key2 < key1); | 469 EXPECT_FALSE(key2 < key1); |
| 469 EXPECT_TRUE(key2 == key1); | 470 EXPECT_TRUE(key2 == key1); |
| 470 break; | 471 break; |
| 471 case 1: | 472 case 1: |
| 472 EXPECT_FALSE(key1 < key2); | 473 EXPECT_FALSE(key1 < key2); |
| 473 EXPECT_TRUE(key2 < key1); | 474 EXPECT_TRUE(key2 < key1); |
| 474 EXPECT_FALSE(key2 == key1); | 475 EXPECT_FALSE(key2 == key1); |
| 475 break; | 476 break; |
| 476 default: | 477 default: |
| 477 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; | 478 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; |
| 478 } | 479 } |
| 479 } | 480 } |
| 480 } | 481 } |
| 481 | 482 |
| 482 } // namespace net | 483 } // namespace net |
| OLD | NEW |