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 |
69 disk_cache::Backend* DiskCacheTestWithCache::CreateBackendAsModeSuggests( | |
rvargas (doing something else)
2013/03/25 22:06:04
looks like the header is not part of the CL... wit
pasko-google - do not use
2013/03/26 16:54:57
argh, I forgot that I need to add files manually w
| |
70 uint32 flags, base::Thread* thread) { | |
71 base::MessageLoopProxy* runner; | |
72 if (use_current_thread_ || !thread) | |
rvargas (doing something else)
2013/03/25 22:06:04
!thread looks like a critical error that should no
pasko-google - do not use
2013/03/26 16:54:57
BackendShutdownWithPendingFileIO was using this co
| |
73 runner = base::MessageLoopProxy::current(); | |
74 else | |
75 runner = thread->message_loop_proxy(); | |
76 | |
77 if (simple_cache_mode_ && type_ == net::DISK_CACHE) { | |
rvargas (doing something else)
2013/03/25 22:06:04
The type should be under control of the specific t
pasko-google - do not use
2013/03/26 16:54:57
Done.
| |
78 net::TestCompletionCallback cb; | |
79 disk_cache::Backend* simple_backend; | |
80 int rv = disk_cache::SimpleBackendImpl::CreateBackend(cache_path_, size_, | |
rvargas (doing something else)
2013/03/25 22:06:04
Why a static factory method instead of following t
pasko-google - do not use
2013/03/26 16:54:57
There is no real reason, I just avoided changing w
| |
81 type_, disk_cache::kNone, make_scoped_refptr(runner), NULL, | |
rvargas (doing something else)
2013/03/25 22:06:04
I think we should assume for the time being that f
pasko-google - do not use
2013/03/26 16:54:57
Yes. I am seeing this as .. for testing reasons we
| |
82 &simple_backend, cb.callback()); | |
83 EXPECT_EQ(net::OK, cb.GetResult(rv)); | |
84 return simple_backend; | |
85 } | |
86 | |
87 if (mask_) | |
88 cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, runner, NULL); | |
89 else | |
90 cache_impl_ = new disk_cache::BackendImpl(cache_path_, runner, NULL); | |
91 cache_impl_->SetFlags(flags); | |
92 cache_impl_->SetMaxSize(size_); | |
93 cache_impl_->SetType(type_); | |
94 if (new_eviction_) | |
95 cache_impl_->SetNewEviction(); | |
96 net::TestCompletionCallback cb; | |
97 int rv = cache_impl_->Init(cb.callback()); | |
98 EXPECT_EQ(net::OK, cb.GetResult(rv)); | |
99 return cache_impl_; | |
100 } | |
101 | |
66 void DiskCacheTestWithCache::InitCache() { | 102 void DiskCacheTestWithCache::InitCache() { |
67 if (mask_ || new_eviction_) | |
68 implementation_ = true; | |
69 | |
70 if (memory_only_) | 103 if (memory_only_) |
71 InitMemoryCache(); | 104 InitMemoryCache(); |
72 else | 105 else |
73 InitDiskCache(); | 106 InitDiskCache(); |
74 | 107 |
75 ASSERT_TRUE(NULL != cache_); | 108 ASSERT_TRUE(NULL != cache_); |
76 if (first_cleanup_) | 109 if (first_cleanup_) |
77 ASSERT_EQ(0, cache_->GetEntryCount()); | 110 ASSERT_EQ(0, cache_->GetEntryCount()); |
78 } | 111 } |
79 | 112 |
80 // We are expected to leak memory when simulating crashes. | 113 // We are expected to leak memory when simulating crashes. |
81 void DiskCacheTestWithCache::SimulateCrash() { | 114 void DiskCacheTestWithCache::SimulateCrash() { |
82 ASSERT_TRUE(implementation_ && !memory_only_); | 115 ASSERT_TRUE(!memory_only_); |
83 net::TestCompletionCallback cb; | 116 net::TestCompletionCallback cb; |
84 int rv = cache_impl_->FlushQueueForTest(cb.callback()); | 117 int rv = cache_impl_->FlushQueueForTest(cb.callback()); |
85 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 118 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
86 cache_impl_->ClearRefCountForTest(); | 119 cache_impl_->ClearRefCountForTest(); |
87 | 120 |
88 delete cache_impl_; | 121 delete cache_impl_; |
89 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); | 122 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); |
90 | 123 |
91 InitDiskCacheImpl(); | 124 cache_ = CreateBackendAsModeSuggests(disk_cache::kNoRandom, &cache_thread_); |
rvargas (doing something else)
2013/03/25 22:06:04
Why exposing the flags if all callers pass the sam
pasko-google - do not use
2013/03/26 16:54:57
In BackendShutdownWithPending* we sometimes add kN
| |
92 } | 125 } |
93 | 126 |
94 void DiskCacheTestWithCache::SetTestMode() { | 127 void DiskCacheTestWithCache::SetTestMode() { |
95 ASSERT_TRUE(implementation_ && !memory_only_); | 128 ASSERT_TRUE(!memory_only_); |
96 cache_impl_->SetUnitTestMode(); | 129 cache_impl_->SetUnitTestMode(); |
97 } | 130 } |
98 | 131 |
99 void DiskCacheTestWithCache::SetMaxSize(int size) { | 132 void DiskCacheTestWithCache::SetMaxSize(int size) { |
100 size_ = size; | 133 size_ = size; |
101 if (cache_impl_) | 134 if (cache_impl_) |
102 EXPECT_TRUE(cache_impl_->SetMaxSize(size)); | 135 EXPECT_TRUE(cache_impl_->SetMaxSize(size)); |
103 | 136 |
104 if (mem_cache_) | 137 if (mem_cache_) |
105 EXPECT_TRUE(mem_cache_->SetMaxSize(size)); | 138 EXPECT_TRUE(mem_cache_->SetMaxSize(size)); |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
228 cache_thread_.Stop(); | 261 cache_thread_.Stop(); |
229 | 262 |
230 if (!memory_only_ && integrity_) { | 263 if (!memory_only_ && integrity_) { |
231 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); | 264 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); |
232 } | 265 } |
233 | 266 |
234 PlatformTest::TearDown(); | 267 PlatformTest::TearDown(); |
235 } | 268 } |
236 | 269 |
237 void DiskCacheTestWithCache::InitMemoryCache() { | 270 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); | 271 mem_cache_ = new disk_cache::MemBackendImpl(NULL); |
244 cache_ = mem_cache_; | 272 cache_ = mem_cache_; |
245 ASSERT_TRUE(NULL != cache_); | 273 ASSERT_TRUE(NULL != cache_); |
246 | 274 |
247 if (size_) | 275 if (size_) |
248 EXPECT_TRUE(mem_cache_->SetMaxSize(size_)); | 276 EXPECT_TRUE(mem_cache_->SetMaxSize(size_)); |
249 | 277 |
250 ASSERT_TRUE(mem_cache_->Init()); | 278 ASSERT_TRUE(mem_cache_->Init()); |
251 } | 279 } |
252 | 280 |
253 void DiskCacheTestWithCache::InitDiskCache() { | 281 void DiskCacheTestWithCache::InitDiskCache() { |
254 if (first_cleanup_) | 282 if (first_cleanup_) |
255 ASSERT_TRUE(CleanupCacheDir()); | 283 EXPECT_TRUE(CleanupCacheDir()); |
rvargas (doing something else)
2013/03/25 22:06:04
Why change this to an EXPECT? Seems better to skip
pasko-google - do not use
2013/03/26 16:54:57
I recently discovered that ASSERT_TRUE does only r
rvargas (doing something else)
2013/03/26 18:30:00
Yes, asserting inside this method doesn't prevent
pasko-google - do not use
2013/03/27 20:36:08
I won't be stubborn, reverting back to using ASSER
| |
256 | 284 |
257 if (!cache_thread_.IsRunning()) { | 285 if (!cache_thread_.IsRunning()) { |
258 EXPECT_TRUE(cache_thread_.StartWithOptions( | 286 EXPECT_TRUE(cache_thread_.StartWithOptions( |
259 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 287 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
260 } | 288 } |
261 ASSERT_TRUE(cache_thread_.message_loop() != NULL); | 289 EXPECT_TRUE(cache_thread_.message_loop() != NULL); |
rvargas (doing something else)
2013/03/25 22:06:04
Definitely an assert
pasko-google - do not use
2013/03/26 16:54:57
Same as above, if you want me to revert, I am OK w
| |
262 | 290 |
263 if (implementation_) | 291 cache_ = CreateBackendAsModeSuggests(disk_cache::kNoRandom, &cache_thread_); |
264 return InitDiskCacheImpl(); | 292 } |
265 | 293 |
266 scoped_refptr<base::MessageLoopProxy> thread = | 294 // Testing backend creation retry logic is hard because the CacheCreator and |
267 use_current_thread_ ? base::MessageLoopProxy::current() : | 295 // cache backend(s) are tightly coupled. So we take the default backend often. |
268 cache_thread_.message_loop_proxy(); | 296 // Tests themselves need to be adjusted for platforms where the BackendImpl is |
297 // not the default backend. | |
298 void DiskCacheTestWithCache::InitDefaultCacheViaCreator() { | |
299 if (!cache_thread_.IsRunning()) { | |
300 EXPECT_TRUE(cache_thread_.StartWithOptions( | |
301 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | |
302 } | |
303 EXPECT_TRUE(cache_thread_.message_loop() != NULL); | |
269 | 304 |
270 net::TestCompletionCallback cb; | 305 net::TestCompletionCallback cb; |
271 int rv = disk_cache::BackendImpl::CreateBackend( | 306 disk_cache::CacheCreator* creator = new disk_cache::CacheCreator(cache_path_, |
rvargas (doing something else)
2013/03/25 22:06:04
Call CreateCacheBackend() instead.
pasko-google - do not use
2013/03/26 16:54:57
My main motivation was in the need of disk_cache::
| |
272 cache_path_, force_creation_, size_, type_, | 307 true, 0, net::DISK_CACHE, disk_cache::kNoRandom, |
273 disk_cache::kNoRandom, thread, NULL, &cache_, cb.callback()); | 308 cache_thread_.message_loop_proxy(), NULL, &cache_, cb.callback()); |
274 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 309 int rv = creator->Run(); |
310 EXPECT_EQ(net::OK, cb.GetResult(rv)); | |
275 } | 311 } |
276 | |
277 void DiskCacheTestWithCache::InitDiskCacheImpl() { | |
278 scoped_refptr<base::MessageLoopProxy> thread = | |
279 use_current_thread_ ? base::MessageLoopProxy::current() : | |
280 cache_thread_.message_loop_proxy(); | |
281 if (mask_) | |
282 cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, thread, NULL); | |
283 else | |
284 cache_impl_ = new disk_cache::BackendImpl(cache_path_, thread, NULL); | |
285 | |
286 cache_ = cache_impl_; | |
rvargas (doing something else)
2013/03/25 22:06:04
missing this line
pasko-google - do not use
2013/03/26 16:54:57
Good catch! Done.
| |
287 ASSERT_TRUE(NULL != cache_); | |
288 | |
289 if (size_) | |
290 EXPECT_TRUE(cache_impl_->SetMaxSize(size_)); | |
291 | |
292 if (new_eviction_) | |
293 cache_impl_->SetNewEviction(); | |
294 | |
295 cache_impl_->SetType(type_); | |
296 cache_impl_->SetFlags(disk_cache::kNoRandom); | |
297 net::TestCompletionCallback cb; | |
298 int rv = cache_impl_->Init(cb.callback()); | |
299 ASSERT_EQ(net::OK, cb.GetResult(rv)); | |
300 } | |
OLD | NEW |