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 19 matching lines...) Expand all Loading... |
30 | 30 |
31 using base::Histogram; | 31 using base::Histogram; |
32 using base::HistogramBase; | 32 using base::HistogramBase; |
33 using base::HistogramSamples; | 33 using base::HistogramSamples; |
34 using base::StatisticsRecorder; | 34 using base::StatisticsRecorder; |
35 | 35 |
36 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { | 36 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { |
37 public: | 37 public: |
38 explicit MockURLRequestThrottlerEntry( | 38 explicit MockURLRequestThrottlerEntry( |
39 net::URLRequestThrottlerManager* manager) | 39 net::URLRequestThrottlerManager* manager) |
40 : net::URLRequestThrottlerEntry(manager, ""), | 40 : net::URLRequestThrottlerEntry(manager, std::string()), |
41 mock_backoff_entry_(&backoff_policy_) { | 41 mock_backoff_entry_(&backoff_policy_) { |
42 InitPolicy(); | 42 InitPolicy(); |
43 } | 43 } |
44 MockURLRequestThrottlerEntry( | 44 MockURLRequestThrottlerEntry( |
45 net::URLRequestThrottlerManager* manager, | 45 net::URLRequestThrottlerManager* manager, |
46 const TimeTicks& exponential_backoff_release_time, | 46 const TimeTicks& exponential_backoff_release_time, |
47 const TimeTicks& sliding_window_release_time, | 47 const TimeTicks& sliding_window_release_time, |
48 const TimeTicks& fake_now) | 48 const TimeTicks& fake_now) |
49 : net::URLRequestThrottlerEntry(manager, ""), | 49 : net::URLRequestThrottlerEntry(manager, std::string()), |
50 fake_time_now_(fake_now), | 50 fake_time_now_(fake_now), |
51 mock_backoff_entry_(&backoff_policy_) { | 51 mock_backoff_entry_(&backoff_policy_) { |
52 InitPolicy(); | 52 InitPolicy(); |
53 | 53 |
54 mock_backoff_entry_.set_fake_now(fake_now); | 54 mock_backoff_entry_.set_fake_now(fake_now); |
55 set_exponential_backoff_release_time(exponential_backoff_release_time); | 55 set_exponential_backoff_release_time(exponential_backoff_release_time); |
56 set_sliding_window_release_time(sliding_window_release_time); | 56 set_sliding_window_release_time(sliding_window_release_time); |
57 } | 57 } |
58 | 58 |
59 void InitPolicy() { | 59 void InitPolicy() { |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); | 272 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); |
273 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 273 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
274 | 274 |
275 CalculateHistogramDeltas(); | 275 CalculateHistogramDeltas(); |
276 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"]->GetCount(0)); | 276 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"]->GetCount(0)); |
277 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"]->GetCount(1)); | 277 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"]->GetCount(1)); |
278 } | 278 } |
279 | 279 |
280 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { | 280 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { |
281 MockURLRequestThrottlerHeaderAdapter failure_response(503); | 281 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
282 entry_->UpdateWithResponse("", &failure_response); | 282 entry_->UpdateWithResponse(std::string(), &failure_response); |
283 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 283 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
284 << "A failure should increase the release_time"; | 284 << "A failure should increase the release_time"; |
285 } | 285 } |
286 | 286 |
287 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { | 287 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { |
288 MockURLRequestThrottlerHeaderAdapter success_response(200); | 288 MockURLRequestThrottlerHeaderAdapter success_response(200); |
289 entry_->UpdateWithResponse("", &success_response); | 289 entry_->UpdateWithResponse(std::string(), &success_response); |
290 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 290 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
291 << "A success should not add any delay"; | 291 << "A success should not add any delay"; |
292 } | 292 } |
293 | 293 |
294 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccessThenFailure) { | 294 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccessThenFailure) { |
295 MockURLRequestThrottlerHeaderAdapter failure_response(503); | 295 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
296 MockURLRequestThrottlerHeaderAdapter success_response(200); | 296 MockURLRequestThrottlerHeaderAdapter success_response(200); |
297 entry_->UpdateWithResponse("", &success_response); | 297 entry_->UpdateWithResponse(std::string(), &success_response); |
298 entry_->UpdateWithResponse("", &failure_response); | 298 entry_->UpdateWithResponse(std::string(), &failure_response); |
299 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 299 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
300 << "This scenario should add delay"; | 300 << "This scenario should add delay"; |
301 entry_->UpdateWithResponse("", &success_response); | 301 entry_->UpdateWithResponse(std::string(), &success_response); |
302 } | 302 } |
303 | 303 |
304 TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) { | 304 TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) { |
305 TimeDelta lifetime = TimeDelta::FromMilliseconds( | 305 TimeDelta lifetime = TimeDelta::FromMilliseconds( |
306 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs); | 306 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs); |
307 const TimeDelta kFiveMs = TimeDelta::FromMilliseconds(5); | 307 const TimeDelta kFiveMs = TimeDelta::FromMilliseconds(5); |
308 | 308 |
309 TimeAndBool test_values[] = { | 309 TimeAndBool test_values[] = { |
310 TimeAndBool(now_, false, __LINE__), | 310 TimeAndBool(now_, false, __LINE__), |
311 TimeAndBool(now_ - kFiveMs, false, __LINE__), | 311 TimeAndBool(now_ - kFiveMs, false, __LINE__), |
312 TimeAndBool(now_ + kFiveMs, false, __LINE__), | 312 TimeAndBool(now_ + kFiveMs, false, __LINE__), |
313 TimeAndBool(now_ - (lifetime - kFiveMs), false, __LINE__), | 313 TimeAndBool(now_ - (lifetime - kFiveMs), false, __LINE__), |
314 TimeAndBool(now_ - lifetime, true, __LINE__), | 314 TimeAndBool(now_ - lifetime, true, __LINE__), |
315 TimeAndBool(now_ - (lifetime + kFiveMs), true, __LINE__)}; | 315 TimeAndBool(now_ - (lifetime + kFiveMs), true, __LINE__)}; |
316 | 316 |
317 for (unsigned int i = 0; i < arraysize(test_values); ++i) { | 317 for (unsigned int i = 0; i < arraysize(test_values); ++i) { |
318 entry_->set_exponential_backoff_release_time(test_values[i].time); | 318 entry_->set_exponential_backoff_release_time(test_values[i].time); |
319 EXPECT_EQ(entry_->IsEntryOutdated(), test_values[i].result) << | 319 EXPECT_EQ(entry_->IsEntryOutdated(), test_values[i].result) << |
320 "Test case #" << i << " line " << test_values[i].line << " failed"; | 320 "Test case #" << i << " line " << test_values[i].line << " failed"; |
321 } | 321 } |
322 } | 322 } |
323 | 323 |
324 TEST_F(URLRequestThrottlerEntryTest, MaxAllowedBackoff) { | 324 TEST_F(URLRequestThrottlerEntryTest, MaxAllowedBackoff) { |
325 for (int i = 0; i < 30; ++i) { | 325 for (int i = 0; i < 30; ++i) { |
326 MockURLRequestThrottlerHeaderAdapter response_adapter(503); | 326 MockURLRequestThrottlerHeaderAdapter response_adapter(503); |
327 entry_->UpdateWithResponse("", &response_adapter); | 327 entry_->UpdateWithResponse(std::string(), &response_adapter); |
328 } | 328 } |
329 | 329 |
330 TimeDelta delay = entry_->GetExponentialBackoffReleaseTime() - now_; | 330 TimeDelta delay = entry_->GetExponentialBackoffReleaseTime() - now_; |
331 EXPECT_EQ(delay.InMilliseconds(), | 331 EXPECT_EQ(delay.InMilliseconds(), |
332 MockURLRequestThrottlerEntry::kDefaultMaximumBackoffMs); | 332 MockURLRequestThrottlerEntry::kDefaultMaximumBackoffMs); |
333 } | 333 } |
334 | 334 |
335 TEST_F(URLRequestThrottlerEntryTest, MalformedContent) { | 335 TEST_F(URLRequestThrottlerEntryTest, MalformedContent) { |
336 MockURLRequestThrottlerHeaderAdapter response_adapter(503); | 336 MockURLRequestThrottlerHeaderAdapter response_adapter(503); |
337 for (int i = 0; i < 5; ++i) | 337 for (int i = 0; i < 5; ++i) |
338 entry_->UpdateWithResponse("", &response_adapter); | 338 entry_->UpdateWithResponse(std::string(), &response_adapter); |
339 | 339 |
340 TimeTicks release_after_failures = entry_->GetExponentialBackoffReleaseTime(); | 340 TimeTicks release_after_failures = entry_->GetExponentialBackoffReleaseTime(); |
341 | 341 |
342 // Inform the entry that a response body was malformed, which is supposed to | 342 // Inform the entry that a response body was malformed, which is supposed to |
343 // increase the back-off time. Note that we also submit a successful | 343 // increase the back-off time. Note that we also submit a successful |
344 // UpdateWithResponse to pair with ReceivedContentWasMalformed() since that | 344 // UpdateWithResponse to pair with ReceivedContentWasMalformed() since that |
345 // is what happens in practice (if a body is received, then a non-500 | 345 // is what happens in practice (if a body is received, then a non-500 |
346 // response must also have been received). | 346 // response must also have been received). |
347 entry_->ReceivedContentWasMalformed(200); | 347 entry_->ReceivedContentWasMalformed(200); |
348 MockURLRequestThrottlerHeaderAdapter success_adapter(200); | 348 MockURLRequestThrottlerHeaderAdapter success_adapter(200); |
349 entry_->UpdateWithResponse("", &success_adapter); | 349 entry_->UpdateWithResponse(std::string(), &success_adapter); |
350 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), release_after_failures); | 350 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), release_after_failures); |
351 } | 351 } |
352 | 352 |
353 TEST_F(URLRequestThrottlerEntryTest, SlidingWindow) { | 353 TEST_F(URLRequestThrottlerEntryTest, SlidingWindow) { |
354 int max_send = URLRequestThrottlerEntry::kDefaultMaxSendThreshold; | 354 int max_send = URLRequestThrottlerEntry::kDefaultMaxSendThreshold; |
355 int sliding_window = | 355 int sliding_window = |
356 URLRequestThrottlerEntry::kDefaultSlidingWindowPeriodMs; | 356 URLRequestThrottlerEntry::kDefaultSlidingWindowPeriodMs; |
357 | 357 |
358 TimeTicks time_1 = entry_->fake_time_now_ + | 358 TimeTicks time_1 = entry_->fake_time_now_ + |
359 TimeDelta::FromMilliseconds(sliding_window / 3); | 359 TimeDelta::FromMilliseconds(sliding_window / 3); |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 } | 468 } |
469 | 469 |
470 void ExpectEntryAllowsAllOnErrorIfOptedOut( | 470 void ExpectEntryAllowsAllOnErrorIfOptedOut( |
471 net::URLRequestThrottlerEntryInterface* entry, | 471 net::URLRequestThrottlerEntryInterface* entry, |
472 bool opted_out, | 472 bool opted_out, |
473 const URLRequest& request) { | 473 const URLRequest& request) { |
474 EXPECT_FALSE(entry->ShouldRejectRequest(request)); | 474 EXPECT_FALSE(entry->ShouldRejectRequest(request)); |
475 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); | 475 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); |
476 for (int i = 0; i < 10; ++i) { | 476 for (int i = 0; i < 10; ++i) { |
477 // Host doesn't really matter in this scenario so we skip it. | 477 // Host doesn't really matter in this scenario so we skip it. |
478 entry->UpdateWithResponse("", &failure_adapter); | 478 entry->UpdateWithResponse(std::string(), &failure_adapter); |
479 } | 479 } |
480 EXPECT_NE(opted_out, entry->ShouldRejectRequest(request)); | 480 EXPECT_NE(opted_out, entry->ShouldRejectRequest(request)); |
481 | 481 |
482 if (opted_out) { | 482 if (opted_out) { |
483 // We're not mocking out GetTimeNow() in this scenario | 483 // We're not mocking out GetTimeNow() in this scenario |
484 // so add a 100 ms buffer to avoid flakiness (that should always | 484 // so add a 100 ms buffer to avoid flakiness (that should always |
485 // give enough time to get from the TimeTicks::Now() call here | 485 // give enough time to get from the TimeTicks::Now() call here |
486 // to the TimeTicks::Now() call in the entry class). | 486 // to the TimeTicks::Now() call in the entry class). |
487 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), | 487 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), |
488 entry->GetExponentialBackoffReleaseTime()); | 488 entry->GetExponentialBackoffReleaseTime()); |
489 } else { | 489 } else { |
490 // As above, add 100 ms. | 490 // As above, add 100 ms. |
491 EXPECT_LT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), | 491 EXPECT_LT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), |
492 entry->GetExponentialBackoffReleaseTime()); | 492 entry->GetExponentialBackoffReleaseTime()); |
493 } | 493 } |
494 } | 494 } |
495 | 495 |
496 TEST_F(URLRequestThrottlerManagerTest, OptOutHeader) { | 496 TEST_F(URLRequestThrottlerManagerTest, OptOutHeader) { |
497 MockURLRequestThrottlerManager manager; | 497 MockURLRequestThrottlerManager manager; |
498 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry = | 498 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry = |
499 manager.RegisterRequestUrl(GURL("http://www.google.com/yodude")); | 499 manager.RegisterRequestUrl(GURL("http://www.google.com/yodude")); |
500 | 500 |
501 // Fake a response with the opt-out header. | 501 // Fake a response with the opt-out header. |
502 MockURLRequestThrottlerHeaderAdapter response_adapter( | 502 MockURLRequestThrottlerHeaderAdapter response_adapter( |
503 "", | 503 std::string(), |
504 MockURLRequestThrottlerEntry::kExponentialThrottlingDisableValue, | 504 MockURLRequestThrottlerEntry::kExponentialThrottlingDisableValue, |
505 200); | 505 200); |
506 entry->UpdateWithResponse("www.google.com", &response_adapter); | 506 entry->UpdateWithResponse("www.google.com", &response_adapter); |
507 | 507 |
508 // Ensure that the same entry on error always allows everything. | 508 // Ensure that the same entry on error always allows everything. |
509 ExpectEntryAllowsAllOnErrorIfOptedOut(entry, true, request_); | 509 ExpectEntryAllowsAllOnErrorIfOptedOut(entry, true, request_); |
510 | 510 |
511 // Ensure that a freshly created entry (for a different URL on an | 511 // Ensure that a freshly created entry (for a different URL on an |
512 // already opted-out host) also gets "always allow" behavior. | 512 // already opted-out host) also gets "always allow" behavior. |
513 scoped_refptr<net::URLRequestThrottlerEntryInterface> other_entry = | 513 scoped_refptr<net::URLRequestThrottlerEntryInterface> other_entry = |
514 manager.RegisterRequestUrl(GURL("http://www.google.com/bingobob")); | 514 manager.RegisterRequestUrl(GURL("http://www.google.com/bingobob")); |
515 ExpectEntryAllowsAllOnErrorIfOptedOut(other_entry, true, request_); | 515 ExpectEntryAllowsAllOnErrorIfOptedOut(other_entry, true, request_); |
516 | 516 |
517 // Fake a response with the opt-out header incorrectly specified. | 517 // Fake a response with the opt-out header incorrectly specified. |
518 scoped_refptr<net::URLRequestThrottlerEntryInterface> no_opt_out_entry = | 518 scoped_refptr<net::URLRequestThrottlerEntryInterface> no_opt_out_entry = |
519 manager.RegisterRequestUrl(GURL("http://www.nike.com/justdoit")); | 519 manager.RegisterRequestUrl(GURL("http://www.nike.com/justdoit")); |
520 MockURLRequestThrottlerHeaderAdapter wrong_adapter("", "yesplease", 200); | 520 MockURLRequestThrottlerHeaderAdapter wrong_adapter( |
| 521 std::string(), "yesplease", 200); |
521 no_opt_out_entry->UpdateWithResponse("www.nike.com", &wrong_adapter); | 522 no_opt_out_entry->UpdateWithResponse("www.nike.com", &wrong_adapter); |
522 ExpectEntryAllowsAllOnErrorIfOptedOut(no_opt_out_entry, false, request_); | 523 ExpectEntryAllowsAllOnErrorIfOptedOut(no_opt_out_entry, false, request_); |
523 | 524 |
524 // A localhost entry should always be opted out. | 525 // A localhost entry should always be opted out. |
525 scoped_refptr<net::URLRequestThrottlerEntryInterface> localhost_entry = | 526 scoped_refptr<net::URLRequestThrottlerEntryInterface> localhost_entry = |
526 manager.RegisterRequestUrl(GURL("http://localhost/hello")); | 527 manager.RegisterRequestUrl(GURL("http://localhost/hello")); |
527 ExpectEntryAllowsAllOnErrorIfOptedOut(localhost_entry, true, request_); | 528 ExpectEntryAllowsAllOnErrorIfOptedOut(localhost_entry, true, request_); |
528 } | 529 } |
529 | 530 |
530 TEST_F(URLRequestThrottlerManagerTest, ClearOnNetworkChange) { | 531 TEST_F(URLRequestThrottlerManagerTest, ClearOnNetworkChange) { |
531 for (int i = 0; i < 3; ++i) { | 532 for (int i = 0; i < 3; ++i) { |
532 MockURLRequestThrottlerManager manager; | 533 MockURLRequestThrottlerManager manager; |
533 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_before = | 534 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_before = |
534 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 535 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
535 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); | 536 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); |
536 for (int j = 0; j < 10; ++j) { | 537 for (int j = 0; j < 10; ++j) { |
537 // Host doesn't really matter in this scenario so we skip it. | 538 // Host doesn't really matter in this scenario so we skip it. |
538 entry_before->UpdateWithResponse("", &failure_adapter); | 539 entry_before->UpdateWithResponse(std::string(), &failure_adapter); |
539 } | 540 } |
540 EXPECT_TRUE(entry_before->ShouldRejectRequest(request_)); | 541 EXPECT_TRUE(entry_before->ShouldRejectRequest(request_)); |
541 | 542 |
542 switch (i) { | 543 switch (i) { |
543 case 0: | 544 case 0: |
544 manager.OnIPAddressChanged(); | 545 manager.OnIPAddressChanged(); |
545 break; | 546 break; |
546 case 1: | 547 case 1: |
547 manager.OnConnectionTypeChanged( | 548 manager.OnConnectionTypeChanged( |
548 net::NetworkChangeNotifier::CONNECTION_UNKNOWN); | 549 net::NetworkChangeNotifier::CONNECTION_UNKNOWN); |
549 break; | 550 break; |
550 case 2: | 551 case 2: |
551 manager.OnConnectionTypeChanged( | 552 manager.OnConnectionTypeChanged( |
552 net::NetworkChangeNotifier::CONNECTION_NONE); | 553 net::NetworkChangeNotifier::CONNECTION_NONE); |
553 break; | 554 break; |
554 default: | 555 default: |
555 FAIL(); | 556 FAIL(); |
556 } | 557 } |
557 | 558 |
558 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = | 559 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = |
559 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 560 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
560 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); | 561 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); |
561 } | 562 } |
562 } | 563 } |
563 | 564 |
564 } // namespace net | 565 } // namespace net |
OLD | NEW |