| 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 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 entry_->set_exponential_backoff_release_time( | 200 entry_->set_exponential_backoff_release_time( |
| 201 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); | 201 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); |
| 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 d.set_can_throttle_requests(true); | 205 d.set_can_throttle_requests(true); |
| 206 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); | 206 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); |
| 207 } | 207 } |
| 208 | 208 |
| 209 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { | 209 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { |
| 210 base::StatisticsDeltaReader statistics_delta_reader; | 210 base::HistogramTester histogram_tester; |
| 211 entry_->set_exponential_backoff_release_time( | 211 entry_->set_exponential_backoff_release_time( |
| 212 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); | 212 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); |
| 213 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); | 213 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); |
| 214 | 214 |
| 215 // Also end-to-end test the load flags exceptions. | 215 // Also end-to-end test the load flags exceptions. |
| 216 request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE); | 216 request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE); |
| 217 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 217 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
| 218 | 218 |
| 219 scoped_ptr<base::HistogramSamples> samples( | 219 histogram_tester.ExpectBucketCount(kRequestThrottledHistogramName, 0, 1); |
| 220 statistics_delta_reader.GetHistogramSamplesSinceCreation( | 220 histogram_tester.ExpectBucketCount(kRequestThrottledHistogramName, 1, 1); |
| 221 kRequestThrottledHistogramName)); | |
| 222 ASSERT_EQ(1, samples->GetCount(0)); | |
| 223 ASSERT_EQ(1, samples->GetCount(1)); | |
| 224 } | 221 } |
| 225 | 222 |
| 226 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { | 223 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { |
| 227 base::StatisticsDeltaReader statistics_delta_reader; | 224 base::HistogramTester histogram_tester; |
| 228 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); | 225 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); |
| 229 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 226 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
| 230 entry_->set_exponential_backoff_release_time( | 227 entry_->set_exponential_backoff_release_time( |
| 231 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); | 228 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); |
| 232 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 229 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
| 233 | 230 |
| 234 scoped_ptr<base::HistogramSamples> samples( | 231 histogram_tester.ExpectBucketCount(kRequestThrottledHistogramName, 0, 2); |
| 235 statistics_delta_reader.GetHistogramSamplesSinceCreation( | 232 histogram_tester.ExpectBucketCount(kRequestThrottledHistogramName, 1, 0); |
| 236 kRequestThrottledHistogramName)); | |
| 237 ASSERT_EQ(2, samples->GetCount(0)); | |
| 238 ASSERT_EQ(0, samples->GetCount(1)); | |
| 239 } | 233 } |
| 240 | 234 |
| 241 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { | 235 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { |
| 242 MockURLRequestThrottlerHeaderAdapter failure_response(503); | 236 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
| 243 entry_->UpdateWithResponse(std::string(), &failure_response); | 237 entry_->UpdateWithResponse(std::string(), &failure_response); |
| 244 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 238 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 245 << "A failure should increase the release_time"; | 239 << "A failure should increase the release_time"; |
| 246 } | 240 } |
| 247 | 241 |
| 248 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { | 242 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 FAIL(); | 511 FAIL(); |
| 518 } | 512 } |
| 519 | 513 |
| 520 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = | 514 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = |
| 521 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 515 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
| 522 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); | 516 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); |
| 523 } | 517 } |
| 524 } | 518 } |
| 525 | 519 |
| 526 } // namespace net | 520 } // namespace net |
| OLD | NEW |