diff options
author | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-28 00:35:25 +0000 |
---|---|---|
committer | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-28 00:35:25 +0000 |
commit | 5c0767d1facaf222cd17956fdf40618173f46a0e (patch) | |
tree | 0a2d3c5895d40e65a12f71545cc640959a6c91fb /net/disk_cache/block_files_unittest.cc | |
parent | ad3d2542c02e8a3dba898d5bcf434d301b0862cf (diff) | |
download | chromium_src-5c0767d1facaf222cd17956fdf40618173f46a0e.zip chromium_src-5c0767d1facaf222cd17956fdf40618173f46a0e.tar.gz chromium_src-5c0767d1facaf222cd17956fdf40618173f46a0e.tar.bz2 |
Disk Cache: Delete chained block files when they become empty.
We were leaving empty block files in the chain, and worst
of all, not reusing them because we were thinking that these
files were "almost full". Now we also check for empty files
when the cache starts.
BUG=16740
TEST=unittest.
Review URL: http://codereview.chromium.org/159451
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21762 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/disk_cache/block_files_unittest.cc')
-rw-r--r-- | net/disk_cache/block_files_unittest.cc | 56 |
1 files changed, 53 insertions, 3 deletions
diff --git a/net/disk_cache/block_files_unittest.cc b/net/disk_cache/block_files_unittest.cc index 8e72820..ce76fd6 100644 --- a/net/disk_cache/block_files_unittest.cc +++ b/net/disk_cache/block_files_unittest.cc @@ -11,6 +11,21 @@ using base::Time; +namespace { + +// Returns the number of files in this folder. +int NumberOfFiles(const std::wstring path) { + file_util::FileEnumerator iter(FilePath::FromWStringHack(path), false, + file_util::FileEnumerator::FILES); + int count = 0; + for (FilePath file = iter.Next(); !file.value().empty(); file = iter.Next()) { + count++; + } + return count; +} + +} // namespace; + TEST_F(DiskCacheTest, BlockFiles_Grow) { std::wstring path = GetCachePath(); ASSERT_TRUE(DeleteCache(path.c_str())); @@ -19,11 +34,46 @@ TEST_F(DiskCacheTest, BlockFiles_Grow) { disk_cache::BlockFiles files(path); ASSERT_TRUE(files.Init(true)); + const int kMaxSize = 35000; + disk_cache::Addr address[kMaxSize]; + // Fill up the 32-byte block file (use three files). - for (int i = 0; i < 35000; i++) { - disk_cache::Addr address(0); - EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, 4, &address)); + for (int i = 0; i < kMaxSize; i++) { + EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, 4, &address[i])); + } + EXPECT_EQ(6, NumberOfFiles(path)); + + // Make sure we don't keep adding files. + for (int i = 0; i < kMaxSize * 4; i += 2) { + int target = i % kMaxSize; + files.DeleteBlock(address[target], false); + EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, 4, &address[target])); + } + EXPECT_EQ(6, NumberOfFiles(path)); +} + +// We should be able to delete empty block files. +TEST_F(DiskCacheTest, BlockFiles_Shrink) { + std::wstring path = GetCachePath(); + ASSERT_TRUE(DeleteCache(path.c_str())); + ASSERT_TRUE(file_util::CreateDirectory(path)); + + disk_cache::BlockFiles files(path); + ASSERT_TRUE(files.Init(true)); + + const int kMaxSize = 35000; + disk_cache::Addr address[kMaxSize]; + + // Fill up the 32-byte block file (use three files). + for (int i = 0; i < kMaxSize; i++) { + EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, 4, &address[i])); + } + + // Now delete all the blocks, so that we can delete the two extra files. + for (int i = 0; i < kMaxSize; i++) { + files.DeleteBlock(address[i], false); } + EXPECT_EQ(4, NumberOfFiles(path)); } // Handling of block files not properly closed. |