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

Side by Side Diff: content/browser/quota/quota_temporary_storage_evictor_unittest.cc

Issue 2814123005: Move storage quota unit tests and test doubles next to the files they cover. (Closed)
Patch Set: Move test from test_support to tests target :) Created 3 years, 8 months 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
(Empty)
1 // Copyright 2014 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 <stdint.h>
6
7 #include <list>
8 #include <map>
9 #include <memory>
10 #include <utility>
11
12 #include "base/bind.h"
13 #include "base/callback.h"
14 #include "base/macros.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/run_loop.h"
17 #include "base/test/scoped_task_environment.h"
18 #include "content/public/test/mock_storage_client.h"
19 #include "storage/browser/quota/quota_manager.h"
20 #include "storage/browser/quota/quota_temporary_storage_evictor.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 using storage::QuotaTemporaryStorageEvictor;
24 using storage::StorageType;
25
26 namespace content {
27
28 class QuotaTemporaryStorageEvictorTest;
29
30 namespace {
31
32 class MockQuotaEvictionHandler : public storage::QuotaEvictionHandler {
33 public:
34 explicit MockQuotaEvictionHandler(QuotaTemporaryStorageEvictorTest* test)
35 : available_space_(0),
36 error_on_evict_origin_data_(false),
37 error_on_get_usage_and_quota_(false) {}
38
39 void EvictOriginData(const GURL& origin,
40 StorageType type,
41 const storage::StatusCallback& callback) override {
42 if (error_on_evict_origin_data_) {
43 callback.Run(storage::kQuotaErrorInvalidModification);
44 return;
45 }
46 int64_t origin_usage = EnsureOriginRemoved(origin);
47 if (origin_usage >= 0)
48 available_space_ += origin_usage;
49 callback.Run(storage::kQuotaStatusOk);
50 }
51
52 void GetEvictionRoundInfo(
53 const EvictionRoundInfoCallback& callback) override {
54 if (error_on_get_usage_and_quota_) {
55 callback.Run(storage::kQuotaErrorAbort, storage::QuotaSettings(), 0, 0,
56 0, false);
57 return;
58 }
59 if (!task_for_get_usage_and_quota_.is_null())
60 task_for_get_usage_and_quota_.Run();
61 callback.Run(storage::kQuotaStatusOk, settings_, available_space_,
62 available_space_ * 2, GetUsage(), true);
63 }
64
65 void GetEvictionOrigin(StorageType type,
66 const std::set<GURL>& exceptions,
67 int64_t global_quota,
68 const storage::GetOriginCallback& callback) override {
69 if (origin_order_.empty())
70 callback.Run(GURL());
71 else
72 callback.Run(GURL(origin_order_.front()));
73 }
74
75 int64_t GetUsage() const {
76 int64_t total_usage = 0;
77 for (std::map<GURL, int64_t>::const_iterator p = origins_.begin();
78 p != origins_.end(); ++p)
79 total_usage += p->second;
80 return total_usage;
81 }
82
83 const storage::QuotaSettings& settings() const { return settings_; }
84 void SetPoolSize(int64_t pool_size) {
85 settings_.pool_size = pool_size;
86 settings_.per_host_quota = pool_size / 5;
87 settings_.should_remain_available = pool_size / 5;
88 settings_.must_remain_available = pool_size / 100;
89 settings_.refresh_interval = base::TimeDelta::Max();
90 }
91 void set_available_space(int64_t available_space) {
92 available_space_ = available_space;
93 }
94 void set_task_for_get_usage_and_quota(const base::Closure& task) {
95 task_for_get_usage_and_quota_= task;
96 }
97 void set_error_on_evict_origin_data(bool error_on_evict_origin_data) {
98 error_on_evict_origin_data_ = error_on_evict_origin_data;
99 }
100 void set_error_on_get_usage_and_quota(bool error_on_get_usage_and_quota) {
101 error_on_get_usage_and_quota_ = error_on_get_usage_and_quota;
102 }
103
104 // Simulates an access to |origin|. It reorders the internal LRU list.
105 // It internally uses AddOrigin().
106 void AccessOrigin(const GURL& origin) {
107 std::map<GURL, int64_t>::iterator found = origins_.find(origin);
108 EXPECT_TRUE(origins_.end() != found);
109 AddOrigin(origin, found->second);
110 }
111
112 // Simulates adding or overwriting the |origin| to the internal origin set
113 // with the |usage|. It also adds or moves the |origin| to the end of the
114 // LRU list.
115 void AddOrigin(const GURL& origin, int64_t usage) {
116 EnsureOriginRemoved(origin);
117 origin_order_.push_back(origin);
118 origins_[origin] = usage;
119 }
120
121 private:
122 int64_t EnsureOriginRemoved(const GURL& origin) {
123 int64_t origin_usage;
124 if (origins_.find(origin) == origins_.end())
125 return -1;
126 else
127 origin_usage = origins_[origin];
128
129 origins_.erase(origin);
130 origin_order_.remove(origin);
131 return origin_usage;
132 }
133
134 storage::QuotaSettings settings_;
135 int64_t available_space_;
136 std::list<GURL> origin_order_;
137 std::map<GURL, int64_t> origins_;
138 bool error_on_evict_origin_data_;
139 bool error_on_get_usage_and_quota_;
140
141 base::Closure task_for_get_usage_and_quota_;
142 };
143
144 } // namespace
145
146 class QuotaTemporaryStorageEvictorTest : public testing::Test {
147 public:
148 QuotaTemporaryStorageEvictorTest()
149 : num_get_usage_and_quota_for_eviction_(0),
150 weak_factory_(this) {}
151
152 void SetUp() override {
153 quota_eviction_handler_.reset(new MockQuotaEvictionHandler(this));
154
155 // Run multiple evictions in a single RunUntilIdle() when interval_ms == 0
156 temporary_storage_evictor_.reset(new QuotaTemporaryStorageEvictor(
157 quota_eviction_handler_.get(), 0));
158 }
159
160 void TearDown() override {
161 temporary_storage_evictor_.reset();
162 quota_eviction_handler_.reset();
163 base::RunLoop().RunUntilIdle();
164 }
165
166 void TaskForRepeatedEvictionTest(
167 const std::pair<GURL, int64_t>& origin_to_be_added,
168 const GURL& origin_to_be_accessed,
169 int expected_usage_after_first,
170 int expected_usage_after_second) {
171 EXPECT_GE(4, num_get_usage_and_quota_for_eviction_);
172 switch (num_get_usage_and_quota_for_eviction_) {
173 case 2:
174 EXPECT_EQ(expected_usage_after_first,
175 quota_eviction_handler()->GetUsage());
176 if (!origin_to_be_added.first.is_empty())
177 quota_eviction_handler()->AddOrigin(origin_to_be_added.first,
178 origin_to_be_added.second);
179 if (!origin_to_be_accessed.is_empty())
180 quota_eviction_handler()->AccessOrigin(origin_to_be_accessed);
181 break;
182 case 3:
183 EXPECT_EQ(expected_usage_after_second,
184 quota_eviction_handler()->GetUsage());
185 temporary_storage_evictor()->timer_disabled_for_testing_ = true;
186 break;
187 }
188 ++num_get_usage_and_quota_for_eviction_;
189 }
190
191 protected:
192 MockQuotaEvictionHandler* quota_eviction_handler() const {
193 return static_cast<MockQuotaEvictionHandler*>(
194 quota_eviction_handler_.get());
195 }
196
197 QuotaTemporaryStorageEvictor* temporary_storage_evictor() const {
198 return temporary_storage_evictor_.get();
199 }
200
201 const QuotaTemporaryStorageEvictor::Statistics& statistics() const {
202 return temporary_storage_evictor()->statistics_;
203 }
204
205 void disable_timer_for_testing() const {
206 temporary_storage_evictor_->timer_disabled_for_testing_ = true;
207 }
208
209 int num_get_usage_and_quota_for_eviction() const {
210 return num_get_usage_and_quota_for_eviction_;
211 }
212
213 base::test::ScopedTaskEnvironment scoped_task_environment_;
214 std::unique_ptr<MockQuotaEvictionHandler> quota_eviction_handler_;
215 std::unique_ptr<QuotaTemporaryStorageEvictor> temporary_storage_evictor_;
216 int num_get_usage_and_quota_for_eviction_;
217 base::WeakPtrFactory<QuotaTemporaryStorageEvictorTest> weak_factory_;
218 DISALLOW_COPY_AND_ASSIGN(QuotaTemporaryStorageEvictorTest);
219 };
220
221 TEST_F(QuotaTemporaryStorageEvictorTest, SimpleEvictionTest) {
222 quota_eviction_handler()->AddOrigin(GURL("http://www.z.com"), 3000);
223 quota_eviction_handler()->AddOrigin(GURL("http://www.y.com"), 200);
224 quota_eviction_handler()->AddOrigin(GURL("http://www.x.com"), 500);
225 quota_eviction_handler()->SetPoolSize(4000);
226 quota_eviction_handler()->set_available_space(1000000000);
227 EXPECT_EQ(3000 + 200 + 500, quota_eviction_handler()->GetUsage());
228 disable_timer_for_testing();
229 temporary_storage_evictor()->Start();
230 base::RunLoop().RunUntilIdle();
231 EXPECT_EQ(200 + 500, quota_eviction_handler()->GetUsage());
232
233 EXPECT_EQ(0, statistics().num_errors_on_evicting_origin);
234 EXPECT_EQ(0, statistics().num_errors_on_getting_usage_and_quota);
235 EXPECT_EQ(1, statistics().num_evicted_origins);
236 EXPECT_EQ(1, statistics().num_eviction_rounds);
237 EXPECT_EQ(0, statistics().num_skipped_eviction_rounds);
238 }
239
240 TEST_F(QuotaTemporaryStorageEvictorTest, MultipleEvictionTest) {
241 quota_eviction_handler()->AddOrigin(GURL("http://www.z.com"), 20);
242 quota_eviction_handler()->AddOrigin(GURL("http://www.y.com"), 2900);
243 quota_eviction_handler()->AddOrigin(GURL("http://www.x.com"), 450);
244 quota_eviction_handler()->AddOrigin(GURL("http://www.w.com"), 400);
245 quota_eviction_handler()->SetPoolSize(4000);
246 quota_eviction_handler()->set_available_space(1000000000);
247 EXPECT_EQ(20 + 2900 + 450 + 400, quota_eviction_handler()->GetUsage());
248 disable_timer_for_testing();
249 temporary_storage_evictor()->Start();
250 base::RunLoop().RunUntilIdle();
251 EXPECT_EQ(450 + 400, quota_eviction_handler()->GetUsage());
252
253 EXPECT_EQ(0, statistics().num_errors_on_evicting_origin);
254 EXPECT_EQ(0, statistics().num_errors_on_getting_usage_and_quota);
255 EXPECT_EQ(2, statistics().num_evicted_origins);
256 EXPECT_EQ(1, statistics().num_eviction_rounds);
257 EXPECT_EQ(0, statistics().num_skipped_eviction_rounds);
258 }
259
260 TEST_F(QuotaTemporaryStorageEvictorTest, RepeatedEvictionTest) {
261 const int64_t a_size = 400;
262 const int64_t b_size = 150;
263 const int64_t c_size = 120;
264 const int64_t d_size = 292;
265 const int64_t initial_total_size = a_size + b_size + c_size + d_size;
266 const int64_t e_size = 275;
267
268 quota_eviction_handler()->AddOrigin(GURL("http://www.d.com"), d_size);
269 quota_eviction_handler()->AddOrigin(GURL("http://www.c.com"), c_size);
270 quota_eviction_handler()->AddOrigin(GURL("http://www.b.com"), b_size);
271 quota_eviction_handler()->AddOrigin(GURL("http://www.a.com"), a_size);
272 quota_eviction_handler()->SetPoolSize(1000);
273 quota_eviction_handler()->set_available_space(1000000000);
274 quota_eviction_handler()->set_task_for_get_usage_and_quota(
275 base::Bind(&QuotaTemporaryStorageEvictorTest::TaskForRepeatedEvictionTest,
276 weak_factory_.GetWeakPtr(),
277 std::make_pair(GURL("http://www.e.com"), e_size), GURL(),
278 initial_total_size - d_size,
279 initial_total_size - d_size + e_size - c_size));
280 EXPECT_EQ(initial_total_size, quota_eviction_handler()->GetUsage());
281 temporary_storage_evictor()->Start();
282 base::RunLoop().RunUntilIdle();
283 EXPECT_EQ(initial_total_size - d_size + e_size - c_size - b_size,
284 quota_eviction_handler()->GetUsage());
285 EXPECT_EQ(5, num_get_usage_and_quota_for_eviction());
286
287 EXPECT_EQ(0, statistics().num_errors_on_evicting_origin);
288 EXPECT_EQ(0, statistics().num_errors_on_getting_usage_and_quota);
289 EXPECT_EQ(3, statistics().num_evicted_origins);
290 EXPECT_EQ(2, statistics().num_eviction_rounds);
291 EXPECT_EQ(0, statistics().num_skipped_eviction_rounds);
292 }
293
294 TEST_F(QuotaTemporaryStorageEvictorTest, RepeatedEvictionSkippedTest) {
295 const int64_t a_size = 400;
296 const int64_t b_size = 150;
297 const int64_t c_size = 120;
298 const int64_t d_size = 292;
299 const int64_t initial_total_size = a_size + b_size + c_size + d_size;
300
301 quota_eviction_handler()->AddOrigin(GURL("http://www.d.com"), d_size);
302 quota_eviction_handler()->AddOrigin(GURL("http://www.c.com"), c_size);
303 quota_eviction_handler()->AddOrigin(GURL("http://www.b.com"), b_size);
304 quota_eviction_handler()->AddOrigin(GURL("http://www.a.com"), a_size);
305 quota_eviction_handler()->SetPoolSize(1000);
306 quota_eviction_handler()->set_available_space(1000000000);
307 quota_eviction_handler()->set_task_for_get_usage_and_quota(
308 base::Bind(&QuotaTemporaryStorageEvictorTest::TaskForRepeatedEvictionTest,
309 weak_factory_.GetWeakPtr(), std::make_pair(GURL(), 0), GURL(),
310 initial_total_size - d_size, initial_total_size - d_size));
311 EXPECT_EQ(initial_total_size, quota_eviction_handler()->GetUsage());
312 // disable_timer_for_testing();
313 temporary_storage_evictor()->Start();
314 base::RunLoop().RunUntilIdle();
315 EXPECT_EQ(initial_total_size - d_size, quota_eviction_handler()->GetUsage());
316 EXPECT_EQ(4, num_get_usage_and_quota_for_eviction());
317
318 EXPECT_EQ(0, statistics().num_errors_on_evicting_origin);
319 EXPECT_EQ(0, statistics().num_errors_on_getting_usage_and_quota);
320 EXPECT_EQ(1, statistics().num_evicted_origins);
321 EXPECT_EQ(3, statistics().num_eviction_rounds);
322 EXPECT_EQ(2, statistics().num_skipped_eviction_rounds);
323 }
324
325 TEST_F(QuotaTemporaryStorageEvictorTest, RepeatedEvictionWithAccessOriginTest) {
326 const int64_t a_size = 400;
327 const int64_t b_size = 150;
328 const int64_t c_size = 120;
329 const int64_t d_size = 292;
330 const int64_t initial_total_size = a_size + b_size + c_size + d_size;
331 const int64_t e_size = 275;
332
333 quota_eviction_handler()->AddOrigin(GURL("http://www.d.com"), d_size);
334 quota_eviction_handler()->AddOrigin(GURL("http://www.c.com"), c_size);
335 quota_eviction_handler()->AddOrigin(GURL("http://www.b.com"), b_size);
336 quota_eviction_handler()->AddOrigin(GURL("http://www.a.com"), a_size);
337 quota_eviction_handler()->SetPoolSize(1000);
338 quota_eviction_handler()->set_available_space(1000000000);
339 quota_eviction_handler()->set_task_for_get_usage_and_quota(
340 base::Bind(&QuotaTemporaryStorageEvictorTest::TaskForRepeatedEvictionTest,
341 weak_factory_.GetWeakPtr(),
342 std::make_pair(GURL("http://www.e.com"), e_size),
343 GURL("http://www.c.com"),
344 initial_total_size - d_size,
345 initial_total_size - d_size + e_size - b_size));
346 EXPECT_EQ(initial_total_size, quota_eviction_handler()->GetUsage());
347 temporary_storage_evictor()->Start();
348 base::RunLoop().RunUntilIdle();
349 EXPECT_EQ(initial_total_size - d_size + e_size - b_size - a_size,
350 quota_eviction_handler()->GetUsage());
351 EXPECT_EQ(5, num_get_usage_and_quota_for_eviction());
352
353 EXPECT_EQ(0, statistics().num_errors_on_evicting_origin);
354 EXPECT_EQ(0, statistics().num_errors_on_getting_usage_and_quota);
355 EXPECT_EQ(3, statistics().num_evicted_origins);
356 EXPECT_EQ(2, statistics().num_eviction_rounds);
357 EXPECT_EQ(0, statistics().num_skipped_eviction_rounds);
358 }
359
360 TEST_F(QuotaTemporaryStorageEvictorTest, DiskSpaceNonEvictionTest) {
361 // If we're using so little that evicting all of it wouldn't
362 // do enough to alleviate a diskspace shortage, we don't evict.
363 quota_eviction_handler()->AddOrigin(GURL("http://www.z.com"), 10);
364 quota_eviction_handler()->AddOrigin(GURL("http://www.x.com"), 20);
365 quota_eviction_handler()->SetPoolSize(10000);
366 quota_eviction_handler()->set_available_space(
367 quota_eviction_handler()->settings().should_remain_available - 350);
368 EXPECT_EQ(10 + 20, quota_eviction_handler()->GetUsage());
369 disable_timer_for_testing();
370 temporary_storage_evictor()->Start();
371 base::RunLoop().RunUntilIdle();
372 EXPECT_EQ(10 + 20, quota_eviction_handler()->GetUsage());
373
374 EXPECT_EQ(0, statistics().num_errors_on_evicting_origin);
375 EXPECT_EQ(0, statistics().num_errors_on_getting_usage_and_quota);
376 EXPECT_EQ(0, statistics().num_evicted_origins);
377 EXPECT_EQ(1, statistics().num_eviction_rounds);
378 EXPECT_EQ(1, statistics().num_skipped_eviction_rounds);
379 }
380
381 TEST_F(QuotaTemporaryStorageEvictorTest, DiskSpaceEvictionTest) {
382 quota_eviction_handler()->AddOrigin(GURL("http://www.z.com"), 294);
383 quota_eviction_handler()->AddOrigin(GURL("http://www.y.com"), 120);
384 quota_eviction_handler()->AddOrigin(GURL("http://www.x.com"), 150);
385 quota_eviction_handler()->AddOrigin(GURL("http://www.w.com"), 300);
386 quota_eviction_handler()->SetPoolSize(10000);
387 quota_eviction_handler()->set_available_space(
388 quota_eviction_handler()->settings().should_remain_available - 350);
389 EXPECT_EQ(294 + 120 + 150 + 300, quota_eviction_handler()->GetUsage());
390 disable_timer_for_testing();
391 temporary_storage_evictor()->Start();
392 base::RunLoop().RunUntilIdle();
393 EXPECT_EQ(150 + 300, quota_eviction_handler()->GetUsage());
394
395 EXPECT_EQ(0, statistics().num_errors_on_evicting_origin);
396 EXPECT_EQ(0, statistics().num_errors_on_getting_usage_and_quota);
397 EXPECT_EQ(2, statistics().num_evicted_origins);
398 EXPECT_EQ(1, statistics().num_eviction_rounds);
399 EXPECT_EQ(0, statistics().num_skipped_eviction_rounds); // FIXME?
400 }
401
402 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/quota/quota_manager_unittest.cc ('k') | content/browser/quota/storage_monitor_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698