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/url_request/url_request_throttler_manager.h" | 5 #include "net/url_request/url_request_throttler_manager.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/metrics/histogram.h" | |
9 #include "base/metrics/histogram_samples.h" | 8 #include "base/metrics/histogram_samples.h" |
10 #include "base/metrics/statistics_recorder.h" | 9 #include "base/metrics/statistics_recorder.h" |
11 #include "base/pickle.h" | 10 #include "base/pickle.h" |
12 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
13 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/test/statistics_delta_reader.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
17 #include "net/base/request_priority.h" | 17 #include "net/base/request_priority.h" |
18 #include "net/base/test_completion_callback.h" | 18 #include "net/base/test_completion_callback.h" |
19 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
20 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
21 #include "net/url_request/url_request_throttler_header_interface.h" | 21 #include "net/url_request/url_request_throttler_header_interface.h" |
22 #include "net/url_request/url_request_throttler_test_support.h" | 22 #include "net/url_request/url_request_throttler_test_support.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 | 24 |
25 using base::TimeDelta; | 25 using base::TimeDelta; |
26 using base::TimeTicks; | 26 using base::TimeTicks; |
27 | 27 |
28 namespace net { | 28 namespace net { |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 using base::Histogram; | 32 const char kRequestThrottledHistogramName[] = "Throttling.RequestThrottled"; |
33 using base::HistogramBase; | |
34 using base::HistogramSamples; | |
35 using base::StatisticsRecorder; | |
36 | 33 |
37 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { | 34 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { |
38 public: | 35 public: |
39 explicit MockURLRequestThrottlerEntry( | 36 explicit MockURLRequestThrottlerEntry( |
40 net::URLRequestThrottlerManager* manager) | 37 net::URLRequestThrottlerManager* manager) |
41 : net::URLRequestThrottlerEntry(manager, std::string()), | 38 : net::URLRequestThrottlerEntry(manager, std::string()), |
42 mock_backoff_entry_(&backoff_policy_) { | 39 mock_backoff_entry_(&backoff_policy_) { |
43 InitPolicy(); | 40 InitPolicy(); |
44 } | 41 } |
45 MockURLRequestThrottlerEntry( | 42 MockURLRequestThrottlerEntry( |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 int line; | 166 int line; |
170 }; | 167 }; |
171 | 168 |
172 } // namespace | 169 } // namespace |
173 | 170 |
174 class URLRequestThrottlerEntryTest : public testing::Test { | 171 class URLRequestThrottlerEntryTest : public testing::Test { |
175 protected: | 172 protected: |
176 URLRequestThrottlerEntryTest() | 173 URLRequestThrottlerEntryTest() |
177 : request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {} | 174 : request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {} |
178 | 175 |
| 176 static void SetUpTestCase() { |
| 177 base::StatisticsRecorder::Initialize(); |
| 178 } |
| 179 |
179 virtual void SetUp(); | 180 virtual void SetUp(); |
180 virtual void TearDown(); | |
181 | |
182 // After calling this function, histogram snapshots in |samples_| contain | |
183 // only the delta caused by the test case currently running. | |
184 void CalculateHistogramDeltas(); | |
185 | 181 |
186 TimeTicks now_; | 182 TimeTicks now_; |
187 MockURLRequestThrottlerManager manager_; // Dummy object, not used. | 183 MockURLRequestThrottlerManager manager_; // Dummy object, not used. |
188 scoped_refptr<MockURLRequestThrottlerEntry> entry_; | 184 scoped_refptr<MockURLRequestThrottlerEntry> entry_; |
189 | 185 |
190 std::map<std::string, HistogramSamples*> original_samples_; | |
191 std::map<std::string, HistogramSamples*> samples_; | |
192 | |
193 TestURLRequestContext context_; | 186 TestURLRequestContext context_; |
194 TestURLRequest request_; | 187 TestURLRequest request_; |
195 }; | 188 }; |
196 | 189 |
197 // List of all histograms we care about in these unit tests. | |
198 const char* kHistogramNames[] = { | |
199 "Throttling.FailureCountAtSuccess", | |
200 "Throttling.PerceivedDowntime", | |
201 "Throttling.RequestThrottled", | |
202 "Throttling.SiteOptedOut", | |
203 }; | |
204 | |
205 void URLRequestThrottlerEntryTest::SetUp() { | 190 void URLRequestThrottlerEntryTest::SetUp() { |
206 request_.SetLoadFlags(0); | 191 request_.SetLoadFlags(0); |
207 | 192 |
208 now_ = TimeTicks::Now(); | 193 now_ = TimeTicks::Now(); |
209 entry_ = new MockURLRequestThrottlerEntry(&manager_); | 194 entry_ = new MockURLRequestThrottlerEntry(&manager_); |
210 entry_->ResetToBlank(now_); | 195 entry_->ResetToBlank(now_); |
211 | |
212 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { | |
213 // Must retrieve original samples for each histogram for comparison | |
214 // as other tests may affect them. | |
215 const char* name = kHistogramNames[i]; | |
216 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); | |
217 if (histogram) { | |
218 original_samples_[name] = histogram->SnapshotSamples().release(); | |
219 } else { | |
220 original_samples_[name] = NULL; | |
221 } | |
222 } | |
223 } | |
224 | |
225 void URLRequestThrottlerEntryTest::TearDown() { | |
226 STLDeleteValues(&original_samples_); | |
227 STLDeleteValues(&samples_); | |
228 } | |
229 | |
230 void URLRequestThrottlerEntryTest::CalculateHistogramDeltas() { | |
231 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { | |
232 const char* name = kHistogramNames[i]; | |
233 HistogramSamples* original = original_samples_[name]; | |
234 | |
235 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); | |
236 if (histogram) { | |
237 ASSERT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags()); | |
238 | |
239 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); | |
240 if (original) | |
241 samples->Subtract(*original); | |
242 samples_[name] = samples.release(); | |
243 } | |
244 } | |
245 | |
246 // Ensure we don't accidentally use the originals in our tests. | |
247 STLDeleteValues(&original_samples_); | |
248 original_samples_.clear(); | |
249 } | 196 } |
250 | 197 |
251 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { | 198 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { |
252 return out << time.ToInternalValue(); | 199 return out << time.ToInternalValue(); |
253 } | 200 } |
254 | 201 |
255 TEST_F(URLRequestThrottlerEntryTest, CanThrottleRequest) { | 202 TEST_F(URLRequestThrottlerEntryTest, CanThrottleRequest) { |
256 TestNetworkDelegate d; | 203 TestNetworkDelegate d; |
257 context_.set_network_delegate(&d); | 204 context_.set_network_delegate(&d); |
258 entry_->set_exponential_backoff_release_time( | 205 entry_->set_exponential_backoff_release_time( |
259 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); | 206 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); |
260 | 207 |
261 d.set_can_throttle_requests(false); | 208 d.set_can_throttle_requests(false); |
262 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 209 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
263 d.set_can_throttle_requests(true); | 210 d.set_can_throttle_requests(true); |
264 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); | 211 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); |
265 } | 212 } |
266 | 213 |
267 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { | 214 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { |
| 215 base::StatisticsDeltaReader statistics_delta_reader; |
268 entry_->set_exponential_backoff_release_time( | 216 entry_->set_exponential_backoff_release_time( |
269 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); | 217 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); |
270 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); | 218 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); |
271 | 219 |
272 // Also end-to-end test the load flags exceptions. | 220 // Also end-to-end test the load flags exceptions. |
273 request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE); | 221 request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE); |
274 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 222 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
275 | 223 |
276 CalculateHistogramDeltas(); | 224 scoped_ptr<base::HistogramSamples> samples( |
277 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(0)); | 225 statistics_delta_reader.GetHistogramSamplesSinceCreation( |
278 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(1)); | 226 kRequestThrottledHistogramName)); |
| 227 ASSERT_EQ(1, samples->GetCount(0)); |
| 228 ASSERT_EQ(1, samples->GetCount(1)); |
279 } | 229 } |
280 | 230 |
281 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { | 231 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { |
| 232 base::StatisticsDeltaReader statistics_delta_reader; |
282 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); | 233 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); |
283 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 234 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
284 entry_->set_exponential_backoff_release_time( | 235 entry_->set_exponential_backoff_release_time( |
285 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); | 236 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); |
286 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 237 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
287 | 238 |
288 CalculateHistogramDeltas(); | 239 scoped_ptr<base::HistogramSamples> samples( |
289 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"]->GetCount(0)); | 240 statistics_delta_reader.GetHistogramSamplesSinceCreation( |
290 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"]->GetCount(1)); | 241 kRequestThrottledHistogramName)); |
| 242 ASSERT_EQ(2, samples->GetCount(0)); |
| 243 ASSERT_EQ(0, samples->GetCount(1)); |
291 } | 244 } |
292 | 245 |
293 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { | 246 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { |
294 MockURLRequestThrottlerHeaderAdapter failure_response(503); | 247 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
295 entry_->UpdateWithResponse(std::string(), &failure_response); | 248 entry_->UpdateWithResponse(std::string(), &failure_response); |
296 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 249 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
297 << "A failure should increase the release_time"; | 250 << "A failure should increase the release_time"; |
298 } | 251 } |
299 | 252 |
300 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { | 253 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
569 FAIL(); | 522 FAIL(); |
570 } | 523 } |
571 | 524 |
572 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = | 525 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = |
573 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 526 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
574 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); | 527 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); |
575 } | 528 } |
576 } | 529 } |
577 | 530 |
578 } // namespace net | 531 } // namespace net |
OLD | NEW |