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

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: make requested changes, and some others, and rebase Created 4 years, 8 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
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..26176ab81fef222df7f4977386a4945b16d200aa 100644
--- a/net/dns/host_cache_unittest.cc
+++ b/net/dns/host_cache_unittest.cc
@@ -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,101 @@ TEST(HostCacheTest, Clear) {
EXPECT_EQ(0u, cache.size());
}
+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));
+}
+
+TEST(HostCacheTest, Stale) {
+ const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
+
+ HostCache cache(kMaxCacheEntries);
+
+ // Start at t=0.
+ base::TimeTicks now;
+ HostCache::StaleEntryInfo 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(0u, 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(0u, 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(0u, stale.network_changes);
+ EXPECT_EQ(1u, 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(0u, stale.network_changes);
+ EXPECT_EQ(2u, 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(1u, stale.network_changes);
+ EXPECT_EQ(3u, stale.stale_hits);
+}
+
// Tests the less than and equal operators for HostCache::Key work.
TEST(HostCacheTest, KeyComparators) {
struct {

Powered by Google App Engine
This is Rietveld 408576698