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" |
(...skipping 15 matching lines...) Expand all Loading... |
26 | 26 |
27 namespace net { | 27 namespace net { |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 const char kRequestThrottledHistogramName[] = "Throttling.RequestThrottled"; | 31 const char kRequestThrottledHistogramName[] = "Throttling.RequestThrottled"; |
32 | 32 |
33 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { | 33 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { |
34 public: | 34 public: |
35 explicit MockURLRequestThrottlerEntry( | 35 explicit MockURLRequestThrottlerEntry( |
36 net::URLRequestThrottlerManager* manager) | 36 URLRequestThrottlerManager* manager) |
37 : net::URLRequestThrottlerEntry(manager, std::string()), | 37 : URLRequestThrottlerEntry(manager, std::string()), |
38 mock_backoff_entry_(&backoff_policy_) { | 38 mock_backoff_entry_(&backoff_policy_) { |
39 InitPolicy(); | 39 InitPolicy(); |
40 } | 40 } |
41 MockURLRequestThrottlerEntry( | 41 MockURLRequestThrottlerEntry( |
42 net::URLRequestThrottlerManager* manager, | 42 URLRequestThrottlerManager* manager, |
43 const TimeTicks& exponential_backoff_release_time, | 43 const TimeTicks& exponential_backoff_release_time, |
44 const TimeTicks& sliding_window_release_time, | 44 const TimeTicks& sliding_window_release_time, |
45 const TimeTicks& fake_now) | 45 const TimeTicks& fake_now) |
46 : net::URLRequestThrottlerEntry(manager, std::string()), | 46 : URLRequestThrottlerEntry(manager, std::string()), |
47 fake_time_now_(fake_now), | 47 fake_time_now_(fake_now), |
48 mock_backoff_entry_(&backoff_policy_) { | 48 mock_backoff_entry_(&backoff_policy_) { |
49 InitPolicy(); | 49 InitPolicy(); |
50 | 50 |
51 mock_backoff_entry_.set_fake_now(fake_now); | 51 mock_backoff_entry_.set_fake_now(fake_now); |
52 set_exponential_backoff_release_time(exponential_backoff_release_time); | 52 set_exponential_backoff_release_time(exponential_backoff_release_time); |
53 set_sliding_window_release_time(sliding_window_release_time); | 53 set_sliding_window_release_time(sliding_window_release_time); |
54 } | 54 } |
55 | 55 |
56 void InitPolicy() { | 56 void InitPolicy() { |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 return out << time.ToInternalValue(); | 194 return out << time.ToInternalValue(); |
195 } | 195 } |
196 | 196 |
197 TEST_F(URLRequestThrottlerEntryTest, CanThrottleRequest) { | 197 TEST_F(URLRequestThrottlerEntryTest, CanThrottleRequest) { |
198 TestNetworkDelegate d; | 198 TestNetworkDelegate d; |
199 context_.set_network_delegate(&d); | 199 context_.set_network_delegate(&d); |
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 context_.network_delegate())); |
205 d.set_can_throttle_requests(true); | 206 d.set_can_throttle_requests(true); |
206 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); | 207 EXPECT_TRUE(entry_->ShouldRejectRequest(request_, |
| 208 context_.network_delegate())); |
207 } | 209 } |
208 | 210 |
209 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { | 211 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { |
210 base::StatisticsDeltaReader statistics_delta_reader; | 212 base::StatisticsDeltaReader statistics_delta_reader; |
211 entry_->set_exponential_backoff_release_time( | 213 entry_->set_exponential_backoff_release_time( |
212 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); | 214 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); |
213 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); | 215 EXPECT_TRUE(entry_->ShouldRejectRequest(request_, |
| 216 context_.network_delegate())); |
214 | 217 |
215 // Also end-to-end test the load flags exceptions. | 218 // Also end-to-end test the load flags exceptions. |
216 request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE); | 219 request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE); |
217 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 220 EXPECT_FALSE(entry_->ShouldRejectRequest(request_, |
| 221 context_.network_delegate())); |
218 | 222 |
219 scoped_ptr<base::HistogramSamples> samples( | 223 scoped_ptr<base::HistogramSamples> samples( |
220 statistics_delta_reader.GetHistogramSamplesSinceCreation( | 224 statistics_delta_reader.GetHistogramSamplesSinceCreation( |
221 kRequestThrottledHistogramName)); | 225 kRequestThrottledHistogramName)); |
222 ASSERT_EQ(1, samples->GetCount(0)); | 226 ASSERT_EQ(1, samples->GetCount(0)); |
223 ASSERT_EQ(1, samples->GetCount(1)); | 227 ASSERT_EQ(1, samples->GetCount(1)); |
224 } | 228 } |
225 | 229 |
226 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { | 230 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { |
227 base::StatisticsDeltaReader statistics_delta_reader; | 231 base::StatisticsDeltaReader statistics_delta_reader; |
228 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); | 232 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); |
229 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 233 EXPECT_FALSE(entry_->ShouldRejectRequest(request_, |
| 234 context_.network_delegate())); |
230 entry_->set_exponential_backoff_release_time( | 235 entry_->set_exponential_backoff_release_time( |
231 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); | 236 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); |
232 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 237 EXPECT_FALSE(entry_->ShouldRejectRequest(request_, |
| 238 context_.network_delegate())); |
233 | 239 |
234 scoped_ptr<base::HistogramSamples> samples( | 240 scoped_ptr<base::HistogramSamples> samples( |
235 statistics_delta_reader.GetHistogramSamplesSinceCreation( | 241 statistics_delta_reader.GetHistogramSamplesSinceCreation( |
236 kRequestThrottledHistogramName)); | 242 kRequestThrottledHistogramName)); |
237 ASSERT_EQ(2, samples->GetCount(0)); | 243 ASSERT_EQ(2, samples->GetCount(0)); |
238 ASSERT_EQ(0, samples->GetCount(1)); | 244 ASSERT_EQ(0, samples->GetCount(1)); |
239 } | 245 } |
240 | 246 |
241 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { | 247 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { |
242 MockURLRequestThrottlerHeaderAdapter failure_response(503); | 248 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 | 357 |
352 class URLRequestThrottlerManagerTest : public testing::Test { | 358 class URLRequestThrottlerManagerTest : public testing::Test { |
353 protected: | 359 protected: |
354 URLRequestThrottlerManagerTest() | 360 URLRequestThrottlerManagerTest() |
355 : request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {} | 361 : request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {} |
356 | 362 |
357 virtual void SetUp() { | 363 virtual void SetUp() { |
358 request_.SetLoadFlags(0); | 364 request_.SetLoadFlags(0); |
359 } | 365 } |
360 | 366 |
| 367 void ExpectEntryAllowsAllOnErrorIfOptedOut( |
| 368 URLRequestThrottlerEntryInterface* entry, |
| 369 bool opted_out, |
| 370 const URLRequest& request) { |
| 371 EXPECT_FALSE(entry->ShouldRejectRequest(request, |
| 372 context_.network_delegate())); |
| 373 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); |
| 374 for (int i = 0; i < 10; ++i) { |
| 375 // Host doesn't really matter in this scenario so we skip it. |
| 376 entry->UpdateWithResponse(std::string(), &failure_adapter); |
| 377 } |
| 378 EXPECT_NE(opted_out, entry->ShouldRejectRequest( |
| 379 request, context_.network_delegate())); |
| 380 |
| 381 if (opted_out) { |
| 382 // We're not mocking out GetTimeNow() in this scenario |
| 383 // so add a 100 ms buffer to avoid flakiness (that should always |
| 384 // give enough time to get from the TimeTicks::Now() call here |
| 385 // to the TimeTicks::Now() call in the entry class). |
| 386 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), |
| 387 entry->GetExponentialBackoffReleaseTime()); |
| 388 } else { |
| 389 // As above, add 100 ms. |
| 390 EXPECT_LT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), |
| 391 entry->GetExponentialBackoffReleaseTime()); |
| 392 } |
| 393 } |
| 394 |
361 // context_ must be declared before request_. | 395 // context_ must be declared before request_. |
362 TestURLRequestContext context_; | 396 TestURLRequestContext context_; |
363 TestURLRequest request_; | 397 TestURLRequest request_; |
364 }; | 398 }; |
365 | 399 |
366 TEST_F(URLRequestThrottlerManagerTest, IsUrlStandardised) { | 400 TEST_F(URLRequestThrottlerManagerTest, IsUrlStandardised) { |
367 MockURLRequestThrottlerManager manager; | 401 MockURLRequestThrottlerManager manager; |
368 GurlAndString test_values[] = { | 402 GurlAndString test_values[] = { |
369 GurlAndString(GURL("http://www.example.com"), | 403 GurlAndString(GURL("http://www.example.com"), |
370 std::string("http://www.example.com/"), | 404 std::string("http://www.example.com/"), |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 | 454 |
421 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 455 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
422 manager.RegisterRequestUrl(GURL("http://www.google.com/")); | 456 manager.RegisterRequestUrl(GURL("http://www.google.com/")); |
423 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0")); | 457 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0")); |
424 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1")); | 458 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1")); |
425 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure")); | 459 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure")); |
426 | 460 |
427 EXPECT_EQ(3, manager.GetNumberOfEntries()); | 461 EXPECT_EQ(3, manager.GetNumberOfEntries()); |
428 } | 462 } |
429 | 463 |
430 void ExpectEntryAllowsAllOnErrorIfOptedOut( | |
431 net::URLRequestThrottlerEntryInterface* entry, | |
432 bool opted_out, | |
433 const URLRequest& request) { | |
434 EXPECT_FALSE(entry->ShouldRejectRequest(request)); | |
435 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); | |
436 for (int i = 0; i < 10; ++i) { | |
437 // Host doesn't really matter in this scenario so we skip it. | |
438 entry->UpdateWithResponse(std::string(), &failure_adapter); | |
439 } | |
440 EXPECT_NE(opted_out, entry->ShouldRejectRequest(request)); | |
441 | |
442 if (opted_out) { | |
443 // We're not mocking out GetTimeNow() in this scenario | |
444 // so add a 100 ms buffer to avoid flakiness (that should always | |
445 // give enough time to get from the TimeTicks::Now() call here | |
446 // to the TimeTicks::Now() call in the entry class). | |
447 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), | |
448 entry->GetExponentialBackoffReleaseTime()); | |
449 } else { | |
450 // As above, add 100 ms. | |
451 EXPECT_LT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), | |
452 entry->GetExponentialBackoffReleaseTime()); | |
453 } | |
454 } | |
455 | |
456 TEST_F(URLRequestThrottlerManagerTest, OptOutHeader) { | 464 TEST_F(URLRequestThrottlerManagerTest, OptOutHeader) { |
457 MockURLRequestThrottlerManager manager; | 465 MockURLRequestThrottlerManager manager; |
458 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry = | 466 scoped_refptr<URLRequestThrottlerEntryInterface> entry = |
459 manager.RegisterRequestUrl(GURL("http://www.google.com/yodude")); | 467 manager.RegisterRequestUrl(GURL("http://www.google.com/yodude")); |
460 | 468 |
461 // Fake a response with the opt-out header. | 469 // Fake a response with the opt-out header. |
462 MockURLRequestThrottlerHeaderAdapter response_adapter( | 470 MockURLRequestThrottlerHeaderAdapter response_adapter( |
463 std::string(), | 471 std::string(), |
464 MockURLRequestThrottlerEntry::kExponentialThrottlingDisableValue, | 472 MockURLRequestThrottlerEntry::kExponentialThrottlingDisableValue, |
465 200); | 473 200); |
466 entry->UpdateWithResponse("www.google.com", &response_adapter); | 474 entry->UpdateWithResponse("www.google.com", &response_adapter); |
467 | 475 |
468 // Ensure that the same entry on error always allows everything. | 476 // Ensure that the same entry on error always allows everything. |
469 ExpectEntryAllowsAllOnErrorIfOptedOut(entry.get(), true, request_); | 477 ExpectEntryAllowsAllOnErrorIfOptedOut(entry.get(), true, request_); |
470 | 478 |
471 // Ensure that a freshly created entry (for a different URL on an | 479 // Ensure that a freshly created entry (for a different URL on an |
472 // already opted-out host) also gets "always allow" behavior. | 480 // already opted-out host) also gets "always allow" behavior. |
473 scoped_refptr<net::URLRequestThrottlerEntryInterface> other_entry = | 481 scoped_refptr<URLRequestThrottlerEntryInterface> other_entry = |
474 manager.RegisterRequestUrl(GURL("http://www.google.com/bingobob")); | 482 manager.RegisterRequestUrl(GURL("http://www.google.com/bingobob")); |
475 ExpectEntryAllowsAllOnErrorIfOptedOut(other_entry.get(), true, request_); | 483 ExpectEntryAllowsAllOnErrorIfOptedOut(other_entry.get(), true, request_); |
476 | 484 |
477 // Fake a response with the opt-out header incorrectly specified. | 485 // Fake a response with the opt-out header incorrectly specified. |
478 scoped_refptr<net::URLRequestThrottlerEntryInterface> no_opt_out_entry = | 486 scoped_refptr<URLRequestThrottlerEntryInterface> no_opt_out_entry = |
479 manager.RegisterRequestUrl(GURL("http://www.nike.com/justdoit")); | 487 manager.RegisterRequestUrl(GURL("http://www.nike.com/justdoit")); |
480 MockURLRequestThrottlerHeaderAdapter wrong_adapter( | 488 MockURLRequestThrottlerHeaderAdapter wrong_adapter( |
481 std::string(), "yesplease", 200); | 489 std::string(), "yesplease", 200); |
482 no_opt_out_entry->UpdateWithResponse("www.nike.com", &wrong_adapter); | 490 no_opt_out_entry->UpdateWithResponse("www.nike.com", &wrong_adapter); |
483 ExpectEntryAllowsAllOnErrorIfOptedOut( | 491 ExpectEntryAllowsAllOnErrorIfOptedOut( |
484 no_opt_out_entry.get(), false, request_); | 492 no_opt_out_entry.get(), false, request_); |
485 | 493 |
486 // A localhost entry should always be opted out. | 494 // A localhost entry should always be opted out. |
487 scoped_refptr<net::URLRequestThrottlerEntryInterface> localhost_entry = | 495 scoped_refptr<URLRequestThrottlerEntryInterface> localhost_entry = |
488 manager.RegisterRequestUrl(GURL("http://localhost/hello")); | 496 manager.RegisterRequestUrl(GURL("http://localhost/hello")); |
489 ExpectEntryAllowsAllOnErrorIfOptedOut(localhost_entry.get(), true, request_); | 497 ExpectEntryAllowsAllOnErrorIfOptedOut(localhost_entry.get(), true, request_); |
490 } | 498 } |
491 | 499 |
492 TEST_F(URLRequestThrottlerManagerTest, ClearOnNetworkChange) { | 500 TEST_F(URLRequestThrottlerManagerTest, ClearOnNetworkChange) { |
493 for (int i = 0; i < 3; ++i) { | 501 for (int i = 0; i < 3; ++i) { |
494 MockURLRequestThrottlerManager manager; | 502 MockURLRequestThrottlerManager manager; |
495 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_before = | 503 scoped_refptr<URLRequestThrottlerEntryInterface> entry_before = |
496 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 504 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
497 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); | 505 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); |
498 for (int j = 0; j < 10; ++j) { | 506 for (int j = 0; j < 10; ++j) { |
499 // Host doesn't really matter in this scenario so we skip it. | 507 // Host doesn't really matter in this scenario so we skip it. |
500 entry_before->UpdateWithResponse(std::string(), &failure_adapter); | 508 entry_before->UpdateWithResponse(std::string(), &failure_adapter); |
501 } | 509 } |
502 EXPECT_TRUE(entry_before->ShouldRejectRequest(request_)); | 510 EXPECT_TRUE(entry_before->ShouldRejectRequest(request_, |
| 511 context_.network_delegate())); |
503 | 512 |
504 switch (i) { | 513 switch (i) { |
505 case 0: | 514 case 0: |
506 manager.OnIPAddressChanged(); | 515 manager.OnIPAddressChanged(); |
507 break; | 516 break; |
508 case 1: | 517 case 1: |
509 manager.OnConnectionTypeChanged( | 518 manager.OnConnectionTypeChanged( |
510 net::NetworkChangeNotifier::CONNECTION_UNKNOWN); | 519 NetworkChangeNotifier::CONNECTION_UNKNOWN); |
511 break; | 520 break; |
512 case 2: | 521 case 2: |
513 manager.OnConnectionTypeChanged( | 522 manager.OnConnectionTypeChanged(NetworkChangeNotifier::CONNECTION_NONE); |
514 net::NetworkChangeNotifier::CONNECTION_NONE); | |
515 break; | 523 break; |
516 default: | 524 default: |
517 FAIL(); | 525 FAIL(); |
518 } | 526 } |
519 | 527 |
520 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = | 528 scoped_refptr<URLRequestThrottlerEntryInterface> entry_after = |
521 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 529 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
522 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); | 530 EXPECT_FALSE(entry_after->ShouldRejectRequest( |
| 531 request_, context_.network_delegate())); |
523 } | 532 } |
524 } | 533 } |
525 | 534 |
526 } // namespace net | 535 } // namespace net |
OLD | NEW |