| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <list> | 5 #include <list> |
| 6 #include <map> | 6 #include <map> |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" |
| 10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 11 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 12 #include "base/message_loop_proxy.h" | 14 #include "base/message_loop_proxy.h" |
| 13 #include "base/task.h" | 15 #include "base/task.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "webkit/quota/mock_storage_client.h" | 17 #include "webkit/quota/mock_storage_client.h" |
| 16 #include "webkit/quota/quota_manager.h" | 18 #include "webkit/quota/quota_manager.h" |
| 17 #include "webkit/quota/quota_temporary_storage_evictor.h" | 19 #include "webkit/quota/quota_temporary_storage_evictor.h" |
| 18 | 20 |
| 19 namespace quota { | 21 namespace quota { |
| 20 | 22 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 45 available_space_ += origin_usage; | 47 available_space_ += origin_usage; |
| 46 callback.Run(quota::kQuotaStatusOk); | 48 callback.Run(quota::kQuotaStatusOk); |
| 47 } | 49 } |
| 48 | 50 |
| 49 virtual void GetUsageAndQuotaForEviction( | 51 virtual void GetUsageAndQuotaForEviction( |
| 50 const GetUsageAndQuotaForEvictionCallback& callback) OVERRIDE { | 52 const GetUsageAndQuotaForEvictionCallback& callback) OVERRIDE { |
| 51 if (error_on_get_usage_and_quota_) { | 53 if (error_on_get_usage_and_quota_) { |
| 52 callback.Run(quota::kQuotaErrorInvalidAccess, QuotaAndUsage()); | 54 callback.Run(quota::kQuotaErrorInvalidAccess, QuotaAndUsage()); |
| 53 return; | 55 return; |
| 54 } | 56 } |
| 55 if (task_for_get_usage_and_quota_.get()) | 57 if (!task_for_get_usage_and_quota_.is_null()) |
| 56 task_for_get_usage_and_quota_->Run(); | 58 task_for_get_usage_and_quota_.Run(); |
| 57 QuotaAndUsage quota_and_usage = { | 59 QuotaAndUsage quota_and_usage = { |
| 58 GetUsage(), unlimited_usage_, quota_, available_space_ }; | 60 GetUsage(), unlimited_usage_, quota_, available_space_ }; |
| 59 callback.Run(quota::kQuotaStatusOk, quota_and_usage); | 61 callback.Run(quota::kQuotaStatusOk, quota_and_usage); |
| 60 } | 62 } |
| 61 | 63 |
| 62 virtual void GetLRUOrigin( | 64 virtual void GetLRUOrigin( |
| 63 StorageType type, | 65 StorageType type, |
| 64 const GetLRUOriginCallback& callback) OVERRIDE { | 66 const GetLRUOriginCallback& callback) OVERRIDE { |
| 65 if (origin_order_.empty()) | 67 if (origin_order_.empty()) |
| 66 callback.Run(GURL()); | 68 callback.Run(GURL()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 79 | 81 |
| 80 void set_quota(int64 quota) { | 82 void set_quota(int64 quota) { |
| 81 quota_ = quota; | 83 quota_ = quota; |
| 82 } | 84 } |
| 83 void set_unlimited_usage(int64 usage) { | 85 void set_unlimited_usage(int64 usage) { |
| 84 unlimited_usage_ = usage; | 86 unlimited_usage_ = usage; |
| 85 } | 87 } |
| 86 void set_available_space(int64 available_space) { | 88 void set_available_space(int64 available_space) { |
| 87 available_space_ = available_space; | 89 available_space_ = available_space; |
| 88 } | 90 } |
| 89 void set_task_for_get_usage_and_quota(CancelableTask* task) { | 91 void set_task_for_get_usage_and_quota(const base::Closure& task) { |
| 90 task_for_get_usage_and_quota_.reset(task); | 92 task_for_get_usage_and_quota_= task; |
| 91 } | 93 } |
| 92 void set_error_on_evict_origin_data(bool error_on_evict_origin_data) { | 94 void set_error_on_evict_origin_data(bool error_on_evict_origin_data) { |
| 93 error_on_evict_origin_data_ = error_on_evict_origin_data; | 95 error_on_evict_origin_data_ = error_on_evict_origin_data; |
| 94 } | 96 } |
| 95 void set_error_on_get_usage_and_quota(bool error_on_get_usage_and_quota) { | 97 void set_error_on_get_usage_and_quota(bool error_on_get_usage_and_quota) { |
| 96 error_on_get_usage_and_quota_ = error_on_get_usage_and_quota; | 98 error_on_get_usage_and_quota_ = error_on_get_usage_and_quota; |
| 97 } | 99 } |
| 98 | 100 |
| 99 // Simulates an access to |origin|. It reorders the internal LRU list. | 101 // Simulates an access to |origin|. It reorders the internal LRU list. |
| 100 // It internally uses AddOrigin(). | 102 // It internally uses AddOrigin(). |
| (...skipping 27 matching lines...) Expand all Loading... |
| 128 | 130 |
| 129 int64 quota_; | 131 int64 quota_; |
| 130 int64 unlimited_usage_; | 132 int64 unlimited_usage_; |
| 131 int64 available_space_; | 133 int64 available_space_; |
| 132 std::list<GURL> origin_order_; | 134 std::list<GURL> origin_order_; |
| 133 std::map<GURL, int64> origins_; | 135 std::map<GURL, int64> origins_; |
| 134 bool error_on_evict_origin_data_; | 136 bool error_on_evict_origin_data_; |
| 135 bool error_on_get_usage_and_quota_; | 137 bool error_on_get_usage_and_quota_; |
| 136 | 138 |
| 137 QuotaTemporaryStorageEvictorTest *test_; | 139 QuotaTemporaryStorageEvictorTest *test_; |
| 138 scoped_ptr<CancelableTask> task_for_get_usage_and_quota_; | 140 base::Closure task_for_get_usage_and_quota_; |
| 139 }; | 141 }; |
| 140 | 142 |
| 141 } // anonymous namespace | 143 } // anonymous namespace |
| 142 | 144 |
| 143 class QuotaTemporaryStorageEvictorTest : public testing::Test { | 145 class QuotaTemporaryStorageEvictorTest : public testing::Test { |
| 144 public: | 146 public: |
| 145 QuotaTemporaryStorageEvictorTest() | 147 QuotaTemporaryStorageEvictorTest() |
| 146 : num_get_usage_and_quota_for_eviction_(0), | 148 : num_get_usage_and_quota_for_eviction_(0), |
| 147 runnable_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} | 149 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} |
| 148 | 150 |
| 149 void SetUp() { | 151 void SetUp() { |
| 150 quota_eviction_handler_.reset(new MockQuotaEvictionHandler(this)); | 152 quota_eviction_handler_.reset(new MockQuotaEvictionHandler(this)); |
| 151 | 153 |
| 152 // Run multiple evictions in a single RunAllPending() when interval_ms == 0 | 154 // Run multiple evictions in a single RunAllPending() when interval_ms == 0 |
| 153 temporary_storage_evictor_.reset(new QuotaTemporaryStorageEvictor( | 155 temporary_storage_evictor_.reset(new QuotaTemporaryStorageEvictor( |
| 154 quota_eviction_handler_.get(), 0)); | 156 quota_eviction_handler_.get(), 0)); |
| 155 } | 157 } |
| 156 | 158 |
| 157 void TearDown() { | 159 void TearDown() { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 void reset_min_available_disk_space_to_start_eviction() const { | 221 void reset_min_available_disk_space_to_start_eviction() const { |
| 220 temporary_storage_evictor_-> | 222 temporary_storage_evictor_-> |
| 221 reset_min_available_disk_space_to_start_eviction(); | 223 reset_min_available_disk_space_to_start_eviction(); |
| 222 } | 224 } |
| 223 | 225 |
| 224 scoped_ptr<QuotaEvictionHandler> quota_eviction_handler_; | 226 scoped_ptr<QuotaEvictionHandler> quota_eviction_handler_; |
| 225 scoped_ptr<QuotaTemporaryStorageEvictor> temporary_storage_evictor_; | 227 scoped_ptr<QuotaTemporaryStorageEvictor> temporary_storage_evictor_; |
| 226 | 228 |
| 227 int num_get_usage_and_quota_for_eviction_; | 229 int num_get_usage_and_quota_for_eviction_; |
| 228 | 230 |
| 229 ScopedRunnableMethodFactory<QuotaTemporaryStorageEvictorTest> | 231 base::WeakPtrFactory<QuotaTemporaryStorageEvictorTest> weak_factory_; |
| 230 runnable_factory_; | |
| 231 | 232 |
| 232 DISALLOW_COPY_AND_ASSIGN(QuotaTemporaryStorageEvictorTest); | 233 DISALLOW_COPY_AND_ASSIGN(QuotaTemporaryStorageEvictorTest); |
| 233 }; | 234 }; |
| 234 | 235 |
| 235 TEST_F(QuotaTemporaryStorageEvictorTest, SimpleEvictionTest) { | 236 TEST_F(QuotaTemporaryStorageEvictorTest, SimpleEvictionTest) { |
| 236 quota_eviction_handler()->AddOrigin(GURL("http://www.z.com"), 3000); | 237 quota_eviction_handler()->AddOrigin(GURL("http://www.z.com"), 3000); |
| 237 quota_eviction_handler()->AddOrigin(GURL("http://www.y.com"), 200); | 238 quota_eviction_handler()->AddOrigin(GURL("http://www.y.com"), 200); |
| 238 quota_eviction_handler()->AddOrigin(GURL("http://www.x.com"), 500); | 239 quota_eviction_handler()->AddOrigin(GURL("http://www.x.com"), 500); |
| 239 quota_eviction_handler()->set_quota(4000); | 240 quota_eviction_handler()->set_quota(4000); |
| 240 quota_eviction_handler()->set_available_space(1000000000); | 241 quota_eviction_handler()->set_available_space(1000000000); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 const int64 initial_total_size = a_size + b_size + c_size + d_size; | 280 const int64 initial_total_size = a_size + b_size + c_size + d_size; |
| 280 const int64 e_size = 275; | 281 const int64 e_size = 275; |
| 281 | 282 |
| 282 quota_eviction_handler()->AddOrigin(GURL("http://www.d.com"), d_size); | 283 quota_eviction_handler()->AddOrigin(GURL("http://www.d.com"), d_size); |
| 283 quota_eviction_handler()->AddOrigin(GURL("http://www.c.com"), c_size); | 284 quota_eviction_handler()->AddOrigin(GURL("http://www.c.com"), c_size); |
| 284 quota_eviction_handler()->AddOrigin(GURL("http://www.b.com"), b_size); | 285 quota_eviction_handler()->AddOrigin(GURL("http://www.b.com"), b_size); |
| 285 quota_eviction_handler()->AddOrigin(GURL("http://www.a.com"), a_size); | 286 quota_eviction_handler()->AddOrigin(GURL("http://www.a.com"), a_size); |
| 286 quota_eviction_handler()->set_quota(1000); | 287 quota_eviction_handler()->set_quota(1000); |
| 287 quota_eviction_handler()->set_available_space(1000000000); | 288 quota_eviction_handler()->set_available_space(1000000000); |
| 288 quota_eviction_handler()->set_task_for_get_usage_and_quota( | 289 quota_eviction_handler()->set_task_for_get_usage_and_quota( |
| 289 runnable_factory_.NewRunnableMethod( | 290 base::Bind(&QuotaTemporaryStorageEvictorTest::TaskForRepeatedEvictionTest, |
| 290 &QuotaTemporaryStorageEvictorTest::TaskForRepeatedEvictionTest, | 291 weak_factory_.GetWeakPtr(), |
| 291 std::make_pair(GURL("http://www.e.com"), e_size), | 292 std::make_pair(GURL("http://www.e.com"), e_size), GURL(), |
| 292 GURL(), | 293 initial_total_size - d_size, |
| 293 initial_total_size - d_size, | 294 initial_total_size - d_size + e_size - c_size)); |
| 294 initial_total_size - d_size + e_size - c_size)); | |
| 295 EXPECT_EQ(initial_total_size, quota_eviction_handler()->GetUsage()); | 295 EXPECT_EQ(initial_total_size, quota_eviction_handler()->GetUsage()); |
| 296 temporary_storage_evictor()->Start(); | 296 temporary_storage_evictor()->Start(); |
| 297 MessageLoop::current()->RunAllPending(); | 297 MessageLoop::current()->RunAllPending(); |
| 298 EXPECT_EQ(initial_total_size - d_size + e_size - c_size - b_size, | 298 EXPECT_EQ(initial_total_size - d_size + e_size - c_size - b_size, |
| 299 quota_eviction_handler()->GetUsage()); | 299 quota_eviction_handler()->GetUsage()); |
| 300 EXPECT_EQ(5, num_get_usage_and_quota_for_eviction()); | 300 EXPECT_EQ(5, num_get_usage_and_quota_for_eviction()); |
| 301 | 301 |
| 302 EXPECT_EQ(0, statistics().num_errors_on_evicting_origin); | 302 EXPECT_EQ(0, statistics().num_errors_on_evicting_origin); |
| 303 EXPECT_EQ(0, statistics().num_errors_on_getting_usage_and_quota); | 303 EXPECT_EQ(0, statistics().num_errors_on_getting_usage_and_quota); |
| 304 EXPECT_EQ(3, statistics().num_evicted_origins); | 304 EXPECT_EQ(3, statistics().num_evicted_origins); |
| 305 EXPECT_EQ(2, statistics().num_eviction_rounds); | 305 EXPECT_EQ(2, statistics().num_eviction_rounds); |
| 306 EXPECT_EQ(0, statistics().num_skipped_eviction_rounds); | 306 EXPECT_EQ(0, statistics().num_skipped_eviction_rounds); |
| 307 } | 307 } |
| 308 | 308 |
| 309 TEST_F(QuotaTemporaryStorageEvictorTest, RepeatedEvictionSkippedTest) { | 309 TEST_F(QuotaTemporaryStorageEvictorTest, RepeatedEvictionSkippedTest) { |
| 310 const int64 a_size = 400; | 310 const int64 a_size = 400; |
| 311 const int64 b_size = 150; | 311 const int64 b_size = 150; |
| 312 const int64 c_size = 120; | 312 const int64 c_size = 120; |
| 313 const int64 d_size = 292; | 313 const int64 d_size = 292; |
| 314 const int64 initial_total_size = a_size + b_size + c_size + d_size; | 314 const int64 initial_total_size = a_size + b_size + c_size + d_size; |
| 315 | 315 |
| 316 quota_eviction_handler()->AddOrigin(GURL("http://www.d.com"), d_size); | 316 quota_eviction_handler()->AddOrigin(GURL("http://www.d.com"), d_size); |
| 317 quota_eviction_handler()->AddOrigin(GURL("http://www.c.com"), c_size); | 317 quota_eviction_handler()->AddOrigin(GURL("http://www.c.com"), c_size); |
| 318 quota_eviction_handler()->AddOrigin(GURL("http://www.b.com"), b_size); | 318 quota_eviction_handler()->AddOrigin(GURL("http://www.b.com"), b_size); |
| 319 quota_eviction_handler()->AddOrigin(GURL("http://www.a.com"), a_size); | 319 quota_eviction_handler()->AddOrigin(GURL("http://www.a.com"), a_size); |
| 320 quota_eviction_handler()->set_quota(1000); | 320 quota_eviction_handler()->set_quota(1000); |
| 321 quota_eviction_handler()->set_available_space(1000000000); | 321 quota_eviction_handler()->set_available_space(1000000000); |
| 322 quota_eviction_handler()->set_task_for_get_usage_and_quota( | 322 quota_eviction_handler()->set_task_for_get_usage_and_quota( |
| 323 runnable_factory_.NewRunnableMethod( | 323 base::Bind(&QuotaTemporaryStorageEvictorTest::TaskForRepeatedEvictionTest, |
| 324 &QuotaTemporaryStorageEvictorTest::TaskForRepeatedEvictionTest, | 324 weak_factory_.GetWeakPtr(), std::make_pair(GURL(), 0), GURL(), |
| 325 std::make_pair(GURL(), 0), | 325 initial_total_size - d_size, initial_total_size - d_size)); |
| 326 GURL(), | |
| 327 initial_total_size - d_size, | |
| 328 initial_total_size - d_size)); | |
| 329 EXPECT_EQ(initial_total_size, quota_eviction_handler()->GetUsage()); | 326 EXPECT_EQ(initial_total_size, quota_eviction_handler()->GetUsage()); |
| 330 set_repeated_eviction(true); | 327 set_repeated_eviction(true); |
| 331 temporary_storage_evictor()->Start(); | 328 temporary_storage_evictor()->Start(); |
| 332 MessageLoop::current()->RunAllPending(); | 329 MessageLoop::current()->RunAllPending(); |
| 333 EXPECT_EQ(initial_total_size - d_size, | 330 EXPECT_EQ(initial_total_size - d_size, |
| 334 quota_eviction_handler()->GetUsage()); | 331 quota_eviction_handler()->GetUsage()); |
| 335 EXPECT_EQ(4, num_get_usage_and_quota_for_eviction()); | 332 EXPECT_EQ(4, num_get_usage_and_quota_for_eviction()); |
| 336 | 333 |
| 337 EXPECT_EQ(0, statistics().num_errors_on_evicting_origin); | 334 EXPECT_EQ(0, statistics().num_errors_on_evicting_origin); |
| 338 EXPECT_EQ(0, statistics().num_errors_on_getting_usage_and_quota); | 335 EXPECT_EQ(0, statistics().num_errors_on_getting_usage_and_quota); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 349 const int64 initial_total_size = a_size + b_size + c_size + d_size; | 346 const int64 initial_total_size = a_size + b_size + c_size + d_size; |
| 350 const int64 e_size = 275; | 347 const int64 e_size = 275; |
| 351 | 348 |
| 352 quota_eviction_handler()->AddOrigin(GURL("http://www.d.com"), d_size); | 349 quota_eviction_handler()->AddOrigin(GURL("http://www.d.com"), d_size); |
| 353 quota_eviction_handler()->AddOrigin(GURL("http://www.c.com"), c_size); | 350 quota_eviction_handler()->AddOrigin(GURL("http://www.c.com"), c_size); |
| 354 quota_eviction_handler()->AddOrigin(GURL("http://www.b.com"), b_size); | 351 quota_eviction_handler()->AddOrigin(GURL("http://www.b.com"), b_size); |
| 355 quota_eviction_handler()->AddOrigin(GURL("http://www.a.com"), a_size); | 352 quota_eviction_handler()->AddOrigin(GURL("http://www.a.com"), a_size); |
| 356 quota_eviction_handler()->set_quota(1000); | 353 quota_eviction_handler()->set_quota(1000); |
| 357 quota_eviction_handler()->set_available_space(1000000000); | 354 quota_eviction_handler()->set_available_space(1000000000); |
| 358 quota_eviction_handler()->set_task_for_get_usage_and_quota( | 355 quota_eviction_handler()->set_task_for_get_usage_and_quota( |
| 359 runnable_factory_.NewRunnableMethod( | 356 base::Bind(&QuotaTemporaryStorageEvictorTest::TaskForRepeatedEvictionTest, |
| 360 &QuotaTemporaryStorageEvictorTest::TaskForRepeatedEvictionTest, | 357 weak_factory_.GetWeakPtr(), |
| 361 std::make_pair(GURL("http://www.e.com"), e_size), | 358 std::make_pair(GURL("http://www.e.com"), e_size), |
| 362 GURL("http://www.c.com"), | 359 GURL("http://www.c.com"), |
| 363 initial_total_size - d_size, | 360 initial_total_size - d_size, |
| 364 initial_total_size - d_size + e_size - b_size)); | 361 initial_total_size - d_size + e_size - b_size)); |
| 365 EXPECT_EQ(initial_total_size, quota_eviction_handler()->GetUsage()); | 362 EXPECT_EQ(initial_total_size, quota_eviction_handler()->GetUsage()); |
| 366 temporary_storage_evictor()->Start(); | 363 temporary_storage_evictor()->Start(); |
| 367 MessageLoop::current()->RunAllPending(); | 364 MessageLoop::current()->RunAllPending(); |
| 368 EXPECT_EQ(initial_total_size - d_size + e_size - b_size - a_size, | 365 EXPECT_EQ(initial_total_size - d_size + e_size - b_size - a_size, |
| 369 quota_eviction_handler()->GetUsage()); | 366 quota_eviction_handler()->GetUsage()); |
| 370 EXPECT_EQ(5, num_get_usage_and_quota_for_eviction()); | 367 EXPECT_EQ(5, num_get_usage_and_quota_for_eviction()); |
| 371 | 368 |
| 372 EXPECT_EQ(0, statistics().num_errors_on_evicting_origin); | 369 EXPECT_EQ(0, statistics().num_errors_on_evicting_origin); |
| 373 EXPECT_EQ(0, statistics().num_errors_on_getting_usage_and_quota); | 370 EXPECT_EQ(0, statistics().num_errors_on_getting_usage_and_quota); |
| 374 EXPECT_EQ(3, statistics().num_evicted_origins); | 371 EXPECT_EQ(3, statistics().num_evicted_origins); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 quota_eviction_handler()->set_available_space(1000000000); | 425 quota_eviction_handler()->set_available_space(1000000000); |
| 429 EXPECT_EQ(3000 + 200 + 500000, quota_eviction_handler()->GetUsage()); | 426 EXPECT_EQ(3000 + 200 + 500000, quota_eviction_handler()->GetUsage()); |
| 430 set_repeated_eviction(false); | 427 set_repeated_eviction(false); |
| 431 temporary_storage_evictor()->Start(); | 428 temporary_storage_evictor()->Start(); |
| 432 MessageLoop::current()->RunAllPending(); | 429 MessageLoop::current()->RunAllPending(); |
| 433 // Nothing should have been evicted. | 430 // Nothing should have been evicted. |
| 434 EXPECT_EQ(3000 + 200 + 500000, quota_eviction_handler()->GetUsage()); | 431 EXPECT_EQ(3000 + 200 + 500000, quota_eviction_handler()->GetUsage()); |
| 435 } | 432 } |
| 436 | 433 |
| 437 } // namespace quota | 434 } // namespace quota |
| OLD | NEW |