Chromium Code Reviews| Index: net/http/http_cache_unittest.cc |
| diff --git a/net/http/http_cache_unittest.cc b/net/http/http_cache_unittest.cc |
| index 493c4e630f6fe9d37c757a0ff497d8d194077721..89aa7e1c80d4cc89ddb166370abaa4e83f0df979 100644 |
| --- a/net/http/http_cache_unittest.cc |
| +++ b/net/http/http_cache_unittest.cc |
| @@ -72,13 +72,13 @@ class MockDiskEntry : public disk_cache::Entry, |
| public base::RefCounted<MockDiskEntry> { |
| public: |
| MockDiskEntry() |
| - : test_mode_(0), doomed_(false), sparse_(false), fail_requests_(false), |
| - busy_(false), delayed_(false) { |
| + : test_mode_(0), rank_(0), doomed_(false), sparse_(false), |
| + fail_requests_(false), busy_(false), delayed_(false) { |
| } |
| explicit MockDiskEntry(const std::string& key) |
| - : key_(key), doomed_(false), sparse_(false), fail_requests_(false), |
| - busy_(false), delayed_(false) { |
| + : key_(key), rank_(0), doomed_(false), sparse_(false), |
| + fail_requests_(false), busy_(false), delayed_(false) { |
| test_mode_ = GetTestModeForEntry(key); |
| } |
| @@ -279,6 +279,12 @@ class MockDiskEntry : public disk_cache::Entry, |
| return net::ERR_IO_PENDING; |
| } |
| + void UpdateRank() { |
| + ++rank_; |
|
rvargas (doing something else)
2011/07/28 22:27:01
I wouldn't actually bother with this. Now you are
|
| + } |
| + |
| + int rank() const { return rank_; } |
| + |
| // Fail most subsequent requests. |
| void set_fail_requests() { fail_requests_ = true; } |
| @@ -353,6 +359,7 @@ class MockDiskEntry : public disk_cache::Entry, |
| std::string key_; |
| std::vector<char> data_[kNumCacheEntryDataIndices]; |
| int test_mode_; |
| + int rank_; |
| bool doomed_; |
| bool sparse_; |
| bool fail_requests_; |
| @@ -487,6 +494,13 @@ class MockDiskCache : public disk_cache::Backend { |
| std::vector<std::pair<std::string, std::string> >* stats) { |
| } |
| + virtual void OnExternalCacheHit(const std::string& key) { |
| + EntryMap::iterator it = entries_.find(key); |
| + if (it != entries_.end()) { |
| + it->second->UpdateRank(); |
| + } |
| + } |
| + |
| // returns number of times a cache entry was successfully opened |
| int open_count() const { return open_count_; } |
| @@ -5099,3 +5113,58 @@ TEST(HttpCache, ReadMetadata) { |
| EXPECT_EQ(4, cache.disk_cache()->open_count()); |
| EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| } |
| + |
| +TEST(HttpCache, ExternalCacheHit_Success) { |
| + MockHttpCache cache; |
| + |
| + RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| + |
| + disk_cache::Entry* entry; |
| + ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); |
| + MockDiskEntry* mock_entry = static_cast<MockDiskEntry*>(entry); |
| + EXPECT_EQ(0, mock_entry->rank()); |
| + |
| + cache.http_cache()->OnExternalCacheHit(GURL(kSimpleGET_Transaction.url), |
| + kSimpleGET_Transaction.method); |
| + |
| + EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| + EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| + EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| + EXPECT_EQ(1, mock_entry->rank()); |
| + |
| + entry->Close(); |
| +} |
| + |
| +TEST(HttpCache, ExternalCacheHit_NoCache) { |
| + MockHttpCache cache; |
| + |
| + cache.http_cache()->OnExternalCacheHit(GURL(kSimpleGET_Transaction.url), |
| + kSimpleGET_Transaction.method); |
| + |
| + disk_cache::Entry* entry; |
| + EXPECT_FALSE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); |
| + EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| + EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| + EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| +} |
| + |
| +TEST(HttpCache, ExternalCacheHit_NotInCache) { |
| + MockHttpCache cache; |
| + |
| + RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| + |
| + disk_cache::Entry* entry; |
| + ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); |
| + MockDiskEntry* valid_entry = static_cast<MockDiskEntry*>(entry); |
| + |
| + cache.http_cache()->OnExternalCacheHit(GURL(kTypicalGET_Transaction.url), |
| + kSimpleGET_Transaction.method); |
| + |
| + EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| + EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| + EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| + EXPECT_EQ(0, valid_entry->rank()); |
| + EXPECT_FALSE(cache.OpenBackendEntry(kTypicalGET_Transaction.url, &entry)); |
| + |
| + valid_entry->Close(); |
| +} |