| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/disk_cache/disk_cache_test_base.h" | 5 #include "net/disk_cache/disk_cache_test_base.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
| 11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 #include "net/base/test_completion_callback.h" | 12 #include "net/base/test_completion_callback.h" |
| 13 #include "net/disk_cache/backend_impl.h" | 13 #include "net/disk_cache/backend_impl.h" |
| 14 #include "net/disk_cache/cache_util.h" |
| 15 #include "net/disk_cache/disk_cache.h" |
| 14 #include "net/disk_cache/disk_cache_test_util.h" | 16 #include "net/disk_cache/disk_cache_test_util.h" |
| 15 #include "net/disk_cache/mem_backend_impl.h" | 17 #include "net/disk_cache/mem_backend_impl.h" |
| 18 #include "net/disk_cache/simple/simple_backend_impl.h" |
| 16 | 19 |
| 17 DiskCacheTest::DiskCacheTest() { | 20 DiskCacheTest::DiskCacheTest() { |
| 18 CHECK(temp_dir_.CreateUniqueTempDir()); | 21 CHECK(temp_dir_.CreateUniqueTempDir()); |
| 19 cache_path_ = temp_dir_.path(); | 22 cache_path_ = temp_dir_.path(); |
| 20 if (!MessageLoop::current()) | 23 if (!MessageLoop::current()) |
| 21 message_loop_.reset(new MessageLoopForIO()); | 24 message_loop_.reset(new MessageLoopForIO()); |
| 22 } | 25 } |
| 23 | 26 |
| 24 DiskCacheTest::~DiskCacheTest() { | 27 DiskCacheTest::~DiskCacheTest() { |
| 25 } | 28 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 46 } | 49 } |
| 47 | 50 |
| 48 DiskCacheTestWithCache::DiskCacheTestWithCache() | 51 DiskCacheTestWithCache::DiskCacheTestWithCache() |
| 49 : cache_(NULL), | 52 : cache_(NULL), |
| 50 cache_impl_(NULL), | 53 cache_impl_(NULL), |
| 51 mem_cache_(NULL), | 54 mem_cache_(NULL), |
| 52 mask_(0), | 55 mask_(0), |
| 53 size_(0), | 56 size_(0), |
| 54 type_(net::DISK_CACHE), | 57 type_(net::DISK_CACHE), |
| 55 memory_only_(false), | 58 memory_only_(false), |
| 56 implementation_(false), | 59 simple_cache_mode_(false), |
| 57 force_creation_(false), | 60 force_creation_(false), |
| 58 new_eviction_(false), | 61 new_eviction_(false), |
| 59 first_cleanup_(true), | 62 first_cleanup_(true), |
| 60 integrity_(true), | 63 integrity_(true), |
| 61 use_current_thread_(false), | 64 use_current_thread_(false), |
| 62 cache_thread_("CacheThread") { | 65 cache_thread_("CacheThread") { |
| 63 } | 66 } |
| 64 | 67 |
| 65 DiskCacheTestWithCache::~DiskCacheTestWithCache() {} | 68 DiskCacheTestWithCache::~DiskCacheTestWithCache() {} |
| 66 | 69 |
| 67 void DiskCacheTestWithCache::InitCache() { | 70 void DiskCacheTestWithCache::InitCache() { |
| 68 if (mask_ || new_eviction_) | |
| 69 implementation_ = true; | |
| 70 | |
| 71 if (memory_only_) | 71 if (memory_only_) |
| 72 InitMemoryCache(); | 72 InitMemoryCache(); |
| 73 else | 73 else |
| 74 InitDiskCache(); | 74 InitDiskCache(); |
| 75 | 75 |
| 76 ASSERT_TRUE(NULL != cache_); | 76 ASSERT_TRUE(NULL != cache_); |
| 77 if (first_cleanup_) | 77 if (first_cleanup_) |
| 78 ASSERT_EQ(0, cache_->GetEntryCount()); | 78 ASSERT_EQ(0, cache_->GetEntryCount()); |
| 79 } | 79 } |
| 80 | 80 |
| 81 // We are expected to leak memory when simulating crashes. | 81 // We are expected to leak memory when simulating crashes. |
| 82 void DiskCacheTestWithCache::SimulateCrash() { | 82 void DiskCacheTestWithCache::SimulateCrash() { |
| 83 ASSERT_TRUE(implementation_ && !memory_only_); | 83 ASSERT_TRUE(!memory_only_); |
| 84 net::TestCompletionCallback cb; | 84 net::TestCompletionCallback cb; |
| 85 int rv = cache_impl_->FlushQueueForTest(cb.callback()); | 85 int rv = cache_impl_->FlushQueueForTest(cb.callback()); |
| 86 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 86 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 87 cache_impl_->ClearRefCountForTest(); | 87 cache_impl_->ClearRefCountForTest(); |
| 88 | 88 |
| 89 delete cache_impl_; | 89 delete cache_impl_; |
| 90 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); | 90 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); |
| 91 | 91 |
| 92 InitDiskCacheImpl(); | 92 CreateBackend(disk_cache::kNoRandom, &cache_thread_); |
| 93 } | 93 } |
| 94 | 94 |
| 95 void DiskCacheTestWithCache::SetTestMode() { | 95 void DiskCacheTestWithCache::SetTestMode() { |
| 96 ASSERT_TRUE(implementation_ && !memory_only_); | 96 ASSERT_TRUE(!memory_only_); |
| 97 cache_impl_->SetUnitTestMode(); | 97 cache_impl_->SetUnitTestMode(); |
| 98 } | 98 } |
| 99 | 99 |
| 100 void DiskCacheTestWithCache::SetMaxSize(int size) { | 100 void DiskCacheTestWithCache::SetMaxSize(int size) { |
| 101 size_ = size; | 101 size_ = size; |
| 102 if (cache_impl_) | 102 if (cache_impl_) |
| 103 EXPECT_TRUE(cache_impl_->SetMaxSize(size)); | 103 EXPECT_TRUE(cache_impl_->SetMaxSize(size)); |
| 104 | 104 |
| 105 if (mem_cache_) | 105 if (mem_cache_) |
| 106 EXPECT_TRUE(mem_cache_->SetMaxSize(size)); | 106 EXPECT_TRUE(mem_cache_->SetMaxSize(size)); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 cache_thread_.Stop(); | 229 cache_thread_.Stop(); |
| 230 | 230 |
| 231 if (!memory_only_ && integrity_) { | 231 if (!memory_only_ && integrity_) { |
| 232 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); | 232 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); |
| 233 } | 233 } |
| 234 | 234 |
| 235 PlatformTest::TearDown(); | 235 PlatformTest::TearDown(); |
| 236 } | 236 } |
| 237 | 237 |
| 238 void DiskCacheTestWithCache::InitMemoryCache() { | 238 void DiskCacheTestWithCache::InitMemoryCache() { |
| 239 if (!implementation_) { | |
| 240 cache_ = disk_cache::MemBackendImpl::CreateBackend(size_, NULL); | |
| 241 return; | |
| 242 } | |
| 243 | |
| 244 mem_cache_ = new disk_cache::MemBackendImpl(NULL); | 239 mem_cache_ = new disk_cache::MemBackendImpl(NULL); |
| 245 cache_ = mem_cache_; | 240 cache_ = mem_cache_; |
| 246 ASSERT_TRUE(NULL != cache_); | 241 ASSERT_TRUE(NULL != cache_); |
| 247 | 242 |
| 248 if (size_) | 243 if (size_) |
| 249 EXPECT_TRUE(mem_cache_->SetMaxSize(size_)); | 244 EXPECT_TRUE(mem_cache_->SetMaxSize(size_)); |
| 250 | 245 |
| 251 ASSERT_TRUE(mem_cache_->Init()); | 246 ASSERT_TRUE(mem_cache_->Init()); |
| 252 } | 247 } |
| 253 | 248 |
| 254 void DiskCacheTestWithCache::InitDiskCache() { | 249 void DiskCacheTestWithCache::InitDiskCache() { |
| 255 if (first_cleanup_) | 250 if (first_cleanup_) |
| 256 ASSERT_TRUE(CleanupCacheDir()); | 251 ASSERT_TRUE(CleanupCacheDir()); |
| 257 | 252 |
| 258 if (!cache_thread_.IsRunning()) { | 253 if (!cache_thread_.IsRunning()) { |
| 259 EXPECT_TRUE(cache_thread_.StartWithOptions( | 254 ASSERT_TRUE(cache_thread_.StartWithOptions( |
| 260 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 255 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
| 261 } | 256 } |
| 262 ASSERT_TRUE(cache_thread_.message_loop() != NULL); | 257 ASSERT_TRUE(cache_thread_.message_loop() != NULL); |
| 263 | 258 |
| 264 if (implementation_) | 259 CreateBackend(disk_cache::kNoRandom, &cache_thread_); |
| 265 return InitDiskCacheImpl(); | 260 } |
| 266 | 261 |
| 267 scoped_refptr<base::MessageLoopProxy> thread = | 262 // Testing backend creation retry logic is hard because the CacheCreator and |
| 268 use_current_thread_ ? base::MessageLoopProxy::current() : | 263 // cache backend(s) are tightly coupled. So we take the default backend often. |
| 269 cache_thread_.message_loop_proxy(); | 264 // Tests themselves need to be adjusted for platforms where the BackendImpl is |
| 265 // not the default backend. |
| 266 void DiskCacheTestWithCache::InitDefaultCacheViaCreator() { |
| 267 if (!cache_thread_.IsRunning()) { |
| 268 ASSERT_TRUE(cache_thread_.StartWithOptions( |
| 269 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
| 270 } |
| 271 ASSERT_TRUE(cache_thread_.message_loop() != NULL); |
| 270 | 272 |
| 271 net::TestCompletionCallback cb; | 273 net::TestCompletionCallback cb; |
| 272 int rv = disk_cache::BackendImpl::CreateBackend( | 274 disk_cache::CacheCreator* creator = new disk_cache::CacheCreator( |
| 273 cache_path_, force_creation_, size_, type_, | 275 cache_path_, true, 0, net::DISK_CACHE, disk_cache::kNoRandom, |
| 274 disk_cache::kNoRandom, thread, NULL, &cache_, cb.callback()); | 276 cache_thread_.message_loop_proxy(), NULL, &cache_, cb.callback()); |
| 277 int rv = creator->Run(); |
| 275 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 278 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 276 } | 279 } |
| 277 | 280 |
| 278 void DiskCacheTestWithCache::InitDiskCacheImpl() { | 281 void DiskCacheTestWithCache::CreateBackend(uint32 flags, base::Thread* thread) { |
| 279 scoped_refptr<base::MessageLoopProxy> thread = | 282 base::MessageLoopProxy* runner; |
| 280 use_current_thread_ ? base::MessageLoopProxy::current() : | 283 if (use_current_thread_) |
| 281 cache_thread_.message_loop_proxy(); | 284 runner = base::MessageLoopProxy::current(); |
| 285 else |
| 286 runner = thread->message_loop_proxy(); |
| 287 |
| 288 if (simple_cache_mode_) { |
| 289 net::TestCompletionCallback cb; |
| 290 disk_cache::Backend* simple_backend; |
| 291 // TODO(pasko): split Simple Backend construction from initialization. |
| 292 int rv = disk_cache::SimpleBackendImpl::CreateBackend(cache_path_, size_, |
| 293 type_, disk_cache::kNone, make_scoped_refptr(runner), NULL, |
| 294 &simple_backend, cb.callback()); |
| 295 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 296 cache_ = simple_backend; |
| 297 return; |
| 298 } |
| 299 |
| 282 if (mask_) | 300 if (mask_) |
| 283 cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, thread, NULL); | 301 cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, runner, NULL); |
| 284 else | 302 else |
| 285 cache_impl_ = new disk_cache::BackendImpl(cache_path_, thread, NULL); | 303 cache_impl_ = new disk_cache::BackendImpl(cache_path_, runner, NULL); |
| 286 | |
| 287 cache_ = cache_impl_; | 304 cache_ = cache_impl_; |
| 288 ASSERT_TRUE(NULL != cache_); | 305 ASSERT_TRUE(NULL != cache_); |
| 289 | |
| 290 if (size_) | 306 if (size_) |
| 291 EXPECT_TRUE(cache_impl_->SetMaxSize(size_)); | 307 EXPECT_TRUE(cache_impl_->SetMaxSize(size_)); |
| 292 | |
| 293 if (new_eviction_) | 308 if (new_eviction_) |
| 294 cache_impl_->SetNewEviction(); | 309 cache_impl_->SetNewEviction(); |
| 295 | |
| 296 cache_impl_->SetType(type_); | 310 cache_impl_->SetType(type_); |
| 297 cache_impl_->SetFlags(disk_cache::kNoRandom); | 311 cache_impl_->SetFlags(flags); |
| 298 net::TestCompletionCallback cb; | 312 net::TestCompletionCallback cb; |
| 299 int rv = cache_impl_->Init(cb.callback()); | 313 int rv = cache_impl_->Init(cb.callback()); |
| 300 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 314 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 315 cache_ = cache_impl_; |
| 301 } | 316 } |
| OLD | NEW |