| 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 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 original_samples_.clear(); | 227 original_samples_.clear(); |
| 228 } | 228 } |
| 229 | 229 |
| 230 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { | 230 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { |
| 231 return out << time.ToInternalValue(); | 231 return out << time.ToInternalValue(); |
| 232 } | 232 } |
| 233 | 233 |
| 234 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { | 234 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { |
| 235 entry_->set_exponential_backoff_release_time( | 235 entry_->set_exponential_backoff_release_time( |
| 236 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); | 236 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); |
| 237 EXPECT_TRUE(entry_->ShouldRejectRequest(0)); | 237 EXPECT_TRUE(entry_->ShouldRejectRequest(NULL, 0)); |
| 238 | 238 |
| 239 // Also end-to-end test the load flags exceptions. | 239 // Also end-to-end test the load flags exceptions. |
| 240 EXPECT_FALSE(entry_->ShouldRejectRequest(LOAD_MAYBE_USER_GESTURE)); | 240 EXPECT_FALSE(entry_->ShouldRejectRequest(NULL, LOAD_MAYBE_USER_GESTURE)); |
| 241 | 241 |
| 242 CalculateHistogramDeltas(); | 242 CalculateHistogramDeltas(); |
| 243 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"].counts(0)); | 243 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"].counts(0)); |
| 244 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"].counts(1)); | 244 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"].counts(1)); |
| 245 } | 245 } |
| 246 | 246 |
| 247 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { | 247 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { |
| 248 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); | 248 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); |
| 249 EXPECT_FALSE(entry_->ShouldRejectRequest(0)); | 249 EXPECT_FALSE(entry_->ShouldRejectRequest(NULL, 0)); |
| 250 entry_->set_exponential_backoff_release_time( | 250 entry_->set_exponential_backoff_release_time( |
| 251 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); | 251 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); |
| 252 EXPECT_FALSE(entry_->ShouldRejectRequest(0)); | 252 EXPECT_FALSE(entry_->ShouldRejectRequest(NULL, 0)); |
| 253 | 253 |
| 254 CalculateHistogramDeltas(); | 254 CalculateHistogramDeltas(); |
| 255 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"].counts(0)); | 255 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"].counts(0)); |
| 256 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"].counts(1)); | 256 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"].counts(1)); |
| 257 } | 257 } |
| 258 | 258 |
| 259 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { | 259 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { |
| 260 MockURLRequestThrottlerHeaderAdapter failure_response(503); | 260 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
| 261 entry_->UpdateWithResponse("", &failure_response); | 261 entry_->UpdateWithResponse("", &failure_response); |
| 262 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 262 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 263 << "A failure should increase the release_time"; | 263 << "A failure should increase the release_time"; |
| 264 } | 264 } |
| 265 | 265 |
| 266 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { | 266 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { |
| 267 MockURLRequestThrottlerHeaderAdapter success_response(200); | 267 MockURLRequestThrottlerHeaderAdapter success_response(200); |
| 268 entry_->UpdateWithResponse("", &success_response); | 268 entry_->UpdateWithResponse("", &success_response); |
| 269 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 269 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 270 << "A success should not add any delay"; | 270 << "A success should not add any delay"; |
| 271 } | 271 } |
| 272 | 272 |
| 273 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccessThenFailure) { | 273 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccessThenFailure) { |
| 274 MockURLRequestThrottlerHeaderAdapter failure_response(503); | 274 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
| 275 MockURLRequestThrottlerHeaderAdapter success_response(200); | 275 MockURLRequestThrottlerHeaderAdapter success_response(200); |
| 276 entry_->UpdateWithResponse("", &success_response); | 276 entry_->UpdateWithResponse("", &success_response); |
| 277 entry_->UpdateWithResponse("", &failure_response); | 277 entry_->UpdateWithResponse("", &failure_response); |
| 278 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 278 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 279 << "This scenario should add delay"; | 279 << "This scenario should add delay"; |
| 280 entry_->UpdateWithResponse("", &success_response); | 280 entry_->UpdateWithResponse("", &success_response); |
| 281 | |
| 282 CalculateHistogramDeltas(); | |
| 283 ASSERT_EQ(1, samples_["Throttling.FailureCountAtSuccess"].counts(1)); | |
| 284 ASSERT_EQ(1, samples_["Throttling.PerceivedDowntime"].TotalCount()); | |
| 285 } | 281 } |
| 286 | 282 |
| 287 TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) { | 283 TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) { |
| 288 TimeDelta lifetime = TimeDelta::FromMilliseconds( | 284 TimeDelta lifetime = TimeDelta::FromMilliseconds( |
| 289 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs); | 285 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs); |
| 290 const TimeDelta kFiveMs = TimeDelta::FromMilliseconds(5); | 286 const TimeDelta kFiveMs = TimeDelta::FromMilliseconds(5); |
| 291 | 287 |
| 292 TimeAndBool test_values[] = { | 288 TimeAndBool test_values[] = { |
| 293 TimeAndBool(now_, false, __LINE__), | 289 TimeAndBool(now_, false, __LINE__), |
| 294 TimeAndBool(now_ - kFiveMs, false, __LINE__), | 290 TimeAndBool(now_ - kFiveMs, false, __LINE__), |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0")); | 427 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0")); |
| 432 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1")); | 428 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1")); |
| 433 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure")); | 429 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure")); |
| 434 | 430 |
| 435 EXPECT_EQ(3, manager.GetNumberOfEntries()); | 431 EXPECT_EQ(3, manager.GetNumberOfEntries()); |
| 436 } | 432 } |
| 437 | 433 |
| 438 void ExpectEntryAllowsAllOnErrorIfOptedOut( | 434 void ExpectEntryAllowsAllOnErrorIfOptedOut( |
| 439 net::URLRequestThrottlerEntryInterface* entry, | 435 net::URLRequestThrottlerEntryInterface* entry, |
| 440 bool opted_out) { | 436 bool opted_out) { |
| 441 EXPECT_FALSE(entry->ShouldRejectRequest(0)); | 437 EXPECT_FALSE(entry->ShouldRejectRequest(NULL, 0)); |
| 442 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); | 438 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); |
| 443 for (int i = 0; i < 10; ++i) { | 439 for (int i = 0; i < 10; ++i) { |
| 444 // Host doesn't really matter in this scenario so we skip it. | 440 // Host doesn't really matter in this scenario so we skip it. |
| 445 entry->UpdateWithResponse("", &failure_adapter); | 441 entry->UpdateWithResponse("", &failure_adapter); |
| 446 } | 442 } |
| 447 EXPECT_NE(opted_out, entry->ShouldRejectRequest(0)); | 443 EXPECT_NE(opted_out, entry->ShouldRejectRequest(NULL, 0)); |
| 448 | 444 |
| 449 if (opted_out) { | 445 if (opted_out) { |
| 450 // We're not mocking out GetTimeNow() in this scenario | 446 // We're not mocking out GetTimeNow() in this scenario |
| 451 // so add a 100 ms buffer to avoid flakiness (that should always | 447 // so add a 100 ms buffer to avoid flakiness (that should always |
| 452 // give enough time to get from the TimeTicks::Now() call here | 448 // give enough time to get from the TimeTicks::Now() call here |
| 453 // to the TimeTicks::Now() call in the entry class). | 449 // to the TimeTicks::Now() call in the entry class). |
| 454 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), | 450 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), |
| 455 entry->GetExponentialBackoffReleaseTime()); | 451 entry->GetExponentialBackoffReleaseTime()); |
| 456 } else { | 452 } else { |
| 457 // As above, add 100 ms. | 453 // As above, add 100 ms. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 TEST(URLRequestThrottlerManager, ClearOnNetworkChange) { | 493 TEST(URLRequestThrottlerManager, ClearOnNetworkChange) { |
| 498 for (int i = 0; i < 3; ++i) { | 494 for (int i = 0; i < 3; ++i) { |
| 499 MockURLRequestThrottlerManager manager; | 495 MockURLRequestThrottlerManager manager; |
| 500 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_before = | 496 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_before = |
| 501 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 497 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
| 502 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); | 498 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); |
| 503 for (int j = 0; j < 10; ++j) { | 499 for (int j = 0; j < 10; ++j) { |
| 504 // Host doesn't really matter in this scenario so we skip it. | 500 // Host doesn't really matter in this scenario so we skip it. |
| 505 entry_before->UpdateWithResponse("", &failure_adapter); | 501 entry_before->UpdateWithResponse("", &failure_adapter); |
| 506 } | 502 } |
| 507 EXPECT_TRUE(entry_before->ShouldRejectRequest(0)); | 503 EXPECT_TRUE(entry_before->ShouldRejectRequest(NULL, 0)); |
| 508 | 504 |
| 509 switch (i) { | 505 switch (i) { |
| 510 case 0: | 506 case 0: |
| 511 manager.OnIPAddressChanged(); | 507 manager.OnIPAddressChanged(); |
| 512 break; | 508 break; |
| 513 case 1: | 509 case 1: |
| 514 manager.OnConnectionTypeChanged( | 510 manager.OnConnectionTypeChanged( |
| 515 net::NetworkChangeNotifier::CONNECTION_UNKNOWN); | 511 net::NetworkChangeNotifier::CONNECTION_UNKNOWN); |
| 516 break; | 512 break; |
| 517 case 2: | 513 case 2: |
| 518 manager.OnConnectionTypeChanged( | 514 manager.OnConnectionTypeChanged( |
| 519 net::NetworkChangeNotifier::CONNECTION_NONE); | 515 net::NetworkChangeNotifier::CONNECTION_NONE); |
| 520 break; | 516 break; |
| 521 default: | 517 default: |
| 522 FAIL(); | 518 FAIL(); |
| 523 } | 519 } |
| 524 | 520 |
| 525 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = | 521 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = |
| 526 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 522 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
| 527 EXPECT_FALSE(entry_after->ShouldRejectRequest(0)); | 523 EXPECT_FALSE(entry_after->ShouldRejectRequest(NULL, 0)); |
| 528 } | 524 } |
| 529 } | 525 } |
| 530 | 526 |
| 531 } // namespace net | 527 } // namespace net |
| OLD | NEW |