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