diff options
author | eroman@chromium.org <eroman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-10 07:23:40 +0000 |
---|---|---|
committer | eroman@chromium.org <eroman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-10 07:23:40 +0000 |
commit | 112bd4674a886777b79dcd03263088b692fb47fd (patch) | |
tree | 7cf667fa50e6e9284d3a4f1d294cf86719698ff9 /net/base/host_cache_unittest.cc | |
parent | a4121a67a6a1383048d31130305e472966b44c97 (diff) | |
download | chromium_src-112bd4674a886777b79dcd03263088b692fb47fd.zip chromium_src-112bd4674a886777b79dcd03263088b692fb47fd.tar.gz chromium_src-112bd4674a886777b79dcd03263088b692fb47fd.tar.bz2 |
Cache failed DNS resolutions for 1 second.
This is a very small time to live, since we want to be able to respond quickly when formerly unresolvable names become resolvable.
Even such a small time is still useful, since cache misses for unresolvable names can be extremely costly (order of several seconds).
For example, in our corp PAC script, the URL's host is resolved 3 times, so:
Without caching, total runtime is (2.5 seconds) * 3 --> 7.5 seconds.
Whereas with caching it would be: (2.5 seconds) * 1 --> 2.5 seconds
This time to live will need to be tuned as part of bug 25472.
BUG=11079
Review URL: http://codereview.chromium.org/464084
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@34238 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base/host_cache_unittest.cc')
-rw-r--r-- | net/base/host_cache_unittest.cc | 88 |
1 files changed, 75 insertions, 13 deletions
diff --git a/net/base/host_cache_unittest.cc b/net/base/host_cache_unittest.cc index b51b2e0..5f4e21b 100644 --- a/net/base/host_cache_unittest.cc +++ b/net/base/host_cache_unittest.cc @@ -14,7 +14,9 @@ namespace net { namespace { const int kMaxCacheEntries = 10; -const int kCacheDurationMs = 10000; // 10 seconds. + +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) { @@ -24,7 +26,7 @@ HostCache::Key Key(const std::string& hostname) { } // namespace TEST(HostCacheTest, Basic) { - HostCache cache(kMaxCacheEntries, kCacheDurationMs); + HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); // Start at t=0. base::TimeTicks now; @@ -81,9 +83,10 @@ TEST(HostCacheTest, Basic) { EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), now) == NULL); } -// Try caching entries for a failed resolve attempt. -TEST(HostCacheTest, NegativeEntry) { - HostCache cache(kMaxCacheEntries, kCacheDurationMs); +// 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); // Set t=0. base::TimeTicks now; @@ -103,9 +106,70 @@ TEST(HostCacheTest, NegativeEntry) { 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. + + // Start at t=0. + base::TimeTicks now; + + const HostCache::Entry* entry1 = NULL; // Entry for foobar.com. + const HostCache::Entry* entry2 = NULL; // Entry for foobar2.com. + + EXPECT_EQ(0U, cache.size()); + + // 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); + entry1 = cache.Lookup(Key("foobar.com"), base::TimeTicks()); + EXPECT_FALSE(entry1 == NULL); + EXPECT_EQ(1U, cache.size()); + + // Advance to t=5. + now += base::TimeDelta::FromSeconds(5); + + // 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); + entry2 = cache.Lookup(Key("foobar2.com"), base::TimeTicks()); + EXPECT_FALSE(NULL == entry1); + EXPECT_EQ(2U, cache.size()); + + // Advance to t=9 + now += base::TimeDelta::FromSeconds(4); + + // Verify that the entries we added are still retrievable, and usable. + EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now)); + EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now)); + + // Advance to t=10; entry1 is now expired. + now += base::TimeDelta::FromSeconds(1); + + EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL); + 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); + // Re-uses existing entry storage. + EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now)); + EXPECT_EQ(2U, cache.size()); + + // Both entries should still be retrievable and usable. + EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now)); + EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now)); + + // Advance to t=20; both entries are now expired. + now += base::TimeDelta::FromSeconds(10); + + EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL); + EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), now) == NULL); +} + TEST(HostCacheTest, Compact) { // Initial entries limit is big enough to accomadate everything we add. - HostCache cache(kMaxCacheEntries, kCacheDurationMs); + HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); EXPECT_EQ(0U, cache.size()); @@ -171,16 +235,14 @@ TEST(HostCacheTest, Compact) { // Add entries while the cache is at capacity, causing evictions. TEST(HostCacheTest, SetWithCompact) { - HostCache cache(3, kCacheDurationMs); + HostCache cache(3, kSuccessEntryTTL, kFailureEntryTTL); // t=10 - base::TimeTicks now = - base::TimeTicks() + base::TimeDelta::FromMilliseconds(kCacheDurationMs); + base::TimeTicks now = base::TimeTicks() + kSuccessEntryTTL; cache.Set(Key("host1"), OK, AddressList(), now); cache.Set(Key("host2"), OK, AddressList(), now); - cache.Set(Key("expired"), OK, AddressList(), - now - base::TimeDelta::FromMilliseconds(kCacheDurationMs)); + cache.Set(Key("expired"), OK, AddressList(), now - kSuccessEntryTTL); EXPECT_EQ(3U, cache.size()); @@ -209,7 +271,7 @@ 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, kCacheDurationMs); + HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); // t=0. base::TimeTicks now; @@ -243,7 +305,7 @@ TEST(HostCacheTest, AddressFamilyIsPartOfKey) { TEST(HostCacheTest, NoCache) { // Disable caching. - HostCache cache(0, kCacheDurationMs); + HostCache cache(0, kSuccessEntryTTL, kFailureEntryTTL); EXPECT_TRUE(cache.caching_is_disabled()); // Set t=0. |