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/command_line.h" |
7 #include "base/file_util.h" | 8 #include "base/file_util.h" |
8 #include "base/path_service.h" | 9 #include "base/path_service.h" |
9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
10 #include "base/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
11 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
12 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
13 #include "net/base/test_completion_callback.h" | 14 #include "net/base/test_completion_callback.h" |
14 #include "net/disk_cache/backend_impl.h" | 15 #include "net/disk_cache/backend_impl.h" |
15 #include "net/disk_cache/cache_util.h" | 16 #include "net/disk_cache/cache_util.h" |
16 #include "net/disk_cache/disk_cache.h" | 17 #include "net/disk_cache/disk_cache.h" |
17 #include "net/disk_cache/disk_cache_test_util.h" | 18 #include "net/disk_cache/disk_cache_test_util.h" |
18 #include "net/disk_cache/mem_backend_impl.h" | 19 #include "net/disk_cache/mem_backend_impl.h" |
19 #include "net/disk_cache/simple/simple_backend_impl.h" | 20 #include "net/disk_cache/simple/simple_backend_impl.h" |
20 #include "net/disk_cache/simple/simple_index.h" | 21 #include "net/disk_cache/simple/simple_index.h" |
| 22 #include "net/disk_cache/v3/backend_impl_v3.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 base::FilePath GetCachePath() { |
| 27 base::FilePath path; |
| 28 PathService::Get(base::DIR_TEMP, &path); // Ignore return value; |
| 29 path = path.AppendASCII("cache_test"); |
| 30 if (!base::PathExists(path)) |
| 31 file_util::CreateDirectory(path); |
| 32 |
| 33 return path; |
| 34 } |
| 35 |
| 36 } // namespace |
21 | 37 |
22 DiskCacheTest::DiskCacheTest() { | 38 DiskCacheTest::DiskCacheTest() { |
23 CHECK(temp_dir_.CreateUniqueTempDir()); | 39 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
24 cache_path_ = temp_dir_.path(); | 40 //if (command_line->HasSwitch("fixed-cache-dir")) { |
| 41 if (true) { |
| 42 cache_path_ = GetCachePath(); |
| 43 } else { |
| 44 CHECK(temp_dir_.CreateUniqueTempDir()); |
| 45 cache_path_ = temp_dir_.path(); |
| 46 } |
| 47 |
25 if (!base::MessageLoop::current()) | 48 if (!base::MessageLoop::current()) |
26 message_loop_.reset(new base::MessageLoopForIO()); | 49 message_loop_.reset(new base::MessageLoopForIO()); |
27 } | 50 } |
28 | 51 |
29 DiskCacheTest::~DiskCacheTest() { | 52 DiskCacheTest::~DiskCacheTest() { |
30 } | 53 } |
31 | 54 |
32 bool DiskCacheTest::CopyTestCache(const std::string& name) { | 55 bool DiskCacheTest::CopyTestCache(const std::string& name) { |
33 base::FilePath path; | 56 base::FilePath path; |
34 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 57 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
35 path = path.AppendASCII("net"); | 58 path = path.AppendASCII("net"); |
36 path = path.AppendASCII("data"); | 59 path = path.AppendASCII("data"); |
37 path = path.AppendASCII("cache_tests"); | 60 path = path.AppendASCII("cache_tests"); |
38 path = path.AppendASCII(name); | 61 path = path.AppendASCII(name); |
39 | 62 |
40 if (!CleanupCacheDir()) | 63 if (!CleanupCacheDir()) |
41 return false; | 64 return false; |
42 return base::CopyDirectory(path, cache_path_, false); | 65 return base::CopyDirectory(path, cache_path_, false); |
43 } | 66 } |
44 | 67 |
45 bool DiskCacheTest::CleanupCacheDir() { | 68 bool DiskCacheTest::CleanupCacheDir() { |
46 return DeleteCache(cache_path_); | 69 return DeleteCache(cache_path_); |
47 } | 70 } |
48 | 71 |
| 72 bool DiskCacheTest::ForceRandomCacheDir() { |
| 73 if (temp_dir_.path().empty()) { |
| 74 if (!temp_dir_.CreateUniqueTempDir()) |
| 75 return false; |
| 76 cache_path_ = temp_dir_.path(); |
| 77 } |
| 78 return true; |
| 79 } |
| 80 |
49 void DiskCacheTest::TearDown() { | 81 void DiskCacheTest::TearDown() { |
50 base::RunLoop().RunUntilIdle(); | 82 base::RunLoop().RunUntilIdle(); |
51 } | 83 } |
52 | 84 |
53 DiskCacheTestWithCache::DiskCacheTestWithCache() | 85 DiskCacheTestWithCache::DiskCacheTestWithCache() |
54 : cache_impl_(NULL), | 86 : cache_impl_(NULL), |
55 simple_cache_impl_(NULL), | 87 simple_cache_impl_(NULL), |
56 mem_cache_(NULL), | 88 mem_cache_(NULL), |
| 89 cache_impl_v3_(NULL), |
57 mask_(0), | 90 mask_(0), |
58 size_(0), | 91 size_(0), |
59 type_(net::DISK_CACHE), | 92 type_(net::DISK_CACHE), |
60 memory_only_(false), | 93 memory_only_(false), |
61 simple_cache_mode_(false), | 94 simple_cache_mode_(false), |
62 simple_cache_wait_for_index_(true), | 95 simple_cache_wait_for_index_(true), |
63 force_creation_(false), | 96 force_creation_(false), |
64 new_eviction_(false), | 97 new_eviction_(false), |
| 98 v3_(false), |
65 first_cleanup_(true), | 99 first_cleanup_(true), |
66 integrity_(true), | 100 integrity_(true), |
67 use_current_thread_(false), | 101 use_current_thread_(false), |
| 102 avoid_test_flag_(false), |
| 103 unit_test_mode_(false), |
68 cache_thread_("CacheThread") { | 104 cache_thread_("CacheThread") { |
69 } | 105 } |
70 | 106 |
71 DiskCacheTestWithCache::~DiskCacheTestWithCache() {} | 107 DiskCacheTestWithCache::~DiskCacheTestWithCache() {} |
72 | 108 |
73 void DiskCacheTestWithCache::InitCache() { | 109 void DiskCacheTestWithCache::InitCache() { |
74 if (memory_only_) | 110 if (memory_only_) |
75 InitMemoryCache(); | 111 InitMemoryCache(); |
76 else | 112 else |
77 InitDiskCache(); | 113 InitDiskCache(); |
78 | 114 |
79 ASSERT_TRUE(NULL != cache_); | 115 ASSERT_TRUE(NULL != cache_); |
80 if (first_cleanup_) | 116 if (first_cleanup_) |
81 ASSERT_EQ(0, cache_->GetEntryCount()); | 117 ASSERT_EQ(0, cache_->GetEntryCount()); |
82 } | 118 } |
83 | 119 |
84 // We are expected to leak memory when simulating crashes. | 120 // We are expected to leak memory when simulating crashes. |
85 void DiskCacheTestWithCache::SimulateCrash() { | 121 void DiskCacheTestWithCache::SimulateCrash() { |
86 ASSERT_TRUE(!memory_only_); | 122 ASSERT_TRUE(!memory_only_); |
87 net::TestCompletionCallback cb; | 123 if (cache_impl_) { |
88 int rv = cache_impl_->FlushQueueForTest(cb.callback()); | 124 net::TestCompletionCallback cb; |
89 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 125 int rv = cache_impl_->FlushQueueForTest(cb.callback()); |
90 cache_impl_->ClearRefCountForTest(); | 126 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 127 cache_impl_->ClearRefCountForTest(); |
| 128 cache_.reset(); |
| 129 cache_impl_ = NULL; |
| 130 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); |
| 131 } else if (cache_impl_v3_) { |
| 132 cache_impl_v3_->SetFlags(disk_cache::BackendImplV3::NO_CLEAN_ON_EXIT); |
| 133 cache_.reset(); |
| 134 cache_impl_v3_ = NULL; |
| 135 } else { |
| 136 ASSERT_TRUE(0); |
| 137 } |
91 | 138 |
92 cache_.reset(); | 139 CreateBackend(&cache_thread_); |
93 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); | |
94 | |
95 CreateBackend(disk_cache::kNoRandom, &cache_thread_); | |
96 } | 140 } |
97 | 141 |
98 void DiskCacheTestWithCache::SetTestMode() { | 142 void DiskCacheTestWithCache::SetTestMode() { |
99 ASSERT_TRUE(!memory_only_); | 143 ASSERT_TRUE(!memory_only_); |
100 cache_impl_->SetUnitTestMode(); | 144 EXPECT_TRUE(cache_); |
| 145 if (cache_impl_) |
| 146 cache_impl_->SetUnitTestMode(); |
| 147 else if (cache_impl_v3_) |
| 148 cache_impl_v3_->SetUnitTestMode(); |
101 } | 149 } |
102 | 150 |
103 void DiskCacheTestWithCache::SetMaxSize(int size) { | 151 void DiskCacheTestWithCache::SetMaxSize(int size) { |
104 size_ = size; | 152 size_ = size; |
105 if (simple_cache_impl_) | 153 if (simple_cache_impl_) |
106 EXPECT_TRUE(simple_cache_impl_->SetMaxSize(size)); | 154 EXPECT_TRUE(simple_cache_impl_->SetMaxSize(size)); |
107 | 155 |
108 if (cache_impl_) | 156 if (cache_impl_) |
109 EXPECT_TRUE(cache_impl_->SetMaxSize(size)); | 157 EXPECT_TRUE(cache_impl_->SetMaxSize(size)); |
110 | 158 |
111 if (mem_cache_) | 159 if (mem_cache_) |
112 EXPECT_TRUE(mem_cache_->SetMaxSize(size)); | 160 EXPECT_TRUE(mem_cache_->SetMaxSize(size)); |
| 161 |
| 162 if (cache_impl_v3_) |
| 163 EXPECT_TRUE(cache_impl_v3_->SetMaxSize(size)); |
113 } | 164 } |
114 | 165 |
115 int DiskCacheTestWithCache::OpenEntry(const std::string& key, | 166 int DiskCacheTestWithCache::OpenEntry(const std::string& key, |
116 disk_cache::Entry** entry) { | 167 disk_cache::Entry** entry) { |
117 net::TestCompletionCallback cb; | 168 net::TestCompletionCallback cb; |
118 int rv = cache_->OpenEntry(key, entry, cb.callback()); | 169 int rv = cache_->OpenEntry(key, entry, cb.callback()); |
119 return cb.GetResult(rv); | 170 return cb.GetResult(rv); |
120 } | 171 } |
121 | 172 |
122 int DiskCacheTestWithCache::CreateEntry(const std::string& key, | 173 int DiskCacheTestWithCache::CreateEntry(const std::string& key, |
(...skipping 29 matching lines...) Expand all Loading... |
152 } | 203 } |
153 | 204 |
154 int DiskCacheTestWithCache::OpenNextEntry(void** iter, | 205 int DiskCacheTestWithCache::OpenNextEntry(void** iter, |
155 disk_cache::Entry** next_entry) { | 206 disk_cache::Entry** next_entry) { |
156 net::TestCompletionCallback cb; | 207 net::TestCompletionCallback cb; |
157 int rv = cache_->OpenNextEntry(iter, next_entry, cb.callback()); | 208 int rv = cache_->OpenNextEntry(iter, next_entry, cb.callback()); |
158 return cb.GetResult(rv); | 209 return cb.GetResult(rv); |
159 } | 210 } |
160 | 211 |
161 void DiskCacheTestWithCache::FlushQueueForTest() { | 212 void DiskCacheTestWithCache::FlushQueueForTest() { |
162 if (memory_only_ || !cache_impl_) | 213 if (memory_only_ || !(cache_impl_ || cache_impl_v3_)) |
163 return; | 214 return; |
164 | 215 |
165 net::TestCompletionCallback cb; | 216 net::TestCompletionCallback cb; |
166 int rv = cache_impl_->FlushQueueForTest(cb.callback()); | 217 int rv; |
| 218 if (cache_impl_) |
| 219 rv = cache_impl_->FlushQueueForTest(cb.callback()); |
| 220 else |
| 221 rv = cache_impl_v3_->FlushQueueForTest(cb.callback()); |
| 222 EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| 223 } |
| 224 |
| 225 void DiskCacheTestWithCache::CleanupForTest() { |
| 226 if (!cache_impl_v3_) |
| 227 return base::RunLoop().RunUntilIdle(); |
| 228 |
| 229 net::TestCompletionCallback cb; |
| 230 int rv = cache_impl_v3_->CleanupForTest(cb.callback()); |
167 EXPECT_EQ(net::OK, cb.GetResult(rv)); | 231 EXPECT_EQ(net::OK, cb.GetResult(rv)); |
168 } | 232 } |
169 | 233 |
170 void DiskCacheTestWithCache::RunTaskForTest(const base::Closure& closure) { | 234 void DiskCacheTestWithCache::RunTaskForTest(const base::Closure& closure) { |
171 if (memory_only_ || !cache_impl_) { | 235 if (memory_only_ || !cache_impl_) { |
172 closure.Run(); | 236 closure.Run(); |
173 return; | 237 return; |
174 } | 238 } |
175 | 239 |
176 net::TestCompletionCallback cb; | 240 net::TestCompletionCallback cb; |
(...skipping 25 matching lines...) Expand all Loading... |
202 } | 266 } |
203 | 267 |
204 int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry, | 268 int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry, |
205 int64 offset, | 269 int64 offset, |
206 net::IOBuffer* buf, int len) { | 270 net::IOBuffer* buf, int len) { |
207 net::TestCompletionCallback cb; | 271 net::TestCompletionCallback cb; |
208 int rv = entry->WriteSparseData(offset, buf, len, cb.callback()); | 272 int rv = entry->WriteSparseData(offset, buf, len, cb.callback()); |
209 return cb.GetResult(rv); | 273 return cb.GetResult(rv); |
210 } | 274 } |
211 | 275 |
| 276 int DiskCacheTestWithCache::GetAvailableRange(disk_cache::Entry* entry, |
| 277 int64 offset, int len, |
| 278 int64* start) { |
| 279 net::TestCompletionCallback cb; |
| 280 int rv = entry->GetAvailableRange(offset, len, start, cb.callback()); |
| 281 return cb.GetResult(rv); |
| 282 } |
| 283 |
| 284 bool DiskCacheTestWithCache::IsAllocAllowed(int current_size, int new_size) { |
| 285 if (cache_impl_) |
| 286 return cache_impl_->IsAllocAllowed(current_size, new_size); |
| 287 if (cache_impl_v3_) |
| 288 return cache_impl_v3_->IsAllocAllowed(current_size, new_size, false); |
| 289 return false; |
| 290 } |
| 291 |
| 292 void DiskCacheTestWithCache::BufferDeleted(int size) { |
| 293 if (cache_impl_) |
| 294 cache_impl_->BufferDeleted(size); |
| 295 else if (cache_impl_v3_) |
| 296 cache_impl_v3_->BufferDeleted(size); |
| 297 } |
| 298 |
| 299 int DiskCacheTestWithCache::GetTotalBuffersSize() { |
| 300 if (cache_impl_) |
| 301 return cache_impl_->GetTotalBuffersSize(); |
| 302 if (cache_impl_v3_) |
| 303 return cache_impl_v3_->GetTotalBuffersSize(); |
| 304 return 0; |
| 305 } |
| 306 |
| 307 void DiskCacheTestWithCache::SetNoBuffering() { |
| 308 EXPECT_TRUE(cache_); |
| 309 if (cache_impl_) |
| 310 cache_impl_->SetFlags(disk_cache::kNoBuffering); |
| 311 else if (cache_impl_v3_) |
| 312 cache_impl_v3_->SetFlags(disk_cache::BackendImplV3::NO_BUFFERING); |
| 313 } |
| 314 |
| 315 void DiskCacheTestWithCache::WaitForEntryToClose(const std::string& key) { |
| 316 FlushQueueForTest(); |
| 317 if (!cache_impl_v3_) |
| 318 return; |
| 319 |
| 320 net::TestCompletionCallback cb; |
| 321 cache_impl_v3_->AddDelayForTest(30); |
| 322 int rv = cache_impl_v3_->WaitForEntryToCloseForTest(key, cb.callback()); |
| 323 EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| 324 } |
| 325 |
212 void DiskCacheTestWithCache::TrimForTest(bool empty) { | 326 void DiskCacheTestWithCache::TrimForTest(bool empty) { |
213 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimForTest, | 327 if (cache_impl_) { |
214 base::Unretained(cache_impl_), | 328 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimForTest, |
215 empty)); | 329 base::Unretained(cache_impl_), empty)); |
| 330 } else { |
| 331 cache_impl_v3_->TrimForTest(empty); |
| 332 } |
216 } | 333 } |
217 | 334 |
218 void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) { | 335 void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) { |
219 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimDeletedListForTest, | 336 if (cache_impl_) { |
220 base::Unretained(cache_impl_), | 337 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimDeletedListForTest, |
221 empty)); | 338 base::Unretained(cache_impl_), empty)); |
| 339 } else { |
| 340 cache_impl_v3_->TrimDeletedListForTest(empty); |
| 341 } |
| 342 } |
| 343 |
| 344 base::Time DiskCacheTestWithCache::GetCurrentTime() { |
| 345 if (cache_impl_v3_) { |
| 346 return cache_impl_v3_->GetCurrentTime(); |
| 347 } else { |
| 348 return base::Time::Now(); |
| 349 } |
| 350 } |
| 351 |
| 352 void DiskCacheTestWithCache::AddDelayForTest(int seconds) { |
| 353 if (cache_impl_v3_) { |
| 354 cache_impl_v3_->AddDelayForTest(seconds); |
| 355 } else { |
| 356 AddDelay(); |
| 357 } |
222 } | 358 } |
223 | 359 |
224 void DiskCacheTestWithCache::AddDelay() { | 360 void DiskCacheTestWithCache::AddDelay() { |
225 if (simple_cache_mode_) { | 361 if (simple_cache_mode_) { |
226 // The simple cache uses second resolution for many timeouts, so it's safest | 362 // The simple cache uses second resolution for many timeouts, so it's safest |
227 // to advance by at least whole seconds before falling back into the normal | 363 // to advance by at least whole seconds before falling back into the normal |
228 // disk cache epsilon advance. | 364 // disk cache epsilon advance. |
229 const base::Time initial_time = base::Time::Now(); | 365 const base::Time initial_time = base::Time::Now(); |
230 do { | 366 do { |
231 base::PlatformThread::YieldCurrentThread(); | 367 base::PlatformThread::YieldCurrentThread(); |
232 } while (base::Time::Now() - | 368 } while (base::Time::Now() - |
233 initial_time < base::TimeDelta::FromSeconds(1)); | 369 initial_time < base::TimeDelta::FromSeconds(1)); |
234 } | 370 } |
235 | 371 |
236 base::Time initial = base::Time::Now(); | 372 base::Time initial = base::Time::Now(); |
237 while (base::Time::Now() <= initial) { | 373 while (base::Time::Now() <= initial) { |
238 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); | 374 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); |
239 }; | 375 }; |
240 } | 376 } |
241 | 377 |
242 void DiskCacheTestWithCache::TearDown() { | 378 void DiskCacheTestWithCache::TearDown() { |
243 base::RunLoop().RunUntilIdle(); | 379 CleanupForTest(); |
244 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); | 380 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); |
245 base::RunLoop().RunUntilIdle(); | 381 base::RunLoop().RunUntilIdle(); |
246 cache_.reset(); | 382 cache_.reset(); |
247 if (cache_thread_.IsRunning()) | 383 if (cache_thread_.IsRunning()) |
248 cache_thread_.Stop(); | 384 cache_thread_.Stop(); |
249 | 385 |
| 386 base::RunLoop().RunUntilIdle(); |
| 387 |
250 if (!memory_only_ && !simple_cache_mode_ && integrity_) { | 388 if (!memory_only_ && !simple_cache_mode_ && integrity_) { |
251 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); | 389 if (!v3_) |
| 390 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); |
252 } | 391 } |
253 base::RunLoop().RunUntilIdle(); | 392 base::RunLoop().RunUntilIdle(); |
254 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); | 393 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); |
255 DiskCacheTest::TearDown(); | 394 DiskCacheTest::TearDown(); |
256 } | 395 } |
257 | 396 |
258 void DiskCacheTestWithCache::InitMemoryCache() { | 397 void DiskCacheTestWithCache::InitMemoryCache() { |
259 mem_cache_ = new disk_cache::MemBackendImpl(NULL); | 398 mem_cache_ = new disk_cache::MemBackendImpl(NULL); |
260 cache_.reset(mem_cache_); | 399 cache_.reset(mem_cache_); |
261 ASSERT_TRUE(cache_); | 400 ASSERT_TRUE(cache_); |
262 | 401 |
263 if (size_) | 402 if (size_) |
264 EXPECT_TRUE(mem_cache_->SetMaxSize(size_)); | 403 EXPECT_TRUE(mem_cache_->SetMaxSize(size_)); |
265 | 404 |
266 ASSERT_TRUE(mem_cache_->Init()); | 405 ASSERT_TRUE(mem_cache_->Init()); |
267 } | 406 } |
268 | 407 |
269 void DiskCacheTestWithCache::InitDiskCache() { | 408 void DiskCacheTestWithCache::InitDiskCache() { |
270 if (first_cleanup_) | 409 if (first_cleanup_) |
271 ASSERT_TRUE(CleanupCacheDir()); | 410 ASSERT_TRUE(CleanupCacheDir()); |
272 | 411 |
273 if (!cache_thread_.IsRunning()) { | 412 if (!cache_thread_.IsRunning()) { |
274 ASSERT_TRUE(cache_thread_.StartWithOptions( | 413 ASSERT_TRUE(cache_thread_.StartWithOptions( |
275 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 414 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
276 } | 415 } |
277 ASSERT_TRUE(cache_thread_.message_loop() != NULL); | 416 ASSERT_TRUE(cache_thread_.message_loop() != NULL); |
278 | 417 |
279 CreateBackend(disk_cache::kNoRandom, &cache_thread_); | 418 CreateBackend(&cache_thread_); |
280 } | 419 } |
281 | 420 |
282 void DiskCacheTestWithCache::CreateBackend(uint32 flags, base::Thread* thread) { | 421 void DiskCacheTestWithCache::CreateBackend(base::Thread* thread) { |
283 base::MessageLoopProxy* runner; | 422 base::MessageLoopProxy* runner; |
284 if (use_current_thread_) | 423 if (use_current_thread_) |
285 runner = base::MessageLoopProxy::current().get(); | 424 runner = base::MessageLoopProxy::current(); |
286 else | 425 else |
287 runner = thread->message_loop_proxy().get(); | 426 runner = thread->message_loop_proxy(); |
288 | 427 |
289 if (simple_cache_mode_) { | 428 if (simple_cache_mode_) { |
290 net::TestCompletionCallback cb; | 429 net::TestCompletionCallback cb; |
291 scoped_ptr<disk_cache::SimpleBackendImpl> simple_backend( | 430 scoped_ptr<disk_cache::SimpleBackendImpl> simple_backend( |
292 new disk_cache::SimpleBackendImpl( | 431 new disk_cache::SimpleBackendImpl(cache_path_, size_, type_, |
293 cache_path_, size_, type_, make_scoped_refptr(runner).get(), NULL)); | 432 make_scoped_refptr(runner).get(), |
| 433 NULL)); |
294 int rv = simple_backend->Init(cb.callback()); | 434 int rv = simple_backend->Init(cb.callback()); |
295 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 435 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
296 simple_cache_impl_ = simple_backend.get(); | 436 simple_cache_impl_ = simple_backend.get(); |
297 cache_ = simple_backend.PassAs<disk_cache::Backend>(); | 437 cache_ = simple_backend.PassAs<disk_cache::Backend>(); |
298 if (simple_cache_wait_for_index_) { | 438 if (simple_cache_wait_for_index_) { |
299 net::TestCompletionCallback wait_for_index_cb; | 439 net::TestCompletionCallback wait_for_index_cb; |
300 rv = simple_cache_impl_->index()->ExecuteWhenReady( | 440 rv = simple_cache_impl_->index()->ExecuteWhenReady( |
301 wait_for_index_cb.callback()); | 441 wait_for_index_cb.callback()); |
302 ASSERT_EQ(net::OK, wait_for_index_cb.GetResult(rv)); | 442 ASSERT_EQ(net::OK, wait_for_index_cb.GetResult(rv)); |
303 } | 443 } |
304 return; | 444 return; |
305 } | 445 } |
306 | 446 |
307 if (mask_) | 447 if (mask_) |
308 cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, runner, NULL); | 448 cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, runner, NULL); |
| 449 else if (v3_) |
| 450 cache_impl_v3_ = new disk_cache::BackendImplV3(cache_path_, runner, NULL); |
309 else | 451 else |
310 cache_impl_ = new disk_cache::BackendImpl(cache_path_, runner, NULL); | 452 cache_impl_ = new disk_cache::BackendImpl(cache_path_, runner, NULL); |
311 cache_.reset(cache_impl_); | 453 cache_.reset(cache_impl_); |
| 454 if (!cache_) |
| 455 cache_.reset(cache_impl_v3_); |
312 ASSERT_TRUE(cache_); | 456 ASSERT_TRUE(cache_); |
313 if (size_) | 457 |
314 EXPECT_TRUE(cache_impl_->SetMaxSize(size_)); | |
315 if (new_eviction_) | |
316 cache_impl_->SetNewEviction(); | |
317 cache_impl_->SetType(type_); | |
318 cache_impl_->SetFlags(flags); | |
319 net::TestCompletionCallback cb; | 458 net::TestCompletionCallback cb; |
320 int rv = cache_impl_->Init(cb.callback()); | 459 int rv = 0; |
| 460 if (cache_impl_) { |
| 461 if (size_) |
| 462 EXPECT_TRUE(cache_impl_->SetMaxSize(size_)); |
| 463 if (new_eviction_) |
| 464 cache_impl_->SetNewEviction(); |
| 465 |
| 466 cache_impl_->SetType(type_); |
| 467 if (unit_test_mode_) |
| 468 cache_impl_->SetFlags(disk_cache::kUnitTestMode); |
| 469 if (!avoid_test_flag_) |
| 470 cache_impl_->SetFlags(disk_cache::kNoRandom); |
| 471 rv = cache_impl_->Init(cb.callback()); |
| 472 } else { |
| 473 if (size_) |
| 474 EXPECT_TRUE(cache_impl_v3_->SetMaxSize(size_)); |
| 475 if (new_eviction_) |
| 476 cache_impl_v3_->SetNewEviction(); |
| 477 |
| 478 cache_impl_v3_->SetType(type_); |
| 479 cache_impl_v3_->SetFlags(disk_cache::BackendImplV3::BASIC_UNIT_TEST); |
| 480 if (unit_test_mode_) |
| 481 cache_impl_v3_->SetFlags(disk_cache::BackendImplV3::UNIT_TEST_MODE); |
| 482 rv = cache_impl_v3_->Init(cb.callback()); |
| 483 } |
321 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 484 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
322 } | 485 } |
OLD | NEW |