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_samples.h" | 8 #include "base/metrics/histogram_samples.h" |
9 #include "base/pickle.h" | 9 #include "base/pickle.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "base/test/statistics_delta_reader.h" | 13 #include "base/test/histogram_tester.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
16 #include "net/base/request_priority.h" | 16 #include "net/base/request_priority.h" |
17 #include "net/base/test_completion_callback.h" | 17 #include "net/base/test_completion_callback.h" |
18 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
19 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
20 #include "net/url_request/url_request_throttler_header_interface.h" | 20 #include "net/url_request/url_request_throttler_header_interface.h" |
21 #include "net/url_request/url_request_throttler_test_support.h" | 21 #include "net/url_request/url_request_throttler_test_support.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 | 202 |
203 d.set_can_throttle_requests(false); | 203 d.set_can_throttle_requests(false); |
204 EXPECT_FALSE(entry_->ShouldRejectRequest(request_, | 204 EXPECT_FALSE(entry_->ShouldRejectRequest(request_, |
205 context_.network_delegate())); | 205 context_.network_delegate())); |
206 d.set_can_throttle_requests(true); | 206 d.set_can_throttle_requests(true); |
207 EXPECT_TRUE(entry_->ShouldRejectRequest(request_, | 207 EXPECT_TRUE(entry_->ShouldRejectRequest(request_, |
208 context_.network_delegate())); | 208 context_.network_delegate())); |
209 } | 209 } |
210 | 210 |
211 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { | 211 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { |
212 base::StatisticsDeltaReader statistics_delta_reader; | 212 base::HistogramTester histogram_tester; |
213 entry_->set_exponential_backoff_release_time( | 213 entry_->set_exponential_backoff_release_time( |
214 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); | 214 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); |
215 EXPECT_TRUE(entry_->ShouldRejectRequest(request_, | 215 EXPECT_TRUE(entry_->ShouldRejectRequest(request_, |
216 context_.network_delegate())); | 216 context_.network_delegate())); |
217 | 217 |
218 // Also end-to-end test the load flags exceptions. | 218 // Also end-to-end test the load flags exceptions. |
219 request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE); | 219 request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE); |
220 EXPECT_FALSE(entry_->ShouldRejectRequest(request_, | 220 EXPECT_FALSE(entry_->ShouldRejectRequest(request_, |
221 context_.network_delegate())); | 221 context_.network_delegate())); |
222 | 222 |
223 scoped_ptr<base::HistogramSamples> samples( | 223 histogram_tester.ExpectBucketCount(kRequestThrottledHistogramName, 0, 1); |
224 statistics_delta_reader.GetHistogramSamplesSinceCreation( | 224 histogram_tester.ExpectBucketCount(kRequestThrottledHistogramName, 1, 1); |
225 kRequestThrottledHistogramName)); | |
226 ASSERT_EQ(1, samples->GetCount(0)); | |
227 ASSERT_EQ(1, samples->GetCount(1)); | |
228 } | 225 } |
229 | 226 |
230 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { | 227 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { |
231 base::StatisticsDeltaReader statistics_delta_reader; | 228 base::HistogramTester histogram_tester; |
232 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); | 229 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); |
233 EXPECT_FALSE(entry_->ShouldRejectRequest(request_, | 230 EXPECT_FALSE(entry_->ShouldRejectRequest(request_, |
234 context_.network_delegate())); | 231 context_.network_delegate())); |
235 entry_->set_exponential_backoff_release_time( | 232 entry_->set_exponential_backoff_release_time( |
236 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); | 233 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); |
237 EXPECT_FALSE(entry_->ShouldRejectRequest(request_, | 234 EXPECT_FALSE(entry_->ShouldRejectRequest(request_, |
238 context_.network_delegate())); | 235 context_.network_delegate())); |
239 | 236 |
240 scoped_ptr<base::HistogramSamples> samples( | 237 histogram_tester.ExpectBucketCount(kRequestThrottledHistogramName, 0, 2); |
241 statistics_delta_reader.GetHistogramSamplesSinceCreation( | 238 histogram_tester.ExpectBucketCount(kRequestThrottledHistogramName, 1, 0); |
242 kRequestThrottledHistogramName)); | |
243 ASSERT_EQ(2, samples->GetCount(0)); | |
244 ASSERT_EQ(0, samples->GetCount(1)); | |
245 } | 239 } |
246 | 240 |
247 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { | 241 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { |
248 MockURLRequestThrottlerHeaderAdapter failure_response(503); | 242 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
249 entry_->UpdateWithResponse(std::string(), &failure_response); | 243 entry_->UpdateWithResponse(std::string(), &failure_response); |
250 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 244 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
251 << "A failure should increase the release_time"; | 245 << "A failure should increase the release_time"; |
252 } | 246 } |
253 | 247 |
254 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { | 248 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 } | 520 } |
527 | 521 |
528 scoped_refptr<URLRequestThrottlerEntryInterface> entry_after = | 522 scoped_refptr<URLRequestThrottlerEntryInterface> entry_after = |
529 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 523 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
530 EXPECT_FALSE(entry_after->ShouldRejectRequest( | 524 EXPECT_FALSE(entry_after->ShouldRejectRequest( |
531 request_, context_.network_delegate())); | 525 request_, context_.network_delegate())); |
532 } | 526 } |
533 } | 527 } |
534 | 528 |
535 } // namespace net | 529 } // namespace net |
OLD | NEW |