| Index: net/base/host_cache_unittest.cc
|
| ===================================================================
|
| --- net/base/host_cache_unittest.cc (revision 34231)
|
| +++ net/base/host_cache_unittest.cc (working copy)
|
| @@ -14,8 +14,10 @@
|
|
|
| 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) {
|
| return HostCache::Key(hostname, ADDRESS_FAMILY_UNSPECIFIED);
|
| @@ -24,7 +26,7 @@
|
| } // namespace
|
|
|
| TEST(HostCacheTest, Basic) {
|
| - HostCache cache(kMaxCacheEntries, kCacheDurationMs);
|
| + HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL);
|
|
|
| // Start at t=0.
|
| base::TimeTicks now;
|
| @@ -81,9 +83,10 @@
|
| 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 @@
|
| 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 @@
|
|
|
| // 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 @@
|
| // 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, NoCache) {
|
| // Disable caching.
|
| - HostCache cache(0, kCacheDurationMs);
|
| + HostCache cache(0, kSuccessEntryTTL, kFailureEntryTTL);
|
| EXPECT_TRUE(cache.caching_is_disabled());
|
|
|
| // Set t=0.
|
|
|