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

Side by Side Diff: chrome/browser/browsing_data/conditional_cache_deletion_helper_browsertest.cc

Issue 2556363003: Refactor cache counting into a separate helper class (Closed)
Patch Set: extract cache_test_util and fixes Created 4 years 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 #include <memory> 6 #include <memory>
7 #include <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
12 #include "base/synchronization/waitable_event.h" 12 #include "base/synchronization/waitable_event.h"
13 #include "base/threading/platform_thread.h" 13 #include "base/threading/platform_thread.h"
14 #include "chrome/browser/browsing_data/cache_test_util.h"
14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h" 16 #include "chrome/browser/ui/browser.h"
16 #include "chrome/test/base/in_process_browser_test.h" 17 #include "chrome/test/base/in_process_browser_test.h"
17 #include "components/browsing_data/content/conditional_cache_deletion_helper.h" 18 #include "components/browsing_data/content/conditional_cache_deletion_helper.h"
18 #include "content/public/browser/browser_context.h" 19 #include "content/public/browser/browser_context.h"
19 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/storage_partition.h" 21 #include "content/public/browser/storage_partition.h"
21 #include "net/disk_cache/disk_cache.h" 22 #include "net/disk_cache/disk_cache.h"
22 #include "net/http/http_cache.h" 23 #include "net/http/http_cache.h"
23 #include "net/url_request/url_request_context.h" 24 #include "net/url_request/url_request_context.h"
(...skipping 14 matching lines...) Expand all
38 return url.GetOrigin() == "https://example.com/"; 39 return url.GetOrigin() == "https://example.com/";
39 } 40 }
40 41
41 } // namespace 42 } // namespace
42 43
43 class ConditionalCacheDeletionHelperBrowserTest : public InProcessBrowserTest { 44 class ConditionalCacheDeletionHelperBrowserTest : public InProcessBrowserTest {
44 public: 45 public:
45 // Initialization ------------------------------------------------------------ 46 // Initialization ------------------------------------------------------------
46 47
47 void SetUpOnMainThread() override { 48 void SetUpOnMainThread() override {
48 // Prepare the commonly used callbacks. 49 cache_util_ = base::MakeUnique<CacheTestUtil>(
49 done_callback_ = base::Bind( 50 content::BrowserContext::GetDefaultStoragePartition(
50 &ConditionalCacheDeletionHelperBrowserTest::DoneCallback, 51 browser()->profile()));
51 base::Unretained(this)); 52 done_callback_ =
53 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::DoneCallback,
54 base::Unretained(this));
52 55
53 // UI and IO thread synchronization. 56 cache_util_->SetUpOnMainThread();
54 waitable_event_ = base::MakeUnique<base::WaitableEvent>(
55 base::WaitableEvent::ResetPolicy::AUTOMATIC,
56 base::WaitableEvent::InitialState::NOT_SIGNALED);
57
58 // Get the storage partition.
59 partition_ = content::BrowserContext::GetDefaultStoragePartition(
60 browser()->profile());
61
62 // Get the cache backends.
63 BrowserThread::PostTask(
64 BrowserThread::IO, FROM_HERE,
65 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::SetUpOnIOThread,
66 base::Unretained(this)));
67 WaitForTasksOnIOThread();
68 } 57 }
69 58
70 void SetUpOnIOThread() { 59 void TearDownOnMainThread() override { cache_util_->TearDownOnMainThread(); }
71 net::URLRequestContextGetter* context = partition_->GetURLRequestContext();
72 60
73 net::HttpCache* cache = context->GetURLRequestContext()-> 61 void DeleteEntries(
74 http_transaction_factory()->GetCache(); 62 const base::Callback<bool(const disk_cache::Entry*)>& condition) {
63 DCHECK_CURRENTLY_ON(BrowserThread::IO);
64 auto* helper =
65 new ConditionalCacheDeletionHelper(cache_util_->backend(), condition);
75 66
76 SetNumberOfWaitedTasks(1); 67 helper->DeleteAndDestroySelfWhenFinished(done_callback_);
77 WaitForCompletion(cache->GetBackend(&backend_, done_callback_));
78 } 68 }
79 69
80 void TearDownOnMainThread() override { 70 void CompareRemainingKeys(std::set<std::string> expected_set) {
81 // The cache iterator must be deleted on the thread where it was created, 71 std::vector<std::string> remaining_keys = cache_util_->GetEntryKeys();
82 // which is the IO thread. 72 std::sort(remaining_keys.begin(), remaining_keys.end());
83 BrowserThread::PostTask( 73 std::vector<std::string> expected;
84 BrowserThread::IO, FROM_HERE, 74 expected.assign(expected_set.begin(), expected_set.end());
85 base::Bind( 75 EXPECT_EQ(expected, remaining_keys);
86 &ConditionalCacheDeletionHelperBrowserTest::TearDownOnIOThread,
87 base::Unretained(this)));
88 WaitForTasksOnIOThread();
89 }
90
91 void TearDownOnIOThread() {
92 iterator_.reset();
93 DoneCallback(net::OK);
94 }
95
96 // Waiting for tasks to be done on IO thread. --------------------------------
97
98 void WaitForTasksOnIOThread() {
99 DCHECK_CURRENTLY_ON(BrowserThread::UI);
100 waitable_event_->Wait();
101 }
102
103 void SetNumberOfWaitedTasks(int count) {
104 DCHECK_CURRENTLY_ON(BrowserThread::IO);
105 remaining_tasks_ = count;
106 }
107
108 void WaitForCompletion(int value) {
109 DCHECK_CURRENTLY_ON(BrowserThread::IO);
110 if (value >= 0) {
111 // We got the result immediately.
112 DoneCallback(value);
113 } else if (value == net::ERR_IO_PENDING) {
114 // We need to wait for the callback.
115 } else {
116 // An error has occurred.
117 NOTREACHED();
118 }
119 } 76 }
120 77
121 void DoneCallback(int value) { 78 void DoneCallback(int value) {
122 DCHECK_GE(value, 0); // Negative values represent an error. 79 DCHECK_GE(value, 0); // Negative values represent an error.
123 DCHECK_CURRENTLY_ON(BrowserThread::IO); 80 DCHECK_CURRENTLY_ON(BrowserThread::IO);
124 if (--remaining_tasks_ > 0)
125 return;
126 81
127 waitable_event_->Signal(); 82 if (run_loop_) {
msramek 2016/12/20 01:03:00 Why the switch from WaitableEvent to base::RunLoop
dullweber 2016/12/21 10:29:19 Oh, thanks. I will revert it here and in the util
msramek 2017/01/03 14:44:36 I agree - because Run() and Quit() are both on the
128 } 83 BrowserThread::PostTask(
129 84 BrowserThread::UI, FROM_HERE,
130 // Cache operation shorthands. ----------------------------------------------- 85 base::Bind(&base::RunLoop::Quit, base::Unretained(run_loop_.get())));
131
132 void CreateCacheEntries(const std::set<std::string>& keys) {
133 DCHECK_CURRENTLY_ON(BrowserThread::IO);
134
135 entries_.resize(keys.size());
136 SetNumberOfWaitedTasks(keys.size());
137
138 int pos = 0;
139 for (const std::string& key : keys) {
140 WaitForCompletion(backend_->CreateEntry(
141 key, &entries_[pos++], done_callback_));
142 } 86 }
143 } 87 }
144 88
145 void DeleteEntries( 89 void WaitForTasksOnIOThread() {
146 const base::Callback<bool(const disk_cache::Entry*)>& condition) { 90 DCHECK_CURRENTLY_ON(BrowserThread::UI);
147 DCHECK_CURRENTLY_ON(BrowserThread::IO); 91 run_loop_.reset(new base::RunLoop());
148 ConditionalCacheDeletionHelper* helper = new ConditionalCacheDeletionHelper( 92 run_loop_->Run();
149 backend_,
150 condition);
151
152 WaitForCompletion(helper->DeleteAndDestroySelfWhenFinished(done_callback_));
153 } 93 }
154 94
155 void GetRemainingKeys() { 95 protected:
156 DCHECK_CURRENTLY_ON(BrowserThread::IO);
157 current_entry_ = nullptr;
158 iterator_ = backend_->CreateIterator();
159 GetNextKey(net::OK);
160 }
161
162 void GetNextKey(int error) {
163 while (error != net::ERR_IO_PENDING) {
164 if (error == net::ERR_FAILED) {
165 DoneCallback(net::OK);
166 return;
167 }
168
169 if (current_entry_) {
170 remaining_keys_.push_back(current_entry_->GetKey());
171 }
172
173 error = iterator_->OpenNextEntry(
174 &current_entry_,
175 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::GetNextKey,
176 base::Unretained(this)));
177 }
178 }
179
180 void CompareRemainingKeys(std::set<std::string> expected_set) {
181 std::vector<std::string> expected;
182 expected.assign(expected_set.begin(), expected_set.end());
183 std::sort(remaining_keys_.begin(), remaining_keys_.end());
184 EXPECT_EQ(expected, remaining_keys_);
185 }
186
187 // Miscellaneous. ------------------------------------------------------------
188
189 private:
190 content::StoragePartition* partition_;
191 disk_cache::Backend* backend_ = nullptr;
192 std::unique_ptr<disk_cache::Backend::Iterator> iterator_;
193 disk_cache::Entry* current_entry_;
194 std::vector<disk_cache::Entry*> entries_;
195
196 base::Callback<void(int)> done_callback_; 96 base::Callback<void(int)> done_callback_;
msramek 2016/12/20 01:03:00 Ditto.
197 97 std::unique_ptr<base::RunLoop> run_loop_;
198 std::unique_ptr<base::WaitableEvent> waitable_event_; 98 std::unique_ptr<CacheTestUtil> cache_util_;
199 int remaining_tasks_;
200
201 std::vector<std::string> remaining_keys_;
202 }; 99 };
203 100
204 // Tests that ConditionalCacheDeletionHelper only deletes those cache entries 101 // Tests that ConditionalCacheDeletionHelper only deletes those cache entries
205 // that match the condition. 102 // that match the condition.
206 IN_PROC_BROWSER_TEST_F(ConditionalCacheDeletionHelperBrowserTest, Condition) { 103 IN_PROC_BROWSER_TEST_F(ConditionalCacheDeletionHelperBrowserTest, Condition) {
207 // Create 5 entries. 104 // Create 5 entries.
208 std::set<std::string> keys; 105 std::set<std::string> keys = {"123", "47", "56", "81", "42"};
209 keys.insert("123");
210 keys.insert("47");
211 keys.insert("56");
212 keys.insert("81");
213 keys.insert("42");
214 106
215 BrowserThread::PostTask( 107 cache_util_->CreateCacheEntries(keys);
216 BrowserThread::IO, FROM_HERE,
217 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::CreateCacheEntries,
218 base::Unretained(this),
219 base::ConstRef(keys)));
220 WaitForTasksOnIOThread();
221 108
222 // Delete the entries whose keys are even numbers. 109 // Delete the entries whose keys are even numbers.
223 BrowserThread::PostTask( 110 BrowserThread::PostTask(
224 BrowserThread::IO, FROM_HERE, 111 BrowserThread::IO, FROM_HERE,
225 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::DeleteEntries, 112 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::DeleteEntries,
226 base::Unretained(this), 113 base::Unretained(this),
227 base::Bind(&KeyIsEven))); 114 base::Bind(&KeyIsEven)));
228 WaitForTasksOnIOThread(); 115 WaitForTasksOnIOThread();
229 116
230 // Expect that the keys with values 56 and 42 were deleted. 117 // Expect that the keys with values 56 and 42 were deleted.
231 BrowserThread::PostTask(
232 BrowserThread::IO, FROM_HERE,
233 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::GetRemainingKeys,
234 base::Unretained(this)));
235 WaitForTasksOnIOThread();
236
237 keys.erase("56"); 118 keys.erase("56");
238 keys.erase("42"); 119 keys.erase("42");
239 CompareRemainingKeys(keys); 120 CompareRemainingKeys(keys);
240 } 121 }
241 122
242 // Tests that ConditionalCacheDeletionHelper correctly constructs a condition 123 // Tests that ConditionalCacheDeletionHelper correctly constructs a condition
243 // for time and URL. 124 // for time and URL.
244 // 125 //
245 // Note: This test depends on the timing in cache backends and can be flaky 126 // Note: This test depends on the timing in cache backends and can be flaky
246 // if those backends are slow. If this turns out to be a problem, consider 127 // if those backends are slow. If this turns out to be a problem, consider
247 // increasing the |timeout_ms| constant. 128 // increasing the |timeout_ms| constant.
248 // 129 //
249 // Flakily timing out on Mac 10.11 (crbug.com/646119) and flakily 130 // Flakily timing out on Mac 10.11 (crbug.com/646119) and flakily
250 // failing on Linux/ChromeOS (crbug.com/624836). 131 // failing on Linux/ChromeOS (crbug.com/624836).
251 #if defined(OS_MACOSX) || defined(OS_LINUX) || defined(OS_CHROMEOS) 132 #if defined(OS_MACOSX) || defined(OS_LINUX) || defined(OS_CHROMEOS)
252 #define MAYBE_TimeAndURL DISABLED_TimeAndURL 133 #define MAYBE_TimeAndURL DISABLED_TimeAndURL
253 #else 134 #else
254 #define MAYBE_TimeAndURL TimeAndURL 135 #define MAYBE_TimeAndURL TimeAndURL
255 #endif 136 #endif
256 IN_PROC_BROWSER_TEST_F(ConditionalCacheDeletionHelperBrowserTest, 137 IN_PROC_BROWSER_TEST_F(ConditionalCacheDeletionHelperBrowserTest,
257 MAYBE_TimeAndURL) { 138 MAYBE_TimeAndURL) {
258 const int64_t timeout_ms = 1; 139 const int64_t timeout_ms = 1;
259 140
260 // Create some entries. 141 // Create some entries.
261 std::set<std::string> keys; 142 std::set<std::string> keys;
262 keys.insert("https://google.com/index.html"); 143 keys.insert("https://google.com/index.html");
263 keys.insert("https://example.com/foo/bar/icon.png"); 144 keys.insert("https://example.com/foo/bar/icon.png");
264 keys.insert("http://chrome.com"); 145 keys.insert("http://chrome.com");
265 146
266 BrowserThread::PostTask( 147 cache_util_->CreateCacheEntries(keys);
267 BrowserThread::IO, FROM_HERE,
268 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::CreateCacheEntries,
269 base::Unretained(this),
270 base::ConstRef(keys)));
271 WaitForTasksOnIOThread();
272 148
273 // Wait |timeout_ms| milliseconds for the cache to write the entries. 149 // Wait |timeout_ms| milliseconds for the cache to write the entries.
274 // This assures that future entries will have timestamps strictly greater than 150 // This assures that future entries will have timestamps strictly greater than
275 // the ones we just added. 151 // the ones we just added.
276 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(timeout_ms)); 152 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(timeout_ms));
277 base::Time now = base::Time::Now(); 153 base::Time now = base::Time::Now();
278 154
279 // Create a few more entries with a later timestamp. 155 // Create a few more entries with a later timestamp.
280 std::set<std::string> newer_keys; 156 std::set<std::string> newer_keys;
281 newer_keys.insert("https://google.com/"); 157 newer_keys.insert("https://google.com/");
282 newer_keys.insert("https://example.com/foo/bar/icon2.png"); 158 newer_keys.insert("https://example.com/foo/bar/icon2.png");
283 newer_keys.insert("https://example.com/foo/bar/icon3.png"); 159 newer_keys.insert("https://example.com/foo/bar/icon3.png");
284 newer_keys.insert("http://example.com/foo/bar/icon4.png"); 160 newer_keys.insert("http://example.com/foo/bar/icon4.png");
285 161
286 BrowserThread::PostTask( 162 cache_util_->CreateCacheEntries(newer_keys);
287 BrowserThread::IO, FROM_HERE,
288 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::CreateCacheEntries,
289 base::Unretained(this),
290 base::ConstRef(newer_keys)));
291 WaitForTasksOnIOThread();
292 163
293 // Create a condition for entries with the "https://example.com" origin 164 // Create a condition for entries with the "https://example.com" origin
294 // created after waiting. 165 // created after waiting.
295 base::Callback<bool(const disk_cache::Entry*)> condition = 166 base::Callback<bool(const disk_cache::Entry*)> condition =
296 ConditionalCacheDeletionHelper::CreateURLAndTimeCondition( 167 ConditionalCacheDeletionHelper::CreateURLAndTimeCondition(
297 base::Bind(&HasHttpsExampleOrigin), now, base::Time::Max()); 168 base::Bind(&HasHttpsExampleOrigin), now, base::Time::Max());
298 169
299 // Delete the entries. 170 // Delete the entries.
300 BrowserThread::PostTask( 171 BrowserThread::PostTask(
301 BrowserThread::IO, FROM_HERE, 172 BrowserThread::IO, FROM_HERE,
302 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::DeleteEntries, 173 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::DeleteEntries,
303 base::Unretained(this), 174 base::Unretained(this),
304 base::ConstRef(condition))); 175 base::ConstRef(condition)));
305 WaitForTasksOnIOThread(); 176 WaitForTasksOnIOThread();
306 177
307 // Expect that only "icon2.png" and "icon3.png" were deleted. 178 // Expect that only "icon2.png" and "icon3.png" were deleted.
308 BrowserThread::PostTask(
309 BrowserThread::IO, FROM_HERE,
310 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::GetRemainingKeys,
311 base::Unretained(this)));
312 WaitForTasksOnIOThread();
313
314 keys.insert(newer_keys.begin(), newer_keys.end()); 179 keys.insert(newer_keys.begin(), newer_keys.end());
315 keys.erase("https://example.com/foo/bar/icon2.png"); 180 keys.erase("https://example.com/foo/bar/icon2.png");
316 keys.erase("https://example.com/foo/bar/icon3.png"); 181 keys.erase("https://example.com/foo/bar/icon3.png");
317 CompareRemainingKeys(keys); 182 CompareRemainingKeys(keys);
318 } 183 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698