Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/http/http_cache.h" | 5 #include "net/http/http_cache.h" |
| 6 | 6 |
| 7 #include "base/hash_tables.h" | 7 #include "base/hash_tables.h" |
| 8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 | 65 |
| 66 //----------------------------------------------------------------------------- | 66 //----------------------------------------------------------------------------- |
| 67 // mock disk cache (a very basic memory cache implementation) | 67 // mock disk cache (a very basic memory cache implementation) |
| 68 | 68 |
| 69 static const int kNumCacheEntryDataIndices = 3; | 69 static const int kNumCacheEntryDataIndices = 3; |
| 70 | 70 |
| 71 class MockDiskEntry : public disk_cache::Entry, | 71 class MockDiskEntry : public disk_cache::Entry, |
| 72 public base::RefCounted<MockDiskEntry> { | 72 public base::RefCounted<MockDiskEntry> { |
| 73 public: | 73 public: |
| 74 MockDiskEntry() | 74 MockDiskEntry() |
| 75 : test_mode_(0), doomed_(false), sparse_(false), fail_requests_(false), | 75 : test_mode_(0), rank_(0), doomed_(false), sparse_(false), |
| 76 busy_(false), delayed_(false) { | 76 fail_requests_(false), busy_(false), delayed_(false) { |
| 77 } | 77 } |
| 78 | 78 |
| 79 explicit MockDiskEntry(const std::string& key) | 79 explicit MockDiskEntry(const std::string& key) |
| 80 : key_(key), doomed_(false), sparse_(false), fail_requests_(false), | 80 : key_(key), rank_(0), doomed_(false), sparse_(false), |
| 81 busy_(false), delayed_(false) { | 81 fail_requests_(false), busy_(false), delayed_(false) { |
| 82 test_mode_ = GetTestModeForEntry(key); | 82 test_mode_ = GetTestModeForEntry(key); |
| 83 } | 83 } |
| 84 | 84 |
| 85 bool is_doomed() const { return doomed_; } | 85 bool is_doomed() const { return doomed_; } |
| 86 | 86 |
| 87 virtual void Doom() { | 87 virtual void Doom() { |
| 88 doomed_ = true; | 88 doomed_ = true; |
| 89 } | 89 } |
| 90 | 90 |
| 91 virtual void Close() { | 91 virtual void Close() { |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 DCHECK(completion_callback); | 272 DCHECK(completion_callback); |
| 273 if (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) | 273 if (GetEffectiveTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ) |
| 274 return net::OK; | 274 return net::OK; |
| 275 | 275 |
| 276 // The pending operation is already in the message loop (and hopefuly | 276 // The pending operation is already in the message loop (and hopefuly |
| 277 // already in the second pass). Just notify the caller that it finished. | 277 // already in the second pass). Just notify the caller that it finished. |
| 278 CallbackLater(completion_callback, 0); | 278 CallbackLater(completion_callback, 0); |
| 279 return net::ERR_IO_PENDING; | 279 return net::ERR_IO_PENDING; |
| 280 } | 280 } |
| 281 | 281 |
| 282 void UpdateRank() { | |
| 283 ++rank_; | |
|
rvargas (doing something else)
2011/07/28 22:27:01
I wouldn't actually bother with this. Now you are
| |
| 284 } | |
| 285 | |
| 286 int rank() const { return rank_; } | |
| 287 | |
| 282 // Fail most subsequent requests. | 288 // Fail most subsequent requests. |
| 283 void set_fail_requests() { fail_requests_ = true; } | 289 void set_fail_requests() { fail_requests_ = true; } |
| 284 | 290 |
| 285 // If |value| is true, don't deliver any completion callbacks until called | 291 // If |value| is true, don't deliver any completion callbacks until called |
| 286 // again with |value| set to false. Caution: remember to enable callbacks | 292 // again with |value| set to false. Caution: remember to enable callbacks |
| 287 // again or all subsequent tests will fail. | 293 // again or all subsequent tests will fail. |
| 288 static void IgnoreCallbacks(bool value) { | 294 static void IgnoreCallbacks(bool value) { |
| 289 if (ignore_callbacks_ == value) | 295 if (ignore_callbacks_ == value) |
| 290 return; | 296 return; |
| 291 ignore_callbacks_ = value; | 297 ignore_callbacks_ = value; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 346 CallbackInfo& c = callback_list[i]; | 352 CallbackInfo& c = callback_list[i]; |
| 347 c.entry->CallbackLater(c.callback, c.result); | 353 c.entry->CallbackLater(c.callback, c.result); |
| 348 } | 354 } |
| 349 callback_list.clear(); | 355 callback_list.clear(); |
| 350 } | 356 } |
| 351 } | 357 } |
| 352 | 358 |
| 353 std::string key_; | 359 std::string key_; |
| 354 std::vector<char> data_[kNumCacheEntryDataIndices]; | 360 std::vector<char> data_[kNumCacheEntryDataIndices]; |
| 355 int test_mode_; | 361 int test_mode_; |
| 362 int rank_; | |
| 356 bool doomed_; | 363 bool doomed_; |
| 357 bool sparse_; | 364 bool sparse_; |
| 358 bool fail_requests_; | 365 bool fail_requests_; |
| 359 bool busy_; | 366 bool busy_; |
| 360 bool delayed_; | 367 bool delayed_; |
| 361 static bool cancel_; | 368 static bool cancel_; |
| 362 static bool ignore_callbacks_; | 369 static bool ignore_callbacks_; |
| 363 }; | 370 }; |
| 364 | 371 |
| 365 // Statics. | 372 // Statics. |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 480 net::CompletionCallback* callback) { | 487 net::CompletionCallback* callback) { |
| 481 return net::ERR_NOT_IMPLEMENTED; | 488 return net::ERR_NOT_IMPLEMENTED; |
| 482 } | 489 } |
| 483 | 490 |
| 484 virtual void EndEnumeration(void** iter) {} | 491 virtual void EndEnumeration(void** iter) {} |
| 485 | 492 |
| 486 virtual void GetStats( | 493 virtual void GetStats( |
| 487 std::vector<std::pair<std::string, std::string> >* stats) { | 494 std::vector<std::pair<std::string, std::string> >* stats) { |
| 488 } | 495 } |
| 489 | 496 |
| 497 virtual void OnExternalCacheHit(const std::string& key) { | |
| 498 EntryMap::iterator it = entries_.find(key); | |
| 499 if (it != entries_.end()) { | |
| 500 it->second->UpdateRank(); | |
| 501 } | |
| 502 } | |
| 503 | |
| 490 // returns number of times a cache entry was successfully opened | 504 // returns number of times a cache entry was successfully opened |
| 491 int open_count() const { return open_count_; } | 505 int open_count() const { return open_count_; } |
| 492 | 506 |
| 493 // returns number of times a cache entry was successfully created | 507 // returns number of times a cache entry was successfully created |
| 494 int create_count() const { return create_count_; } | 508 int create_count() const { return create_count_; } |
| 495 | 509 |
| 496 // Fail any subsequent CreateEntry and OpenEntry. | 510 // Fail any subsequent CreateEntry and OpenEntry. |
| 497 void set_fail_requests() { fail_requests_ = true; } | 511 void set_fail_requests() { fail_requests_ = true; } |
| 498 | 512 |
| 499 // Return entries that fail some of their requests. | 513 // Return entries that fail some of their requests. |
| (...skipping 4592 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 5092 // Now return 200 when validating the entry so the metadata will be lost. | 5106 // Now return 200 when validating the entry so the metadata will be lost. |
| 5093 MockTransaction trans2(kTypicalGET_Transaction); | 5107 MockTransaction trans2(kTypicalGET_Transaction); |
| 5094 trans2.load_flags = net::LOAD_VALIDATE_CACHE; | 5108 trans2.load_flags = net::LOAD_VALIDATE_CACHE; |
| 5095 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); | 5109 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); |
| 5096 EXPECT_TRUE(response.metadata.get() == NULL); | 5110 EXPECT_TRUE(response.metadata.get() == NULL); |
| 5097 | 5111 |
| 5098 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 5112 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 5099 EXPECT_EQ(4, cache.disk_cache()->open_count()); | 5113 EXPECT_EQ(4, cache.disk_cache()->open_count()); |
| 5100 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 5114 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 5101 } | 5115 } |
| 5116 | |
| 5117 TEST(HttpCache, ExternalCacheHit_Success) { | |
| 5118 MockHttpCache cache; | |
| 5119 | |
| 5120 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
| 5121 | |
| 5122 disk_cache::Entry* entry; | |
| 5123 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | |
| 5124 MockDiskEntry* mock_entry = static_cast<MockDiskEntry*>(entry); | |
| 5125 EXPECT_EQ(0, mock_entry->rank()); | |
| 5126 | |
| 5127 cache.http_cache()->OnExternalCacheHit(GURL(kSimpleGET_Transaction.url), | |
| 5128 kSimpleGET_Transaction.method); | |
| 5129 | |
| 5130 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
| 5131 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
| 5132 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
| 5133 EXPECT_EQ(1, mock_entry->rank()); | |
| 5134 | |
| 5135 entry->Close(); | |
| 5136 } | |
| 5137 | |
| 5138 TEST(HttpCache, ExternalCacheHit_NoCache) { | |
| 5139 MockHttpCache cache; | |
| 5140 | |
| 5141 cache.http_cache()->OnExternalCacheHit(GURL(kSimpleGET_Transaction.url), | |
| 5142 kSimpleGET_Transaction.method); | |
| 5143 | |
| 5144 disk_cache::Entry* entry; | |
| 5145 EXPECT_FALSE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | |
| 5146 EXPECT_EQ(0, cache.network_layer()->transaction_count()); | |
| 5147 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
| 5148 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
| 5149 } | |
| 5150 | |
| 5151 TEST(HttpCache, ExternalCacheHit_NotInCache) { | |
| 5152 MockHttpCache cache; | |
| 5153 | |
| 5154 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
| 5155 | |
| 5156 disk_cache::Entry* entry; | |
| 5157 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | |
| 5158 MockDiskEntry* valid_entry = static_cast<MockDiskEntry*>(entry); | |
| 5159 | |
| 5160 cache.http_cache()->OnExternalCacheHit(GURL(kTypicalGET_Transaction.url), | |
| 5161 kSimpleGET_Transaction.method); | |
| 5162 | |
| 5163 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
| 5164 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
| 5165 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
| 5166 EXPECT_EQ(0, valid_entry->rank()); | |
| 5167 EXPECT_FALSE(cache.OpenBackendEntry(kTypicalGET_Transaction.url, &entry)); | |
| 5168 | |
| 5169 valid_entry->Close(); | |
| 5170 } | |
| OLD | NEW |