OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/cronet/host_cache_persistence_manager.h" |
| 6 |
| 7 #include "base/test/scoped_mock_time_message_loop_task_runner.h" |
| 8 #include "base/test/scoped_task_environment.h" |
| 9 #include "base/values.h" |
| 10 #include "components/prefs/pref_registry_simple.h" |
| 11 #include "components/prefs/testing_pref_service.h" |
| 12 #include "net/base/net_errors.h" |
| 13 #include "net/dns/host_cache.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace cronet { |
| 17 |
| 18 class HostCachePersistenceManagerTest : public testing::Test { |
| 19 protected: |
| 20 void SetUp() override { |
| 21 cache_ = net::HostCache::CreateDefaultCache(); |
| 22 pref_service_ = base::MakeUnique<TestingPrefServiceSimple>(); |
| 23 pref_service_->registry()->RegisterListPref(kPrefName); |
| 24 } |
| 25 |
| 26 void MakePersistenceManager(base::TimeDelta delay) { |
| 27 persistence_manager_ = base::MakeUnique<HostCachePersistenceManager>( |
| 28 cache_.get(), pref_service_.get(), kPrefName, delay); |
| 29 } |
| 30 |
| 31 // Sets an entry in the HostCache in order to trigger a pref write. The |
| 32 // caller is responsible for making sure this is a change that will trigger |
| 33 // a write, and the HostCache's interaction with its PersistenceDelegate is |
| 34 // assumed to work (it's tested in net/dns/host_cache_unittest.cc). |
| 35 void WriteToCache(const std::string& host) { |
| 36 net::HostCache::Key key(host, net::ADDRESS_FAMILY_UNSPECIFIED, 0); |
| 37 net::HostCache::Entry entry(net::OK, net::AddressList()); |
| 38 cache_->Set(key, entry, base::TimeTicks::Now(), |
| 39 base::TimeDelta::FromSeconds(1)); |
| 40 } |
| 41 |
| 42 // Reads the current value of the pref from the TestingPrefServiceSimple |
| 43 // and deserializes it into a temporary new HostCache. Only checks the size, |
| 44 // not the full contents, since the tests in this file are only intended |
| 45 // to test that writes happen when they're supposed to, not serialization |
| 46 // correctness. |
| 47 void CheckPref(uint size) { |
| 48 const base::Value* value = pref_service_->GetUserPref(kPrefName); |
| 49 base::ListValue list; |
| 50 if (value) |
| 51 list = base::ListValue(value->GetList()); |
| 52 net::HostCache temp_cache(10); |
| 53 temp_cache.RestoreFromListValue(list); |
| 54 ASSERT_EQ(size, temp_cache.size()); |
| 55 } |
| 56 |
| 57 // Generates a temporary HostCache with a few entries and uses it to |
| 58 // initialize the value in prefs. |
| 59 void InitializePref() { |
| 60 net::HostCache temp_cache(10); |
| 61 |
| 62 net::HostCache::Key key1("1", net::ADDRESS_FAMILY_UNSPECIFIED, 0); |
| 63 net::HostCache::Key key2("2", net::ADDRESS_FAMILY_UNSPECIFIED, 0); |
| 64 net::HostCache::Key key3("3", net::ADDRESS_FAMILY_UNSPECIFIED, 0); |
| 65 net::HostCache::Entry entry(net::OK, net::AddressList()); |
| 66 |
| 67 temp_cache.Set(key1, entry, base::TimeTicks::Now(), |
| 68 base::TimeDelta::FromSeconds(1)); |
| 69 temp_cache.Set(key2, entry, base::TimeTicks::Now(), |
| 70 base::TimeDelta::FromSeconds(1)); |
| 71 temp_cache.Set(key3, entry, base::TimeTicks::Now(), |
| 72 base::TimeDelta::FromSeconds(1)); |
| 73 |
| 74 base::ListValue value; |
| 75 temp_cache.GetAsListValue(&value, false); |
| 76 pref_service_->Set(kPrefName, value); |
| 77 } |
| 78 |
| 79 static const char kPrefName[]; |
| 80 |
| 81 base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 82 base::ScopedMockTimeMessageLoopTaskRunner task_runner_; |
| 83 |
| 84 // The HostCache and PrefService have to outlive the |
| 85 // HostCachePersistenceManager. |
| 86 std::unique_ptr<net::HostCache> cache_; |
| 87 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 88 std::unique_ptr<HostCachePersistenceManager> persistence_manager_; |
| 89 }; |
| 90 |
| 91 const char HostCachePersistenceManagerTest::kPrefName[] = "net.test"; |
| 92 |
| 93 // Make a single change to the HostCache and make sure that it's written |
| 94 // when the timer expires. Then repeat. |
| 95 TEST_F(HostCachePersistenceManagerTest, SeparateWrites) { |
| 96 MakePersistenceManager(base::TimeDelta::FromSeconds(60)); |
| 97 |
| 98 WriteToCache("1"); |
| 99 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(59)); |
| 100 CheckPref(0); |
| 101 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(1)); |
| 102 CheckPref(1); |
| 103 |
| 104 WriteToCache("2"); |
| 105 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(59)); |
| 106 CheckPref(1); |
| 107 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(1)); |
| 108 CheckPref(2); |
| 109 } |
| 110 |
| 111 // Write to the HostCache multiple times and make sure that all changes |
| 112 // are written to prefs at the appropriate times. |
| 113 TEST_F(HostCachePersistenceManagerTest, MultipleWrites) { |
| 114 MakePersistenceManager(base::TimeDelta::FromSeconds(300)); |
| 115 |
| 116 WriteToCache("1"); |
| 117 WriteToCache("2"); |
| 118 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(299)); |
| 119 CheckPref(0); |
| 120 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(1)); |
| 121 CheckPref(2); |
| 122 |
| 123 WriteToCache("3"); |
| 124 WriteToCache("4"); |
| 125 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(299)); |
| 126 CheckPref(2); |
| 127 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(1)); |
| 128 CheckPref(4); |
| 129 } |
| 130 |
| 131 // Make changes to the HostCache at different times and ensure that the writes |
| 132 // to prefs are batched as expected. |
| 133 TEST_F(HostCachePersistenceManagerTest, BatchedWrites) { |
| 134 MakePersistenceManager(base::TimeDelta::FromMilliseconds(100)); |
| 135 |
| 136 WriteToCache("1"); |
| 137 task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(30)); |
| 138 WriteToCache("2"); |
| 139 task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(30)); |
| 140 WriteToCache("3"); |
| 141 CheckPref(0); |
| 142 task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(40)); |
| 143 CheckPref(3); |
| 144 |
| 145 // Add a delay in between batches. |
| 146 task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(50)); |
| 147 |
| 148 WriteToCache("4"); |
| 149 task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(30)); |
| 150 WriteToCache("5"); |
| 151 task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(30)); |
| 152 WriteToCache("6"); |
| 153 CheckPref(3); |
| 154 task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(40)); |
| 155 CheckPref(6); |
| 156 } |
| 157 |
| 158 // Set the pref before the HostCachePersistenceManager is created, and make |
| 159 // sure it gets picked up by the HostCache. |
| 160 TEST_F(HostCachePersistenceManagerTest, InitAfterPrefs) { |
| 161 CheckPref(0); |
| 162 InitializePref(); |
| 163 CheckPref(3); |
| 164 |
| 165 MakePersistenceManager(base::TimeDelta::FromSeconds(1)); |
| 166 task_runner_->RunUntilIdle(); |
| 167 ASSERT_EQ(3u, cache_->size()); |
| 168 } |
| 169 |
| 170 // Set the pref after the HostCachePersistenceManager is created, and make |
| 171 // sure it gets picked up by the HostCache. |
| 172 TEST_F(HostCachePersistenceManagerTest, InitBeforePrefs) { |
| 173 MakePersistenceManager(base::TimeDelta::FromSeconds(1)); |
| 174 ASSERT_EQ(0u, cache_->size()); |
| 175 |
| 176 CheckPref(0); |
| 177 InitializePref(); |
| 178 CheckPref(3); |
| 179 ASSERT_EQ(3u, cache_->size()); |
| 180 } |
| 181 |
| 182 } // namespace cronet |
OLD | NEW |