Chromium Code Reviews| 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..1d7b71f479153aac341ebcc97a22cd5aeb098000 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) { |
|
Randy Smith (Not in Mondays)
2016/05/04 18:15:25
Any chance you'd be willing to add a description o
Julia Tuttle
2016/05/05 14:35:53
Oh! Sure.
|
| + 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::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 { |