Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: net/disk_cache/backend_unittest.cc

Issue 12794003: Initialize the simple cache backend at runtime. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 #include "base/stringprintf.h" 8 #include "base/stringprintf.h"
9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" 9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
10 #include "base/threading/platform_thread.h" 10 #include "base/threading/platform_thread.h"
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 210
211 TEST_F(DiskCacheTest, CreateBackend) { 211 TEST_F(DiskCacheTest, CreateBackend) {
212 net::TestCompletionCallback cb; 212 net::TestCompletionCallback cb;
213 213
214 { 214 {
215 ASSERT_TRUE(CleanupCacheDir()); 215 ASSERT_TRUE(CleanupCacheDir());
216 base::Thread cache_thread("CacheThread"); 216 base::Thread cache_thread("CacheThread");
217 ASSERT_TRUE(cache_thread.StartWithOptions( 217 ASSERT_TRUE(cache_thread.StartWithOptions(
218 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 218 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
219 219
220 // Test the private factory methods. 220 // Test the private factory method(s).
221 disk_cache::Backend* cache = NULL; 221 disk_cache::Backend* cache = NULL;
222 int rv = disk_cache::BackendImpl::CreateBackend(
223 cache_path_, false, 0, net::DISK_CACHE, disk_cache::kNoRandom,
224 cache_thread.message_loop_proxy(), NULL, &cache, cb.callback());
225 ASSERT_EQ(net::OK, cb.GetResult(rv));
226 ASSERT_TRUE(cache);
227 delete cache;
228
229 cache = disk_cache::MemBackendImpl::CreateBackend(0, NULL); 222 cache = disk_cache::MemBackendImpl::CreateBackend(0, NULL);
230 ASSERT_TRUE(cache); 223 ASSERT_TRUE(cache);
231 delete cache; 224 delete cache;
232 cache = NULL; 225 cache = NULL;
233 226
234 // Now test the public API. 227 // Now test the public API.
235 rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, cache_path_, 0, false, 228 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, cache_path_, 0,
236 cache_thread.message_loop_proxy(), 229 false,
237 NULL, &cache, cb.callback()); 230 cache_thread.message_loop_proxy(),
231 NULL, &cache, cb.callback());
238 ASSERT_EQ(net::OK, cb.GetResult(rv)); 232 ASSERT_EQ(net::OK, cb.GetResult(rv));
239 ASSERT_TRUE(cache); 233 ASSERT_TRUE(cache);
240 delete cache; 234 delete cache;
241 cache = NULL; 235 cache = NULL;
242 236
243 rv = disk_cache::CreateCacheBackend(net::MEMORY_CACHE, base::FilePath(), 0, 237 rv = disk_cache::CreateCacheBackend(net::MEMORY_CACHE, base::FilePath(), 0,
244 false, NULL, NULL, &cache, 238 false, NULL, NULL, &cache,
245 cb.callback()); 239 cb.callback());
246 ASSERT_EQ(net::OK, cb.GetResult(rv)); 240 ASSERT_EQ(net::OK, cb.GetResult(rv));
247 ASSERT_TRUE(cache); 241 ASSERT_TRUE(cache);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 { 288 {
295 ASSERT_TRUE(CleanupCacheDir()); 289 ASSERT_TRUE(CleanupCacheDir());
296 base::Thread cache_thread("CacheThread"); 290 base::Thread cache_thread("CacheThread");
297 ASSERT_TRUE(cache_thread.StartWithOptions( 291 ASSERT_TRUE(cache_thread.StartWithOptions(
298 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 292 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
299 293
300 disk_cache::Backend* cache; 294 disk_cache::Backend* cache;
301 uint32 flags = disk_cache::kNoBuffering; 295 uint32 flags = disk_cache::kNoBuffering;
302 if (!fast) 296 if (!fast)
303 flags |= disk_cache::kNoRandom; 297 flags |= disk_cache::kNoRandom;
304 rv = disk_cache::BackendImpl::CreateBackend( 298 rv = disk_cache::CreateCacheBackendWithFlags(
305 cache_path_, false, 0, net::DISK_CACHE, flags, 299 net::DISK_CACHE, cache_path_, 0, false, flags,
306 base::MessageLoopProxy::current(), NULL, 300 base::MessageLoopProxy::current(), NULL, &cache, cb.callback());
307 &cache, cb.callback());
308 ASSERT_EQ(net::OK, cb.GetResult(rv)); 301 ASSERT_EQ(net::OK, cb.GetResult(rv));
309 302
310 disk_cache::EntryImpl* entry; 303 disk_cache::EntryImpl* entry;
311 rv = cache->CreateEntry( 304 rv = cache->CreateEntry(
312 "some key", reinterpret_cast<disk_cache::Entry**>(&entry), 305 "some key", reinterpret_cast<disk_cache::Entry**>(&entry),
313 cb.callback()); 306 cb.callback());
314 ASSERT_EQ(net::OK, cb.GetResult(rv)); 307 ASSERT_EQ(net::OK, cb.GetResult(rv));
315 308
316 const int kSize = 25000; 309 const int kSize = 25000;
317 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 310 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 { 365 {
373 ASSERT_TRUE(CleanupCacheDir()); 366 ASSERT_TRUE(CleanupCacheDir());
374 base::Thread cache_thread("CacheThread"); 367 base::Thread cache_thread("CacheThread");
375 ASSERT_TRUE(cache_thread.StartWithOptions( 368 ASSERT_TRUE(cache_thread.StartWithOptions(
376 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 369 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
377 370
378 disk_cache::Backend* cache; 371 disk_cache::Backend* cache;
379 uint32 flags = disk_cache::kNoBuffering; 372 uint32 flags = disk_cache::kNoBuffering;
380 if (!fast) 373 if (!fast)
381 flags |= disk_cache::kNoRandom; 374 flags |= disk_cache::kNoRandom;
382 int rv = disk_cache::BackendImpl::CreateBackend( 375 int rv = disk_cache::CreateCacheBackendWithFlags(
383 cache_path_, false, 0, net::DISK_CACHE, flags, 376 net::DISK_CACHE, cache_path_, 0, false, flags,
384 cache_thread.message_loop_proxy(), NULL, &cache, cb.callback()); 377 cache_thread.message_loop_proxy(), NULL, &cache, cb.callback());
385 ASSERT_EQ(net::OK, cb.GetResult(rv)); 378 ASSERT_EQ(net::OK, cb.GetResult(rv));
386 379
387 disk_cache::Entry* entry; 380 disk_cache::Entry* entry;
388 rv = cache->CreateEntry("some key", &entry, cb.callback()); 381 rv = cache->CreateEntry("some key", &entry, cb.callback());
389 ASSERT_EQ(net::OK, cb.GetResult(rv)); 382 ASSERT_EQ(net::OK, cb.GetResult(rv));
390 383
391 entry->Close(); 384 entry->Close();
392 385
393 // The cache destructor will see one pending operation here. 386 // The cache destructor will see one pending operation here.
(...skipping 21 matching lines...) Expand all
415 408
416 { 409 {
417 ASSERT_TRUE(CleanupCacheDir()); 410 ASSERT_TRUE(CleanupCacheDir());
418 base::Thread cache_thread("CacheThread"); 411 base::Thread cache_thread("CacheThread");
419 ASSERT_TRUE(cache_thread.StartWithOptions( 412 ASSERT_TRUE(cache_thread.StartWithOptions(
420 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 413 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
421 414
422 disk_cache::Backend* cache; 415 disk_cache::Backend* cache;
423 disk_cache::BackendFlags flags = 416 disk_cache::BackendFlags flags =
424 fast ? disk_cache::kNone : disk_cache::kNoRandom; 417 fast ? disk_cache::kNone : disk_cache::kNoRandom;
425 int rv = disk_cache::BackendImpl::CreateBackend( 418 int rv = disk_cache::CreateCacheBackendWithFlags(
426 cache_path_, false, 0, net::DISK_CACHE, flags, 419 net::DISK_CACHE, cache_path_, 0, false, flags,
427 cache_thread.message_loop_proxy(), NULL, &cache, cb.callback()); 420 cache_thread.message_loop_proxy(), NULL, &cache, cb.callback());
428 ASSERT_EQ(net::OK, cb.GetResult(rv)); 421 ASSERT_EQ(net::OK, cb.GetResult(rv));
429 422
430 disk_cache::Entry* entry; 423 disk_cache::Entry* entry;
431 rv = cache->CreateEntry("some key", &entry, cb.callback()); 424 rv = cache->CreateEntry("some key", &entry, cb.callback());
432 ASSERT_EQ(net::ERR_IO_PENDING, rv); 425 ASSERT_EQ(net::ERR_IO_PENDING, rv);
433 426
434 delete cache; 427 delete cache;
435 EXPECT_FALSE(cb.have_result()); 428 EXPECT_FALSE(cb.have_result());
436 } 429 }
(...skipping 17 matching lines...) Expand all
454 ASSERT_TRUE(CleanupCacheDir()); 447 ASSERT_TRUE(CleanupCacheDir());
455 base::FilePath index = cache_path_.AppendASCII("index"); 448 base::FilePath index = cache_path_.AppendASCII("index");
456 ASSERT_EQ(5, file_util::WriteFile(index, "hello", 5)); 449 ASSERT_EQ(5, file_util::WriteFile(index, "hello", 5));
457 450
458 base::Thread cache_thread("CacheThread"); 451 base::Thread cache_thread("CacheThread");
459 ASSERT_TRUE(cache_thread.StartWithOptions( 452 ASSERT_TRUE(cache_thread.StartWithOptions(
460 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 453 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
461 net::TestCompletionCallback cb; 454 net::TestCompletionCallback cb;
462 455
463 disk_cache::Backend* backend = NULL; 456 disk_cache::Backend* backend = NULL;
464 int rv = disk_cache::BackendImpl::CreateBackend( 457 int rv = disk_cache::CreateCacheBackend(
465 cache_path_, false, 0, net::DISK_CACHE, disk_cache::kNone, 458 net::DISK_CACHE, cache_path_, 0, false,
466 cache_thread.message_loop_proxy(), NULL, &backend, cb.callback()); 459 cache_thread.message_loop_proxy(), NULL, &backend, cb.callback());
467 ASSERT_NE(net::OK, cb.GetResult(rv)); 460 ASSERT_NE(net::OK, cb.GetResult(rv));
468 461
469 ASSERT_TRUE(backend == NULL); 462 ASSERT_TRUE(backend == NULL);
470 delete backend; 463 delete backend;
471 } 464 }
472 465
473 void DiskCacheBackendTest::BackendSetSize() { 466 void DiskCacheBackendTest::BackendSetSize() {
474 SetDirectMode(); 467 SetDirectMode();
475 const int cache_size = 0x10000; // 64 kB 468 const int cache_size = 0x10000; // 64 kB
(...skipping 1072 matching lines...) Expand 10 before | Expand all | Expand 10 after
1548 1541
1549 // Tests dealing with cache files that cannot be recovered. 1542 // Tests dealing with cache files that cannot be recovered.
1550 TEST_F(DiskCacheTest, DeleteOld) { 1543 TEST_F(DiskCacheTest, DeleteOld) {
1551 ASSERT_TRUE(CopyTestCache("wrong_version")); 1544 ASSERT_TRUE(CopyTestCache("wrong_version"));
1552 base::Thread cache_thread("CacheThread"); 1545 base::Thread cache_thread("CacheThread");
1553 ASSERT_TRUE(cache_thread.StartWithOptions( 1546 ASSERT_TRUE(cache_thread.StartWithOptions(
1554 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 1547 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
1555 net::TestCompletionCallback cb; 1548 net::TestCompletionCallback cb;
1556 1549
1557 disk_cache::Backend* cache; 1550 disk_cache::Backend* cache;
1558 int rv = disk_cache::BackendImpl::CreateBackend( 1551 int rv = disk_cache::CreateCacheBackendWithFlags(
1559 cache_path_, true, 0, net::DISK_CACHE, disk_cache::kNoRandom, 1552 net::DISK_CACHE, cache_path_, 0, true, disk_cache::kNoRandom,
1560 cache_thread.message_loop_proxy(), NULL, &cache, cb.callback()); 1553 cache_thread.message_loop_proxy(), NULL, &cache, cb.callback());
1561 ASSERT_EQ(net::OK, cb.GetResult(rv)); 1554 ASSERT_EQ(net::OK, cb.GetResult(rv));
1562 1555
1563 MessageLoopHelper helper; 1556 MessageLoopHelper helper;
1564 1557
1565 ASSERT_TRUE(NULL != cache); 1558 ASSERT_TRUE(NULL != cache);
1566 ASSERT_EQ(0, cache->GetEntryCount()); 1559 ASSERT_EQ(0, cache->GetEntryCount());
1567 1560
1568 delete cache; 1561 delete cache;
1569 } 1562 }
1570 1563
(...skipping 882 matching lines...) Expand 10 before | Expand all | Expand 10 after
2453 ASSERT_TRUE(store2.CreateUniqueTempDir()); 2446 ASSERT_TRUE(store2.CreateUniqueTempDir());
2454 2447
2455 base::Thread cache_thread("CacheThread"); 2448 base::Thread cache_thread("CacheThread");
2456 ASSERT_TRUE(cache_thread.StartWithOptions( 2449 ASSERT_TRUE(cache_thread.StartWithOptions(
2457 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 2450 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
2458 net::TestCompletionCallback cb; 2451 net::TestCompletionCallback cb;
2459 2452
2460 const int kNumberOfCaches = 2; 2453 const int kNumberOfCaches = 2;
2461 disk_cache::Backend* cache[kNumberOfCaches]; 2454 disk_cache::Backend* cache[kNumberOfCaches];
2462 2455
2463 int rv = disk_cache::BackendImpl::CreateBackend( 2456 int rv = disk_cache::CreateCacheBackend(
2464 store1.path(), false, 0, net::DISK_CACHE, disk_cache::kNone, 2457 net::DISK_CACHE, store1.path(), 0, false,
2465 cache_thread.message_loop_proxy(), NULL, &cache[0], cb.callback()); 2458 cache_thread.message_loop_proxy(), NULL, &cache[0], cb.callback());
2466 ASSERT_EQ(net::OK, cb.GetResult(rv)); 2459 ASSERT_EQ(net::OK, cb.GetResult(rv));
2467 rv = disk_cache::BackendImpl::CreateBackend( 2460 rv = disk_cache::CreateCacheBackend(
2468 store2.path(), false, 0, net::MEDIA_CACHE, disk_cache::kNone, 2461 net::MEDIA_CACHE, store2.path(), 0, false,
2469 cache_thread.message_loop_proxy(), NULL, &cache[1], cb.callback()); 2462 cache_thread.message_loop_proxy(), NULL, &cache[1], cb.callback());
2470 ASSERT_EQ(net::OK, cb.GetResult(rv)); 2463 ASSERT_EQ(net::OK, cb.GetResult(rv));
2471 2464
2472 ASSERT_TRUE(cache[0] != NULL && cache[1] != NULL); 2465 ASSERT_TRUE(cache[0] != NULL && cache[1] != NULL);
2473 2466
2474 std::string key("the first key"); 2467 std::string key("the first key");
2475 disk_cache::Entry* entry; 2468 disk_cache::Entry* entry;
2476 for (int i = 0; i < kNumberOfCaches; i++) { 2469 for (int i = 0; i < kNumberOfCaches; i++) {
2477 rv = cache[i]->CreateEntry(key, &entry, cb.callback()); 2470 rv = cache[i]->CreateEntry(key, &entry, cb.callback());
2478 ASSERT_EQ(net::OK, cb.GetResult(rv)); 2471 ASSERT_EQ(net::OK, cb.GetResult(rv));
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
2684 // Ping the oldest entry. 2677 // Ping the oldest entry.
2685 cache_->OnExternalCacheHit("key0"); 2678 cache_->OnExternalCacheHit("key0");
2686 2679
2687 TrimForTest(false); 2680 TrimForTest(false);
2688 2681
2689 // Make sure the older key remains. 2682 // Make sure the older key remains.
2690 EXPECT_EQ(1, cache_->GetEntryCount()); 2683 EXPECT_EQ(1, cache_->GetEntryCount());
2691 ASSERT_EQ(net::OK, OpenEntry("key0", &entry)); 2684 ASSERT_EQ(net::OK, OpenEntry("key0", &entry));
2692 entry->Close(); 2685 entry->Close();
2693 } 2686 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698