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

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

Issue 2591743002: Implement a method to get the total size of a cache entry (Closed)
Patch Set: rebase Created 3 years, 11 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/files/file.h" 7 #include "base/files/file.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/metrics/field_trial.h" 10 #include "base/metrics/field_trial.h"
(...skipping 1787 matching lines...) Expand 10 before | Expand all | Expand 10 after
1798 void DiskCacheBackendTest::BackendCalculateSizeOfAllEntries() { 1798 void DiskCacheBackendTest::BackendCalculateSizeOfAllEntries() {
1799 InitCache(); 1799 InitCache();
1800 1800
1801 // The cache is initially empty. 1801 // The cache is initially empty.
1802 EXPECT_EQ(0, CalculateSizeOfAllEntries()); 1802 EXPECT_EQ(0, CalculateSizeOfAllEntries());
1803 1803
1804 // Generate random entries and populate them with data of respective 1804 // Generate random entries and populate them with data of respective
1805 // sizes 0, 1, ..., count - 1 bytes. 1805 // sizes 0, 1, ..., count - 1 bytes.
1806 std::set<std::string> key_pool; 1806 std::set<std::string> key_pool;
1807 CreateSetOfRandomEntries(&key_pool); 1807 CreateSetOfRandomEntries(&key_pool);
1808 // Keep track of the sum of the individual entry sizes and check that it is
1809 // consistent with CalculateSizeOfAllEntries().
1810 int64_t sum_entry_size = 0;
1808 1811
1809 int count = 0; 1812 int count = 0;
1810 for (std::string key : key_pool) { 1813 for (std::string key : key_pool) {
1811 std::string data(count, ' '); 1814 std::string data(count, ' ');
1812 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); 1815 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data);
1813 1816
1814 // Alternate between writing to first two streams to test that we do not 1817 // Alternate between writing to first two streams to test that we do not
1815 // take only one stream into account. 1818 // take only one stream into account.
1816 disk_cache::Entry* entry; 1819 disk_cache::Entry* entry;
1817 ASSERT_THAT(OpenEntry(key, &entry), IsOk()); 1820 ASSERT_THAT(OpenEntry(key, &entry), IsOk());
1818 ASSERT_EQ(count, WriteData(entry, count % 2, 0, buffer.get(), count, true)); 1821 ASSERT_EQ(count, WriteData(entry, count % 2, 0, buffer.get(), count, true));
1822 sum_entry_size += entry->GetEntrySize();
1819 entry->Close(); 1823 entry->Close();
1820 1824
1821 ++count; 1825 ++count;
1822 } 1826 }
1823 1827
1824 // The resulting size should be (0 + 1 + ... + count - 1) plus keys. 1828 // The resulting size should be (0 + 1 + ... + count - 1) plus keys.
1825 int result = CalculateSizeOfAllEntries(); 1829 int result = CalculateSizeOfAllEntries();
1826 int total_metadata_size = 0; 1830 int total_metadata_size = 0;
1827 for (std::string key : key_pool) 1831 for (std::string key : key_pool)
1828 total_metadata_size += GetEntryMetadataSize(key); 1832 total_metadata_size += GetEntryMetadataSize(key);
1829 EXPECT_EQ((count - 1) * count / 2 + total_metadata_size, result); 1833 EXPECT_EQ((count - 1) * count / 2 + total_metadata_size, result);
1834 EXPECT_EQ(sum_entry_size, result);
1830 1835
1831 // Add another entry and test if the size is updated. Then remove it and test 1836 // Add another entry and test if the size is updated. Then remove it and test
1832 // if the size is back to original value. 1837 // if the size is back to original value.
1833 { 1838 {
1834 const int last_entry_size = 47; 1839 const int last_entry_size = 47;
1835 std::string data(last_entry_size, ' '); 1840 std::string data(last_entry_size, ' ');
1836 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); 1841 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data);
1837 1842
1838 disk_cache::Entry* entry; 1843 disk_cache::Entry* entry;
1839 std::string key = GenerateKey(true); 1844 std::string key = GenerateKey(true);
1840 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); 1845 ASSERT_THAT(CreateEntry(key, &entry), IsOk());
1841 ASSERT_EQ(last_entry_size, 1846 ASSERT_EQ(last_entry_size,
1842 WriteData(entry, 0, 0, buffer.get(), last_entry_size, true)); 1847 WriteData(entry, 0, 0, buffer.get(), last_entry_size, true));
1848 ASSERT_EQ(last_entry_size + GetEntryMetadataSize(key),
1849 entry->GetEntrySize());
1843 entry->Close(); 1850 entry->Close();
1844 1851
1845 int new_result = CalculateSizeOfAllEntries(); 1852 int new_result = CalculateSizeOfAllEntries();
1846 EXPECT_EQ(result + last_entry_size + GetEntryMetadataSize(key), new_result); 1853 EXPECT_EQ(result + last_entry_size + GetEntryMetadataSize(key), new_result);
1847 1854
1848 DoomEntry(key); 1855 DoomEntry(key);
1849 new_result = CalculateSizeOfAllEntries(); 1856 new_result = CalculateSizeOfAllEntries();
1850 EXPECT_EQ(result, new_result); 1857 EXPECT_EQ(result, new_result);
1858 EXPECT_EQ(sum_entry_size, new_result);
1851 } 1859 }
1852 1860
1853 // After dooming the entries, the size should be back to zero. 1861 // After dooming the entries, the size should be back to zero.
1854 ASSERT_THAT(DoomAllEntries(), IsOk()); 1862 ASSERT_THAT(DoomAllEntries(), IsOk());
1855 EXPECT_EQ(0, CalculateSizeOfAllEntries()); 1863 EXPECT_EQ(0, CalculateSizeOfAllEntries());
1856 } 1864 }
1857 1865
1858 TEST_F(DiskCacheBackendTest, CalculateSizeOfAllEntries) { 1866 TEST_F(DiskCacheBackendTest, CalculateSizeOfAllEntries) {
1859 BackendCalculateSizeOfAllEntries(); 1867 BackendCalculateSizeOfAllEntries();
1860 } 1868 }
(...skipping 1995 matching lines...) Expand 10 before | Expand all | Expand 10 after
3856 // because that would advance the cache directory mtime and invalidate the 3864 // because that would advance the cache directory mtime and invalidate the
3857 // index. 3865 // index.
3858 entry2->Doom(); 3866 entry2->Doom();
3859 entry2->Close(); 3867 entry2->Close();
3860 3868
3861 DisableFirstCleanup(); 3869 DisableFirstCleanup();
3862 InitCache(); 3870 InitCache();
3863 EXPECT_EQ(disk_cache::SimpleIndex::INITIALIZE_METHOD_LOADED, 3871 EXPECT_EQ(disk_cache::SimpleIndex::INITIALIZE_METHOD_LOADED,
3864 simple_cache_impl_->index()->init_method()); 3872 simple_cache_impl_->index()->init_method());
3865 } 3873 }
OLDNEW
« no previous file with comments | « content/browser/blob_storage/blob_reader_unittest.cc ('k') | net/disk_cache/blockfile/entry_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698