Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: net/http/http_cache_unittest.cc

Issue 10834313: Add histograms for network activity, and total/cumulative (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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_callbacks_observed_(0),
68 num_remaining_cache_actions_to_observe_(num_cache_actions_to_observe),
69 num_remaining_network_actions_to_observe_(
70 num_network_actions_to_observe),
71 cache_action_in_progress_(false),
72 network_action_in_progress_(false) {
68 } 73 }
69 virtual ~TestHttpTransactionDelegate() { 74 virtual ~TestHttpTransactionDelegate() {
70 EXPECT_EQ(0, num_remaining_actions_to_observe_); 75 EXPECT_EQ(0, num_remaining_cache_actions_to_observe_);
71 EXPECT_FALSE(action_in_progress_); 76 EXPECT_EQ(0, num_remaining_network_actions_to_observe_);
77 EXPECT_FALSE(cache_action_in_progress_);
78 EXPECT_FALSE(network_action_in_progress_);
72 } 79 }
73 virtual void OnCacheActionStart() { 80 virtual void OnCacheActionStart() {
74 EXPECT_FALSE(action_in_progress_); 81 num_callbacks_observed_++;
75 EXPECT_GT(num_remaining_actions_to_observe_, 0); 82 EXPECT_FALSE(cache_action_in_progress_);
76 num_remaining_actions_to_observe_--; 83 EXPECT_FALSE(network_action_in_progress_);
77 action_in_progress_ = true; 84 EXPECT_GT(num_remaining_cache_actions_to_observe_, 0);
85 num_remaining_cache_actions_to_observe_--;
86 cache_action_in_progress_ = true;
78 } 87 }
79 virtual void OnCacheActionFinish() { 88 virtual void OnCacheActionFinish() {
80 EXPECT_TRUE(action_in_progress_); 89 num_callbacks_observed_++;
81 action_in_progress_ = false; 90 EXPECT_TRUE(cache_action_in_progress_);
91 cache_action_in_progress_ = false;
92 }
93 virtual void OnNetworkActionStart() {
94 num_callbacks_observed_++;
95 EXPECT_FALSE(cache_action_in_progress_);
96 EXPECT_FALSE(network_action_in_progress_);
97 EXPECT_GT(num_remaining_network_actions_to_observe_, 0);
98 num_remaining_network_actions_to_observe_--;
99 network_action_in_progress_ = true;
100 }
101 virtual void OnNetworkActionFinish() {
mmenke 2012/08/20 16:15:45 nit: All these virtual functions should be OVERRI
tburkard 2012/08/20 19:23:39 Done.
102 num_callbacks_observed_++;
103 EXPECT_TRUE(network_action_in_progress_);
104 network_action_in_progress_ = false;
82 } 105 }
83 106
107 int num_callbacks_observed() { return num_callbacks_observed_; }
108
84 private: 109 private:
85 int num_remaining_actions_to_observe_; 110 int num_callbacks_observed_;
86 bool action_in_progress_; 111 int num_remaining_cache_actions_to_observe_;
112 int num_remaining_network_actions_to_observe_;
113 bool cache_action_in_progress_;
114 bool network_action_in_progress_;
87 }; 115 };
88 116
89 void ReadAndVerifyTransaction(net::HttpTransaction* trans, 117 void ReadAndVerifyTransaction(net::HttpTransaction* trans,
90 const MockTransaction& trans_info) { 118 const MockTransaction& trans_info) {
91 std::string content; 119 std::string content;
92 int rv = ReadTransaction(trans, &content); 120 int rv = ReadTransaction(trans, &content);
93 121
94 EXPECT_EQ(net::OK, rv); 122 EXPECT_EQ(net::OK, rv);
95 std::string expected(trans_info.data); 123 std::string expected(trans_info.data);
96 EXPECT_EQ(expected, content); 124 EXPECT_EQ(expected, content);
97 } 125 }
98 126
99 const int kNoDelegateTransactionCheck = -1; 127 const int kNoDelegateTransactionCheck = -1;
100 128
101 void RunTransactionTestWithRequestAndLogAndDelegate( 129 void RunTransactionTestWithRequestAndLogAndDelegate(
102 net::HttpCache* cache, 130 net::HttpCache* cache,
103 const MockTransaction& trans_info, 131 const MockTransaction& trans_info,
104 const MockHttpRequest& request, 132 const MockHttpRequest& request,
105 net::HttpResponseInfo* response_info, 133 net::HttpResponseInfo* response_info,
106 const net::BoundNetLog& net_log, 134 const net::BoundNetLog& net_log,
107 int num_delegate_actions) { 135 int num_cache_delegate_actions,
136 int num_network_delegate_actions) {
108 net::TestCompletionCallback callback; 137 net::TestCompletionCallback callback;
109 138
110 // write to the cache 139 // write to the cache
111 140
112 scoped_ptr<TestHttpTransactionDelegate> delegate; 141 scoped_ptr<TestHttpTransactionDelegate> delegate;
113 if (num_delegate_actions != kNoDelegateTransactionCheck) { 142 if (num_cache_delegate_actions != kNoDelegateTransactionCheck &&
114 delegate.reset(new TestHttpTransactionDelegate(num_delegate_actions)); 143 num_network_delegate_actions != kNoDelegateTransactionCheck) {
mmenke 2012/08/20 16:15:45 I suggest making it fail here when only one is kNo
tburkard 2012/08/20 19:23:39 Choosing to keep it the way it is.
144 delegate.reset(
145 new TestHttpTransactionDelegate(num_cache_delegate_actions,
146 num_network_delegate_actions));
115 } 147 }
116 scoped_ptr<net::HttpTransaction> trans; 148 scoped_ptr<net::HttpTransaction> trans;
117 int rv = cache->CreateTransaction(&trans, delegate.get()); 149 int rv = cache->CreateTransaction(&trans, delegate.get());
118 EXPECT_EQ(net::OK, rv); 150 EXPECT_EQ(net::OK, rv);
119 ASSERT_TRUE(trans.get()); 151 ASSERT_TRUE(trans.get());
120 152
121 rv = trans->Start(&request, callback.callback(), net_log); 153 rv = trans->Start(&request, callback.callback(), net_log);
122 if (rv == net::ERR_IO_PENDING) 154 if (rv == net::ERR_IO_PENDING)
123 rv = callback.WaitForResult(); 155 rv = callback.WaitForResult();
124 ASSERT_EQ(net::OK, rv); 156 ASSERT_EQ(net::OK, rv);
125 157
126 const net::HttpResponseInfo* response = trans->GetResponseInfo(); 158 const net::HttpResponseInfo* response = trans->GetResponseInfo();
127 ASSERT_TRUE(response); 159 ASSERT_TRUE(response);
128 160
129 if (response_info) 161 if (response_info)
130 *response_info = *response; 162 *response_info = *response;
131 163
132 ReadAndVerifyTransaction(trans.get(), trans_info); 164 ReadAndVerifyTransaction(trans.get(), trans_info);
133 } 165 }
134 166
135 void RunTransactionTestWithRequest(net::HttpCache* cache, 167 void RunTransactionTestWithRequest(net::HttpCache* cache,
136 const MockTransaction& trans_info, 168 const MockTransaction& trans_info,
137 const MockHttpRequest& request, 169 const MockHttpRequest& request,
138 net::HttpResponseInfo* response_info) { 170 net::HttpResponseInfo* response_info) {
139 RunTransactionTestWithRequestAndLogAndDelegate( 171 RunTransactionTestWithRequestAndLogAndDelegate(
140 cache, trans_info, request, response_info, net::BoundNetLog(), 172 cache, trans_info, request, response_info, net::BoundNetLog(),
141 kNoDelegateTransactionCheck); 173 kNoDelegateTransactionCheck, kNoDelegateTransactionCheck);
142 } 174 }
143 175
144 void RunTransactionTestWithLog(net::HttpCache* cache, 176 void RunTransactionTestWithLog(net::HttpCache* cache,
145 const MockTransaction& trans_info, 177 const MockTransaction& trans_info,
146 const net::BoundNetLog& log) { 178 const net::BoundNetLog& log) {
147 RunTransactionTestWithRequestAndLogAndDelegate( 179 RunTransactionTestWithRequestAndLogAndDelegate(
148 cache, trans_info, MockHttpRequest(trans_info), NULL, log, 180 cache, trans_info, MockHttpRequest(trans_info), NULL, log,
149 kNoDelegateTransactionCheck); 181 kNoDelegateTransactionCheck, kNoDelegateTransactionCheck);
150 } 182 }
151 183
152 void RunTransactionTestWithDelegate(net::HttpCache* cache, 184 void RunTransactionTestWithDelegate(net::HttpCache* cache,
153 const MockTransaction& trans_info, 185 const MockTransaction& trans_info,
154 int num_delegate_actions) { 186 int num_cache_delegate_actions,
187 int num_network_delegate_actions) {
155 RunTransactionTestWithRequestAndLogAndDelegate( 188 RunTransactionTestWithRequestAndLogAndDelegate(
156 cache, trans_info, MockHttpRequest(trans_info), NULL, net::BoundNetLog(), 189 cache, trans_info, MockHttpRequest(trans_info), NULL, net::BoundNetLog(),
157 num_delegate_actions); 190 num_cache_delegate_actions, num_network_delegate_actions);
158 } 191 }
159 192
160 void RunTransactionTest(net::HttpCache* cache, 193 void RunTransactionTest(net::HttpCache* cache,
161 const MockTransaction& trans_info) { 194 const MockTransaction& trans_info) {
162 RunTransactionTestWithLog(cache, trans_info, net::BoundNetLog()); 195 RunTransactionTestWithLog(cache, trans_info, net::BoundNetLog());
163 } 196 }
164 197
165 void RunTransactionTestWithResponseInfo(net::HttpCache* cache, 198 void RunTransactionTestWithResponseInfo(net::HttpCache* cache,
166 const MockTransaction& trans_info, 199 const MockTransaction& trans_info,
167 net::HttpResponseInfo* response) { 200 net::HttpResponseInfo* response) {
(...skipping 3730 matching lines...) Expand 10 before | Expand all | Expand 10 after
3898 3931
3899 MockTransaction transaction(kSimpleGET_Transaction); 3932 MockTransaction transaction(kSimpleGET_Transaction);
3900 transaction.response_headers = 3933 transaction.response_headers =
3901 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" 3934 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
3902 "Content-Length: 22\n" 3935 "Content-Length: 22\n"
3903 "Etag: \"foopy\"\n"; 3936 "Etag: \"foopy\"\n";
3904 AddMockTransaction(&transaction); 3937 AddMockTransaction(&transaction);
3905 MockHttpRequest request(transaction); 3938 MockHttpRequest request(transaction);
3906 3939
3907 scoped_ptr<Context> c(new Context()); 3940 scoped_ptr<Context> c(new Context());
3908 int rv = cache.http_cache()->CreateTransaction(&c->trans, NULL); 3941 // We use a test delegate to ensure that after initiating destruction
3942 // of the transaction, no further delegate callbacks happen.
3943 // We initialize the TestHttpTransactionDelegate with the correct number of
3944 // cache actions and network actions to be reported.
3945 scoped_ptr<TestHttpTransactionDelegate> delegate(
3946 new TestHttpTransactionDelegate(7, 3));
3947 int rv = cache.http_cache()->CreateTransaction(&c->trans, delegate.get());
3909 EXPECT_EQ(net::OK, rv); 3948 EXPECT_EQ(net::OK, rv);
3910 3949
3911 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); 3950 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog());
3912 if (rv == net::ERR_IO_PENDING) 3951 if (rv == net::ERR_IO_PENDING)
3913 rv = c->callback.WaitForResult(); 3952 rv = c->callback.WaitForResult();
3914 3953
3915 EXPECT_EQ(1, cache.network_layer()->transaction_count()); 3954 EXPECT_EQ(1, cache.network_layer()->transaction_count());
3916 EXPECT_EQ(0, cache.disk_cache()->open_count()); 3955 EXPECT_EQ(0, cache.disk_cache()->open_count());
3917 EXPECT_EQ(1, cache.disk_cache()->create_count()); 3956 EXPECT_EQ(1, cache.disk_cache()->create_count());
3918 3957
3919 // Make sure that the entry has some data stored. 3958 // Make sure that the entry has some data stored.
3920 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10)); 3959 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10));
3921 rv = c->trans->Read(buf, buf->size(), c->callback.callback()); 3960 rv = c->trans->Read(buf, buf->size(), c->callback.callback());
3922 if (rv == net::ERR_IO_PENDING) 3961 if (rv == net::ERR_IO_PENDING)
3923 rv = c->callback.WaitForResult(); 3962 rv = c->callback.WaitForResult();
3924 EXPECT_EQ(buf->size(), rv); 3963 EXPECT_EQ(buf->size(), rv);
3925 3964
3926 // We want to cancel the request when the transaction is busy. 3965 // We want to cancel the request when the transaction is busy.
3927 rv = c->trans->Read(buf, buf->size(), c->callback.callback()); 3966 rv = c->trans->Read(buf, buf->size(), c->callback.callback());
3928 EXPECT_EQ(net::ERR_IO_PENDING, rv); 3967 EXPECT_EQ(net::ERR_IO_PENDING, rv);
3929 EXPECT_FALSE(c->callback.have_result()); 3968 EXPECT_FALSE(c->callback.have_result());
3930 3969
3931 MockHttpCache::SetTestMode(TEST_MODE_SYNC_ALL); 3970 MockHttpCache::SetTestMode(TEST_MODE_SYNC_ALL);
3971 int num_delegate_callbacks_before_destruction =
3972 delegate->num_callbacks_observed();
3932 3973
3933 // Destroy the transaction. 3974 // Destroy the transaction.
3934 c->trans.reset(); 3975 c->trans.reset();
3935 MockHttpCache::SetTestMode(0); 3976 MockHttpCache::SetTestMode(0);
3936 3977
3978 // Ensure the delegate received no callbacks during destruction.
3979 EXPECT_EQ(num_delegate_callbacks_before_destruction,
3980 delegate->num_callbacks_observed());
3981
3982 // Since the transaction was aborted in the middle of network I/O, we will
3983 // manually call the delegate so that its pending I/O operation will be
3984 // closed (which is what the test delegate is expecting).
3985 delegate->OnNetworkActionFinish();
3986
3937 // Make sure that we don't invoke the callback. We may have an issue if the 3987 // 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 3988 // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we
3939 // could end up with the transaction being deleted twice if we send any 3989 // could end up with the transaction being deleted twice if we send any
3940 // notification from the transaction destructor (see http://crbug.com/31723). 3990 // notification from the transaction destructor (see http://crbug.com/31723).
3941 EXPECT_FALSE(c->callback.have_result()); 3991 EXPECT_FALSE(c->callback.have_result());
3942 3992
3943 // Verify that the entry is marked as incomplete. 3993 // Verify that the entry is marked as incomplete.
3944 disk_cache::Entry* entry; 3994 disk_cache::Entry* entry;
3945 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); 3995 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry));
3946 net::HttpResponseInfo response; 3996 net::HttpResponseInfo response;
(...skipping 1069 matching lines...) Expand 10 before | Expand all | Expand 10 after
5016 EXPECT_TRUE(truncated); 5066 EXPECT_TRUE(truncated);
5017 entry->Close(); 5067 entry->Close();
5018 } 5068 }
5019 5069
5020 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit_TransactionDelegate) { 5070 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit_TransactionDelegate) {
5021 MockHttpCache cache; 5071 MockHttpCache cache;
5022 5072
5023 // Write to the cache. 5073 // Write to the cache.
5024 RunTransactionTestWithDelegate(cache.http_cache(), 5074 RunTransactionTestWithDelegate(cache.http_cache(),
5025 kSimpleGET_Transaction, 5075 kSimpleGET_Transaction,
5026 8); 5076 8,
5077 3);
5027 5078
5028 // Force this transaction to read from the cache. 5079 // Force this transaction to read from the cache.
5029 MockTransaction transaction(kSimpleGET_Transaction); 5080 MockTransaction transaction(kSimpleGET_Transaction);
5030 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; 5081 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE;
5031 5082
5032 RunTransactionTestWithDelegate(cache.http_cache(), 5083 RunTransactionTestWithDelegate(cache.http_cache(),
5033 kSimpleGET_Transaction, 5084 kSimpleGET_Transaction,
5034 5); 5085 5,
5086 0);
5035 } 5087 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698