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

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

Issue 1304363013: Add a size estimation mechanism to StoragePartitionHttpCacheDataRemover. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed compilation errors. Created 5 years, 2 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 <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/metrics/field_trial.h" 9 #include "base/metrics/field_trial.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 void BackendInvalidEntryRead(); 103 void BackendInvalidEntryRead();
104 void BackendInvalidEntryWithLoad(); 104 void BackendInvalidEntryWithLoad();
105 void BackendTrimInvalidEntry(); 105 void BackendTrimInvalidEntry();
106 void BackendTrimInvalidEntry2(); 106 void BackendTrimInvalidEntry2();
107 void BackendEnumerations(); 107 void BackendEnumerations();
108 void BackendEnumerations2(); 108 void BackendEnumerations2();
109 void BackendInvalidEntryEnumeration(); 109 void BackendInvalidEntryEnumeration();
110 void BackendFixEnumerators(); 110 void BackendFixEnumerators();
111 void BackendDoomRecent(); 111 void BackendDoomRecent();
112 void BackendDoomBetween(); 112 void BackendDoomBetween();
113 void BackendCalculateSizeOfAllEntries();
113 void BackendTransaction(const std::string& name, int num_entries, bool load); 114 void BackendTransaction(const std::string& name, int num_entries, bool load);
114 void BackendRecoverInsert(); 115 void BackendRecoverInsert();
115 void BackendRecoverRemove(); 116 void BackendRecoverRemove();
116 void BackendRecoverWithEviction(); 117 void BackendRecoverWithEviction();
117 void BackendInvalidEntry2(); 118 void BackendInvalidEntry2();
118 void BackendInvalidEntry3(); 119 void BackendInvalidEntry3();
119 void BackendInvalidEntry7(); 120 void BackendInvalidEntry7();
120 void BackendInvalidEntry8(); 121 void BackendInvalidEntry8();
121 void BackendInvalidEntry9(bool eviction); 122 void BackendInvalidEntry9(bool eviction);
122 void BackendInvalidEntry10(bool eviction); 123 void BackendInvalidEntry10(bool eviction);
(...skipping 1544 matching lines...) Expand 10 before | Expand all | Expand 10 after
1667 InitSparseCache(&start, &end); 1668 InitSparseCache(&start, &end);
1668 DoomEntriesBetween(start, end); 1669 DoomEntriesBetween(start, end);
1669 EXPECT_EQ(9, cache_->GetEntryCount()); 1670 EXPECT_EQ(9, cache_->GetEntryCount());
1670 1671
1671 start = end; 1672 start = end;
1672 end = base::Time::Now(); 1673 end = base::Time::Now();
1673 DoomEntriesBetween(start, end); 1674 DoomEntriesBetween(start, end);
1674 EXPECT_EQ(3, cache_->GetEntryCount()); 1675 EXPECT_EQ(3, cache_->GetEntryCount());
1675 } 1676 }
1676 1677
1678 void DiskCacheBackendTest::BackendCalculateSizeOfAllEntries() {
1679 InitCache();
1680
1681 // The cache is initially empty.
1682 if (memory_only_ || simple_cache_mode_) {
1683 // TODO(msramek): Implement.
1684 EXPECT_EQ(net::ERR_NOT_IMPLEMENTED, CalculateSizeOfAllEntries());
1685 } else {
1686 EXPECT_EQ(0, CalculateSizeOfAllEntries());
1687 }
1688
1689 // Generate random entries and populate them with data of respective
1690 // sizes 0, 1, ..., count - 1 bytes.
1691 std::set<std::string> key_pool;
1692 CreateSetOfRandomEntries(&key_pool);
1693
1694 int count = 0;
1695 for (std::string key : key_pool) {
1696 std::string data(count, ' ');
1697 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data);
1698
1699 // Alternate between writing to the first and second stream to test that
1700 // we are not taking just the first stream into account.
1701 disk_cache::Entry* entry;
1702 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
1703 ASSERT_EQ(count, WriteData(entry, count % 2, 0, buffer.get(), count, true));
1704 entry->Close();
1705
1706 ++count;
1707 }
1708
1709 // The resulting size should be (0 + 1 + ... + count - 1) plus keys.
1710 int result = CalculateSizeOfAllEntries();
1711 if (memory_only_ || simple_cache_mode_) {
1712 // TODO(msramek): Implement.
1713 EXPECT_EQ(net::ERR_NOT_IMPLEMENTED, result);
1714 } else {
1715 int total_key_size = 0;
1716 for (std::string key : key_pool)
1717 total_key_size += key.size();
1718
1719 EXPECT_EQ((count - 1) * count / 2 + total_key_size, result);
1720 }
1721
1722 // Add another entry and test if the size is updated. Then remove it and test
1723 // if the size is back to original value.
1724 {
1725 const int last_entry_size = 47;
1726 std::string data(last_entry_size, ' ');
1727 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data);
1728
1729 disk_cache::Entry* entry;
1730 std::string key = GenerateKey(true);
1731 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1732 ASSERT_EQ(last_entry_size,
1733 WriteData(entry, 0, 0, buffer.get(), last_entry_size, true));
1734 entry->Close();
1735
1736 int new_result = CalculateSizeOfAllEntries();
1737 if (memory_only_ || simple_cache_mode_) {
1738 // TODO(msramek): Implement.
1739 EXPECT_EQ(net::ERR_NOT_IMPLEMENTED, new_result);
1740 } else {
1741 EXPECT_EQ(result + last_entry_size + (int)key.size(), new_result);
rvargas (doing something else) 2015/09/30 17:04:41 nit: static_cast.
msramek 2015/10/06 10:55:29 Done.
1742 }
1743
1744 DoomEntry(key);
1745 new_result = CalculateSizeOfAllEntries();
1746 if (memory_only_ || simple_cache_mode_) {
1747 // TODO(msramek): Implement.
1748 EXPECT_EQ(net::ERR_NOT_IMPLEMENTED, new_result);
1749 } else {
1750 EXPECT_EQ(result, new_result);
1751 }
1752 }
1753
1754 // After dooming the entries, the size should be back to zero.
1755 ASSERT_EQ(net::OK, DoomAllEntries());
1756 if (memory_only_ || simple_cache_mode_) {
1757 // TODO(msramek): Implement.
1758 EXPECT_EQ(net::ERR_NOT_IMPLEMENTED, CalculateSizeOfAllEntries());
1759 } else {
1760 EXPECT_EQ(0, CalculateSizeOfAllEntries());
1761 }
1762 }
1763
1764 TEST_F(DiskCacheBackendTest, CalculateSizeOfAllEntries) {
1765 BackendCalculateSizeOfAllEntries();
1766 }
1767
1768 TEST_F(DiskCacheBackendTest, MemoryOnlyCalculateSizeOfAllEntries) {
1769 SetMemoryOnlyMode();
1770 BackendCalculateSizeOfAllEntries();
1771 }
1772
1773 TEST_F(DiskCacheBackendTest, SimpleCacheCalculateSizeOfAllEntries) {
1774 SetSimpleCacheMode();
1775 BackendCalculateSizeOfAllEntries();
1776 }
1777
1677 void DiskCacheBackendTest::BackendTransaction(const std::string& name, 1778 void DiskCacheBackendTest::BackendTransaction(const std::string& name,
1678 int num_entries, bool load) { 1779 int num_entries, bool load) {
1679 success_ = false; 1780 success_ = false;
1680 ASSERT_TRUE(CopyTestCache(name)); 1781 ASSERT_TRUE(CopyTestCache(name));
1681 DisableFirstCleanup(); 1782 DisableFirstCleanup();
1682 1783
1683 uint32 mask; 1784 uint32 mask;
1684 if (load) { 1785 if (load) {
1685 mask = 0xf; 1786 mask = 0xf;
1686 SetMaxSize(0x100000); 1787 SetMaxSize(0x100000);
(...skipping 1836 matching lines...) Expand 10 before | Expand all | Expand 10 after
3523 // after closing. 3624 // after closing.
3524 // NOTE: IF THIS TEST IS FLAKY THEN IT IS FAILING. See https://crbug.com/416940 3625 // NOTE: IF THIS TEST IS FLAKY THEN IT IS FAILING. See https://crbug.com/416940
3525 TEST_F(DiskCacheBackendTest, SimpleCacheDeleteQuickly) { 3626 TEST_F(DiskCacheBackendTest, SimpleCacheDeleteQuickly) {
3526 SetSimpleCacheMode(); 3627 SetSimpleCacheMode();
3527 for (int i = 0; i < 100; ++i) { 3628 for (int i = 0; i < 100; ++i) {
3528 InitCache(); 3629 InitCache();
3529 cache_.reset(); 3630 cache_.reset();
3530 EXPECT_TRUE(CleanupCacheDir()); 3631 EXPECT_TRUE(CleanupCacheDir());
3531 } 3632 }
3532 } 3633 }
OLDNEW
« no previous file with comments | « content/browser/cache_storage/cache_storage_cache_unittest.cc ('k') | net/disk_cache/blockfile/backend_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698