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

Unified 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: static_cast 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 side-by-side diff with in-line comments
Download patch
Index: net/disk_cache/backend_unittest.cc
diff --git a/net/disk_cache/backend_unittest.cc b/net/disk_cache/backend_unittest.cc
index d1d72a4084d6013eec9f3ed3f07d9e3b3e9f8070..8833729f6965ce3a78f837a9e08d97f08105d3a5 100644
--- a/net/disk_cache/backend_unittest.cc
+++ b/net/disk_cache/backend_unittest.cc
@@ -110,6 +110,7 @@ class DiskCacheBackendTest : public DiskCacheTestWithCache {
void BackendFixEnumerators();
void BackendDoomRecent();
void BackendDoomBetween();
+ void BackendCalculateSizeOfAllEntries();
void BackendTransaction(const std::string& name, int num_entries, bool load);
void BackendRecoverInsert();
void BackendRecoverRemove();
@@ -1674,6 +1675,107 @@ TEST_F(DiskCacheBackendTest, DoomEntriesBetweenSparse) {
EXPECT_EQ(3, cache_->GetEntryCount());
}
+void DiskCacheBackendTest::BackendCalculateSizeOfAllEntries() {
+ InitCache();
+
+ // The cache is initially empty.
+ if (memory_only_ || simple_cache_mode_) {
+ // TODO(msramek): Implement.
+ EXPECT_EQ(net::ERR_NOT_IMPLEMENTED, CalculateSizeOfAllEntries());
+ } else {
+ EXPECT_EQ(0, CalculateSizeOfAllEntries());
+ }
+
+ // Generate random entries and populate them with data of respective
+ // sizes 0, 1, ..., count - 1 bytes.
+ std::set<std::string> key_pool;
+ CreateSetOfRandomEntries(&key_pool);
+
+ int count = 0;
+ for (std::string key : key_pool) {
+ std::string data(count, ' ');
+ scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data);
+
+ // Alternate between writing to the first and second stream to test that
+ // we are not taking just the first stream into account.
+ disk_cache::Entry* entry;
+ ASSERT_EQ(net::OK, OpenEntry(key, &entry));
+ ASSERT_EQ(count, WriteData(entry, count % 2, 0, buffer.get(), count, true));
+ entry->Close();
+
+ ++count;
+ }
+
+ // The resulting size should be (0 + 1 + ... + count - 1) plus keys.
+ int result = CalculateSizeOfAllEntries();
+ if (memory_only_ || simple_cache_mode_) {
+ // TODO(msramek): Implement.
+ EXPECT_EQ(net::ERR_NOT_IMPLEMENTED, result);
+ } else {
+ int total_key_size = 0;
+ for (std::string key : key_pool)
+ total_key_size += key.size();
+
+ EXPECT_EQ((count - 1) * count / 2 + total_key_size, result);
+ }
+
+ // Add another entry and test if the size is updated. Then remove it and test
+ // if the size is back to original value.
+ {
+ const int last_entry_size = 47;
+ std::string data(last_entry_size, ' ');
+ scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data);
+
+ disk_cache::Entry* entry;
+ std::string key = GenerateKey(true);
+ ASSERT_EQ(net::OK, CreateEntry(key, &entry));
+ ASSERT_EQ(last_entry_size,
+ WriteData(entry, 0, 0, buffer.get(), last_entry_size, true));
+ entry->Close();
+
+ int new_result = CalculateSizeOfAllEntries();
+ if (memory_only_ || simple_cache_mode_) {
+ // TODO(msramek): Implement.
+ EXPECT_EQ(net::ERR_NOT_IMPLEMENTED, new_result);
+ } else {
+ EXPECT_EQ(result + last_entry_size + static_cast<int>(key.size()),
+ new_result);
+ }
+
+ DoomEntry(key);
+ new_result = CalculateSizeOfAllEntries();
+ if (memory_only_ || simple_cache_mode_) {
+ // TODO(msramek): Implement.
+ EXPECT_EQ(net::ERR_NOT_IMPLEMENTED, new_result);
+ } else {
+ EXPECT_EQ(result, new_result);
+ }
+ }
+
+ // After dooming the entries, the size should be back to zero.
+ ASSERT_EQ(net::OK, DoomAllEntries());
+ if (memory_only_ || simple_cache_mode_) {
+ // TODO(msramek): Implement.
+ EXPECT_EQ(net::ERR_NOT_IMPLEMENTED, CalculateSizeOfAllEntries());
+ } else {
+ EXPECT_EQ(0, CalculateSizeOfAllEntries());
+ }
+}
+
+TEST_F(DiskCacheBackendTest, CalculateSizeOfAllEntries) {
+ BackendCalculateSizeOfAllEntries();
+}
+
+TEST_F(DiskCacheBackendTest, MemoryOnlyCalculateSizeOfAllEntries) {
+ SetMemoryOnlyMode();
+ BackendCalculateSizeOfAllEntries();
+}
+
+TEST_F(DiskCacheBackendTest, SimpleCacheCalculateSizeOfAllEntries) {
+ SetSimpleCacheMode();
+ BackendCalculateSizeOfAllEntries();
+}
+
void DiskCacheBackendTest::BackendTransaction(const std::string& name,
int num_entries, bool load) {
success_ = false;
« 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