Chromium Code Reviews| Index: net/base/host_cache_unittest.cc |
| diff --git a/net/base/host_cache_unittest.cc b/net/base/host_cache_unittest.cc |
| index 4c99688b6cfde83f221b5cb9ee284d6a9bc5e402..c0c0dbab6d6be5b6097aff9ee6eec1dbf4f3f1a3 100644 |
| --- a/net/base/host_cache_unittest.cc |
| +++ b/net/base/host_cache_unittest.cc |
| @@ -16,9 +16,6 @@ namespace net { |
| namespace { |
|
mmenke
2012/01/19 22:12:43
nit: While you're here, could you add a line brea
|
| const int kMaxCacheEntries = 10; |
| -const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10); |
| -const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(0); |
| - |
| // Builds a key for |hostname|, defaulting the address family to unspecified. |
| HostCache::Key Key(const std::string& hostname) { |
| return HostCache::Key(hostname, ADDRESS_FAMILY_UNSPECIFIED, 0); |
| @@ -27,7 +24,9 @@ HostCache::Key Key(const std::string& hostname) { |
| } // namespace |
| TEST(HostCacheTest, Basic) { |
| - HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); |
| + const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); |
| + |
| + HostCache cache(kMaxCacheEntries); |
| // Start at t=0. |
| base::TimeTicks now; |
| @@ -39,7 +38,7 @@ TEST(HostCacheTest, Basic) { |
| // Add an entry for "foobar.com" at t=0. |
| EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); |
| - cache.Set(Key("foobar.com"), OK, AddressList(), now); |
| + cache.Set(Key("foobar.com"), OK, AddressList(), kTTL, now); |
| entry1 = cache.Lookup(Key("foobar.com"), base::TimeTicks()); |
| EXPECT_FALSE(entry1 == NULL); |
| EXPECT_EQ(1U, cache.size()); |
| @@ -49,7 +48,7 @@ TEST(HostCacheTest, Basic) { |
| // Add an entry for "foobar2.com" at t=5. |
| EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), base::TimeTicks()) == NULL); |
| - cache.Set(Key("foobar2.com"), OK, AddressList(), now); |
| + cache.Set(Key("foobar2.com"), OK, AddressList(), kTTL, now); |
| entry2 = cache.Lookup(Key("foobar2.com"), base::TimeTicks()); |
| EXPECT_FALSE(NULL == entry1); |
| EXPECT_EQ(2U, cache.size()); |
| @@ -68,7 +67,7 @@ TEST(HostCacheTest, Basic) { |
| EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now)); |
| // Update entry1, so it is no longer expired. |
| - cache.Set(Key("foobar.com"), OK, AddressList(), now); |
| + cache.Set(Key("foobar.com"), OK, AddressList(), kTTL, now); |
| // Re-uses existing entry storage. |
| EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now)); |
| EXPECT_EQ(2U, cache.size()); |
| @@ -87,13 +86,17 @@ TEST(HostCacheTest, Basic) { |
| // Try caching entries for a failed resolve attempt -- since we set |
| // the TTL of such entries to 0 it won't work. |
| TEST(HostCacheTest, NoCacheNegative) { |
| - HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); |
| + const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10); |
| + const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(0); |
| + |
| + HostCache cache(kMaxCacheEntries); |
| // Set t=0. |
| base::TimeTicks now; |
| EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); |
| - cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now); |
| + cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), |
| + kFailureEntryTTL, now); |
| EXPECT_EQ(1U, cache.size()); |
| // We disallow use of negative entries. |
| @@ -101,17 +104,18 @@ TEST(HostCacheTest, NoCacheNegative) { |
| // Now overwrite with a valid entry, and then overwrite with negative entry |
| // again -- the valid entry should be kicked out. |
| - cache.Set(Key("foobar.com"), OK, AddressList(), now); |
| + cache.Set(Key("foobar.com"), OK, AddressList(), kSuccessEntryTTL, now); |
| EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now) == NULL); |
| - cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now); |
| + cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), |
| + kFailureEntryTTL, now); |
| EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL); |
| } |
| // Try caching entries for a failed resolves for 10 seconds. |
| TEST(HostCacheTest, CacheNegativeEntry) { |
| - HostCache cache(kMaxCacheEntries, |
| - base::TimeDelta::FromSeconds(0), // success entry TTL. |
| - base::TimeDelta::FromSeconds(10)); // failure entry TTL. |
| + const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(10); |
| + |
| + HostCache cache(kMaxCacheEntries); |
| // Start at t=0. |
| base::TimeTicks now; |
| @@ -123,7 +127,8 @@ TEST(HostCacheTest, CacheNegativeEntry) { |
| // Add an entry for "foobar.com" at t=0. |
| EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); |
| - cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now); |
| + cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), |
| + kFailureEntryTTL, now); |
| entry1 = cache.Lookup(Key("foobar.com"), base::TimeTicks()); |
| EXPECT_FALSE(entry1 == NULL); |
| EXPECT_EQ(1U, cache.size()); |
| @@ -133,7 +138,8 @@ TEST(HostCacheTest, CacheNegativeEntry) { |
| // Add an entry for "foobar2.com" at t=5. |
| EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), base::TimeTicks()) == NULL); |
| - cache.Set(Key("foobar2.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now); |
| + cache.Set(Key("foobar2.com"), ERR_NAME_NOT_RESOLVED, AddressList(), |
| + kFailureEntryTTL, now); |
| entry2 = cache.Lookup(Key("foobar2.com"), base::TimeTicks()); |
| EXPECT_FALSE(NULL == entry1); |
| EXPECT_EQ(2U, cache.size()); |
| @@ -152,7 +158,8 @@ TEST(HostCacheTest, CacheNegativeEntry) { |
| EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now)); |
| // Update entry1, so it is no longer expired. |
| - cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now); |
| + cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), |
| + kFailureEntryTTL, now); |
| // Re-uses existing entry storage. |
| EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now)); |
| EXPECT_EQ(2U, cache.size()); |
| @@ -170,7 +177,9 @@ TEST(HostCacheTest, CacheNegativeEntry) { |
| TEST(HostCacheTest, Compact) { |
| // Initial entries limit is big enough to accomadate everything we add. |
| - HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); |
| + const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10); |
| + const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(0); |
| + HostCache cache(kMaxCacheEntries); |
| EXPECT_EQ(0U, cache.size()); |
| @@ -180,7 +189,7 @@ TEST(HostCacheTest, Compact) { |
| // Add five valid entries at t=10. |
| for (int i = 0; i < 5; ++i) { |
| std::string hostname = base::StringPrintf("valid%d", i); |
| - cache.Set(Key(hostname), OK, AddressList(), now); |
| + cache.Set(Key(hostname), OK, AddressList(), kSuccessEntryTTL, now); |
| } |
| EXPECT_EQ(5U, cache.size()); |
| @@ -188,14 +197,15 @@ TEST(HostCacheTest, Compact) { |
| for (int i = 0; i < 3; ++i) { |
| std::string hostname = base::StringPrintf("expired%d", i); |
| base::TimeTicks t = now - base::TimeDelta::FromSeconds(10); |
| - cache.Set(Key(hostname), OK, AddressList(), t); |
| + cache.Set(Key(hostname), OK, AddressList(), kSuccessEntryTTL, t); |
| } |
| EXPECT_EQ(8U, cache.size()); |
| // Add 2 negative entries at t=10 |
| for (int i = 0; i < 2; ++i) { |
| std::string hostname = base::StringPrintf("negative%d", i); |
| - cache.Set(Key(hostname), ERR_NAME_NOT_RESOLVED, AddressList(), now); |
| + cache.Set(Key(hostname), ERR_NAME_NOT_RESOLVED, AddressList(), |
| + kFailureEntryTTL, now); |
| } |
| EXPECT_EQ(10U, cache.size()); |
| @@ -236,14 +246,16 @@ TEST(HostCacheTest, Compact) { |
| // Add entries while the cache is at capacity, causing evictions. |
| TEST(HostCacheTest, SetWithCompact) { |
| - HostCache cache(3, kSuccessEntryTTL, kFailureEntryTTL); |
| + const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); |
| + |
| + HostCache cache(3); |
| // t=10 |
| - base::TimeTicks now = base::TimeTicks() + kSuccessEntryTTL; |
| + base::TimeTicks now = base::TimeTicks() + kTTL; |
| - cache.Set(Key("host1"), OK, AddressList(), now); |
| - cache.Set(Key("host2"), OK, AddressList(), now); |
| - cache.Set(Key("expired"), OK, AddressList(), now - kSuccessEntryTTL); |
| + cache.Set(Key("host1"), OK, AddressList(), kTTL, now); |
| + cache.Set(Key("host2"), OK, AddressList(), kTTL, now); |
| + cache.Set(Key("expired"), OK, AddressList(), kTTL, now - kTTL); |
| EXPECT_EQ(3U, cache.size()); |
| @@ -253,7 +265,7 @@ TEST(HostCacheTest, SetWithCompact) { |
| EXPECT_TRUE(NULL == cache.Lookup(Key("expired"), now)); |
| // Adding the fourth entry will cause "expired" to be evicted. |
| - cache.Set(Key("host3"), OK, AddressList(), now); |
| + cache.Set(Key("host3"), OK, AddressList(), kTTL, now); |
| EXPECT_EQ(3U, cache.size()); |
| EXPECT_TRUE(cache.Lookup(Key("expired"), now) == NULL); |
| EXPECT_FALSE(cache.Lookup(Key("host1"), now) == NULL); |
| @@ -262,9 +274,9 @@ TEST(HostCacheTest, SetWithCompact) { |
| // Add two more entries. Something should be evicted, however "host5" |
| // should definitely be in there (since it was last inserted). |
| - cache.Set(Key("host4"), OK, AddressList(), now); |
| + cache.Set(Key("host4"), OK, AddressList(), kTTL, now); |
| EXPECT_EQ(3U, cache.size()); |
| - cache.Set(Key("host5"), OK, AddressList(), now); |
| + cache.Set(Key("host5"), OK, AddressList(), kTTL, now); |
| EXPECT_EQ(3U, cache.size()); |
| EXPECT_FALSE(cache.Lookup(Key("host5"), now) == NULL); |
| } |
| @@ -272,7 +284,9 @@ TEST(HostCacheTest, SetWithCompact) { |
| // Tests that the same hostname can be duplicated in the cache, so long as |
| // the address family differs. |
| TEST(HostCacheTest, AddressFamilyIsPartOfKey) { |
| - HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); |
| + const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10); |
| + |
| + HostCache cache(kMaxCacheEntries); |
| // t=0. |
| base::TimeTicks now; |
| @@ -287,14 +301,14 @@ TEST(HostCacheTest, AddressFamilyIsPartOfKey) { |
| // Add an entry for ("foobar.com", UNSPECIFIED) at t=0. |
| EXPECT_TRUE(cache.Lookup(key1, base::TimeTicks()) == NULL); |
| - cache.Set(key1, OK, AddressList(), now); |
| + cache.Set(key1, OK, AddressList(), kSuccessEntryTTL, now); |
| entry1 = cache.Lookup(key1, base::TimeTicks()); |
| EXPECT_FALSE(entry1 == NULL); |
| EXPECT_EQ(1U, cache.size()); |
| // Add an entry for ("foobar.com", IPV4_ONLY) at t=0. |
| EXPECT_TRUE(cache.Lookup(key2, base::TimeTicks()) == NULL); |
| - cache.Set(key2, OK, AddressList(), now); |
| + cache.Set(key2, OK, AddressList(), kSuccessEntryTTL, now); |
| entry2 = cache.Lookup(key2, base::TimeTicks()); |
| EXPECT_FALSE(entry2 == NULL); |
| EXPECT_EQ(2U, cache.size()); |
| @@ -307,7 +321,9 @@ TEST(HostCacheTest, AddressFamilyIsPartOfKey) { |
| // Tests that the same hostname can be duplicated in the cache, so long as |
| // the HostResolverFlags differ. |
| TEST(HostCacheTest, HostResolverFlagsArePartOfKey) { |
| - HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); |
| + const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); |
| + |
| + HostCache cache(kMaxCacheEntries); |
| // t=0. |
| base::TimeTicks now; |
| @@ -326,21 +342,21 @@ TEST(HostCacheTest, HostResolverFlagsArePartOfKey) { |
| // Add an entry for ("foobar.com", IPV4, NONE) at t=0. |
| EXPECT_TRUE(cache.Lookup(key1, base::TimeTicks()) == NULL); |
| - cache.Set(key1, OK, AddressList(), now); |
| + cache.Set(key1, OK, AddressList(), kTTL, now); |
| entry1 = cache.Lookup(key1, base::TimeTicks()); |
| EXPECT_FALSE(entry1 == NULL); |
| EXPECT_EQ(1U, cache.size()); |
| // Add an entry for ("foobar.com", IPV4, CANONNAME) at t=0. |
| EXPECT_TRUE(cache.Lookup(key2, base::TimeTicks()) == NULL); |
| - cache.Set(key2, OK, AddressList(), now); |
| + cache.Set(key2, OK, AddressList(), kTTL, now); |
| entry2 = cache.Lookup(key2, base::TimeTicks()); |
| EXPECT_FALSE(entry2 == NULL); |
| EXPECT_EQ(2U, cache.size()); |
| // Add an entry for ("foobar.com", IPV4, LOOPBACK_ONLY) at t=0. |
| EXPECT_TRUE(cache.Lookup(key3, base::TimeTicks()) == NULL); |
| - cache.Set(key3, OK, AddressList(), now); |
| + cache.Set(key3, OK, AddressList(), kTTL, now); |
| entry3 = cache.Lookup(key3, base::TimeTicks()); |
| EXPECT_FALSE(entry3 == NULL); |
| EXPECT_EQ(3U, cache.size()); |
| @@ -354,7 +370,9 @@ TEST(HostCacheTest, HostResolverFlagsArePartOfKey) { |
| TEST(HostCacheTest, NoCache) { |
| // Disable caching. |
| - HostCache cache(0, kSuccessEntryTTL, kFailureEntryTTL); |
| + const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); |
| + |
| + HostCache cache(0); |
| EXPECT_TRUE(cache.caching_is_disabled()); |
| // Set t=0. |
| @@ -362,14 +380,16 @@ TEST(HostCacheTest, NoCache) { |
| // Lookup and Set should have no effect. |
| EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); |
| - cache.Set(Key("foobar.com"), OK, AddressList(), now); |
| + cache.Set(Key("foobar.com"), OK, AddressList(), kTTL, now); |
| EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); |
| EXPECT_EQ(0U, cache.size()); |
| } |
| TEST(HostCacheTest, Clear) { |
| - HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); |
| + const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); |
| + |
| + HostCache cache(kMaxCacheEntries); |
| // Set t=0. |
| base::TimeTicks now; |
| @@ -377,9 +397,9 @@ TEST(HostCacheTest, Clear) { |
| EXPECT_EQ(0u, cache.size()); |
| // Add three entries. |
| - cache.Set(Key("foobar1.com"), OK, AddressList(), now); |
| - cache.Set(Key("foobar2.com"), OK, AddressList(), now); |
| - cache.Set(Key("foobar3.com"), OK, AddressList(), now); |
| + cache.Set(Key("foobar1.com"), OK, AddressList(), kTTL, now); |
| + cache.Set(Key("foobar2.com"), OK, AddressList(), kTTL, now); |
| + cache.Set(Key("foobar3.com"), OK, AddressList(), kTTL, now); |
| EXPECT_EQ(3u, cache.size()); |