Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 55 net::CompletionCallback callback_; | 55 net::CompletionCallback callback_; |
| 56 | 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(DeleteCacheCompletionCallback); | 57 DISALLOW_COPY_AND_ASSIGN(DeleteCacheCompletionCallback); |
| 58 }; | 58 }; |
| 59 | 59 |
| 60 //----------------------------------------------------------------------------- | 60 //----------------------------------------------------------------------------- |
| 61 // helpers | 61 // helpers |
| 62 | 62 |
| 63 class TestHttpTransactionDelegate : public net::HttpTransactionDelegate { | 63 class TestHttpTransactionDelegate : public net::HttpTransactionDelegate { |
| 64 public: | 64 public: |
| 65 explicit TestHttpTransactionDelegate(int num_actions_to_observe) | 65 TestHttpTransactionDelegate(int num_cache_actions_to_observe, |
| 66 : num_remaining_actions_to_observe_(num_actions_to_observe), | 66 int num_network_actions_to_observe) |
| 67 action_in_progress_(false) { | 67 : num_remaining_cache_actions_to_observe_(num_cache_actions_to_observe), |
| 68 num_remaining_network_actions_to_observe_( | |
| 69 num_network_actions_to_observe), | |
| 70 cache_action_in_progress_(false), | |
| 71 network_action_in_progress_(false) { | |
| 68 } | 72 } |
| 69 virtual ~TestHttpTransactionDelegate() { | 73 virtual ~TestHttpTransactionDelegate() { |
| 70 EXPECT_EQ(0, num_remaining_actions_to_observe_); | 74 EXPECT_EQ(0, num_remaining_cache_actions_to_observe_); |
| 71 EXPECT_FALSE(action_in_progress_); | 75 EXPECT_EQ(0, num_remaining_network_actions_to_observe_); |
| 76 EXPECT_FALSE(cache_action_in_progress_); | |
| 77 EXPECT_FALSE(network_action_in_progress_); | |
| 72 } | 78 } |
| 73 virtual void OnCacheActionStart() { | 79 virtual void OnCacheActionStart() { |
| 74 EXPECT_FALSE(action_in_progress_); | 80 EXPECT_FALSE(cache_action_in_progress_); |
| 75 EXPECT_GT(num_remaining_actions_to_observe_, 0); | 81 EXPECT_FALSE(network_action_in_progress_); |
| 76 num_remaining_actions_to_observe_--; | 82 EXPECT_GT(num_remaining_cache_actions_to_observe_, 0); |
| 77 action_in_progress_ = true; | 83 num_remaining_cache_actions_to_observe_--; |
| 84 cache_action_in_progress_ = true; | |
| 78 } | 85 } |
| 79 virtual void OnCacheActionFinish() { | 86 virtual void OnCacheActionFinish() { |
| 80 EXPECT_TRUE(action_in_progress_); | 87 EXPECT_TRUE(cache_action_in_progress_); |
| 81 action_in_progress_ = false; | 88 cache_action_in_progress_ = false; |
| 89 } | |
| 90 virtual void OnNetworkActionStart() { | |
| 91 EXPECT_FALSE(cache_action_in_progress_); | |
| 92 EXPECT_FALSE(network_action_in_progress_); | |
| 93 EXPECT_GT(num_remaining_network_actions_to_observe_, 0); | |
| 94 num_remaining_network_actions_to_observe_--; | |
| 95 network_action_in_progress_ = true; | |
| 96 } | |
| 97 virtual void OnNetworkActionFinish() { | |
| 98 EXPECT_TRUE(network_action_in_progress_); | |
| 99 network_action_in_progress_ = false; | |
| 82 } | 100 } |
| 83 | 101 |
| 84 private: | 102 private: |
| 85 int num_remaining_actions_to_observe_; | 103 int num_remaining_cache_actions_to_observe_; |
| 86 bool action_in_progress_; | 104 int num_remaining_network_actions_to_observe_; |
| 105 bool cache_action_in_progress_; | |
| 106 bool network_action_in_progress_; | |
| 87 }; | 107 }; |
| 88 | 108 |
| 89 void ReadAndVerifyTransaction(net::HttpTransaction* trans, | 109 void ReadAndVerifyTransaction(net::HttpTransaction* trans, |
| 90 const MockTransaction& trans_info) { | 110 const MockTransaction& trans_info) { |
| 91 std::string content; | 111 std::string content; |
| 92 int rv = ReadTransaction(trans, &content); | 112 int rv = ReadTransaction(trans, &content); |
| 93 | 113 |
| 94 EXPECT_EQ(net::OK, rv); | 114 EXPECT_EQ(net::OK, rv); |
| 95 std::string expected(trans_info.data); | 115 std::string expected(trans_info.data); |
| 96 EXPECT_EQ(expected, content); | 116 EXPECT_EQ(expected, content); |
| 97 } | 117 } |
| 98 | 118 |
| 99 const int kNoDelegateTransactionCheck = -1; | 119 const int kNoDelegateTransactionCheck = -1; |
| 100 | 120 |
| 101 void RunTransactionTestWithRequestAndLogAndDelegate( | 121 void RunTransactionTestWithRequestAndLogAndDelegate( |
| 102 net::HttpCache* cache, | 122 net::HttpCache* cache, |
| 103 const MockTransaction& trans_info, | 123 const MockTransaction& trans_info, |
| 104 const MockHttpRequest& request, | 124 const MockHttpRequest& request, |
| 105 net::HttpResponseInfo* response_info, | 125 net::HttpResponseInfo* response_info, |
| 106 const net::BoundNetLog& net_log, | 126 const net::BoundNetLog& net_log, |
| 107 int num_delegate_actions) { | 127 int num_cache_delegate_actions, |
| 128 int num_network_delegate_actions) { | |
| 108 net::TestCompletionCallback callback; | 129 net::TestCompletionCallback callback; |
| 109 | 130 |
| 110 // write to the cache | 131 // write to the cache |
| 111 | 132 |
| 112 scoped_ptr<TestHttpTransactionDelegate> delegate; | 133 scoped_ptr<TestHttpTransactionDelegate> delegate; |
| 113 if (num_delegate_actions != kNoDelegateTransactionCheck) { | 134 if (num_cache_delegate_actions != kNoDelegateTransactionCheck && |
| 114 delegate.reset(new TestHttpTransactionDelegate(num_delegate_actions)); | 135 num_network_delegate_actions != kNoDelegateTransactionCheck) { |
| 136 delegate.reset( | |
| 137 new TestHttpTransactionDelegate(num_cache_delegate_actions, | |
| 138 num_network_delegate_actions)); | |
| 115 } | 139 } |
| 116 scoped_ptr<net::HttpTransaction> trans; | 140 scoped_ptr<net::HttpTransaction> trans; |
| 117 int rv = cache->CreateTransaction(&trans, delegate.get()); | 141 int rv = cache->CreateTransaction(&trans, delegate.get()); |
| 118 EXPECT_EQ(net::OK, rv); | 142 EXPECT_EQ(net::OK, rv); |
| 119 ASSERT_TRUE(trans.get()); | 143 ASSERT_TRUE(trans.get()); |
| 120 | 144 |
| 121 rv = trans->Start(&request, callback.callback(), net_log); | 145 rv = trans->Start(&request, callback.callback(), net_log); |
| 122 if (rv == net::ERR_IO_PENDING) | 146 if (rv == net::ERR_IO_PENDING) |
| 123 rv = callback.WaitForResult(); | 147 rv = callback.WaitForResult(); |
| 124 ASSERT_EQ(net::OK, rv); | 148 ASSERT_EQ(net::OK, rv); |
| 125 | 149 |
| 126 const net::HttpResponseInfo* response = trans->GetResponseInfo(); | 150 const net::HttpResponseInfo* response = trans->GetResponseInfo(); |
| 127 ASSERT_TRUE(response); | 151 ASSERT_TRUE(response); |
| 128 | 152 |
| 129 if (response_info) | 153 if (response_info) |
| 130 *response_info = *response; | 154 *response_info = *response; |
| 131 | 155 |
| 132 ReadAndVerifyTransaction(trans.get(), trans_info); | 156 ReadAndVerifyTransaction(trans.get(), trans_info); |
| 133 } | 157 } |
| 134 | 158 |
| 135 void RunTransactionTestWithRequest(net::HttpCache* cache, | 159 void RunTransactionTestWithRequest(net::HttpCache* cache, |
| 136 const MockTransaction& trans_info, | 160 const MockTransaction& trans_info, |
| 137 const MockHttpRequest& request, | 161 const MockHttpRequest& request, |
| 138 net::HttpResponseInfo* response_info) { | 162 net::HttpResponseInfo* response_info) { |
| 139 RunTransactionTestWithRequestAndLogAndDelegate( | 163 RunTransactionTestWithRequestAndLogAndDelegate( |
| 140 cache, trans_info, request, response_info, net::BoundNetLog(), | 164 cache, trans_info, request, response_info, net::BoundNetLog(), |
| 141 kNoDelegateTransactionCheck); | 165 kNoDelegateTransactionCheck, kNoDelegateTransactionCheck); |
| 142 } | 166 } |
| 143 | 167 |
| 144 void RunTransactionTestWithLog(net::HttpCache* cache, | 168 void RunTransactionTestWithLog(net::HttpCache* cache, |
| 145 const MockTransaction& trans_info, | 169 const MockTransaction& trans_info, |
| 146 const net::BoundNetLog& log) { | 170 const net::BoundNetLog& log) { |
| 147 RunTransactionTestWithRequestAndLogAndDelegate( | 171 RunTransactionTestWithRequestAndLogAndDelegate( |
| 148 cache, trans_info, MockHttpRequest(trans_info), NULL, log, | 172 cache, trans_info, MockHttpRequest(trans_info), NULL, log, |
| 149 kNoDelegateTransactionCheck); | 173 kNoDelegateTransactionCheck, kNoDelegateTransactionCheck); |
| 150 } | 174 } |
| 151 | 175 |
| 152 void RunTransactionTestWithDelegate(net::HttpCache* cache, | 176 void RunTransactionTestWithDelegate(net::HttpCache* cache, |
| 153 const MockTransaction& trans_info, | 177 const MockTransaction& trans_info, |
| 154 int num_delegate_actions) { | 178 int num_cache_delegate_actions, |
| 179 int num_network_delegate_actions) { | |
| 155 RunTransactionTestWithRequestAndLogAndDelegate( | 180 RunTransactionTestWithRequestAndLogAndDelegate( |
| 156 cache, trans_info, MockHttpRequest(trans_info), NULL, net::BoundNetLog(), | 181 cache, trans_info, MockHttpRequest(trans_info), NULL, net::BoundNetLog(), |
| 157 num_delegate_actions); | 182 num_cache_delegate_actions, num_network_delegate_actions); |
| 158 } | 183 } |
| 159 | 184 |
| 160 void RunTransactionTest(net::HttpCache* cache, | 185 void RunTransactionTest(net::HttpCache* cache, |
| 161 const MockTransaction& trans_info) { | 186 const MockTransaction& trans_info) { |
| 162 RunTransactionTestWithLog(cache, trans_info, net::BoundNetLog()); | 187 RunTransactionTestWithLog(cache, trans_info, net::BoundNetLog()); |
| 163 } | 188 } |
| 164 | 189 |
| 165 void RunTransactionTestWithResponseInfo(net::HttpCache* cache, | 190 void RunTransactionTestWithResponseInfo(net::HttpCache* cache, |
| 166 const MockTransaction& trans_info, | 191 const MockTransaction& trans_info, |
| 167 net::HttpResponseInfo* response) { | 192 net::HttpResponseInfo* response) { |
| (...skipping 3730 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3898 | 3923 |
| 3899 MockTransaction transaction(kSimpleGET_Transaction); | 3924 MockTransaction transaction(kSimpleGET_Transaction); |
| 3900 transaction.response_headers = | 3925 transaction.response_headers = |
| 3901 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | 3926 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
| 3902 "Content-Length: 22\n" | 3927 "Content-Length: 22\n" |
| 3903 "Etag: \"foopy\"\n"; | 3928 "Etag: \"foopy\"\n"; |
| 3904 AddMockTransaction(&transaction); | 3929 AddMockTransaction(&transaction); |
| 3905 MockHttpRequest request(transaction); | 3930 MockHttpRequest request(transaction); |
| 3906 | 3931 |
| 3907 scoped_ptr<Context> c(new Context()); | 3932 scoped_ptr<Context> c(new Context()); |
| 3908 int rv = cache.http_cache()->CreateTransaction(&c->trans, NULL); | 3933 // We use a test delegate to ensure that after initiating destruction |
| 3934 // of the transaction, no further delegate callbacks happen. | |
| 3935 // We initialize the TestHttpTransactionDelegate with the correct number of | |
| 3936 // cache actions and network actions to be reported. | |
| 3937 scoped_ptr<TestHttpTransactionDelegate> delegate( | |
| 3938 new TestHttpTransactionDelegate(7, 3)); | |
| 3939 int rv = cache.http_cache()->CreateTransaction(&c->trans, delegate.get()); | |
| 3909 EXPECT_EQ(net::OK, rv); | 3940 EXPECT_EQ(net::OK, rv); |
| 3910 | 3941 |
| 3911 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | 3942 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); |
| 3912 if (rv == net::ERR_IO_PENDING) | 3943 if (rv == net::ERR_IO_PENDING) |
| 3913 rv = c->callback.WaitForResult(); | 3944 rv = c->callback.WaitForResult(); |
| 3914 | 3945 |
| 3915 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 3946 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 3916 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3947 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 3917 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3948 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 3918 | 3949 |
| 3919 // Make sure that the entry has some data stored. | 3950 // Make sure that the entry has some data stored. |
| 3920 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10)); | 3951 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10)); |
| 3921 rv = c->trans->Read(buf, buf->size(), c->callback.callback()); | 3952 rv = c->trans->Read(buf, buf->size(), c->callback.callback()); |
| 3922 if (rv == net::ERR_IO_PENDING) | 3953 if (rv == net::ERR_IO_PENDING) |
| 3923 rv = c->callback.WaitForResult(); | 3954 rv = c->callback.WaitForResult(); |
| 3924 EXPECT_EQ(buf->size(), rv); | 3955 EXPECT_EQ(buf->size(), rv); |
| 3925 | 3956 |
| 3926 // We want to cancel the request when the transaction is busy. | 3957 // We want to cancel the request when the transaction is busy. |
| 3927 rv = c->trans->Read(buf, buf->size(), c->callback.callback()); | 3958 rv = c->trans->Read(buf, buf->size(), c->callback.callback()); |
| 3928 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 3959 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 3929 EXPECT_FALSE(c->callback.have_result()); | 3960 EXPECT_FALSE(c->callback.have_result()); |
| 3930 | 3961 |
| 3931 MockHttpCache::SetTestMode(TEST_MODE_SYNC_ALL); | 3962 MockHttpCache::SetTestMode(TEST_MODE_SYNC_ALL); |
| 3932 | 3963 |
| 3933 // Destroy the transaction. | 3964 // Destroy the transaction. |
| 3934 c->trans.reset(); | 3965 c->trans.reset(); |
| 3935 MockHttpCache::SetTestMode(0); | 3966 MockHttpCache::SetTestMode(0); |
| 3936 | 3967 |
| 3968 // Since the transaction was aborted in the middle of network I/O, we will | |
| 3969 // manually call the delegate so that its pending I/O operation will be | |
| 3970 // closed (which is what the test delegate is expecting). | |
| 3971 delegate->OnNetworkActionFinish(); | |
|
rvargas (doing something else)
2012/08/17 22:05:00
It's better to have a way to explicitly check that
tburkard
2012/08/17 22:39:56
Done.
| |
| 3972 | |
| 3937 // Make sure that we don't invoke the callback. We may have an issue if the | 3973 // Make sure that we don't invoke the callback. We may have an issue if the |
| 3938 // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we | 3974 // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we |
| 3939 // could end up with the transaction being deleted twice if we send any | 3975 // could end up with the transaction being deleted twice if we send any |
| 3940 // notification from the transaction destructor (see http://crbug.com/31723). | 3976 // notification from the transaction destructor (see http://crbug.com/31723). |
| 3941 EXPECT_FALSE(c->callback.have_result()); | 3977 EXPECT_FALSE(c->callback.have_result()); |
| 3942 | 3978 |
| 3943 // Verify that the entry is marked as incomplete. | 3979 // Verify that the entry is marked as incomplete. |
| 3944 disk_cache::Entry* entry; | 3980 disk_cache::Entry* entry; |
| 3945 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | 3981 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); |
| 3946 net::HttpResponseInfo response; | 3982 net::HttpResponseInfo response; |
| (...skipping 1069 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 5016 EXPECT_TRUE(truncated); | 5052 EXPECT_TRUE(truncated); |
| 5017 entry->Close(); | 5053 entry->Close(); |
| 5018 } | 5054 } |
| 5019 | 5055 |
| 5020 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit_TransactionDelegate) { | 5056 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit_TransactionDelegate) { |
| 5021 MockHttpCache cache; | 5057 MockHttpCache cache; |
| 5022 | 5058 |
| 5023 // Write to the cache. | 5059 // Write to the cache. |
| 5024 RunTransactionTestWithDelegate(cache.http_cache(), | 5060 RunTransactionTestWithDelegate(cache.http_cache(), |
| 5025 kSimpleGET_Transaction, | 5061 kSimpleGET_Transaction, |
| 5026 8); | 5062 8, |
| 5063 3); | |
| 5027 | 5064 |
| 5028 // Force this transaction to read from the cache. | 5065 // Force this transaction to read from the cache. |
| 5029 MockTransaction transaction(kSimpleGET_Transaction); | 5066 MockTransaction transaction(kSimpleGET_Transaction); |
| 5030 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | 5067 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 5031 | 5068 |
| 5032 RunTransactionTestWithDelegate(cache.http_cache(), | 5069 RunTransactionTestWithDelegate(cache.http_cache(), |
| 5033 kSimpleGET_Transaction, | 5070 kSimpleGET_Transaction, |
| 5034 5); | 5071 5, |
| 5072 0); | |
| 5035 } | 5073 } |
| OLD | NEW |