| Index: net/disk_cache/block_files_unittest.cc
|
| ===================================================================
|
| --- net/disk_cache/block_files_unittest.cc (revision 21550)
|
| +++ net/disk_cache/block_files_unittest.cc (working copy)
|
| @@ -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,13 +34,48 @@
|
| 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.
|
| TEST_F(DiskCacheTest, BlockFiles_Recover) {
|
| std::wstring path = GetCachePath();
|
|
|