OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "net/base/load_flags.h" |
13 #include "net/base/test_completion_callback.h" | 14 #include "net/base/test_completion_callback.h" |
14 #include "net/url_request/url_request_context.h" | 15 #include "net/url_request/url_request_context.h" |
15 #include "net/url_request/url_request_throttler_header_interface.h" | 16 #include "net/url_request/url_request_throttler_header_interface.h" |
16 #include "net/url_request/url_request_throttler_test_support.h" | 17 #include "net/url_request/url_request_throttler_test_support.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 using base::TimeDelta; | 20 using base::TimeDelta; |
20 using base::TimeTicks; | 21 using base::TimeTicks; |
21 | 22 |
22 namespace net { | 23 namespace net { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 } | 61 } |
61 | 62 |
62 const BackoffEntry* GetBackoffEntry() const { | 63 const BackoffEntry* GetBackoffEntry() const { |
63 return &mock_backoff_entry_; | 64 return &mock_backoff_entry_; |
64 } | 65 } |
65 | 66 |
66 BackoffEntry* GetBackoffEntry() { | 67 BackoffEntry* GetBackoffEntry() { |
67 return &mock_backoff_entry_; | 68 return &mock_backoff_entry_; |
68 } | 69 } |
69 | 70 |
| 71 static bool ExplicitUserRequest(int load_flags) { |
| 72 return URLRequestThrottlerEntry::ExplicitUserRequest(load_flags); |
| 73 } |
| 74 |
70 void ResetToBlank(const TimeTicks& time_now) { | 75 void ResetToBlank(const TimeTicks& time_now) { |
71 fake_time_now_ = time_now; | 76 fake_time_now_ = time_now; |
72 mock_backoff_entry_.set_fake_now(time_now); | 77 mock_backoff_entry_.set_fake_now(time_now); |
73 | 78 |
74 GetBackoffEntry()->Reset(); | 79 GetBackoffEntry()->Reset(); |
75 GetBackoffEntry()->SetCustomReleaseTime(time_now); | 80 GetBackoffEntry()->SetCustomReleaseTime(time_now); |
76 set_sliding_window_release_time(time_now); | 81 set_sliding_window_release_time(time_now); |
77 } | 82 } |
78 | 83 |
79 // Overridden for tests. | 84 // Overridden for tests. |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 original_samples_.clear(); | 227 original_samples_.clear(); |
223 } | 228 } |
224 | 229 |
225 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { | 230 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { |
226 return out << time.ToInternalValue(); | 231 return out << time.ToInternalValue(); |
227 } | 232 } |
228 | 233 |
229 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { | 234 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { |
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_TRUE(entry_->IsDuringExponentialBackoff()); | 237 EXPECT_TRUE(entry_->ShouldRejectRequest(0)); |
| 238 |
| 239 // Also end-to-end test the load flags exceptions. |
| 240 EXPECT_FALSE(entry_->ShouldRejectRequest(LOAD_MAYBE_USER_GESTURE)); |
233 | 241 |
234 CalculateHistogramDeltas(); | 242 CalculateHistogramDeltas(); |
235 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"].counts(0)); | 243 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"].counts(0)); |
236 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"].counts(1)); | 244 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"].counts(1)); |
237 } | 245 } |
238 | 246 |
239 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { | 247 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { |
240 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); | 248 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); |
241 EXPECT_FALSE(entry_->IsDuringExponentialBackoff()); | 249 EXPECT_FALSE(entry_->ShouldRejectRequest(0)); |
242 entry_->set_exponential_backoff_release_time( | 250 entry_->set_exponential_backoff_release_time( |
243 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); | 251 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); |
244 EXPECT_FALSE(entry_->IsDuringExponentialBackoff()); | 252 EXPECT_FALSE(entry_->ShouldRejectRequest(0)); |
245 | 253 |
246 CalculateHistogramDeltas(); | 254 CalculateHistogramDeltas(); |
247 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"].counts(0)); | 255 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"].counts(0)); |
248 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"].counts(1)); | 256 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"].counts(1)); |
249 } | 257 } |
250 | 258 |
251 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateRetryAfter) { | 259 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateRetryAfter) { |
252 // If the response we received has a retry-after field, | 260 // If the response we received has a retry-after field, |
253 // the request should be delayed. | 261 // the request should be delayed. |
254 MockURLRequestThrottlerHeaderAdapter header_w_delay_header("5.5", "", 200); | 262 MockURLRequestThrottlerHeaderAdapter header_w_delay_header("5.5", "", 200); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 EXPECT_EQ(time_2, entry_->sliding_window_release_time()); | 378 EXPECT_EQ(time_2, entry_->sliding_window_release_time()); |
371 | 379 |
372 entry_->fake_time_now_ = time_3; | 380 entry_->fake_time_now_ = time_3; |
373 | 381 |
374 for (int i = 0; i < (max_send + 1) / 2; ++i) | 382 for (int i = 0; i < (max_send + 1) / 2; ++i) |
375 EXPECT_EQ(0, entry_->ReserveSendingTimeForNextRequest(TimeTicks())); | 383 EXPECT_EQ(0, entry_->ReserveSendingTimeForNextRequest(TimeTicks())); |
376 | 384 |
377 EXPECT_EQ(time_4, entry_->sliding_window_release_time()); | 385 EXPECT_EQ(time_4, entry_->sliding_window_release_time()); |
378 } | 386 } |
379 | 387 |
| 388 TEST_F(URLRequestThrottlerEntryTest, ExplicitUserRequest) { |
| 389 ASSERT_FALSE(MockURLRequestThrottlerEntry::ExplicitUserRequest(0)); |
| 390 ASSERT_TRUE(MockURLRequestThrottlerEntry::ExplicitUserRequest( |
| 391 LOAD_MAYBE_USER_GESTURE)); |
| 392 ASSERT_FALSE(MockURLRequestThrottlerEntry::ExplicitUserRequest( |
| 393 ~LOAD_MAYBE_USER_GESTURE)); |
| 394 } |
| 395 |
380 TEST(URLRequestThrottlerManager, IsUrlStandardised) { | 396 TEST(URLRequestThrottlerManager, IsUrlStandardised) { |
381 MockURLRequestThrottlerManager manager; | 397 MockURLRequestThrottlerManager manager; |
382 GurlAndString test_values[] = { | 398 GurlAndString test_values[] = { |
383 GurlAndString(GURL("http://www.example.com"), | 399 GurlAndString(GURL("http://www.example.com"), |
384 std::string("http://www.example.com/"), | 400 std::string("http://www.example.com/"), |
385 __LINE__), | 401 __LINE__), |
386 GurlAndString(GURL("http://www.Example.com"), | 402 GurlAndString(GURL("http://www.Example.com"), |
387 std::string("http://www.example.com/"), | 403 std::string("http://www.example.com/"), |
388 __LINE__), | 404 __LINE__), |
389 GurlAndString(GURL("http://www.ex4mple.com/Pr4c71c41"), | 405 GurlAndString(GURL("http://www.ex4mple.com/Pr4c71c41"), |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0")); | 453 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0")); |
438 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1")); | 454 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1")); |
439 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure")); | 455 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure")); |
440 | 456 |
441 EXPECT_EQ(3, manager.GetNumberOfEntries()); | 457 EXPECT_EQ(3, manager.GetNumberOfEntries()); |
442 } | 458 } |
443 | 459 |
444 void ExpectEntryAllowsAllOnErrorIfOptedOut( | 460 void ExpectEntryAllowsAllOnErrorIfOptedOut( |
445 net::URLRequestThrottlerEntryInterface* entry, | 461 net::URLRequestThrottlerEntryInterface* entry, |
446 bool opted_out) { | 462 bool opted_out) { |
447 EXPECT_FALSE(entry->IsDuringExponentialBackoff()); | 463 EXPECT_FALSE(entry->ShouldRejectRequest(0)); |
448 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); | 464 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); |
449 for (int i = 0; i < 10; ++i) { | 465 for (int i = 0; i < 10; ++i) { |
450 // Host doesn't really matter in this scenario so we skip it. | 466 // Host doesn't really matter in this scenario so we skip it. |
451 entry->UpdateWithResponse("", &failure_adapter); | 467 entry->UpdateWithResponse("", &failure_adapter); |
452 } | 468 } |
453 EXPECT_NE(opted_out, entry->IsDuringExponentialBackoff()); | 469 EXPECT_NE(opted_out, entry->ShouldRejectRequest(0)); |
454 | 470 |
455 if (opted_out) { | 471 if (opted_out) { |
456 // We're not mocking out GetTimeNow() in this scenario | 472 // We're not mocking out GetTimeNow() in this scenario |
457 // so add a 100 ms buffer to avoid flakiness (that should always | 473 // so add a 100 ms buffer to avoid flakiness (that should always |
458 // give enough time to get from the TimeTicks::Now() call here | 474 // give enough time to get from the TimeTicks::Now() call here |
459 // to the TimeTicks::Now() call in the entry class). | 475 // to the TimeTicks::Now() call in the entry class). |
460 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), | 476 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), |
461 entry->GetExponentialBackoffReleaseTime()); | 477 entry->GetExponentialBackoffReleaseTime()); |
462 } else { | 478 } else { |
463 // As above, add 100 ms. | 479 // As above, add 100 ms. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 TEST(URLRequestThrottlerManager, ClearOnNetworkChange) { | 519 TEST(URLRequestThrottlerManager, ClearOnNetworkChange) { |
504 for (int i = 0; i < 3; ++i) { | 520 for (int i = 0; i < 3; ++i) { |
505 MockURLRequestThrottlerManager manager; | 521 MockURLRequestThrottlerManager manager; |
506 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_before = | 522 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_before = |
507 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 523 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
508 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); | 524 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); |
509 for (int j = 0; j < 10; ++j) { | 525 for (int j = 0; j < 10; ++j) { |
510 // Host doesn't really matter in this scenario so we skip it. | 526 // Host doesn't really matter in this scenario so we skip it. |
511 entry_before->UpdateWithResponse("", &failure_adapter); | 527 entry_before->UpdateWithResponse("", &failure_adapter); |
512 } | 528 } |
513 EXPECT_TRUE(entry_before->IsDuringExponentialBackoff()); | 529 EXPECT_TRUE(entry_before->ShouldRejectRequest(0)); |
514 | 530 |
515 switch (i) { | 531 switch (i) { |
516 case 0: | 532 case 0: |
517 manager.OnIPAddressChanged(); | 533 manager.OnIPAddressChanged(); |
518 break; | 534 break; |
519 case 1: | 535 case 1: |
520 manager.OnOnlineStateChanged(true); | 536 manager.OnOnlineStateChanged(true); |
521 break; | 537 break; |
522 case 2: | 538 case 2: |
523 manager.OnOnlineStateChanged(false); | 539 manager.OnOnlineStateChanged(false); |
524 break; | 540 break; |
525 default: | 541 default: |
526 FAIL(); | 542 FAIL(); |
527 } | 543 } |
528 | 544 |
529 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = | 545 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = |
530 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 546 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
531 EXPECT_FALSE(entry_after->IsDuringExponentialBackoff()); | 547 EXPECT_FALSE(entry_after->ShouldRejectRequest(0)); |
532 } | 548 } |
533 } | 549 } |
534 | 550 |
535 } // namespace net | 551 } // namespace net |
OLD | NEW |