diff options
author | cbentzel@chromium.org <cbentzel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-25 15:30:35 +0000 |
---|---|---|
committer | cbentzel@chromium.org <cbentzel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-25 15:30:35 +0000 |
commit | 5968e672836c22ba0b81fbdfdd07737fe280fa5b (patch) | |
tree | a6baa4d45ce1dd4af22d97cf651cedf0f354b07c /net/disk_cache | |
parent | aaf7715233598a13688e7b94a4b6f22b97aa88ce (diff) | |
download | chromium_src-5968e672836c22ba0b81fbdfdd07737fe280fa5b.zip chromium_src-5968e672836c22ba0b81fbdfdd07737fe280fa5b.tar.gz chromium_src-5968e672836c22ba0b81fbdfdd07737fe280fa5b.tar.bz2 |
Revert 164064 - Cache backend for devices with flash storage
Fixed tests.
BUG=157187
TEST=net_unittests --gtest_filter="FlashCacheTest.*" --gtest_repeat=10 --shuffle
Review URL: https://chromiumcodereview.appspot.com/11269008
TBR=agayev@chromium.org
Review URL: https://codereview.chromium.org/11263041
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@164079 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/disk_cache')
-rw-r--r-- | net/disk_cache/flash/flash_cache_test_base.cc | 39 | ||||
-rw-r--r-- | net/disk_cache/flash/flash_cache_test_base.h | 36 | ||||
-rw-r--r-- | net/disk_cache/flash/segment.cc | 6 | ||||
-rw-r--r-- | net/disk_cache/flash/segment.h | 2 | ||||
-rw-r--r-- | net/disk_cache/flash/segment_unittest.cc | 217 | ||||
-rw-r--r-- | net/disk_cache/flash/storage_unittest.cc | 14 |
6 files changed, 136 insertions, 178 deletions
diff --git a/net/disk_cache/flash/flash_cache_test_base.cc b/net/disk_cache/flash/flash_cache_test_base.cc deleted file mode 100644 index bbe6c6f..0000000 --- a/net/disk_cache/flash/flash_cache_test_base.cc +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "net/disk_cache/flash/flash_cache_test_base.h" - -#include "base/file_path.h" -#include "base/scoped_temp_dir.h" -#include "base/time.h" -#include "net/disk_cache/flash/format.h" -#include "net/disk_cache/flash/storage.h" - -namespace { - -const int32 kSegmentCount = 10; -const FilePath::StringType kCachePath = FILE_PATH_LITERAL("cache"); - -} // namespace - -FlashCacheTest::FlashCacheTest() : num_segments_in_storage_(kSegmentCount) { - int seed = static_cast<int>(base::Time::Now().ToInternalValue()); - srand(seed); -} - -FlashCacheTest::~FlashCacheTest() { -} - -void FlashCacheTest::SetUp() { - ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); - const FilePath path(temp_dir_.path().Append(kCachePath)); - - int32 storage_size = num_segments_in_storage_ * disk_cache::kFlashSegmentSize; - storage_.reset(new disk_cache::Storage(path, storage_size)); - ASSERT_TRUE(storage_->Init()); -} - -void FlashCacheTest::TearDown() { - storage_.reset(); -} diff --git a/net/disk_cache/flash/flash_cache_test_base.h b/net/disk_cache/flash/flash_cache_test_base.h deleted file mode 100644 index 1c8bae1..0000000 --- a/net/disk_cache/flash/flash_cache_test_base.h +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef NET_DISK_CACHE_DISK_CACHE_FLASH_TEST_BASE_H_ -#define NET_DISK_CACHE_DISK_CACHE_FLASH_TEST_BASE_H_ - -#include "base/basictypes.h" -#include "base/compiler_specific.h" -#include "base/scoped_temp_dir.h" -#include "base/memory/scoped_ptr.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace disk_cache { - -class Storage; - -} // namespace disk_cache - -class FlashCacheTest : public testing::Test { - protected: - FlashCacheTest(); - virtual ~FlashCacheTest(); - - virtual void SetUp() OVERRIDE; - virtual void TearDown() OVERRIDE; - - scoped_ptr<disk_cache::Storage> storage_; - ScopedTempDir temp_dir_; - int32 num_segments_in_storage_; - - private: - DISALLOW_COPY_AND_ASSIGN(FlashCacheTest); -}; - -#endif // NET_DISK_CACHE_DISK_CACHE_FLASH_TEST_BASE_H_ diff --git a/net/disk_cache/flash/segment.cc b/net/disk_cache/flash/segment.cc index a0d277f..a63a5a3 100644 --- a/net/disk_cache/flash/segment.cc +++ b/net/disk_cache/flash/segment.cc @@ -18,19 +18,17 @@ Segment::Segment(int32 index, bool read_only, Storage* storage) write_offset_(offset_) { DCHECK(storage); DCHECK(storage->size() % kFlashSegmentSize == 0); + DCHECK(offset_ >= 0 && offset_ + kFlashSegmentSize <= storage->size()); } Segment::~Segment() { - DCHECK(!init_ || read_only_); + DCHECK(read_only_); } bool Segment::Init() { if (init_) return false; - if (offset_ < 0 || offset_ + kFlashSegmentSize > storage_->size()) - return false; - if (!read_only_) { init_ = true; return true; diff --git a/net/disk_cache/flash/segment.h b/net/disk_cache/flash/segment.h index 4a10c65..6dcdf46 100644 --- a/net/disk_cache/flash/segment.h +++ b/net/disk_cache/flash/segment.h @@ -97,6 +97,8 @@ class NET_EXPORT_PRIVATE Segment { bool CanHold(int32 size) const; private: + FRIEND_TEST_ALL_PREFIXES(SegmentTest, CreateDestroy); + bool read_only_; // Indicates whether the segment can be written to. bool init_; // Indicates whether segment was initialized. Storage* storage_; // Storage on which the segment resides. diff --git a/net/disk_cache/flash/segment_unittest.cc b/net/disk_cache/flash/segment_unittest.cc index bbbcc80..5bf3b6b 100644 --- a/net/disk_cache/flash/segment_unittest.cc +++ b/net/disk_cache/flash/segment_unittest.cc @@ -2,16 +2,27 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include <map> + +#include "base/logging.h" +#include "base/scoped_temp_dir.h" +#include "base/stl_util.h" #include "base/memory/scoped_ptr.h" +#include "net/base/net_errors.h" #include "net/disk_cache/disk_cache_test_util.h" #include "net/disk_cache/flash/segment.h" #include "net/disk_cache/flash/storage.h" -#include "net/disk_cache/flash/flash_cache_test_base.h" #include "net/disk_cache/flash/format.h" #include "testing/gtest/include/gtest/gtest.h" namespace { +const int kSegmentCount = 3; +const int kEntryCount = 10; +const int32 kStorageSize = disk_cache::kFlashSegmentSize * kSegmentCount; +const int32 kSegmentFreeSpace = disk_cache::kFlashSegmentSize - + disk_cache::kFlashSummarySize; + template<int SIZE> struct Entry { enum { size = SIZE }; @@ -31,110 +42,126 @@ const int32 kLargeEntrySize = disk_cache::kFlashSegmentSize / 4; typedef Entry<kSmallEntrySize> SmallEntry; typedef Entry<kLargeEntrySize> LargeEntry; -const int32 kSegmentFreeSpace = disk_cache::kFlashSegmentSize - - disk_cache::kFlashSummarySize; - } // namespace -TEST_F(FlashCacheTest, CreateDestroy) { - int32 index = 0; - scoped_ptr<disk_cache::Segment> segment( - new disk_cache::Segment(index, false, storage_.get())); - EXPECT_TRUE(segment->Init()); - EXPECT_TRUE(segment->Close()); - - index = num_segments_in_storage_ - 1; - segment.reset(new disk_cache::Segment(index, false, storage_.get())); - EXPECT_TRUE(segment->Init()); - EXPECT_TRUE(segment->Close()); - - int32 invalid_index = num_segments_in_storage_; - segment.reset(new disk_cache::Segment(invalid_index, false, storage_.get())); - EXPECT_FALSE(segment->Init()); - - invalid_index = -1; - segment.reset(new disk_cache::Segment(invalid_index, false, storage_.get())); - EXPECT_FALSE(segment->Init()); +class SegmentTest : public testing::Test { + protected: + virtual void SetUp() { + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); + const FilePath path(temp_dir_.path().Append(FILE_PATH_LITERAL("cache"))); + storage_.reset(new disk_cache::Storage(path, kStorageSize)); + ASSERT_TRUE(storage_->Init()); + } + + virtual void TearDown() { + storage_.reset(); + } + + scoped_ptr<disk_cache::Storage> storage_; + ScopedTempDir temp_dir_; +}; + +namespace disk_cache { + +TEST_F(SegmentTest, CreateDestroy) { + for (int i = 0; i < kSegmentCount; ++i) { + scoped_ptr<disk_cache::Segment> segment( + new disk_cache::Segment(i, false, storage_.get())); + + EXPECT_TRUE(segment->Init()); + // TODO(agayev): check offset validity via Init. + EXPECT_EQ(segment->offset_, disk_cache::kFlashSegmentSize * i); + EXPECT_EQ(segment->write_offset_, segment->offset_); + EXPECT_EQ(segment->summary_offset_, disk_cache::kFlashSegmentSize * (i+1) - + disk_cache::kFlashSummarySize); + EXPECT_TRUE(segment->Close()); + } } -TEST_F(FlashCacheTest, WriteDataReadData) { - int32 index = rand() % num_segments_in_storage_; - scoped_ptr<disk_cache::Segment> segment( - new disk_cache::Segment(index, false, storage_.get())); - - EXPECT_TRUE(segment->Init()); - SmallEntry entry1; - EXPECT_TRUE(segment->CanHold(entry1.size)); - int32 offset; - EXPECT_TRUE(segment->WriteData(entry1.data, entry1.size, &offset)); - EXPECT_TRUE(segment->Close()); - - segment.reset(new disk_cache::Segment(index, true, storage_.get())); - EXPECT_TRUE(segment->Init()); - SmallEntry entry2; - EXPECT_TRUE(segment->ReadData(entry2.data, entry2.size, offset)); - EXPECT_EQ(entry1, entry2); - EXPECT_TRUE(segment->Close()); +TEST_F(SegmentTest, WriteDataReadData) { + for (int i = 0; i < kSegmentCount; ++i) { + scoped_ptr<disk_cache::Segment> segment( + new disk_cache::Segment(i, false, storage_.get())); + + EXPECT_TRUE(segment->Init()); + SmallEntry entry1; + EXPECT_TRUE(segment->CanHold(entry1.size)); + int32 offset; + EXPECT_TRUE(segment->WriteData(entry1.data, entry1.size, &offset)); + EXPECT_TRUE(segment->Close()); + + segment.reset(new disk_cache::Segment(i, true, storage_.get())); + EXPECT_TRUE(segment->Init()); + SmallEntry entry2; + EXPECT_TRUE(segment->ReadData(entry2.data, entry2.size, offset)); + EXPECT_EQ(entry1, entry2); + EXPECT_TRUE(segment->Close()); + } } -TEST_F(FlashCacheTest, WriteHeaderReadData) { - int32 index = rand() % num_segments_in_storage_; - scoped_ptr<disk_cache::Segment> segment( - new disk_cache::Segment(index, false, storage_.get())); - - EXPECT_TRUE(segment->Init()); - SmallEntry entry1; - EXPECT_TRUE(segment->CanHold(entry1.size)); - int32 offset; - EXPECT_TRUE(segment->WriteHeader(entry1.data, entry1.size, &offset)); - EXPECT_EQ(1u, segment->header_offsets().size()); - EXPECT_EQ(offset, segment->header_offsets().front()); - EXPECT_TRUE(segment->Close()); - - segment.reset(new disk_cache::Segment(index, true, storage_.get())); - EXPECT_TRUE(segment->Init()); - SmallEntry entry2; - EXPECT_EQ(1u, segment->header_offsets().size()); - offset = segment->header_offsets().front(); - EXPECT_TRUE(segment->ReadData(entry2.data, entry2.size, offset)); - EXPECT_EQ(entry1, entry2); - EXPECT_TRUE(segment->Close()); +TEST_F(SegmentTest, WriteHeaderReadData) { + for (int i = 0; i < kSegmentCount; ++i) { + scoped_ptr<disk_cache::Segment> segment( + new disk_cache::Segment(i, false, storage_.get())); + + EXPECT_TRUE(segment->Init()); + SmallEntry entry1; + EXPECT_TRUE(segment->CanHold(entry1.size)); + int32 offset; + EXPECT_TRUE(segment->WriteHeader(entry1.data, entry1.size, &offset)); + EXPECT_EQ(1u, segment->header_offsets().size()); + EXPECT_EQ(offset, segment->header_offsets().front()); + EXPECT_TRUE(segment->Close()); + + segment.reset(new disk_cache::Segment(i, true, storage_.get())); + EXPECT_TRUE(segment->Init()); + SmallEntry entry2; + EXPECT_EQ(1u, segment->header_offsets().size()); + offset = segment->header_offsets().front(); + EXPECT_TRUE(segment->ReadData(entry2.data, entry2.size, offset)); + EXPECT_EQ(entry1, entry2); + EXPECT_TRUE(segment->Close()); + } } -TEST_F(FlashCacheTest, FillWithSmallEntries) { - int32 index = rand() % num_segments_in_storage_; - scoped_ptr<disk_cache::Segment> segment( - new disk_cache::Segment(index, false, storage_.get())); - - EXPECT_TRUE(segment->Init()); - SmallEntry entry; - int32 num_bytes_written = 0; - while (segment->CanHold(entry.size)) { - EXPECT_TRUE(segment->WriteHeader(entry.data, entry.size, NULL)); - num_bytes_written += entry.size; +TEST_F(SegmentTest, FillWithSmallEntries) { + for (int i = 0; i < kSegmentCount; ++i) { + scoped_ptr<disk_cache::Segment> segment( + new disk_cache::Segment(i, false, storage_.get())); + + EXPECT_TRUE(segment->Init()); + SmallEntry entry; + int32 num_bytes_written = 0; + while (segment->CanHold(entry.size)) { + EXPECT_TRUE(segment->WriteHeader(entry.data, entry.size, NULL)); + num_bytes_written += entry.size; + } + int32 space_left = kSegmentFreeSpace - num_bytes_written; + EXPECT_GE(space_left, entry.size); + EXPECT_EQ(segment->header_offsets().size(), + disk_cache::kFlashMaxEntryCount); + EXPECT_TRUE(segment->Close()); } - int32 space_left = kSegmentFreeSpace - num_bytes_written; - EXPECT_GE(space_left, entry.size); - EXPECT_EQ(segment->header_offsets().size(), - disk_cache::kFlashMaxEntryCount); - EXPECT_TRUE(segment->Close()); } -TEST_F(FlashCacheTest, FillWithLargeEntries) { - int32 index = rand() % num_segments_in_storage_; - scoped_ptr<disk_cache::Segment> segment( - new disk_cache::Segment(index, false, storage_.get())); - - EXPECT_TRUE(segment->Init()); - scoped_ptr<LargeEntry> entry(new LargeEntry); - int32 num_bytes_written = 0; - while (segment->CanHold(entry->size)) { - EXPECT_TRUE(segment->WriteHeader(entry->data, entry->size, NULL)); - num_bytes_written += entry->size; +TEST_F(SegmentTest, FillWithLargeEntries) { + for (int i = 0; i < kSegmentCount; ++i) { + scoped_ptr<disk_cache::Segment> segment( + new disk_cache::Segment(i, false, storage_.get())); + + EXPECT_TRUE(segment->Init()); + LargeEntry entry; + int32 num_bytes_written = 0; + while (segment->CanHold(entry.size)) { + EXPECT_TRUE(segment->WriteHeader(entry.data, entry.size, NULL)); + num_bytes_written += entry.size; + } + int32 space_left = kSegmentFreeSpace - num_bytes_written; + EXPECT_LT(space_left, entry.size); + EXPECT_LT(segment->header_offsets().size(), + disk_cache::kFlashMaxEntryCount); + EXPECT_TRUE(segment->Close()); } - int32 space_left = kSegmentFreeSpace - num_bytes_written; - EXPECT_LT(space_left, entry->size); - EXPECT_LT(segment->header_offsets().size(), - disk_cache::kFlashMaxEntryCount); - EXPECT_TRUE(segment->Close()); } + +} // namespace disk_cache diff --git a/net/disk_cache/flash/storage_unittest.cc b/net/disk_cache/flash/storage_unittest.cc index 2bacb11..73a78ef 100644 --- a/net/disk_cache/flash/storage_unittest.cc +++ b/net/disk_cache/flash/storage_unittest.cc @@ -7,7 +7,6 @@ #include "net/base/io_buffer.h" #include "net/disk_cache/disk_cache_test_util.h" #include "net/disk_cache/flash/storage.h" -#include "net/disk_cache/flash/flash_cache_test_base.h" #include "testing/gtest/include/gtest/gtest.h" namespace { @@ -18,7 +17,14 @@ const int32 kStorageSize = 16 * 1024 * 1024; } // namespace -TEST_F(FlashCacheTest, StorageReadWrite) { +TEST(FlashCacheTest, StorageReadWrite) { + ScopedTempDir tempdir; + ASSERT_TRUE(tempdir.CreateUniqueTempDir()); + const FilePath path(tempdir.path().Append(FILE_PATH_LITERAL("cache"))); + + disk_cache::Storage storage(path, kStorageSize); + EXPECT_TRUE(storage.Init()); + for (size_t i = 0; i < arraysize(kOffsets); ++i) { int32 size = kSizes[i]; int32 offset = kOffsets[i]; @@ -28,10 +34,10 @@ TEST_F(FlashCacheTest, StorageReadWrite) { CacheTestFillBuffer(write_buffer->data(), size, false); - bool rv = storage_->Write(write_buffer->data(), size, offset); + bool rv = storage.Write(write_buffer->data(), size, offset); EXPECT_TRUE(rv); - rv = storage_->Read(read_buffer->data(), size, offset); + rv = storage.Read(read_buffer->data(), size, offset); EXPECT_TRUE(rv); EXPECT_EQ(0, memcmp(read_buffer->data(), write_buffer->data(), size)); |