| 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" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/metrics/histogram_samples.h" | 9 #include "base/metrics/histogram_samples.h" |
| 10 #include "base/metrics/statistics_recorder.h" | 10 #include "base/metrics/statistics_recorder.h" |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 | 196 |
| 197 // List of all histograms we care about in these unit tests. | 197 // List of all histograms we care about in these unit tests. |
| 198 const char* kHistogramNames[] = { | 198 const char* kHistogramNames[] = { |
| 199 "Throttling.FailureCountAtSuccess", | 199 "Throttling.FailureCountAtSuccess", |
| 200 "Throttling.PerceivedDowntime", | 200 "Throttling.PerceivedDowntime", |
| 201 "Throttling.RequestThrottled", | 201 "Throttling.RequestThrottled", |
| 202 "Throttling.SiteOptedOut", | 202 "Throttling.SiteOptedOut", |
| 203 }; | 203 }; |
| 204 | 204 |
| 205 void URLRequestThrottlerEntryTest::SetUp() { | 205 void URLRequestThrottlerEntryTest::SetUp() { |
| 206 request_.set_load_flags(0); | 206 request_.SetLoadFlags(0); |
| 207 | 207 |
| 208 now_ = TimeTicks::Now(); | 208 now_ = TimeTicks::Now(); |
| 209 entry_ = new MockURLRequestThrottlerEntry(&manager_); | 209 entry_ = new MockURLRequestThrottlerEntry(&manager_); |
| 210 entry_->ResetToBlank(now_); | 210 entry_->ResetToBlank(now_); |
| 211 | 211 |
| 212 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { | 212 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { |
| 213 // Must retrieve original samples for each histogram for comparison | 213 // Must retrieve original samples for each histogram for comparison |
| 214 // as other tests may affect them. | 214 // as other tests may affect them. |
| 215 const char* name = kHistogramNames[i]; | 215 const char* name = kHistogramNames[i]; |
| 216 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); | 216 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { | 251 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { |
| 252 return out << time.ToInternalValue(); | 252 return out << time.ToInternalValue(); |
| 253 } | 253 } |
| 254 | 254 |
| 255 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { | 255 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { |
| 256 entry_->set_exponential_backoff_release_time( | 256 entry_->set_exponential_backoff_release_time( |
| 257 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); | 257 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); |
| 258 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); | 258 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); |
| 259 | 259 |
| 260 // Also end-to-end test the load flags exceptions. | 260 // Also end-to-end test the load flags exceptions. |
| 261 request_.set_load_flags(LOAD_MAYBE_USER_GESTURE); | 261 request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE); |
| 262 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 262 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
| 263 | 263 |
| 264 CalculateHistogramDeltas(); | 264 CalculateHistogramDeltas(); |
| 265 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(0)); | 265 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(0)); |
| 266 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(1)); | 266 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(1)); |
| 267 } | 267 } |
| 268 | 268 |
| 269 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { | 269 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { |
| 270 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); | 270 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); |
| 271 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 271 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 ASSERT_FALSE(MockURLRequestThrottlerEntry::ExplicitUserRequest( | 388 ASSERT_FALSE(MockURLRequestThrottlerEntry::ExplicitUserRequest( |
| 389 ~LOAD_MAYBE_USER_GESTURE)); | 389 ~LOAD_MAYBE_USER_GESTURE)); |
| 390 } | 390 } |
| 391 | 391 |
| 392 class URLRequestThrottlerManagerTest : public testing::Test { | 392 class URLRequestThrottlerManagerTest : public testing::Test { |
| 393 protected: | 393 protected: |
| 394 URLRequestThrottlerManagerTest() | 394 URLRequestThrottlerManagerTest() |
| 395 : request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {} | 395 : request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {} |
| 396 | 396 |
| 397 virtual void SetUp() { | 397 virtual void SetUp() { |
| 398 request_.set_load_flags(0); | 398 request_.SetLoadFlags(0); |
| 399 } | 399 } |
| 400 | 400 |
| 401 // context_ must be declared before request_. | 401 // context_ must be declared before request_. |
| 402 TestURLRequestContext context_; | 402 TestURLRequestContext context_; |
| 403 TestURLRequest request_; | 403 TestURLRequest request_; |
| 404 }; | 404 }; |
| 405 | 405 |
| 406 TEST_F(URLRequestThrottlerManagerTest, IsUrlStandardised) { | 406 TEST_F(URLRequestThrottlerManagerTest, IsUrlStandardised) { |
| 407 MockURLRequestThrottlerManager manager; | 407 MockURLRequestThrottlerManager manager; |
| 408 GurlAndString test_values[] = { | 408 GurlAndString test_values[] = { |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 FAIL(); | 557 FAIL(); |
| 558 } | 558 } |
| 559 | 559 |
| 560 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = | 560 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = |
| 561 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 561 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
| 562 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); | 562 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); |
| 563 } | 563 } |
| 564 } | 564 } |
| 565 | 565 |
| 566 } // namespace net | 566 } // namespace net |
| OLD | NEW |