Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1422)

Unified Diff: net/dns/host_cache_unittest.cc

Issue 1908543002: DNS: Retain stale entries in HostCache and return when requested (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase, make all members of Entry private Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/dns/host_cache.cc ('k') | net/dns/host_resolver_impl.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/dns/host_cache_unittest.cc
diff --git a/net/dns/host_cache_unittest.cc b/net/dns/host_cache_unittest.cc
index 838fedcfaa893ada8757235aa050ac135c3e2f54..c34627b1696a82a88e6aab1bc0a61e7c75758844 100644
--- a/net/dns/host_cache_unittest.cc
+++ b/net/dns/host_cache_unittest.cc
@@ -42,7 +42,7 @@ TEST(HostCacheTest, Basic) {
EXPECT_FALSE(cache.Lookup(key1, now));
cache.Set(key1, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
- EXPECT_TRUE(cache.Lookup(key1, now)->error == entry.error);
+ EXPECT_TRUE(cache.Lookup(key1, now)->error() == entry.error());
EXPECT_EQ(1U, cache.size());
@@ -253,9 +253,9 @@ TEST(HostCacheTest, HostResolverFlagsArePartOfKey) {
}
TEST(HostCacheTest, NoCache) {
- // Disable caching.
const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
+ // Disable caching.
HostCache cache(0);
EXPECT_TRUE(cache.caching_is_disabled());
@@ -296,6 +296,105 @@ TEST(HostCacheTest, Clear) {
EXPECT_EQ(0u, cache.size());
}
+// Try to add too many entries to cache; it should evict the one with the oldest
+// expiration time.
+TEST(HostCacheTest, Evict) {
+ HostCache cache(2);
+
+ base::TimeTicks now;
+
+ HostCache::Key key1 = Key("foobar.com");
+ HostCache::Key key2 = Key("foobar2.com");
+ HostCache::Key key3 = Key("foobar3.com");
+ HostCache::Entry entry = HostCache::Entry(OK, AddressList());
+
+ EXPECT_EQ(0u, cache.size());
+ EXPECT_FALSE(cache.Lookup(key1, now));
+ EXPECT_FALSE(cache.Lookup(key2, now));
+ EXPECT_FALSE(cache.Lookup(key3, now));
+
+ // |key1| expires in 10 seconds, but |key2| in just 5.
+ cache.Set(key1, entry, now, base::TimeDelta::FromSeconds(10));
+ cache.Set(key2, entry, now, base::TimeDelta::FromSeconds(5));
+ EXPECT_EQ(2u, cache.size());
+ EXPECT_TRUE(cache.Lookup(key1, now));
+ EXPECT_TRUE(cache.Lookup(key2, now));
+ EXPECT_FALSE(cache.Lookup(key3, now));
+
+ // |key2| should be chosen for eviction, since it expires sooner.
+ cache.Set(key3, entry, now, base::TimeDelta::FromSeconds(10));
+ EXPECT_EQ(2u, cache.size());
+ EXPECT_TRUE(cache.Lookup(key1, now));
+ EXPECT_FALSE(cache.Lookup(key2, now));
+ EXPECT_TRUE(cache.Lookup(key3, now));
+}
+
+// Try to retrieve stale entries from the cache. They should be returned by
+// |LookupStale()| but not |Lookup()|, with correct |EntryStaleness| data.
+TEST(HostCacheTest, Stale) {
+ const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
+
+ HostCache cache(kMaxCacheEntries);
+
+ // Start at t=0.
+ base::TimeTicks now;
+ HostCache::EntryStaleness stale;
+
+ HostCache::Key key = Key("foobar.com");
+ HostCache::Entry entry = HostCache::Entry(OK, AddressList());
+
+ EXPECT_EQ(0U, cache.size());
+
+ // Add an entry for "foobar.com" at t=0.
+ EXPECT_FALSE(cache.Lookup(key, now));
+ EXPECT_FALSE(cache.LookupStale(key, now, &stale));
+ cache.Set(key, entry, now, kTTL);
+ EXPECT_TRUE(cache.Lookup(key, now));
+ EXPECT_TRUE(cache.LookupStale(key, now, &stale));
+ EXPECT_FALSE(stale.is_stale());
+ EXPECT_EQ(0, stale.stale_hits);
+
+ EXPECT_EQ(1U, cache.size());
+
+ // Advance to t=5.
+ now += base::TimeDelta::FromSeconds(5);
+
+ EXPECT_TRUE(cache.Lookup(key, now));
+ EXPECT_TRUE(cache.LookupStale(key, now, &stale));
+ EXPECT_FALSE(stale.is_stale());
+ EXPECT_EQ(0, stale.stale_hits);
+
+ // Advance to t=15.
+ now += base::TimeDelta::FromSeconds(10);
+
+ EXPECT_FALSE(cache.Lookup(key, now));
+ EXPECT_TRUE(cache.LookupStale(key, now, &stale));
+ EXPECT_TRUE(stale.is_stale());
+ EXPECT_EQ(base::TimeDelta::FromSeconds(5), stale.expired_by);
+ EXPECT_EQ(0, stale.network_changes);
+ EXPECT_EQ(1, stale.stale_hits);
+
+ // Advance to t=20.
+ now += base::TimeDelta::FromSeconds(5);
+
+ EXPECT_FALSE(cache.Lookup(key, now));
+ EXPECT_TRUE(cache.LookupStale(key, now, &stale));
+ EXPECT_TRUE(stale.is_stale());
+ EXPECT_EQ(base::TimeDelta::FromSeconds(10), stale.expired_by);
+ EXPECT_EQ(0, stale.network_changes);
+ EXPECT_EQ(2, stale.stale_hits);
+
+ // Simulate network change.
+ cache.OnNetworkChange();
+
+ EXPECT_FALSE(cache.Lookup(key, now));
+ EXPECT_TRUE(cache.LookupStale(key, now, &stale));
+ EXPECT_TRUE(stale.is_stale());
+ EXPECT_EQ(base::TimeDelta::FromSeconds(10), stale.expired_by);
+ EXPECT_EQ(1, stale.network_changes);
+ EXPECT_EQ(3, stale.stale_hits);
+}
+
// Tests the less than and equal operators for HostCache::Key work.
TEST(HostCacheTest, KeyComparators) {
struct {
« no previous file with comments | « net/dns/host_cache.cc ('k') | net/dns/host_resolver_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698