diff options
-rw-r--r-- | net/disk_cache/simple/simple_backend_impl.cc | 2 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_entry_impl.cc | 10 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_index.cc | 26 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_index.h | 15 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_index_unittest.cc | 260 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_test_util.cc | 2 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_test_util.h | 25 |
7 files changed, 182 insertions, 158 deletions
diff --git a/net/disk_cache/simple/simple_backend_impl.cc b/net/disk_cache/simple/simple_backend_impl.cc index 2877c01..4aed36c 100644 --- a/net/disk_cache/simple/simple_backend_impl.cc +++ b/net/disk_cache/simple/simple_backend_impl.cc @@ -380,7 +380,7 @@ void SimpleBackendImpl::GetStats( } void SimpleBackendImpl::OnExternalCacheHit(const std::string& key) { - index_->UseIfExists(key); + index_->UseIfExists(simple_util::GetEntryHashKey(key)); } void SimpleBackendImpl::InitializeIndex(const CompletionCallback& callback, diff --git a/net/disk_cache/simple/simple_entry_impl.cc b/net/disk_cache/simple/simple_entry_impl.cc index 3c3ec7d..1abe5b1 100644 --- a/net/disk_cache/simple/simple_entry_impl.cc +++ b/net/disk_cache/simple/simple_entry_impl.cc @@ -246,7 +246,7 @@ int SimpleEntryImpl::CreateEntry(Entry** out_entry, // have the entry in the index but we don't have the created files yet, this // way we never leak files. CreationOperationComplete will remove the entry // from the index if the creation fails. - backend_->index()->Insert(key_); + backend_->index()->Insert(entry_hash_); RunNextOperationIfNeeded(); return ret_value; @@ -536,7 +536,7 @@ void SimpleEntryImpl::RemoveSelfFromBackend() { void SimpleEntryImpl::MarkAsDoomed() { if (!backend_.get()) return; - backend_->index()->Remove(key_); + backend_->index()->Remove(entry_hash_); RemoveSelfFromBackend(); } @@ -780,7 +780,7 @@ void SimpleEntryImpl::ReadDataInternal(int stream_index, state_ = STATE_IO_PENDING; if (backend_.get()) - backend_->index()->UseIfExists(key_); + backend_->index()->UseIfExists(entry_hash_); scoped_ptr<uint32> read_crc32(new uint32()); scoped_ptr<int> result(new int()); @@ -840,7 +840,7 @@ void SimpleEntryImpl::WriteDataInternal(int stream_index, DCHECK_EQ(STATE_READY, state_); state_ = STATE_IO_PENDING; if (backend_.get()) - backend_->index()->UseIfExists(key_); + backend_->index()->UseIfExists(entry_hash_); // It is easy to incrementally compute the CRC from [0 .. |offset + buf_len|) // if |offset == 0| or we have already computed the CRC for [0 .. offset). // We rely on most write operations being sequential, start to end to compute @@ -1120,7 +1120,7 @@ void SimpleEntryImpl::UpdateDataFromEntryStat( data_size_[i] = entry_stat.data_size[i]; } if (backend_.get()) - backend_->index()->UpdateEntrySize(key_, GetDiskUsage()); + backend_->index()->UpdateEntrySize(entry_hash_, GetDiskUsage()); } int64 SimpleEntryImpl::GetDiskUsage() const { diff --git a/net/disk_cache/simple/simple_index.cc b/net/disk_cache/simple/simple_index.cc index caec19f60..f5c1270 100644 --- a/net/disk_cache/simple/simple_index.cc +++ b/net/disk_cache/simple/simple_index.cc @@ -199,30 +199,28 @@ int32 SimpleIndex::GetEntryCount() const { return entries_set_.size(); } -void SimpleIndex::Insert(const std::string& key) { +void SimpleIndex::Insert(uint64 entry_hash) { DCHECK(io_thread_checker_.CalledOnValidThread()); // Upon insert we don't know yet the size of the entry. // It will be updated later when the SimpleEntryImpl finishes opening or // creating the new entry, and then UpdateEntrySize will be called. - const uint64 hash_key = simple_util::GetEntryHashKey(key); InsertInEntrySet( - hash_key, EntryMetadata(base::Time::Now(), 0), &entries_set_); + entry_hash, EntryMetadata(base::Time::Now(), 0), &entries_set_); if (!initialized_) - removed_entries_.erase(hash_key); + removed_entries_.erase(entry_hash); PostponeWritingToDisk(); } -void SimpleIndex::Remove(const std::string& key) { +void SimpleIndex::Remove(uint64 entry_hash) { DCHECK(io_thread_checker_.CalledOnValidThread()); - const uint64 hash_key = simple_util::GetEntryHashKey(key); - EntrySet::iterator it = entries_set_.find(hash_key); + EntrySet::iterator it = entries_set_.find(entry_hash); if (it != entries_set_.end()) { UpdateEntryIteratorSize(&it, 0); entries_set_.erase(it); } if (!initialized_) - removed_entries_.insert(hash_key); + removed_entries_.insert(entry_hash); PostponeWritingToDisk(); } @@ -232,11 +230,11 @@ bool SimpleIndex::Has(uint64 hash) const { return !initialized_ || entries_set_.count(hash) > 0; } -bool SimpleIndex::UseIfExists(const std::string& key) { +bool SimpleIndex::UseIfExists(uint64 entry_hash) { DCHECK(io_thread_checker_.CalledOnValidThread()); // Always update the last used time, even if it is during initialization. // It will be merged later. - EntrySet::iterator it = entries_set_.find(simple_util::GetEntryHashKey(key)); + EntrySet::iterator it = entries_set_.find(entry_hash); if (it == entries_set_.end()) // If not initialized, always return true, forcing it to go to the disk. return !initialized_; @@ -292,9 +290,9 @@ void SimpleIndex::StartEvictionIfNeeded() { base::Bind(&SimpleIndex::EvictionDone, AsWeakPtr())); } -bool SimpleIndex::UpdateEntrySize(const std::string& key, uint64 entry_size) { +bool SimpleIndex::UpdateEntrySize(uint64 entry_hash, uint64 entry_size) { DCHECK(io_thread_checker_.CalledOnValidThread()); - EntrySet::iterator it = entries_set_.find(simple_util::GetEntryHashKey(key)); + EntrySet::iterator it = entries_set_.find(entry_hash); if (it == entries_set_.end()) return false; @@ -318,11 +316,11 @@ void SimpleIndex::EvictionDone(int result) { // static void SimpleIndex::InsertInEntrySet( - uint64 hash_key, + uint64 entry_hash, const disk_cache::EntryMetadata& entry_metadata, EntrySet* entry_set) { DCHECK(entry_set); - entry_set->insert(std::make_pair(hash_key, entry_metadata)); + entry_set->insert(std::make_pair(entry_hash, entry_metadata)); } void SimpleIndex::PostponeWritingToDisk() { diff --git a/net/disk_cache/simple/simple_index.h b/net/disk_cache/simple/simple_index.h index 788ffb2..9890e29 100644 --- a/net/disk_cache/simple/simple_index.h +++ b/net/disk_cache/simple/simple_index.h @@ -6,7 +6,6 @@ #define NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ #include <list> -#include <string> #include <vector> #include "base/basictypes.h" @@ -83,26 +82,26 @@ class NET_EXPORT_PRIVATE SimpleIndex bool SetMaxSize(int max_bytes); int max_size() const { return max_size_; } - void Insert(const std::string& key); - void Remove(const std::string& key); + void Insert(uint64 entry_hash); + void Remove(uint64 entry_hash); // Check whether the index has the entry given the hash of its key. - bool Has(uint64 hash) const; + bool Has(uint64 entry_hash) const; // Update the last used time of the entry with the given key and return true // iff the entry exist in the index. - bool UseIfExists(const std::string& key); + bool UseIfExists(uint64 entry_hash); void WriteToDisk(); // Update the size (in bytes) of an entry, in the metadata stored in the // index. This should be the total disk-file size including all streams of the // entry. - bool UpdateEntrySize(const std::string& key, uint64 entry_size); + bool UpdateEntrySize(uint64 entry_hash, uint64 entry_size); typedef base::hash_map<uint64, EntryMetadata> EntrySet; - static void InsertInEntrySet(uint64 hash_key, + static void InsertInEntrySet(uint64 entry_hash, const EntryMetadata& entry_metadata, EntrySet* entry_set); @@ -161,7 +160,7 @@ class NET_EXPORT_PRIVATE SimpleIndex bool eviction_in_progress_; base::TimeTicks eviction_start_time_; - // This stores all the hash_key of entries that are removed during + // This stores all the entry_hash of entries that are removed during // initialization. base::hash_set<uint64> removed_entries_; bool initialized_; diff --git a/net/disk_cache/simple/simple_index_unittest.cc b/net/disk_cache/simple/simple_index_unittest.cc index cfc83b3..9e34117 100644 --- a/net/disk_cache/simple/simple_index_unittest.cc +++ b/net/disk_cache/simple/simple_index_unittest.cc @@ -14,22 +14,28 @@ #include "base/time/time.h" #include "net/disk_cache/simple/simple_index.h" #include "net/disk_cache/simple/simple_index_file.h" +#include "net/disk_cache/simple/simple_test_util.h" #include "net/disk_cache/simple/simple_util.h" #include "testing/gtest/include/gtest/gtest.h" +namespace disk_cache { namespace { const int64 kTestLastUsedTimeInternal = 12345; const base::Time kTestLastUsedTime = base::Time::FromInternalValue(kTestLastUsedTimeInternal); const uint64 kTestEntrySize = 789; -const uint64 kKey1Hash = disk_cache::simple_util::GetEntryHashKey("key1"); -const uint64 kKey2Hash = disk_cache::simple_util::GetEntryHashKey("key2"); -const uint64 kKey3Hash = disk_cache::simple_util::GetEntryHashKey("key3"); + +uint64 HashesInitializer(size_t hash_index) { + return disk_cache::simple_util::GetEntryHashKey( + base::StringPrintf("key%zu", hash_index)); +} + +const simple_util::ImmutableArray<uint64, 8> kHashes( + base::Bind(&HashesInitializer)); } // namespace -namespace disk_cache { class EntryMetadataTest : public testing::Test { public: @@ -122,19 +128,17 @@ class SimpleIndexTest : public testing::Test { } // Redirect to allow single "friend" declaration in base class. - bool GetEntryForTesting(const std::string& key, EntryMetadata* metadata) { - const uint64 hash_key = simple_util::GetEntryHashKey(key); - SimpleIndex::EntrySet::iterator it = index_->entries_set_.find(hash_key); + bool GetEntryForTesting(uint64 key, EntryMetadata* metadata) { + SimpleIndex::EntrySet::iterator it = index_->entries_set_.find(key); if (index_->entries_set_.end() == it) return false; *metadata = it->second; return true; } - void InsertIntoIndexFileReturn(const std::string& key, + void InsertIntoIndexFileReturn(uint64 hash_key, base::Time last_used_time, uint64 entry_size) { - uint64 hash_key(simple_util::GetEntryHashKey(key)); index_file_->load_result()->entries.insert(std::make_pair( hash_key, EntryMetadata(last_used_time, entry_size))); } @@ -181,28 +185,27 @@ TEST_F(EntryMetadataTest, Serialize) { TEST_F(SimpleIndexTest, IndexSizeCorrectOnMerge) { typedef disk_cache::SimpleIndex::EntrySet EntrySet; index()->SetMaxSize(100); - index()->Insert("two"); - index()->UpdateEntrySize("two", 2); - index()->Insert("five"); - index()->UpdateEntrySize("five", 5); - index()->Insert("seven"); - index()->UpdateEntrySize("seven", 7); - EXPECT_EQ(14U, index()->cache_size_); + index()->Insert(kHashes.at<2>()); + index()->UpdateEntrySize(kHashes.at<2>(), 2); + index()->Insert(kHashes.at<3>()); + index()->UpdateEntrySize(kHashes.at<3>(), 3); + index()->Insert(kHashes.at<4>()); + index()->UpdateEntrySize(kHashes.at<4>(), 4); + EXPECT_EQ(9U, index()->cache_size_); { scoped_ptr<SimpleIndexLoadResult> result(new SimpleIndexLoadResult()); result->did_load = true; index()->MergeInitializingSet(result.Pass()); } - EXPECT_EQ(14U, index()->cache_size_); + EXPECT_EQ(9U, index()->cache_size_); { scoped_ptr<SimpleIndexLoadResult> result(new SimpleIndexLoadResult()); result->did_load = true; - const uint64 hash_key = simple_util::GetEntryHashKey("eleven"); result->entries.insert( - std::make_pair(hash_key, EntryMetadata(base::Time::Now(), 11))); + std::make_pair(kHashes.at<5>(), EntryMetadata(base::Time::Now(), 5))); index()->MergeInitializingSet(result.Pass()); } - EXPECT_EQ(25U, index()->cache_size_); + EXPECT_EQ(14U, index()->cache_size_); } // State of index changes as expected with an insert and a remove. @@ -213,8 +216,8 @@ TEST_F(SimpleIndexTest, BasicInsertRemove) { EXPECT_EQ(0ul, metadata.GetEntrySize()); // Confirm state after insert. - index()->Insert("key1"); - EXPECT_TRUE(GetEntryForTesting("key1", &metadata)); + index()->Insert(kHashes.at<1>()); + ASSERT_TRUE(GetEntryForTesting(kHashes.at<1>(), &metadata)); base::Time now(base::Time::Now()); EXPECT_LT(now - base::TimeDelta::FromMinutes(1), metadata.GetLastUsedTime()); EXPECT_GT(now + base::TimeDelta::FromMinutes(1), metadata.GetLastUsedTime()); @@ -222,8 +225,8 @@ TEST_F(SimpleIndexTest, BasicInsertRemove) { // Confirm state after remove. metadata = EntryMetadata(); - index()->Remove("key1"); - EXPECT_FALSE(GetEntryForTesting("key1", &metadata)); + index()->Remove(kHashes.at<1>()); + EXPECT_FALSE(GetEntryForTesting(kHashes.at<1>(), &metadata)); EXPECT_EQ(base::Time(), metadata.GetLastUsedTime()); EXPECT_EQ(0ul, metadata.GetEntrySize()); } @@ -234,20 +237,21 @@ TEST_F(SimpleIndexTest, Has) { EXPECT_EQ(1, index_file_->load_index_entries_calls()); // Confirm "Has()" always returns true before the callback is called. - EXPECT_TRUE(index()->Has(kKey1Hash)); - index()->Insert("key1"); - EXPECT_TRUE(index()->Has(kKey1Hash)); - index()->Remove("key1"); + const uint64 kHash1 = kHashes.at<1>(); + EXPECT_TRUE(index()->Has(kHash1)); + index()->Insert(kHash1); + EXPECT_TRUE(index()->Has(kHash1)); + index()->Remove(kHash1); // TODO(rdsmith): Maybe return false on explicitly removed entries? - EXPECT_TRUE(index()->Has(kKey1Hash)); + EXPECT_TRUE(index()->Has(kHash1)); ReturnIndexFile(); // Confirm "Has() returns conditionally now. - EXPECT_FALSE(index()->Has(kKey1Hash)); - index()->Insert("key1"); - EXPECT_TRUE(index()->Has(kKey1Hash)); - index()->Remove("key1"); + EXPECT_FALSE(index()->Has(kHash1)); + index()->Insert(kHash1); + EXPECT_TRUE(index()->Has(kHash1)); + index()->Remove(kHash1); } TEST_F(SimpleIndexTest, UseIfExists) { @@ -257,37 +261,38 @@ TEST_F(SimpleIndexTest, UseIfExists) { // Confirm "UseIfExists()" always returns true before the callback is called // and updates mod time if the entry was really there. + const uint64 kHash1 = kHashes.at<1>(); EntryMetadata metadata1, metadata2; - EXPECT_TRUE(index()->UseIfExists("key1")); - EXPECT_FALSE(GetEntryForTesting("key1", &metadata1)); - index()->Insert("key1"); - EXPECT_TRUE(index()->UseIfExists("key1")); - EXPECT_TRUE(GetEntryForTesting("key1", &metadata1)); + EXPECT_TRUE(index()->UseIfExists(kHash1)); + EXPECT_FALSE(GetEntryForTesting(kHash1, &metadata1)); + index()->Insert(kHash1); + EXPECT_TRUE(index()->UseIfExists(kHash1)); + EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata1)); WaitForTimeChange(); - EXPECT_TRUE(GetEntryForTesting("key1", &metadata2)); + EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata2)); EXPECT_EQ(metadata1.GetLastUsedTime(), metadata2.GetLastUsedTime()); - EXPECT_TRUE(index()->UseIfExists("key1")); - EXPECT_TRUE(GetEntryForTesting("key1", &metadata2)); + EXPECT_TRUE(index()->UseIfExists(kHash1)); + EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata2)); EXPECT_LT(metadata1.GetLastUsedTime(), metadata2.GetLastUsedTime()); - index()->Remove("key1"); - EXPECT_TRUE(index()->UseIfExists("key1")); + index()->Remove(kHash1); + EXPECT_TRUE(index()->UseIfExists(kHash1)); ReturnIndexFile(); // Confirm "UseIfExists() returns conditionally now - EXPECT_FALSE(index()->UseIfExists("key1")); - EXPECT_FALSE(GetEntryForTesting("key1", &metadata1)); - index()->Insert("key1"); - EXPECT_TRUE(index()->UseIfExists("key1")); - EXPECT_TRUE(GetEntryForTesting("key1", &metadata1)); + EXPECT_FALSE(index()->UseIfExists(kHash1)); + EXPECT_FALSE(GetEntryForTesting(kHash1, &metadata1)); + index()->Insert(kHash1); + EXPECT_TRUE(index()->UseIfExists(kHash1)); + EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata1)); WaitForTimeChange(); - EXPECT_TRUE(GetEntryForTesting("key1", &metadata2)); + EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata2)); EXPECT_EQ(metadata1.GetLastUsedTime(), metadata2.GetLastUsedTime()); - EXPECT_TRUE(index()->UseIfExists("key1")); - EXPECT_TRUE(GetEntryForTesting("key1", &metadata2)); + EXPECT_TRUE(index()->UseIfExists(kHash1)); + EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata2)); EXPECT_LT(metadata1.GetLastUsedTime(), metadata2.GetLastUsedTime()); - index()->Remove("key1"); - EXPECT_FALSE(index()->UseIfExists("key1")); + index()->Remove(kHash1); + EXPECT_FALSE(index()->UseIfExists(kHash1)); } TEST_F(SimpleIndexTest, UpdateEntrySize) { @@ -295,43 +300,42 @@ TEST_F(SimpleIndexTest, UpdateEntrySize) { index()->SetMaxSize(1000); - InsertIntoIndexFileReturn("key1", - now - base::TimeDelta::FromDays(2), - 475u); + const uint64 kHash1 = kHashes.at<1>(); + InsertIntoIndexFileReturn(kHash1, now - base::TimeDelta::FromDays(2), 475u); ReturnIndexFile(); EntryMetadata metadata; - EXPECT_TRUE(GetEntryForTesting("key1", &metadata)); + EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata)); EXPECT_EQ(now - base::TimeDelta::FromDays(2), metadata.GetLastUsedTime()); EXPECT_EQ(475u, metadata.GetEntrySize()); - index()->UpdateEntrySize("key1", 600u); - EXPECT_TRUE(GetEntryForTesting("key1", &metadata)); + index()->UpdateEntrySize(kHash1, 600u); + EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata)); EXPECT_EQ(600u, metadata.GetEntrySize()); EXPECT_EQ(1, index()->GetEntryCount()); } TEST_F(SimpleIndexTest, GetEntryCount) { EXPECT_EQ(0, index()->GetEntryCount()); - index()->Insert("key1"); + index()->Insert(kHashes.at<1>()); EXPECT_EQ(1, index()->GetEntryCount()); - index()->Insert("key2"); + index()->Insert(kHashes.at<2>()); EXPECT_EQ(2, index()->GetEntryCount()); - index()->Insert("key3"); + index()->Insert(kHashes.at<3>()); EXPECT_EQ(3, index()->GetEntryCount()); - index()->Insert("key3"); + index()->Insert(kHashes.at<3>()); EXPECT_EQ(3, index()->GetEntryCount()); - index()->Remove("key2"); + index()->Remove(kHashes.at<2>()); EXPECT_EQ(2, index()->GetEntryCount()); - index()->Insert("key4"); + index()->Insert(kHashes.at<4>()); EXPECT_EQ(3, index()->GetEntryCount()); - index()->Remove("key3"); + index()->Remove(kHashes.at<3>()); EXPECT_EQ(2, index()->GetEntryCount()); - index()->Remove("key3"); + index()->Remove(kHashes.at<3>()); EXPECT_EQ(2, index()->GetEntryCount()); - index()->Remove("key1"); + index()->Remove(kHashes.at<1>()); EXPECT_EQ(1, index()->GetEntryCount()); - index()->Remove("key4"); + index()->Remove(kHashes.at<4>()); EXPECT_EQ(0, index()->GetEntryCount()); } @@ -339,48 +343,50 @@ TEST_F(SimpleIndexTest, GetEntryCount) { TEST_F(SimpleIndexTest, BasicInit) { base::Time now(base::Time::Now()); - InsertIntoIndexFileReturn("key1", + InsertIntoIndexFileReturn(kHashes.at<1>(), now - base::TimeDelta::FromDays(2), 10u); - InsertIntoIndexFileReturn("key2", + InsertIntoIndexFileReturn(kHashes.at<2>(), now - base::TimeDelta::FromDays(3), 100u); ReturnIndexFile(); EntryMetadata metadata; - EXPECT_TRUE(GetEntryForTesting("key1", &metadata)); + EXPECT_TRUE(GetEntryForTesting(kHashes.at<1>(), &metadata)); EXPECT_EQ(now - base::TimeDelta::FromDays(2), metadata.GetLastUsedTime()); EXPECT_EQ(10ul, metadata.GetEntrySize()); - EXPECT_TRUE(GetEntryForTesting("key2", &metadata)); + EXPECT_TRUE(GetEntryForTesting(kHashes.at<2>(), &metadata)); EXPECT_EQ(now - base::TimeDelta::FromDays(3), metadata.GetLastUsedTime()); EXPECT_EQ(100ul, metadata.GetEntrySize()); } // Remove something that's going to come in from the loaded index. TEST_F(SimpleIndexTest, RemoveBeforeInit) { - index()->Remove("key1"); + const uint64 kHash1 = kHashes.at<1>(); + index()->Remove(kHash1); - InsertIntoIndexFileReturn("key1", + InsertIntoIndexFileReturn(kHash1, base::Time::Now() - base::TimeDelta::FromDays(2), 10u); ReturnIndexFile(); - EXPECT_FALSE(index()->Has(kKey1Hash)); + EXPECT_FALSE(index()->Has(kHash1)); } // Insert something that's going to come in from the loaded index; correct // result? TEST_F(SimpleIndexTest, InsertBeforeInit) { - index()->Insert("key1"); + const uint64 kHash1 = kHashes.at<1>(); + index()->Insert(kHash1); - InsertIntoIndexFileReturn("key1", + InsertIntoIndexFileReturn(kHash1, base::Time::Now() - base::TimeDelta::FromDays(2), 10u); ReturnIndexFile(); EntryMetadata metadata; - EXPECT_TRUE(GetEntryForTesting("key1", &metadata)); + EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata)); base::Time now(base::Time::Now()); EXPECT_LT(now - base::TimeDelta::FromMinutes(1), metadata.GetLastUsedTime()); EXPECT_GT(now + base::TimeDelta::FromMinutes(1), metadata.GetLastUsedTime()); @@ -389,29 +395,31 @@ TEST_F(SimpleIndexTest, InsertBeforeInit) { // Insert and Remove something that's going to come in from the loaded index. TEST_F(SimpleIndexTest, InsertRemoveBeforeInit) { - index()->Insert("key1"); - index()->Remove("key1"); + const uint64 kHash1 = kHashes.at<1>(); + index()->Insert(kHash1); + index()->Remove(kHash1); - InsertIntoIndexFileReturn("key1", + InsertIntoIndexFileReturn(kHash1, base::Time::Now() - base::TimeDelta::FromDays(2), 10u); ReturnIndexFile(); - EXPECT_FALSE(index()->Has(kKey1Hash)); + EXPECT_FALSE(index()->Has(kHash1)); } // Insert and Remove something that's going to come in from the loaded index. TEST_F(SimpleIndexTest, RemoveInsertBeforeInit) { - index()->Remove("key1"); - index()->Insert("key1"); + const uint64 kHash1 = kHashes.at<1>(); + index()->Remove(kHash1); + index()->Insert(kHash1); - InsertIntoIndexFileReturn("key1", + InsertIntoIndexFileReturn(kHash1, base::Time::Now() - base::TimeDelta::FromDays(2), 10u); ReturnIndexFile(); EntryMetadata metadata; - EXPECT_TRUE(GetEntryForTesting("key1", &metadata)); + EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata)); base::Time now(base::Time::Now()); EXPECT_LT(now - base::TimeDelta::FromMinutes(1), metadata.GetLastUsedTime()); EXPECT_GT(now + base::TimeDelta::FromMinutes(1), metadata.GetLastUsedTime()); @@ -423,46 +431,46 @@ TEST_F(SimpleIndexTest, RemoveInsertBeforeInit) { TEST_F(SimpleIndexTest, AllInitConflicts) { base::Time now(base::Time::Now()); - index()->Remove("key1"); - InsertIntoIndexFileReturn("key1", + index()->Remove(kHashes.at<1>()); + InsertIntoIndexFileReturn(kHashes.at<1>(), now - base::TimeDelta::FromDays(2), 10u); - index()->Insert("key2"); - InsertIntoIndexFileReturn("key2", + index()->Insert(kHashes.at<2>()); + InsertIntoIndexFileReturn(kHashes.at<2>(), now - base::TimeDelta::FromDays(3), 100u); - index()->Insert("key3"); - index()->Remove("key3"); - InsertIntoIndexFileReturn("key3", + index()->Insert(kHashes.at<3>()); + index()->Remove(kHashes.at<3>()); + InsertIntoIndexFileReturn(kHashes.at<3>(), now - base::TimeDelta::FromDays(4), 1000u); - index()->Remove("key4"); - index()->Insert("key4"); - InsertIntoIndexFileReturn("key4", + index()->Remove(kHashes.at<4>()); + index()->Insert(kHashes.at<4>()); + InsertIntoIndexFileReturn(kHashes.at<4>(), now - base::TimeDelta::FromDays(5), 10000u); - InsertIntoIndexFileReturn("key5", + InsertIntoIndexFileReturn(kHashes.at<5>(), now - base::TimeDelta::FromDays(6), 100000u); ReturnIndexFile(); - EXPECT_FALSE(index()->Has(kKey1Hash)); + EXPECT_FALSE(index()->Has(kHashes.at<1>())); EntryMetadata metadata; - EXPECT_TRUE(GetEntryForTesting("key2", &metadata)); + EXPECT_TRUE(GetEntryForTesting(kHashes.at<2>(), &metadata)); EXPECT_LT(now - base::TimeDelta::FromMinutes(1), metadata.GetLastUsedTime()); EXPECT_GT(now + base::TimeDelta::FromMinutes(1), metadata.GetLastUsedTime()); EXPECT_EQ(0ul, metadata.GetEntrySize()); - EXPECT_FALSE(index()->Has(kKey3Hash)); + EXPECT_FALSE(index()->Has(kHashes.at<3>())); - EXPECT_TRUE(GetEntryForTesting("key4", &metadata)); + EXPECT_TRUE(GetEntryForTesting(kHashes.at<4>(), &metadata)); EXPECT_LT(now - base::TimeDelta::FromMinutes(1), metadata.GetLastUsedTime()); EXPECT_GT(now + base::TimeDelta::FromMinutes(1), metadata.GetLastUsedTime()); EXPECT_EQ(0ul, metadata.GetEntrySize()); - EXPECT_TRUE(GetEntryForTesting("key5", &metadata)); + EXPECT_TRUE(GetEntryForTesting(kHashes.at<5>(), &metadata)); EXPECT_EQ(now - base::TimeDelta::FromDays(6), metadata.GetLastUsedTime()); EXPECT_EQ(100000u, metadata.GetEntrySize()); } @@ -470,33 +478,33 @@ TEST_F(SimpleIndexTest, AllInitConflicts) { TEST_F(SimpleIndexTest, BasicEviction) { base::Time now(base::Time::Now()); index()->SetMaxSize(1000); - InsertIntoIndexFileReturn("key1", + InsertIntoIndexFileReturn(kHashes.at<1>(), now - base::TimeDelta::FromDays(2), 475u); - index()->Insert("key2"); - index()->UpdateEntrySize("key2", 475); + index()->Insert(kHashes.at<2>()); + index()->UpdateEntrySize(kHashes.at<2>(), 475); ReturnIndexFile(); WaitForTimeChange(); - index()->Insert("key3"); + index()->Insert(kHashes.at<3>()); // Confirm index is as expected: No eviction, everything there. EXPECT_EQ(3, index()->GetEntryCount()); EXPECT_EQ(0, index_file()->doom_entry_set_calls()); - EXPECT_TRUE(index()->Has(kKey1Hash)); - EXPECT_TRUE(index()->Has(kKey2Hash)); - EXPECT_TRUE(index()->Has(kKey3Hash)); + EXPECT_TRUE(index()->Has(kHashes.at<1>())); + EXPECT_TRUE(index()->Has(kHashes.at<2>())); + EXPECT_TRUE(index()->Has(kHashes.at<3>())); // Trigger an eviction, and make sure the right things are tossed. // TODO(rdsmith): This is dependent on the innards of the implementation // as to at exactly what point we trigger eviction. Not sure how to fix // that. - index()->UpdateEntrySize("key3", 475); + index()->UpdateEntrySize(kHashes.at<3>(), 475); EXPECT_EQ(1, index_file()->doom_entry_set_calls()); EXPECT_EQ(1, index()->GetEntryCount()); - EXPECT_FALSE(index()->Has(kKey1Hash)); - EXPECT_FALSE(index()->Has(kKey2Hash)); - EXPECT_TRUE(index()->Has(kKey3Hash)); + EXPECT_FALSE(index()->Has(kHashes.at<1>())); + EXPECT_FALSE(index()->Has(kHashes.at<2>())); + EXPECT_TRUE(index()->Has(kHashes.at<3>())); ASSERT_EQ(2u, index_file_->last_doom_entry_hashes().size()); } @@ -508,20 +516,21 @@ TEST_F(SimpleIndexTest, DiskWriteQueued) { EXPECT_FALSE(index()->write_to_disk_timer_.IsRunning()); - index()->Insert("key1"); + const uint64 kHash1 = kHashes.at<1>(); + index()->Insert(kHash1); EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); index()->write_to_disk_timer_.Stop(); EXPECT_FALSE(index()->write_to_disk_timer_.IsRunning()); - index()->UseIfExists("key1"); + index()->UseIfExists(kHash1); EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); index()->write_to_disk_timer_.Stop(); - index()->UpdateEntrySize("key1", 20); + index()->UpdateEntrySize(kHash1, 20); EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); index()->write_to_disk_timer_.Stop(); - index()->Remove("key1"); + index()->Remove(kHash1); EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); index()->write_to_disk_timer_.Stop(); } @@ -532,8 +541,9 @@ TEST_F(SimpleIndexTest, DiskWriteExecuted) { EXPECT_FALSE(index()->write_to_disk_timer_.IsRunning()); - index()->Insert("key1"); - index()->UpdateEntrySize("key1", 20); + const uint64 kHash1 = kHashes.at<1>(); + index()->Insert(kHash1); + index()->UpdateEntrySize(kHash1, 20); EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); base::Closure user_task(index()->write_to_disk_timer_.user_task()); index()->write_to_disk_timer_.Stop(); @@ -544,7 +554,7 @@ TEST_F(SimpleIndexTest, DiskWriteExecuted) { SimpleIndex::EntrySet entry_set; index_file_->GetAndResetDiskWriteEntrySet(&entry_set); - uint64 hash_key(simple_util::GetEntryHashKey("key1")); + uint64 hash_key = kHash1; base::Time now(base::Time::Now()); ASSERT_EQ(1u, entry_set.size()); EXPECT_EQ(hash_key, entry_set.begin()->first); @@ -560,16 +570,16 @@ TEST_F(SimpleIndexTest, DiskWritePostponed) { EXPECT_FALSE(index()->write_to_disk_timer_.IsRunning()); - index()->Insert("key1"); - index()->UpdateEntrySize("key1", 20); + index()->Insert(kHashes.at<1>()); + index()->UpdateEntrySize(kHashes.at<1>(), 20); EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); base::TimeTicks expected_trigger( index()->write_to_disk_timer_.desired_run_time()); WaitForTimeChange(); EXPECT_EQ(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); - index()->Insert("key2"); - index()->UpdateEntrySize("key2", 40); + index()->Insert(kHashes.at<2>()); + index()->UpdateEntrySize(kHashes.at<2>(), 40); EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); EXPECT_LT(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); index()->write_to_disk_timer_.Stop(); diff --git a/net/disk_cache/simple/simple_test_util.cc b/net/disk_cache/simple/simple_test_util.cc index 483cbec..9f09974 100644 --- a/net/disk_cache/simple/simple_test_util.cc +++ b/net/disk_cache/simple/simple_test_util.cc @@ -8,7 +8,6 @@ #include "net/disk_cache/simple/simple_util.h" namespace disk_cache { - namespace simple_util { bool CreateCorruptFileForTests(const std::string& key, @@ -30,5 +29,4 @@ bool CreateCorruptFileForTests(const std::string& key, } } // namespace simple_backend - } // namespace disk_cache diff --git a/net/disk_cache/simple/simple_test_util.h b/net/disk_cache/simple/simple_test_util.h index 98c140b..82eebbe 100644 --- a/net/disk_cache/simple/simple_test_util.h +++ b/net/disk_cache/simple/simple_test_util.h @@ -8,22 +8,41 @@ #include <string> #include "base/basictypes.h" -#include "net/base/net_export.h" +#include "base/callback.h" namespace base { class FilePath; } namespace disk_cache { - namespace simple_util { +// Immutable array with compile-time bound-checking. +template <typename T, size_t Size> +class ImmutableArray { + public: + static const size_t size = Size; + + ImmutableArray(const base::Callback<T (size_t index)>& initializer) { + for (size_t i = 0; i < size; ++i) + data_[i] = initializer.Run(i); + } + + template <size_t Index> + const T& at() const { + COMPILE_ASSERT(Index < size, array_out_of_bounds); + return data_[Index]; + } + + private: + T data_[size]; +}; + // Creates a corrupt file to be used in tests. bool CreateCorruptFileForTests(const std::string& key, const base::FilePath& cache_path); } // namespace simple_backend - } // namespace disk_cache #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_TEST_UTIL_H_ |