OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <algorithm> | |
6 #include <memory> | |
7 #include <set> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/run_loop.h" | |
12 #include "base/threading/platform_thread.h" | |
13 #include "chrome/browser/profiles/profile.h" | |
14 #include "chrome/browser/ui/browser.h" | |
15 #include "chrome/test/base/in_process_browser_test.h" | |
16 #include "components/browsing_data/content/conditional_cache_counting_helper.h" | |
17 #include "content/public/browser/browser_context.h" | |
18 #include "content/public/browser/browser_thread.h" | |
19 #include "content/public/browser/storage_partition.h" | |
20 #include "net/disk_cache/disk_cache.h" | |
21 #include "net/http/http_cache.h" | |
22 #include "net/url_request/url_request_context.h" | |
23 #include "net/url_request/url_request_context_getter.h" | |
24 | |
25 using browsing_data::ConditionalCacheCountingHelper; | |
26 using content::BrowserThread; | |
27 | |
28 class ConditionalCacheCountingHelperBrowserTest : public InProcessBrowserTest { | |
msramek
2016/12/15 15:58:50
Optional: I wonder if we could extract the common
dullweber
2016/12/16 16:41:10
I extracted creating and retrieving cache entries
msramek
2016/12/20 01:02:59
Thanks!
| |
29 public: | |
30 // Initialization ------------------------------------------------------------ | |
31 | |
32 const int64_t kTimeoutMs = 1; | |
msramek
2016/12/15 15:58:50
Let's try 10ms. The CCDH browsertest got a lot of
dullweber
2016/12/16 16:41:10
Done.
| |
33 | |
34 void SetUpOnMainThread() override { | |
35 // Prepare the commonly used callbacks. | |
36 done_callback_ = | |
37 base::Bind(&ConditionalCacheCountingHelperBrowserTest::DoneCallback, | |
38 base::Unretained(this)); | |
39 | |
40 count_callback_ = | |
41 base::Bind(&ConditionalCacheCountingHelperBrowserTest::CountCallback, | |
42 base::Unretained(this)); | |
43 | |
44 // Get the storage partition. | |
45 partition_ = content::BrowserContext::GetDefaultStoragePartition( | |
46 browser()->profile()); | |
47 | |
48 // Get the cache backends. | |
49 BrowserThread::PostTask( | |
50 BrowserThread::IO, FROM_HERE, | |
51 base::Bind(&ConditionalCacheCountingHelperBrowserTest::SetUpOnIOThread, | |
52 base::Unretained(this))); | |
53 WaitForTasksOnIOThread(); | |
54 } | |
55 | |
56 void SetUpOnIOThread() { | |
57 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
58 net::URLRequestContextGetter* context = partition_->GetURLRequestContext(); | |
59 | |
60 net::HttpCache* cache = | |
61 context->GetURLRequestContext()->http_transaction_factory()->GetCache(); | |
62 | |
63 SetNumberOfWaitedTasks(1); | |
64 WaitForCompletion(cache->GetBackend(&backend_, done_callback_)); | |
65 } | |
66 | |
67 // Waiting for tasks to be done on IO thread. -------------------------------- | |
68 | |
69 void WaitForTasksOnIOThread() { | |
70 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
71 run_loop_.reset(new base::RunLoop()); | |
72 run_loop_->Run(); | |
73 } | |
74 | |
75 void SetNumberOfWaitedTasks(int count) { | |
76 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
77 remaining_tasks_ = count; | |
78 } | |
79 | |
80 void WaitForCompletion(int value) { | |
81 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
82 if (value >= 0) { | |
83 // We got the result immediately. | |
84 DoneCallback(value); | |
85 } else if (value == net::ERR_IO_PENDING) { | |
86 // We need to wait for the callback. | |
87 } else { | |
88 // An error has occurred. | |
89 NOTREACHED(); | |
90 } | |
91 } | |
92 | |
93 void DoneCallback(int value) { | |
94 DCHECK_GE(value, 0); // Negative values represent an error. | |
95 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
96 if (--remaining_tasks_ > 0) | |
97 return; | |
98 | |
99 if (run_loop_) { | |
100 BrowserThread::PostTask( | |
101 BrowserThread::UI, FROM_HERE, | |
102 base::Bind(&base::RunLoop::Quit, base::Unretained(run_loop_.get()))); | |
103 } | |
104 } | |
105 | |
106 // Cache operation shorthands. ----------------------------------------------- | |
107 | |
108 void CreateCacheEntries(const std::set<std::string>& keys) { | |
109 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
110 | |
111 entries_.resize(keys.size()); | |
112 SetNumberOfWaitedTasks(keys.size()); | |
113 | |
114 int pos = 0; | |
115 for (const std::string& key : keys) { | |
116 WaitForCompletion( | |
117 backend_->CreateEntry(key, &entries_[pos++], done_callback_)); | |
118 } | |
119 } | |
120 | |
121 // Counting helpers. --------------------------------------------------------- | |
122 | |
123 void CountCallback(int64_t size) { | |
124 // Negative values represent an unexpected error. | |
125 DCHECK(size >= 0 || size == net::ERR_ABORTED); | |
126 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
127 last_size_ = size; | |
128 | |
129 if (run_loop_) | |
130 run_loop_->Quit(); | |
131 } | |
132 | |
133 void CountEntries(base::Time begin_time, base::Time end_time) { | |
134 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
135 last_size_ = -1; | |
136 auto helper = ConditionalCacheCountingHelper::CreateForRange( | |
137 partition_, begin_time, end_time); | |
138 helper->CountAndDestroySelfWhenFinished(count_callback_); | |
139 } | |
140 | |
141 void CountEntriesAndCancel(base::Time begin_time, base::Time end_time) { | |
142 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
143 last_size_ = -1; | |
144 auto helper = ConditionalCacheCountingHelper::CreateForRange( | |
145 partition_, begin_time, end_time); | |
146 // Cancel first to make sure we don't cancel after the IO thread finishes. | |
147 helper->CancelCounting(); | |
148 helper->CountAndDestroySelfWhenFinished(count_callback_); | |
149 } | |
150 | |
151 int64_t GetResult() { | |
152 DCHECK_GT(last_size_, 0); | |
153 return last_size_; | |
154 } | |
155 | |
156 int64_t GetResultOrError() { return last_size_; } | |
157 | |
158 // Miscellaneous. ------------------------------------------------------------ | |
msramek
2016/12/15 15:58:50
This section looks empty :)
dullweber
2016/12/16 16:41:10
Done.
| |
159 private: | |
160 content::StoragePartition* partition_; | |
161 disk_cache::Backend* backend_ = nullptr; | |
162 std::vector<disk_cache::Entry*> entries_; | |
163 | |
164 base::Callback<void(int)> done_callback_; | |
165 ConditionalCacheCountingHelper::CacheCountCallback count_callback_; | |
166 | |
167 std::unique_ptr<base::RunLoop> run_loop_; | |
168 int remaining_tasks_; | |
169 int64_t last_size_; | |
170 }; | |
171 | |
172 // Tests that ConditionalCacheCountingHelper only counts those cache entries | |
173 // that match the condition. | |
174 IN_PROC_BROWSER_TEST_F(ConditionalCacheCountingHelperBrowserTest, | |
175 CountingWithTimeout) { | |
msramek
2016/12/15 15:58:50
What does "Timeout" refer to?
dullweber
2016/12/16 16:41:10
It doesn't refer to anything anymore, I forgot to
| |
176 // Create 5 entries. | |
177 std::set<std::string> keys1 = {"1", "2", "3", "4", "5"}; | |
178 | |
179 base::Time t1 = base::Time::Now(); | |
180 BrowserThread::PostTask( | |
181 BrowserThread::IO, FROM_HERE, | |
182 base::Bind(&ConditionalCacheCountingHelperBrowserTest::CreateCacheEntries, | |
183 base::Unretained(this), base::ConstRef(keys1))); | |
184 WaitForTasksOnIOThread(); | |
185 | |
186 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(kTimeoutMs)); | |
187 base::Time t2 = base::Time::Now(); | |
188 | |
189 std::set<std::string> keys2 = {"6", "7"}; | |
190 BrowserThread::PostTask( | |
191 BrowserThread::IO, FROM_HERE, | |
192 base::Bind(&ConditionalCacheCountingHelperBrowserTest::CreateCacheEntries, | |
193 base::Unretained(this), base::ConstRef(keys2))); | |
194 WaitForTasksOnIOThread(); | |
195 | |
196 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(kTimeoutMs)); | |
197 base::Time t3 = base::Time::Now(); | |
198 | |
199 // Count the size of the first set of entries. | |
200 CountEntries(t1, t2); | |
201 WaitForTasksOnIOThread(); | |
202 int64_t size_1_2 = GetResult(); | |
203 | |
204 // Count the size of the second set of entries. | |
205 CountEntries(t2, t3); | |
206 WaitForTasksOnIOThread(); | |
207 int64_t size_2_3 = GetResult(); | |
208 | |
209 // Count all entries. | |
210 CountEntries(t1, t3); | |
211 WaitForTasksOnIOThread(); | |
212 int64_t size_1_3 = GetResult(); | |
213 EXPECT_EQ(size_1_2 + size_2_3, size_1_3); | |
214 | |
215 // Count everything | |
216 CountEntries(base::Time(), base::Time::Max()); | |
217 WaitForTasksOnIOThread(); | |
218 EXPECT_EQ(size_1_3, GetResult()); | |
219 } | |
220 | |
221 // Tests that ConditionalCacheCountingHelper returns net::ERR_ABORTED when | |
222 // cancelled. | |
223 IN_PROC_BROWSER_TEST_F(ConditionalCacheCountingHelperBrowserTest, | |
224 CancelCounting) { | |
225 std::set<std::string> keys = {"1", "2", "3", "4", "5"}; | |
226 base::Time t1 = base::Time::Now(); | |
227 BrowserThread::PostTask( | |
228 BrowserThread::IO, FROM_HERE, | |
229 base::Bind(&ConditionalCacheCountingHelperBrowserTest::CreateCacheEntries, | |
230 base::Unretained(this), base::ConstRef(keys))); | |
231 WaitForTasksOnIOThread(); | |
232 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(kTimeoutMs)); | |
233 | |
234 // Count and cancel | |
235 CountEntriesAndCancel(t1, base::Time::Now()); | |
236 WaitForTasksOnIOThread(); | |
237 EXPECT_EQ(net::ERR_ABORTED, GetResultOrError()); | |
238 } | |
OLD | NEW |