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