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

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

Issue 20737002: Change the API of disk_cache::CreateCacheBackend to use scoped_ptr (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix windows specific file. Created 7 years, 4 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 | Annotate | Revision Log
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/metrics/field_trial.h" 7 #include "base/metrics/field_trial.h"
8 #include "base/port.h" 8 #include "base/port.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
(...skipping 23 matching lines...) Expand all
34 #if defined(OS_WIN) 34 #if defined(OS_WIN)
35 #include "base/win/scoped_handle.h" 35 #include "base/win/scoped_handle.h"
36 #endif 36 #endif
37 37
38 using base::Time; 38 using base::Time;
39 39
40 namespace { 40 namespace {
41 41
42 const char kExistingEntryKey[] = "existing entry key"; 42 const char kExistingEntryKey[] = "existing entry key";
43 43
44 disk_cache::BackendImpl* CreateExistingEntryCache( 44 scoped_ptr<disk_cache::BackendImpl> CreateExistingEntryCache(
45 const base::Thread& cache_thread, 45 const base::Thread& cache_thread,
46 base::FilePath& cache_path) { 46 base::FilePath& cache_path) {
47 net::TestCompletionCallback cb; 47 net::TestCompletionCallback cb;
48 48
49 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( 49 scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl(
50 cache_path, cache_thread.message_loop_proxy(), NULL); 50 cache_path, cache_thread.message_loop_proxy(), NULL));
51 int rv = cache->Init(cb.callback()); 51 int rv = cache->Init(cb.callback());
52 if (cb.GetResult(rv) != net::OK) 52 if (cb.GetResult(rv) != net::OK)
53 return NULL; 53 return scoped_ptr<disk_cache::BackendImpl>();
54 54
55 disk_cache::Entry* entry = NULL; 55 disk_cache::Entry* entry = NULL;
56 rv = cache->CreateEntry(kExistingEntryKey, &entry, cb.callback()); 56 rv = cache->CreateEntry(kExistingEntryKey, &entry, cb.callback());
57 if (cb.GetResult(rv) != net::OK) { 57 if (cb.GetResult(rv) != net::OK) {
58 delete cache; 58 return scoped_ptr<disk_cache::BackendImpl>();
59 return NULL;
60 } 59 }
rvargas (doing something else) 2013/07/26 21:05:01 nit: remove {}
qsr 2013/07/29 08:26:28 Done.
61 entry->Close(); 60 entry->Close();
62 61
63 return cache; 62 return cache.Pass();
64 } 63 }
65 64
66 } // namespace 65 } // namespace
67 66
68 // Tests that can run with different types of caches. 67 // Tests that can run with different types of caches.
69 class DiskCacheBackendTest : public DiskCacheTestWithCache { 68 class DiskCacheBackendTest : public DiskCacheTestWithCache {
70 protected: 69 protected:
71 void BackendBasics(); 70 void BackendBasics();
72 void BackendKeying(); 71 void BackendKeying();
73 void BackendShutdownWithPendingFileIO(bool fast); 72 void BackendShutdownWithPendingFileIO(bool fast);
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 TEST_F(DiskCacheTest, CreateBackend) { 260 TEST_F(DiskCacheTest, CreateBackend) {
262 net::TestCompletionCallback cb; 261 net::TestCompletionCallback cb;
263 262
264 { 263 {
265 ASSERT_TRUE(CleanupCacheDir()); 264 ASSERT_TRUE(CleanupCacheDir());
266 base::Thread cache_thread("CacheThread"); 265 base::Thread cache_thread("CacheThread");
267 ASSERT_TRUE(cache_thread.StartWithOptions( 266 ASSERT_TRUE(cache_thread.StartWithOptions(
268 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); 267 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
269 268
270 // Test the private factory method(s). 269 // Test the private factory method(s).
271 disk_cache::Backend* cache = NULL; 270 scoped_ptr<disk_cache::Backend> cache;
272 cache = disk_cache::MemBackendImpl::CreateBackend(0, NULL); 271 cache = disk_cache::MemBackendImpl::CreateBackend(0, NULL);
273 ASSERT_TRUE(cache); 272 ASSERT_TRUE(cache.get());
274 delete cache; 273 cache.reset();
275 cache = NULL;
276 274
277 // Now test the public API. 275 // Now test the public API.
278 int rv = 276 int rv =
279 disk_cache::CreateCacheBackend(net::DISK_CACHE, 277 disk_cache::CreateCacheBackend(net::DISK_CACHE,
280 net::CACHE_BACKEND_DEFAULT, 278 net::CACHE_BACKEND_DEFAULT,
281 cache_path_, 279 cache_path_,
282 0, 280 0,
283 false, 281 false,
284 cache_thread.message_loop_proxy().get(), 282 cache_thread.message_loop_proxy().get(),
285 NULL, 283 NULL,
286 &cache, 284 &cache,
287 cb.callback()); 285 cb.callback());
288 ASSERT_EQ(net::OK, cb.GetResult(rv)); 286 ASSERT_EQ(net::OK, cb.GetResult(rv));
289 ASSERT_TRUE(cache); 287 ASSERT_TRUE(cache.get());
290 delete cache; 288 cache.reset();
291 cache = NULL;
292 289
293 rv = disk_cache::CreateCacheBackend(net::MEMORY_CACHE, 290 rv = disk_cache::CreateCacheBackend(net::MEMORY_CACHE,
294 net::CACHE_BACKEND_DEFAULT, 291 net::CACHE_BACKEND_DEFAULT,
295 base::FilePath(), 0, 292 base::FilePath(), 0,
296 false, NULL, NULL, &cache, 293 false, NULL, NULL, &cache,
297 cb.callback()); 294 cb.callback());
298 ASSERT_EQ(net::OK, cb.GetResult(rv)); 295 ASSERT_EQ(net::OK, cb.GetResult(rv));
299 ASSERT_TRUE(cache); 296 ASSERT_TRUE(cache.get());
300 delete cache; 297 cache.reset();
301 } 298 }
302 299
303 base::MessageLoop::current()->RunUntilIdle(); 300 base::MessageLoop::current()->RunUntilIdle();
304 } 301 }
305 302
306 // Tests that |BackendImpl| fails to initialize with a missing file. 303 // Tests that |BackendImpl| fails to initialize with a missing file.
307 TEST_F(DiskCacheBackendTest, CreateBackend_MissingFile) { 304 TEST_F(DiskCacheBackendTest, CreateBackend_MissingFile) {
308 ASSERT_TRUE(CopyTestCache("bad_entry")); 305 ASSERT_TRUE(CopyTestCache("bad_entry"));
309 base::FilePath filename = cache_path_.AppendASCII("data_1"); 306 base::FilePath filename = cache_path_.AppendASCII("data_1");
310 base::DeleteFile(filename, false); 307 base::DeleteFile(filename, false);
311 base::Thread cache_thread("CacheThread"); 308 base::Thread cache_thread("CacheThread");
312 ASSERT_TRUE(cache_thread.StartWithOptions( 309 ASSERT_TRUE(cache_thread.StartWithOptions(
313 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); 310 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
314 net::TestCompletionCallback cb; 311 net::TestCompletionCallback cb;
315 312
316 bool prev = base::ThreadRestrictions::SetIOAllowed(false); 313 bool prev = base::ThreadRestrictions::SetIOAllowed(false);
317 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( 314 scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl(
318 cache_path_, cache_thread.message_loop_proxy().get(), NULL); 315 cache_path_, cache_thread.message_loop_proxy().get(), NULL));
319 int rv = cache->Init(cb.callback()); 316 int rv = cache->Init(cb.callback());
320 ASSERT_EQ(net::ERR_FAILED, cb.GetResult(rv)); 317 ASSERT_EQ(net::ERR_FAILED, cb.GetResult(rv));
321 base::ThreadRestrictions::SetIOAllowed(prev); 318 base::ThreadRestrictions::SetIOAllowed(prev);
322 319
323 delete cache; 320 cache.reset();
324 DisableIntegrityCheck(); 321 DisableIntegrityCheck();
325 } 322 }
326 323
327 TEST_F(DiskCacheBackendTest, ExternalFiles) { 324 TEST_F(DiskCacheBackendTest, ExternalFiles) {
328 InitCache(); 325 InitCache();
329 // First, let's create a file on the folder. 326 // First, let's create a file on the folder.
330 base::FilePath filename = cache_path_.AppendASCII("f_000001"); 327 base::FilePath filename = cache_path_.AppendASCII("f_000001");
331 328
332 const int kSize = 50; 329 const int kSize = 50;
333 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); 330 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 if (rv == net::ERR_IO_PENDING) 380 if (rv == net::ERR_IO_PENDING)
384 break; 381 break;
385 EXPECT_EQ(kSize, rv); 382 EXPECT_EQ(kSize, rv);
386 } 383 }
387 384
388 // Don't call Close() to avoid going through the queue or we'll deadlock 385 // Don't call Close() to avoid going through the queue or we'll deadlock
389 // waiting for the operation to finish. 386 // waiting for the operation to finish.
390 entry->Release(); 387 entry->Release();
391 388
392 // The cache destructor will see one pending operation here. 389 // The cache destructor will see one pending operation here.
393 delete cache_; 390 cache_.reset();
394 // Prevent the TearDown() to delete the backend again.
395 cache_ = NULL;
396 391
397 if (rv == net::ERR_IO_PENDING) { 392 if (rv == net::ERR_IO_PENDING) {
398 if (fast) 393 if (fast)
399 EXPECT_FALSE(cb.have_result()); 394 EXPECT_FALSE(cb.have_result());
400 else 395 else
401 EXPECT_TRUE(cb.have_result()); 396 EXPECT_TRUE(cb.have_result());
402 } 397 }
403 } 398 }
404 399
405 base::MessageLoop::current()->RunUntilIdle(); 400 base::MessageLoop::current()->RunUntilIdle();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 436
442 CreateBackend(flags, &cache_thread); 437 CreateBackend(flags, &cache_thread);
443 438
444 disk_cache::Entry* entry; 439 disk_cache::Entry* entry;
445 int rv = cache_->CreateEntry("some key", &entry, cb.callback()); 440 int rv = cache_->CreateEntry("some key", &entry, cb.callback());
446 ASSERT_EQ(net::OK, cb.GetResult(rv)); 441 ASSERT_EQ(net::OK, cb.GetResult(rv));
447 442
448 entry->Close(); 443 entry->Close();
449 444
450 // The cache destructor will see one pending operation here. 445 // The cache destructor will see one pending operation here.
451 delete cache_; 446 cache_.reset();
452 // Prevent the TearDown() to delete the backend again.
453 cache_ = NULL;
454 } 447 }
455 448
456 base::MessageLoop::current()->RunUntilIdle(); 449 base::MessageLoop::current()->RunUntilIdle();
457 } 450 }
458 451
459 TEST_F(DiskCacheBackendTest, ShutdownWithPendingIO) { 452 TEST_F(DiskCacheBackendTest, ShutdownWithPendingIO) {
460 BackendShutdownWithPendingIO(false); 453 BackendShutdownWithPendingIO(false);
461 } 454 }
462 455
463 // We'll be leaking from this test. 456 // We'll be leaking from this test.
(...skipping 15 matching lines...) Expand all
479 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); 472 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
480 473
481 disk_cache::BackendFlags flags = 474 disk_cache::BackendFlags flags =
482 fast ? disk_cache::kNone : disk_cache::kNoRandom; 475 fast ? disk_cache::kNone : disk_cache::kNoRandom;
483 CreateBackend(flags, &cache_thread); 476 CreateBackend(flags, &cache_thread);
484 477
485 disk_cache::Entry* entry; 478 disk_cache::Entry* entry;
486 int rv = cache_->CreateEntry("some key", &entry, cb.callback()); 479 int rv = cache_->CreateEntry("some key", &entry, cb.callback());
487 ASSERT_EQ(net::ERR_IO_PENDING, rv); 480 ASSERT_EQ(net::ERR_IO_PENDING, rv);
488 481
489 delete cache_; 482 cache_.reset();
490 // Prevent the TearDown() to delete the backend again.
491 cache_ = NULL;
492 EXPECT_FALSE(cb.have_result()); 483 EXPECT_FALSE(cb.have_result());
493 } 484 }
494 485
495 base::MessageLoop::current()->RunUntilIdle(); 486 base::MessageLoop::current()->RunUntilIdle();
496 } 487 }
497 488
498 TEST_F(DiskCacheBackendTest, ShutdownWithPendingCreate) { 489 TEST_F(DiskCacheBackendTest, ShutdownWithPendingCreate) {
499 BackendShutdownWithPendingCreate(false); 490 BackendShutdownWithPendingCreate(false);
500 } 491 }
501 492
502 // We'll be leaking an entry from this test. 493 // We'll be leaking an entry from this test.
503 TEST_F(DiskCacheBackendTest, ShutdownWithPendingCreate_Fast) { 494 TEST_F(DiskCacheBackendTest, ShutdownWithPendingCreate_Fast) {
504 // The integrity test sets kNoRandom so there's a version mismatch if we don't 495 // The integrity test sets kNoRandom so there's a version mismatch if we don't
505 // force new eviction. 496 // force new eviction.
506 SetNewEviction(); 497 SetNewEviction();
507 BackendShutdownWithPendingCreate(true); 498 BackendShutdownWithPendingCreate(true);
508 } 499 }
509 500
510 TEST_F(DiskCacheTest, TruncatedIndex) { 501 TEST_F(DiskCacheTest, TruncatedIndex) {
511 ASSERT_TRUE(CleanupCacheDir()); 502 ASSERT_TRUE(CleanupCacheDir());
512 base::FilePath index = cache_path_.AppendASCII("index"); 503 base::FilePath index = cache_path_.AppendASCII("index");
513 ASSERT_EQ(5, file_util::WriteFile(index, "hello", 5)); 504 ASSERT_EQ(5, file_util::WriteFile(index, "hello", 5));
514 505
515 base::Thread cache_thread("CacheThread"); 506 base::Thread cache_thread("CacheThread");
516 ASSERT_TRUE(cache_thread.StartWithOptions( 507 ASSERT_TRUE(cache_thread.StartWithOptions(
517 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); 508 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
518 net::TestCompletionCallback cb; 509 net::TestCompletionCallback cb;
519 510
520 disk_cache::Backend* backend = NULL; 511 scoped_ptr<disk_cache::Backend> backend;
521 int rv = 512 int rv =
522 disk_cache::CreateCacheBackend(net::DISK_CACHE, 513 disk_cache::CreateCacheBackend(net::DISK_CACHE,
523 net::CACHE_BACKEND_BLOCKFILE, 514 net::CACHE_BACKEND_BLOCKFILE,
524 cache_path_, 515 cache_path_,
525 0, 516 0,
526 false, 517 false,
527 cache_thread.message_loop_proxy().get(), 518 cache_thread.message_loop_proxy().get(),
528 NULL, 519 NULL,
529 &backend, 520 &backend,
530 cb.callback()); 521 cb.callback());
531 ASSERT_NE(net::OK, cb.GetResult(rv)); 522 ASSERT_NE(net::OK, cb.GetResult(rv));
532 523
533 ASSERT_TRUE(backend == NULL); 524 ASSERT_TRUE(backend.get() == NULL);
rvargas (doing something else) 2013/07/26 21:05:01 nit: false(get())
qsr 2013/07/29 08:26:28 Done.
534 delete backend;
535 } 525 }
536 526
537 void DiskCacheBackendTest::BackendSetSize() { 527 void DiskCacheBackendTest::BackendSetSize() {
538 const int cache_size = 0x10000; // 64 kB 528 const int cache_size = 0x10000; // 64 kB
539 SetMaxSize(cache_size); 529 SetMaxSize(cache_size);
540 InitCache(); 530 InitCache();
541 531
542 std::string first("some key"); 532 std::string first("some key");
543 std::string second("something else"); 533 std::string second("something else");
544 disk_cache::Entry* entry; 534 disk_cache::Entry* entry;
(...skipping 1051 matching lines...) Expand 10 before | Expand all | Expand 10 after
1596 ASSERT_NE(net::OK, OpenEntry(key, &entry1)); 1586 ASSERT_NE(net::OK, OpenEntry(key, &entry1));
1597 1587
1598 int actual = cache_->GetEntryCount(); 1588 int actual = cache_->GetEntryCount();
1599 if (num_entries != actual) { 1589 if (num_entries != actual) {
1600 ASSERT_TRUE(load); 1590 ASSERT_TRUE(load);
1601 // If there is a heavy load, inserting an entry will make another entry 1591 // If there is a heavy load, inserting an entry will make another entry
1602 // dirty (on the hash bucket) so two entries are removed. 1592 // dirty (on the hash bucket) so two entries are removed.
1603 ASSERT_EQ(num_entries - 1, actual); 1593 ASSERT_EQ(num_entries - 1, actual);
1604 } 1594 }
1605 1595
1606 delete cache_; 1596 cache_.reset();
1607 cache_ = NULL;
1608 cache_impl_ = NULL; 1597 cache_impl_ = NULL;
1609 1598
1610 ASSERT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask)); 1599 ASSERT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask));
1611 success_ = true; 1600 success_ = true;
1612 } 1601 }
1613 1602
1614 void DiskCacheBackendTest::BackendRecoverInsert() { 1603 void DiskCacheBackendTest::BackendRecoverInsert() {
1615 // Tests with an empty cache. 1604 // Tests with an empty cache.
1616 BackendTransaction("insert_empty1", 0, false); 1605 BackendTransaction("insert_empty1", 0, false);
1617 ASSERT_TRUE(success_) << "insert_empty1"; 1606 ASSERT_TRUE(success_) << "insert_empty1";
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
1716 } 1705 }
1717 1706
1718 // Tests that the |BackendImpl| fails to start with the wrong cache version. 1707 // Tests that the |BackendImpl| fails to start with the wrong cache version.
1719 TEST_F(DiskCacheTest, WrongVersion) { 1708 TEST_F(DiskCacheTest, WrongVersion) {
1720 ASSERT_TRUE(CopyTestCache("wrong_version")); 1709 ASSERT_TRUE(CopyTestCache("wrong_version"));
1721 base::Thread cache_thread("CacheThread"); 1710 base::Thread cache_thread("CacheThread");
1722 ASSERT_TRUE(cache_thread.StartWithOptions( 1711 ASSERT_TRUE(cache_thread.StartWithOptions(
1723 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); 1712 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
1724 net::TestCompletionCallback cb; 1713 net::TestCompletionCallback cb;
1725 1714
1726 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( 1715 scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl(
1727 cache_path_, cache_thread.message_loop_proxy().get(), NULL); 1716 cache_path_, cache_thread.message_loop_proxy().get(), NULL));
1728 int rv = cache->Init(cb.callback()); 1717 int rv = cache->Init(cb.callback());
1729 ASSERT_EQ(net::ERR_FAILED, cb.GetResult(rv)); 1718 ASSERT_EQ(net::ERR_FAILED, cb.GetResult(rv));
1730
1731 delete cache;
1732 } 1719 }
1733 1720
1734 class BadEntropyProvider : public base::FieldTrial::EntropyProvider { 1721 class BadEntropyProvider : public base::FieldTrial::EntropyProvider {
1735 public: 1722 public:
1736 virtual ~BadEntropyProvider() {} 1723 virtual ~BadEntropyProvider() {}
1737 1724
1738 virtual double GetEntropyForTrial(const std::string& trial_name, 1725 virtual double GetEntropyForTrial(const std::string& trial_name,
1739 uint32 randomization_seed) const OVERRIDE { 1726 uint32 randomization_seed) const OVERRIDE {
1740 return 0.5; 1727 return 0.5;
1741 } 1728 }
1742 }; 1729 };
1743 1730
1744 // Tests that the disk cache successfully joins the control group, dropping the 1731 // Tests that the disk cache successfully joins the control group, dropping the
1745 // existing cache in favour of a new empty cache. 1732 // existing cache in favour of a new empty cache.
1746 TEST_F(DiskCacheTest, SimpleCacheControlJoin) { 1733 TEST_F(DiskCacheTest, SimpleCacheControlJoin) {
1747 base::Thread cache_thread("CacheThread"); 1734 base::Thread cache_thread("CacheThread");
1748 ASSERT_TRUE(cache_thread.StartWithOptions( 1735 ASSERT_TRUE(cache_thread.StartWithOptions(
1749 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); 1736 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
1750 1737
1751 disk_cache::BackendImpl* cache = CreateExistingEntryCache(cache_thread, 1738 scoped_ptr<disk_cache::BackendImpl> cache =
1752 cache_path_); 1739 CreateExistingEntryCache(cache_thread, cache_path_);
1753 ASSERT_TRUE(cache); 1740 ASSERT_TRUE(cache.get());
1754 delete cache; 1741 cache.reset();
1755 cache = NULL;
1756 1742
1757 // Instantiate the SimpleCacheTrial, forcing this run into the 1743 // Instantiate the SimpleCacheTrial, forcing this run into the
1758 // ExperimentControl group. 1744 // ExperimentControl group.
1759 base::FieldTrialList field_trial_list(new BadEntropyProvider()); 1745 base::FieldTrialList field_trial_list(new BadEntropyProvider());
1760 base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial", 1746 base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial",
1761 "ExperimentControl"); 1747 "ExperimentControl");
1762 net::TestCompletionCallback cb; 1748 net::TestCompletionCallback cb;
1763 disk_cache::Backend* base_cache = NULL; 1749 scoped_ptr<disk_cache::Backend> base_cache;
1764 int rv = 1750 int rv =
1765 disk_cache::CreateCacheBackend(net::DISK_CACHE, 1751 disk_cache::CreateCacheBackend(net::DISK_CACHE,
1766 net::CACHE_BACKEND_BLOCKFILE, 1752 net::CACHE_BACKEND_BLOCKFILE,
1767 cache_path_, 1753 cache_path_,
1768 0, 1754 0,
1769 true, 1755 true,
1770 cache_thread.message_loop_proxy().get(), 1756 cache_thread.message_loop_proxy().get(),
1771 NULL, 1757 NULL,
1772 &base_cache, 1758 &base_cache,
1773 cb.callback()); 1759 cb.callback());
1774 ASSERT_EQ(net::OK, cb.GetResult(rv)); 1760 ASSERT_EQ(net::OK, cb.GetResult(rv));
1775 cache = static_cast<disk_cache::BackendImpl*>(base_cache);
1776 EXPECT_EQ(0, base_cache->GetEntryCount()); 1761 EXPECT_EQ(0, base_cache->GetEntryCount());
1777 delete cache; 1762 cache.reset(static_cast<disk_cache::BackendImpl*>(base_cache.release()));
rvargas (doing something else) 2013/07/26 21:05:01 nit: remove this line
qsr 2013/07/29 08:26:28 Done.
1778 } 1763 }
1779 1764
1780 // Tests that the disk cache can restart in the control group preserving 1765 // Tests that the disk cache can restart in the control group preserving
1781 // existing entries. 1766 // existing entries.
1782 TEST_F(DiskCacheTest, SimpleCacheControlRestart) { 1767 TEST_F(DiskCacheTest, SimpleCacheControlRestart) {
1783 // Instantiate the SimpleCacheTrial, forcing this run into the 1768 // Instantiate the SimpleCacheTrial, forcing this run into the
1784 // ExperimentControl group. 1769 // ExperimentControl group.
1785 base::FieldTrialList field_trial_list(new BadEntropyProvider()); 1770 base::FieldTrialList field_trial_list(new BadEntropyProvider());
1786 base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial", 1771 base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial",
1787 "ExperimentControl"); 1772 "ExperimentControl");
1788 1773
1789 base::Thread cache_thread("CacheThread"); 1774 base::Thread cache_thread("CacheThread");
1790 ASSERT_TRUE(cache_thread.StartWithOptions( 1775 ASSERT_TRUE(cache_thread.StartWithOptions(
1791 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); 1776 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
1792 1777
1793 disk_cache::BackendImpl* cache = CreateExistingEntryCache(cache_thread, 1778 scoped_ptr<disk_cache::BackendImpl> cache =
1794 cache_path_); 1779 CreateExistingEntryCache(cache_thread, cache_path_);
1795 ASSERT_TRUE(cache); 1780 ASSERT_TRUE(cache.get());
1796 delete cache;
1797 cache = NULL;
1798 1781
1799 net::TestCompletionCallback cb; 1782 net::TestCompletionCallback cb;
1800 1783
1801 const int kRestartCount = 5; 1784 const int kRestartCount = 5;
1802 for (int i=0; i < kRestartCount; ++i) { 1785 for (int i=0; i < kRestartCount; ++i) {
1803 cache = new disk_cache::BackendImpl(cache_path_, 1786 cache.reset(new disk_cache::BackendImpl(
1804 cache_thread.message_loop_proxy(), 1787 cache_path_, cache_thread.message_loop_proxy(), NULL));
1805 NULL);
1806 int rv = cache->Init(cb.callback()); 1788 int rv = cache->Init(cb.callback());
1807 ASSERT_EQ(net::OK, cb.GetResult(rv)); 1789 ASSERT_EQ(net::OK, cb.GetResult(rv));
1808 EXPECT_EQ(1, cache->GetEntryCount()); 1790 EXPECT_EQ(1, cache->GetEntryCount());
1809 1791
1810 disk_cache::Entry* entry = NULL; 1792 disk_cache::Entry* entry = NULL;
1811 rv = cache->OpenEntry(kExistingEntryKey, &entry, cb.callback()); 1793 rv = cache->OpenEntry(kExistingEntryKey, &entry, cb.callback());
1812 EXPECT_EQ(net::OK, cb.GetResult(rv)); 1794 EXPECT_EQ(net::OK, cb.GetResult(rv));
1813 EXPECT_TRUE(entry); 1795 EXPECT_TRUE(entry);
1814 entry->Close(); 1796 entry->Close();
1815 delete cache;
1816 cache = NULL;
1817 } 1797 }
1818 } 1798 }
1819 1799
1820 // Tests that the disk cache can leave the control group preserving existing 1800 // Tests that the disk cache can leave the control group preserving existing
1821 // entries. 1801 // entries.
1822 TEST_F(DiskCacheTest, SimpleCacheControlLeave) { 1802 TEST_F(DiskCacheTest, SimpleCacheControlLeave) {
1823 base::Thread cache_thread("CacheThread"); 1803 base::Thread cache_thread("CacheThread");
1824 ASSERT_TRUE(cache_thread.StartWithOptions( 1804 ASSERT_TRUE(cache_thread.StartWithOptions(
1825 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); 1805 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
1826 1806
1827 { 1807 {
1828 // Instantiate the SimpleCacheTrial, forcing this run into the 1808 // Instantiate the SimpleCacheTrial, forcing this run into the
1829 // ExperimentControl group. 1809 // ExperimentControl group.
1830 base::FieldTrialList field_trial_list(new BadEntropyProvider()); 1810 base::FieldTrialList field_trial_list(new BadEntropyProvider());
1831 base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial", 1811 base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial",
1832 "ExperimentControl"); 1812 "ExperimentControl");
1833 1813
1834 disk_cache::BackendImpl* cache = CreateExistingEntryCache(cache_thread, 1814 scoped_ptr<disk_cache::BackendImpl> cache =
1835 cache_path_); 1815 CreateExistingEntryCache(cache_thread, cache_path_);
1836 ASSERT_TRUE(cache); 1816 ASSERT_TRUE(cache.get());
1837 delete cache;
1838 } 1817 }
1839 1818
1840 // Instantiate the SimpleCacheTrial, forcing this run into the 1819 // Instantiate the SimpleCacheTrial, forcing this run into the
1841 // ExperimentNo group. 1820 // ExperimentNo group.
1842 base::FieldTrialList field_trial_list(new BadEntropyProvider()); 1821 base::FieldTrialList field_trial_list(new BadEntropyProvider());
1843 base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial", "ExperimentNo"); 1822 base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial", "ExperimentNo");
1844 net::TestCompletionCallback cb; 1823 net::TestCompletionCallback cb;
1845 1824
1846 const int kRestartCount = 5; 1825 const int kRestartCount = 5;
1847 for (int i = 0; i < kRestartCount; ++i) { 1826 for (int i = 0; i < kRestartCount; ++i) {
1848 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( 1827 scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl(
1849 cache_path_, cache_thread.message_loop_proxy(), NULL); 1828 cache_path_, cache_thread.message_loop_proxy(), NULL));
1850 int rv = cache->Init(cb.callback()); 1829 int rv = cache->Init(cb.callback());
1851 ASSERT_EQ(net::OK, cb.GetResult(rv)); 1830 ASSERT_EQ(net::OK, cb.GetResult(rv));
1852 EXPECT_EQ(1, cache->GetEntryCount()); 1831 EXPECT_EQ(1, cache->GetEntryCount());
1853 1832
1854 disk_cache::Entry* entry = NULL; 1833 disk_cache::Entry* entry = NULL;
1855 rv = cache->OpenEntry(kExistingEntryKey, &entry, cb.callback()); 1834 rv = cache->OpenEntry(kExistingEntryKey, &entry, cb.callback());
1856 EXPECT_EQ(net::OK, cb.GetResult(rv)); 1835 EXPECT_EQ(net::OK, cb.GetResult(rv));
1857 EXPECT_TRUE(entry); 1836 EXPECT_TRUE(entry);
1858 entry->Close(); 1837 entry->Close();
1859 delete cache;
1860 cache = NULL;
1861 } 1838 }
1862 } 1839 }
1863 1840
1864 // Tests that the cache is properly restarted on recovery error. 1841 // Tests that the cache is properly restarted on recovery error.
1865 TEST_F(DiskCacheBackendTest, DeleteOld) { 1842 TEST_F(DiskCacheBackendTest, DeleteOld) {
1866 ASSERT_TRUE(CopyTestCache("wrong_version")); 1843 ASSERT_TRUE(CopyTestCache("wrong_version"));
1867 SetNewEviction(); 1844 SetNewEviction();
1868 base::Thread cache_thread("CacheThread"); 1845 base::Thread cache_thread("CacheThread");
1869 ASSERT_TRUE(cache_thread.StartWithOptions( 1846 ASSERT_TRUE(cache_thread.StartWithOptions(
1870 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); 1847 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
1871 1848
1872 net::TestCompletionCallback cb; 1849 net::TestCompletionCallback cb;
1873 bool prev = base::ThreadRestrictions::SetIOAllowed(false); 1850 bool prev = base::ThreadRestrictions::SetIOAllowed(false);
1874 base::FilePath path(cache_path_); 1851 base::FilePath path(cache_path_);
1875 int rv = 1852 int rv =
1876 disk_cache::CreateCacheBackend(net::DISK_CACHE, 1853 disk_cache::CreateCacheBackend(net::DISK_CACHE,
1877 net::CACHE_BACKEND_BLOCKFILE, 1854 net::CACHE_BACKEND_BLOCKFILE,
1878 path, 1855 path,
1879 0, 1856 0,
1880 true, 1857 true,
1881 cache_thread.message_loop_proxy().get(), 1858 cache_thread.message_loop_proxy().get(),
1882 NULL, 1859 NULL,
1883 &cache_, 1860 &cache_,
1884 cb.callback()); 1861 cb.callback());
1885 path.clear(); // Make sure path was captured by the previous call. 1862 path.clear(); // Make sure path was captured by the previous call.
1886 ASSERT_EQ(net::OK, cb.GetResult(rv)); 1863 ASSERT_EQ(net::OK, cb.GetResult(rv));
1887 base::ThreadRestrictions::SetIOAllowed(prev); 1864 base::ThreadRestrictions::SetIOAllowed(prev);
1888 delete cache_; 1865 cache_.reset();
1889 cache_ = NULL;
1890 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); 1866 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
1891 } 1867 }
1892 1868
1893 // We want to be able to deal with messed up entries on disk. 1869 // We want to be able to deal with messed up entries on disk.
1894 void DiskCacheBackendTest::BackendInvalidEntry2() { 1870 void DiskCacheBackendTest::BackendInvalidEntry2() {
1895 ASSERT_TRUE(CopyTestCache("bad_entry")); 1871 ASSERT_TRUE(CopyTestCache("bad_entry"));
1896 DisableFirstCleanup(); 1872 DisableFirstCleanup();
1897 InitCache(); 1873 InitCache();
1898 1874
1899 disk_cache::Entry *entry1, *entry2; 1875 disk_cache::Entry *entry1, *entry2;
(...skipping 853 matching lines...) Expand 10 before | Expand all | Expand 10 after
2753 base::ScopedTempDir store1, store2; 2729 base::ScopedTempDir store1, store2;
2754 ASSERT_TRUE(store1.CreateUniqueTempDir()); 2730 ASSERT_TRUE(store1.CreateUniqueTempDir());
2755 ASSERT_TRUE(store2.CreateUniqueTempDir()); 2731 ASSERT_TRUE(store2.CreateUniqueTempDir());
2756 2732
2757 base::Thread cache_thread("CacheThread"); 2733 base::Thread cache_thread("CacheThread");
2758 ASSERT_TRUE(cache_thread.StartWithOptions( 2734 ASSERT_TRUE(cache_thread.StartWithOptions(
2759 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); 2735 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
2760 net::TestCompletionCallback cb; 2736 net::TestCompletionCallback cb;
2761 2737
2762 const int kNumberOfCaches = 2; 2738 const int kNumberOfCaches = 2;
2763 disk_cache::Backend* cache[kNumberOfCaches]; 2739 scoped_ptr<disk_cache::Backend> cache[kNumberOfCaches];
2764 2740
2765 int rv = 2741 int rv =
2766 disk_cache::CreateCacheBackend(net::DISK_CACHE, 2742 disk_cache::CreateCacheBackend(net::DISK_CACHE,
2767 net::CACHE_BACKEND_DEFAULT, 2743 net::CACHE_BACKEND_DEFAULT,
2768 store1.path(), 2744 store1.path(),
2769 0, 2745 0,
2770 false, 2746 false,
2771 cache_thread.message_loop_proxy().get(), 2747 cache_thread.message_loop_proxy().get(),
2772 NULL, 2748 NULL,
2773 &cache[0], 2749 &cache[0],
2774 cb.callback()); 2750 cb.callback());
2775 ASSERT_EQ(net::OK, cb.GetResult(rv)); 2751 ASSERT_EQ(net::OK, cb.GetResult(rv));
2776 rv = disk_cache::CreateCacheBackend(net::MEDIA_CACHE, 2752 rv = disk_cache::CreateCacheBackend(net::MEDIA_CACHE,
2777 net::CACHE_BACKEND_DEFAULT, 2753 net::CACHE_BACKEND_DEFAULT,
2778 store2.path(), 2754 store2.path(),
2779 0, 2755 0,
2780 false, 2756 false,
2781 cache_thread.message_loop_proxy().get(), 2757 cache_thread.message_loop_proxy().get(),
2782 NULL, 2758 NULL,
2783 &cache[1], 2759 &cache[1],
2784 cb.callback()); 2760 cb.callback());
2785 ASSERT_EQ(net::OK, cb.GetResult(rv)); 2761 ASSERT_EQ(net::OK, cb.GetResult(rv));
2786 2762
2787 ASSERT_TRUE(cache[0] != NULL && cache[1] != NULL); 2763 ASSERT_TRUE(cache[0].get() != NULL && cache[1].get() != NULL);
2788 2764
2789 std::string key("the first key"); 2765 std::string key("the first key");
2790 disk_cache::Entry* entry; 2766 disk_cache::Entry* entry;
2791 for (int i = 0; i < kNumberOfCaches; i++) { 2767 for (int i = 0; i < kNumberOfCaches; i++) {
2792 rv = cache[i]->CreateEntry(key, &entry, cb.callback()); 2768 rv = cache[i]->CreateEntry(key, &entry, cb.callback());
2793 ASSERT_EQ(net::OK, cb.GetResult(rv)); 2769 ASSERT_EQ(net::OK, cb.GetResult(rv));
2794 entry->Close(); 2770 entry->Close();
2795 } 2771 }
2796 delete cache[0];
2797 delete cache[1];
2798 } 2772 }
2799 2773
2800 // Test the six regions of the curve that determines the max cache size. 2774 // Test the six regions of the curve that determines the max cache size.
2801 TEST_F(DiskCacheTest, AutomaticMaxSize) { 2775 TEST_F(DiskCacheTest, AutomaticMaxSize) {
2802 const int kDefaultSize = 80 * 1024 * 1024; 2776 const int kDefaultSize = 80 * 1024 * 1024;
2803 int64 large_size = kDefaultSize; 2777 int64 large_size = kDefaultSize;
2804 int64 largest_size = kint32max; 2778 int64 largest_size = kint32max;
2805 2779
2806 // Region 1: expected = available * 0.8 2780 // Region 1: expected = available * 0.8
2807 EXPECT_EQ((kDefaultSize - 1) * 8 / 10, 2781 EXPECT_EQ((kDefaultSize - 1) * 8 / 10,
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
2998 TrimForTest(false); 2972 TrimForTest(false);
2999 2973
3000 // Make sure the older key remains. 2974 // Make sure the older key remains.
3001 EXPECT_EQ(1, cache_->GetEntryCount()); 2975 EXPECT_EQ(1, cache_->GetEntryCount());
3002 ASSERT_EQ(net::OK, OpenEntry("key0", &entry)); 2976 ASSERT_EQ(net::OK, OpenEntry("key0", &entry));
3003 entry->Close(); 2977 entry->Close();
3004 } 2978 }
3005 2979
3006 void DiskCacheBackendTest::TracingBackendBasics() { 2980 void DiskCacheBackendTest::TracingBackendBasics() {
3007 InitCache(); 2981 InitCache();
3008 cache_ = new disk_cache::TracingCacheBackend(cache_); 2982 cache_.reset(new disk_cache::TracingCacheBackend(cache_.release()));
3009 cache_impl_ = NULL; 2983 cache_impl_ = NULL;
3010 EXPECT_EQ(net::DISK_CACHE, cache_->GetCacheType()); 2984 EXPECT_EQ(net::DISK_CACHE, cache_->GetCacheType());
3011 if (!simple_cache_mode_) { 2985 if (!simple_cache_mode_) {
3012 EXPECT_EQ(0, cache_->GetEntryCount()); 2986 EXPECT_EQ(0, cache_->GetEntryCount());
3013 } 2987 }
3014 2988
3015 net::TestCompletionCallback cb; 2989 net::TestCompletionCallback cb;
3016 disk_cache::Entry* entry = NULL; 2990 disk_cache::Entry* entry = NULL;
3017 EXPECT_NE(net::OK, OpenEntry("key", &entry)); 2991 EXPECT_NE(net::OK, OpenEntry("key", &entry));
3018 EXPECT_TRUE(NULL == entry); 2992 EXPECT_TRUE(NULL == entry);
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
3193 TEST_F(DiskCacheBackendTest, SimpleCacheOverBlockfileCache) { 3167 TEST_F(DiskCacheBackendTest, SimpleCacheOverBlockfileCache) {
3194 // Create a cache structure with the |BackendImpl|. 3168 // Create a cache structure with the |BackendImpl|.
3195 InitCache(); 3169 InitCache();
3196 disk_cache::Entry* entry; 3170 disk_cache::Entry* entry;
3197 const int kSize = 50; 3171 const int kSize = 50;
3198 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 3172 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
3199 CacheTestFillBuffer(buffer->data(), kSize, false); 3173 CacheTestFillBuffer(buffer->data(), kSize, false);
3200 ASSERT_EQ(net::OK, CreateEntry("key", &entry)); 3174 ASSERT_EQ(net::OK, CreateEntry("key", &entry));
3201 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false)); 3175 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false));
3202 entry->Close(); 3176 entry->Close();
3203 delete cache_; 3177 cache_.reset();
3204 cache_ = NULL;
3205 3178
3206 // Check that the |SimpleBackendImpl| does not favor this structure. 3179 // Check that the |SimpleBackendImpl| does not favor this structure.
3207 base::Thread cache_thread("CacheThread"); 3180 base::Thread cache_thread("CacheThread");
3208 ASSERT_TRUE(cache_thread.StartWithOptions( 3181 ASSERT_TRUE(cache_thread.StartWithOptions(
3209 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); 3182 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
3210 disk_cache::SimpleBackendImpl* simple_cache = 3183 disk_cache::SimpleBackendImpl* simple_cache =
3211 new disk_cache::SimpleBackendImpl(cache_path_, 3184 new disk_cache::SimpleBackendImpl(cache_path_,
3212 0, 3185 0,
3213 net::DISK_CACHE, 3186 net::DISK_CACHE,
3214 cache_thread.message_loop_proxy().get(), 3187 cache_thread.message_loop_proxy().get(),
(...skipping 11 matching lines...) Expand all
3226 // Create a cache structure with the |SimpleBackendImpl|. 3199 // Create a cache structure with the |SimpleBackendImpl|.
3227 SetSimpleCacheMode(); 3200 SetSimpleCacheMode();
3228 InitCache(); 3201 InitCache();
3229 disk_cache::Entry* entry; 3202 disk_cache::Entry* entry;
3230 const int kSize = 50; 3203 const int kSize = 50;
3231 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 3204 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
3232 CacheTestFillBuffer(buffer->data(), kSize, false); 3205 CacheTestFillBuffer(buffer->data(), kSize, false);
3233 ASSERT_EQ(net::OK, CreateEntry("key", &entry)); 3206 ASSERT_EQ(net::OK, CreateEntry("key", &entry));
3234 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false)); 3207 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false));
3235 entry->Close(); 3208 entry->Close();
3236 delete cache_; 3209 cache_.reset();
3237 cache_ = NULL;
3238 3210
3239 // Check that the |BackendImpl| does not favor this structure. 3211 // Check that the |BackendImpl| does not favor this structure.
3240 base::Thread cache_thread("CacheThread"); 3212 base::Thread cache_thread("CacheThread");
3241 ASSERT_TRUE(cache_thread.StartWithOptions( 3213 ASSERT_TRUE(cache_thread.StartWithOptions(
3242 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); 3214 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
3243 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( 3215 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl(
3244 cache_path_, base::MessageLoopProxy::current().get(), NULL); 3216 cache_path_, base::MessageLoopProxy::current().get(), NULL);
3245 cache->SetUnitTestMode(); 3217 cache->SetUnitTestMode();
3246 net::TestCompletionCallback cb; 3218 net::TestCompletionCallback cb;
3247 int rv = cache->Init(cb.callback()); 3219 int rv = cache->Init(cb.callback());
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
3396 void* iter = NULL; 3368 void* iter = NULL;
3397 size_t count = 0; 3369 size_t count = 0;
3398 ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count)); 3370 ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count));
3399 cache_->EndEnumeration(&iter); 3371 cache_->EndEnumeration(&iter);
3400 3372
3401 EXPECT_EQ(key_pool.size(), count); 3373 EXPECT_EQ(key_pool.size(), count);
3402 EXPECT_TRUE(keys_to_match.empty()); 3374 EXPECT_TRUE(keys_to_match.empty());
3403 } 3375 }
3404 3376
3405 #endif // !defined(OS_WIN) 3377 #endif // !defined(OS_WIN)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698