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 15a43e3c5cda78c79898dfe94b585baad1a07a0e..862709dbcca2fefb75fbc20486351345e32de6b3 100644 |
| --- a/net/dns/host_cache_unittest.cc |
| +++ b/net/dns/host_cache_unittest.cc |
| @@ -5,6 +5,7 @@ |
| #include "net/dns/host_cache.h" |
| #include <string> |
| +#include <utility> |
| #include "base/bind.h" |
| #include "base/callback.h" |
| @@ -31,6 +32,16 @@ bool FoobarIndexIsOdd(const std::string& foobarx_com) { |
| return (foobarx_com[6] - '0') % 2 == 1; |
| } |
| +class MockPersistenceDelegate : public HostCache::PersistenceDelegate { |
| + public: |
| + void ScheduleWrite() override { num_changes_++; } |
|
mef
2017/06/20 16:18:12
nit: I think ++num_changes_ is preferred.
mgersh
2017/06/20 17:16:29
The rule is technically only for iterators, but su
|
| + |
| + int num_changes() { return num_changes_; } |
|
mef
2017/06/20 16:18:12
nit: const
mgersh
2017/06/20 17:16:29
Done.
|
| + |
| + private: |
| + int num_changes_ = 0; |
| +}; |
| + |
| } // namespace |
| TEST(HostCacheTest, Basic) { |
| @@ -274,7 +285,7 @@ TEST(HostCacheTest, NoCache) { |
| HostCache::Entry entry = HostCache::Entry(OK, AddressList()); |
| // Lookup and Set should have no effect. |
| - EXPECT_FALSE(cache.Lookup(Key("foobar.com"),now)); |
| + EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now)); |
| cache.Set(Key("foobar.com"), entry, now, kTTL); |
| EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now)); |
| @@ -752,4 +763,76 @@ TEST(HostCacheTest, SerializeAndDeserialize) { |
| EXPECT_EQ(address_ipv4, result4->addresses().front().address()); |
| } |
| +TEST(HostCacheTest, PersistenceDelegate) { |
| + const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); |
| + HostCache cache(kMaxCacheEntries); |
| + MockPersistenceDelegate delegate; |
| + cache.set_persistence_delegate(&delegate); |
| + |
| + HostCache::Key key1 = Key("foobar.com"); |
| + HostCache::Key key2 = Key("foobar2.com"); |
| + |
| + IPAddress address_ipv4(1, 2, 3, 4); |
| + IPAddress address_ipv6(0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| + IPEndPoint endpoint_ipv4(address_ipv4, 0); |
| + IPEndPoint endpoint_ipv6(address_ipv6, 0); |
| + |
| + HostCache::Entry entry1 = HostCache::Entry(OK, AddressList(endpoint_ipv4)); |
| + AddressList addresses2 = AddressList(endpoint_ipv6); |
| + addresses2.push_back(endpoint_ipv4); |
| + HostCache::Entry entry2 = HostCache::Entry(OK, addresses2); |
| + HostCache::Entry entry3 = |
| + HostCache::Entry(ERR_NAME_NOT_RESOLVED, AddressList()); |
| + |
| + // Start at t=0. |
| + base::TimeTicks now; |
| + EXPECT_EQ(0u, cache.size()); |
| + |
| + // Add two entries at t=0. |
| + EXPECT_FALSE(cache.Lookup(key1, now)); |
| + cache.Set(key1, entry1, now, kTTL); |
| + EXPECT_TRUE(cache.Lookup(key1, now)); |
| + EXPECT_EQ(1u, cache.size()); |
| + EXPECT_EQ(1, delegate.num_changes()); |
| + |
| + EXPECT_FALSE(cache.Lookup(key2, now)); |
| + cache.Set(key2, entry1, now, kTTL); |
| + EXPECT_TRUE(cache.Lookup(key2, now)); |
| + EXPECT_EQ(2u, cache.size()); |
| + EXPECT_EQ(2, delegate.num_changes()); |
| + |
| + // Advance to t=5. |
| + now += base::TimeDelta::FromSeconds(5); |
| + |
| + // Changes that shouldn't trigger a write: |
| + // Add an entry for "foobar.com" with different expiration time. |
| + EXPECT_TRUE(cache.Lookup(key1, now)); |
| + cache.Set(key1, entry1, now, kTTL); |
| + EXPECT_TRUE(cache.Lookup(key1, now)); |
| + EXPECT_EQ(2u, cache.size()); |
| + EXPECT_EQ(2, delegate.num_changes()); |
| + |
| + // Add an entry for "foobar.com" with different TTL. |
| + EXPECT_TRUE(cache.Lookup(key1, now)); |
| + cache.Set(key1, entry1, now, kTTL - base::TimeDelta::FromSeconds(5)); |
| + EXPECT_TRUE(cache.Lookup(key1, now)); |
| + EXPECT_EQ(2u, cache.size()); |
| + EXPECT_EQ(2, delegate.num_changes()); |
| + |
| + // Changes that should trigger a write: |
| + // Add an entry for "foobar.com" with different address list. |
| + EXPECT_TRUE(cache.Lookup(key1, now)); |
| + cache.Set(key1, entry2, now, kTTL); |
| + EXPECT_TRUE(cache.Lookup(key1, now)); |
| + EXPECT_EQ(2u, cache.size()); |
| + EXPECT_EQ(3, delegate.num_changes()); |
| + |
| + // Add an entry for "foobar.com" with different error. |
| + EXPECT_TRUE(cache.Lookup(key1, now)); |
| + cache.Set(key1, entry3, now, kTTL); |
| + EXPECT_TRUE(cache.Lookup(key1, now)); |
| + EXPECT_EQ(2u, cache.size()); |
| + EXPECT_EQ(4, delegate.num_changes()); |
| +} |
| + |
| } // namespace net |