| 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/pickle.h" | 9 #include "base/pickle.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 TimeTicks now_; | 173 TimeTicks now_; |
| 174 MockURLRequestThrottlerManager manager_; // Dummy object, not used. | 174 MockURLRequestThrottlerManager manager_; // Dummy object, not used. |
| 175 scoped_refptr<MockURLRequestThrottlerEntry> entry_; | 175 scoped_refptr<MockURLRequestThrottlerEntry> entry_; |
| 176 | 176 |
| 177 std::map<std::string, Histogram::SampleSet> original_samples_; | 177 std::map<std::string, Histogram::SampleSet> original_samples_; |
| 178 std::map<std::string, Histogram::SampleSet> samples_; | 178 std::map<std::string, Histogram::SampleSet> samples_; |
| 179 }; | 179 }; |
| 180 | 180 |
| 181 // List of all histograms we care about in these unit tests. | 181 // List of all histograms we care about in these unit tests. |
| 182 const char* kHistogramNames[] = { | 182 const char* kHistogramNames[] = { |
| 183 "Throttling.CustomRetryAfterMs", | |
| 184 "Throttling.FailureCountAtSuccess", | 183 "Throttling.FailureCountAtSuccess", |
| 185 "Throttling.PerceivedDowntime", | 184 "Throttling.PerceivedDowntime", |
| 186 "Throttling.RequestThrottled", | 185 "Throttling.RequestThrottled", |
| 187 "Throttling.SiteOptedOut", | 186 "Throttling.SiteOptedOut", |
| 188 }; | 187 }; |
| 189 | 188 |
| 190 void URLRequestThrottlerEntryTest::SetUp() { | 189 void URLRequestThrottlerEntryTest::SetUp() { |
| 191 now_ = TimeTicks::Now(); | 190 now_ = TimeTicks::Now(); |
| 192 entry_ = new MockURLRequestThrottlerEntry(&manager_); | 191 entry_ = new MockURLRequestThrottlerEntry(&manager_); |
| 193 entry_->ResetToBlank(now_); | 192 entry_->ResetToBlank(now_); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 EXPECT_FALSE(entry_->ShouldRejectRequest(0)); | 247 EXPECT_FALSE(entry_->ShouldRejectRequest(0)); |
| 249 entry_->set_exponential_backoff_release_time( | 248 entry_->set_exponential_backoff_release_time( |
| 250 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); | 249 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); |
| 251 EXPECT_FALSE(entry_->ShouldRejectRequest(0)); | 250 EXPECT_FALSE(entry_->ShouldRejectRequest(0)); |
| 252 | 251 |
| 253 CalculateHistogramDeltas(); | 252 CalculateHistogramDeltas(); |
| 254 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"].counts(0)); | 253 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"].counts(0)); |
| 255 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"].counts(1)); | 254 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"].counts(1)); |
| 256 } | 255 } |
| 257 | 256 |
| 258 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateRetryAfter) { | |
| 259 // If the response we received has a retry-after field, | |
| 260 // the request should be delayed. | |
| 261 MockURLRequestThrottlerHeaderAdapter header_w_delay_header("5.5", "", 200); | |
| 262 entry_->UpdateWithResponse("", &header_w_delay_header); | |
| 263 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | |
| 264 << "When the server put a positive value in retry-after we should " | |
| 265 "increase release_time"; | |
| 266 | |
| 267 entry_->ResetToBlank(now_); | |
| 268 MockURLRequestThrottlerHeaderAdapter header_w_negative_header( | |
| 269 "-5.5", "", 200); | |
| 270 entry_->UpdateWithResponse("", &header_w_negative_header); | |
| 271 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | |
| 272 << "When given a negative value, it should not change the release_time"; | |
| 273 | |
| 274 CalculateHistogramDeltas(); | |
| 275 ASSERT_EQ(1, samples_["Throttling.CustomRetryAfterMs"].TotalCount()); | |
| 276 } | |
| 277 | |
| 278 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { | 257 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { |
| 279 MockURLRequestThrottlerHeaderAdapter failure_response(503); | 258 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
| 280 entry_->UpdateWithResponse("", &failure_response); | 259 entry_->UpdateWithResponse("", &failure_response); |
| 281 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 260 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 282 << "A failure should increase the release_time"; | 261 << "A failure should increase the release_time"; |
| 283 } | 262 } |
| 284 | 263 |
| 285 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { | 264 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { |
| 286 MockURLRequestThrottlerHeaderAdapter success_response(200); | 265 MockURLRequestThrottlerHeaderAdapter success_response(200); |
| 287 entry_->UpdateWithResponse("", &success_response); | 266 entry_->UpdateWithResponse("", &success_response); |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 FAIL(); | 518 FAIL(); |
| 540 } | 519 } |
| 541 | 520 |
| 542 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = | 521 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = |
| 543 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 522 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
| 544 EXPECT_FALSE(entry_after->ShouldRejectRequest(0)); | 523 EXPECT_FALSE(entry_after->ShouldRejectRequest(0)); |
| 545 } | 524 } |
| 546 } | 525 } |
| 547 | 526 |
| 548 } // namespace net | 527 } // namespace net |
| OLD | NEW |