OLD | NEW |
1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2010 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/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/scoped_vector.h" | 9 #include "base/scoped_vector.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 bool MockDiskEntry::ignore_callbacks_ = false; | 362 bool MockDiskEntry::ignore_callbacks_ = false; |
363 | 363 |
364 class MockDiskCache : public disk_cache::Backend { | 364 class MockDiskCache : public disk_cache::Backend { |
365 public: | 365 public: |
366 MockDiskCache() | 366 MockDiskCache() |
367 : open_count_(0), create_count_(0), fail_requests_(false), | 367 : open_count_(0), create_count_(0), fail_requests_(false), |
368 soft_failures_(false) { | 368 soft_failures_(false) { |
369 } | 369 } |
370 | 370 |
371 ~MockDiskCache() { | 371 ~MockDiskCache() { |
372 EntryMap::iterator it = entries_.begin(); | 372 ReleaseAll(); |
373 for (; it != entries_.end(); ++it) | |
374 it->second->Release(); | |
375 } | 373 } |
376 | 374 |
377 virtual int32 GetEntryCount() const { | 375 virtual int32 GetEntryCount() const { |
378 return static_cast<int32>(entries_.size()); | 376 return static_cast<int32>(entries_.size()); |
379 } | 377 } |
380 | 378 |
381 virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry, | 379 virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry, |
382 net::CompletionCallback* callback) { | 380 net::CompletionCallback* callback) { |
383 DCHECK(callback); | 381 DCHECK(callback); |
384 if (fail_requests_) | 382 if (fail_requests_) |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 | 487 |
490 // returns number of times a cache entry was successfully created | 488 // returns number of times a cache entry was successfully created |
491 int create_count() const { return create_count_; } | 489 int create_count() const { return create_count_; } |
492 | 490 |
493 // Fail any subsequent CreateEntry and OpenEntry. | 491 // Fail any subsequent CreateEntry and OpenEntry. |
494 void set_fail_requests() { fail_requests_ = true; } | 492 void set_fail_requests() { fail_requests_ = true; } |
495 | 493 |
496 // Return entries that fail some of their requests. | 494 // Return entries that fail some of their requests. |
497 void set_soft_failures(bool value) { soft_failures_ = value; } | 495 void set_soft_failures(bool value) { soft_failures_ = value; } |
498 | 496 |
| 497 void ReleaseAll() { |
| 498 EntryMap::iterator it = entries_.begin(); |
| 499 for (; it != entries_.end(); ++it) |
| 500 it->second->Release(); |
| 501 entries_.clear(); |
| 502 } |
| 503 |
499 private: | 504 private: |
500 typedef base::hash_map<std::string, MockDiskEntry*> EntryMap; | 505 typedef base::hash_map<std::string, MockDiskEntry*> EntryMap; |
501 | 506 |
502 class CallbackRunner : public Task { | 507 class CallbackRunner : public Task { |
503 public: | 508 public: |
504 CallbackRunner(net::CompletionCallback* callback, int result) | 509 CallbackRunner(net::CompletionCallback* callback, int result) |
505 : callback_(callback), result_(result) {} | 510 : callback_(callback), result_(result) {} |
506 virtual void Run() { | 511 virtual void Run() { |
507 callback_->Run(result_); | 512 callback_->Run(result_); |
508 } | 513 } |
(...skipping 1204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1713 | 1718 |
1714 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1719 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1715 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1720 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1716 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 1721 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
1717 | 1722 |
1718 for (int i = 1; i < kNumTransactions; ++i) { | 1723 for (int i = 1; i < kNumTransactions; ++i) { |
1719 delete context_list[i]; | 1724 delete context_list[i]; |
1720 } | 1725 } |
1721 } | 1726 } |
1722 | 1727 |
| 1728 // Tests that we can cancel a single request to open a disk cache entry. |
| 1729 TEST(HttpCache, SimpleGET_CancelCreate) { |
| 1730 MockHttpCache cache; |
| 1731 |
| 1732 MockHttpRequest request(kSimpleGET_Transaction); |
| 1733 |
| 1734 Context* c = new Context(); |
| 1735 |
| 1736 c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 1737 EXPECT_EQ(net::OK, c->result); |
| 1738 |
| 1739 c->result = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
| 1740 EXPECT_EQ(net::ERR_IO_PENDING, c->result); |
| 1741 |
| 1742 // Release the reference that the mock disk cache keeps for this entry, so |
| 1743 // that we test that the http cache handles the cancelation correctly. |
| 1744 cache.disk_cache()->ReleaseAll(); |
| 1745 delete c; |
| 1746 |
| 1747 MessageLoop::current()->RunAllPending(); |
| 1748 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1749 } |
| 1750 |
1723 // Tests that we delete/create entries even if multiple requests are queued. | 1751 // Tests that we delete/create entries even if multiple requests are queued. |
1724 TEST(HttpCache, SimpleGET_ManyWriters_BypassCache) { | 1752 TEST(HttpCache, SimpleGET_ManyWriters_BypassCache) { |
1725 MockHttpCache cache; | 1753 MockHttpCache cache; |
1726 | 1754 |
1727 MockHttpRequest request(kSimpleGET_Transaction); | 1755 MockHttpRequest request(kSimpleGET_Transaction); |
1728 request.load_flags = net::LOAD_BYPASS_CACHE; | 1756 request.load_flags = net::LOAD_BYPASS_CACHE; |
1729 | 1757 |
1730 std::vector<Context*> context_list; | 1758 std::vector<Context*> context_list; |
1731 const int kNumTransactions = 5; | 1759 const int kNumTransactions = 5; |
1732 | 1760 |
(...skipping 2944 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4677 // Now return 200 when validating the entry so the metadata will be lost. | 4705 // Now return 200 when validating the entry so the metadata will be lost. |
4678 MockTransaction trans2(kTypicalGET_Transaction); | 4706 MockTransaction trans2(kTypicalGET_Transaction); |
4679 trans2.load_flags = net::LOAD_VALIDATE_CACHE; | 4707 trans2.load_flags = net::LOAD_VALIDATE_CACHE; |
4680 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); | 4708 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); |
4681 EXPECT_TRUE(response.metadata.get() == NULL); | 4709 EXPECT_TRUE(response.metadata.get() == NULL); |
4682 | 4710 |
4683 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 4711 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
4684 EXPECT_EQ(4, cache.disk_cache()->open_count()); | 4712 EXPECT_EQ(4, cache.disk_cache()->open_count()); |
4685 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 4713 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
4686 } | 4714 } |
OLD | NEW |