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" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "base/test/histogram_tester.h" | 13 #include "base/test/histogram_tester.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
16 #include "net/base/request_priority.h" | 16 #include "net/base/request_priority.h" |
17 #include "net/base/test_completion_callback.h" | 17 #include "net/base/test_completion_callback.h" |
18 #include "net/url_request/url_request.h" | 18 #include "net/url_request/url_request.h" |
19 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
20 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
21 #include "net/url_request/url_request_throttler_header_interface.h" | |
22 #include "net/url_request/url_request_throttler_test_support.h" | 21 #include "net/url_request/url_request_throttler_test_support.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
24 | 23 |
25 using base::TimeDelta; | 24 using base::TimeDelta; |
26 using base::TimeTicks; | 25 using base::TimeTicks; |
27 | 26 |
28 namespace net { | 27 namespace net { |
29 | 28 |
30 namespace { | 29 namespace { |
31 | 30 |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 entry_->set_exponential_backoff_release_time( | 227 entry_->set_exponential_backoff_release_time( |
229 entry_->ImplGetTimeNow() - TimeDelta::FromMilliseconds(1)); | 228 entry_->ImplGetTimeNow() - TimeDelta::FromMilliseconds(1)); |
230 EXPECT_FALSE(entry_->ShouldRejectRequest(*request_, | 229 EXPECT_FALSE(entry_->ShouldRejectRequest(*request_, |
231 context_.network_delegate())); | 230 context_.network_delegate())); |
232 | 231 |
233 histogram_tester.ExpectBucketCount(kRequestThrottledHistogramName, 0, 2); | 232 histogram_tester.ExpectBucketCount(kRequestThrottledHistogramName, 0, 2); |
234 histogram_tester.ExpectBucketCount(kRequestThrottledHistogramName, 1, 0); | 233 histogram_tester.ExpectBucketCount(kRequestThrottledHistogramName, 1, 0); |
235 } | 234 } |
236 | 235 |
237 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { | 236 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { |
238 MockURLRequestThrottlerHeaderAdapter failure_response(503); | 237 entry_->UpdateWithResponse(503); |
239 entry_->UpdateWithResponse(std::string(), &failure_response); | |
240 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), | 238 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), |
241 entry_->ImplGetTimeNow()) | 239 entry_->ImplGetTimeNow()) |
242 << "A failure should increase the release_time"; | 240 << "A failure should increase the release_time"; |
243 } | 241 } |
244 | 242 |
245 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { | 243 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { |
246 MockURLRequestThrottlerHeaderAdapter success_response(200); | 244 entry_->UpdateWithResponse(200); |
247 entry_->UpdateWithResponse(std::string(), &success_response); | |
248 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), | 245 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), |
249 entry_->ImplGetTimeNow()) | 246 entry_->ImplGetTimeNow()) |
250 << "A success should not add any delay"; | 247 << "A success should not add any delay"; |
251 } | 248 } |
252 | 249 |
253 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccessThenFailure) { | 250 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccessThenFailure) { |
254 MockURLRequestThrottlerHeaderAdapter failure_response(503); | 251 entry_->UpdateWithResponse(200); |
255 MockURLRequestThrottlerHeaderAdapter success_response(200); | 252 entry_->UpdateWithResponse(503); |
256 entry_->UpdateWithResponse(std::string(), &success_response); | |
257 entry_->UpdateWithResponse(std::string(), &failure_response); | |
258 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), | 253 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), |
259 entry_->ImplGetTimeNow()) | 254 entry_->ImplGetTimeNow()) |
260 << "This scenario should add delay"; | 255 << "This scenario should add delay"; |
261 entry_->UpdateWithResponse(std::string(), &success_response); | 256 entry_->UpdateWithResponse(200); |
262 } | 257 } |
263 | 258 |
264 TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) { | 259 TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) { |
265 TimeDelta lifetime = TimeDelta::FromMilliseconds( | 260 TimeDelta lifetime = TimeDelta::FromMilliseconds( |
266 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs); | 261 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs); |
267 const TimeDelta kFiveMs = TimeDelta::FromMilliseconds(5); | 262 const TimeDelta kFiveMs = TimeDelta::FromMilliseconds(5); |
268 | 263 |
269 TimeAndBool test_values[] = { | 264 TimeAndBool test_values[] = { |
270 TimeAndBool(now_, false, __LINE__), | 265 TimeAndBool(now_, false, __LINE__), |
271 TimeAndBool(now_ - kFiveMs, false, __LINE__), | 266 TimeAndBool(now_ - kFiveMs, false, __LINE__), |
272 TimeAndBool(now_ + kFiveMs, false, __LINE__), | 267 TimeAndBool(now_ + kFiveMs, false, __LINE__), |
273 TimeAndBool(now_ - (lifetime - kFiveMs), false, __LINE__), | 268 TimeAndBool(now_ - (lifetime - kFiveMs), false, __LINE__), |
274 TimeAndBool(now_ - lifetime, true, __LINE__), | 269 TimeAndBool(now_ - lifetime, true, __LINE__), |
275 TimeAndBool(now_ - (lifetime + kFiveMs), true, __LINE__)}; | 270 TimeAndBool(now_ - (lifetime + kFiveMs), true, __LINE__)}; |
276 | 271 |
277 for (unsigned int i = 0; i < arraysize(test_values); ++i) { | 272 for (unsigned int i = 0; i < arraysize(test_values); ++i) { |
278 entry_->set_exponential_backoff_release_time(test_values[i].time); | 273 entry_->set_exponential_backoff_release_time(test_values[i].time); |
279 EXPECT_EQ(entry_->IsEntryOutdated(), test_values[i].result) << | 274 EXPECT_EQ(entry_->IsEntryOutdated(), test_values[i].result) << |
280 "Test case #" << i << " line " << test_values[i].line << " failed"; | 275 "Test case #" << i << " line " << test_values[i].line << " failed"; |
281 } | 276 } |
282 } | 277 } |
283 | 278 |
284 TEST_F(URLRequestThrottlerEntryTest, MaxAllowedBackoff) { | 279 TEST_F(URLRequestThrottlerEntryTest, MaxAllowedBackoff) { |
285 for (int i = 0; i < 30; ++i) { | 280 for (int i = 0; i < 30; ++i) { |
286 MockURLRequestThrottlerHeaderAdapter response_adapter(503); | 281 entry_->UpdateWithResponse(503); |
287 entry_->UpdateWithResponse(std::string(), &response_adapter); | |
288 } | 282 } |
289 | 283 |
290 TimeDelta delay = entry_->GetExponentialBackoffReleaseTime() - now_; | 284 TimeDelta delay = entry_->GetExponentialBackoffReleaseTime() - now_; |
291 EXPECT_EQ(delay.InMilliseconds(), | 285 EXPECT_EQ(delay.InMilliseconds(), |
292 MockURLRequestThrottlerEntry::kDefaultMaximumBackoffMs); | 286 MockURLRequestThrottlerEntry::kDefaultMaximumBackoffMs); |
293 } | 287 } |
294 | 288 |
295 TEST_F(URLRequestThrottlerEntryTest, MalformedContent) { | 289 TEST_F(URLRequestThrottlerEntryTest, MalformedContent) { |
296 MockURLRequestThrottlerHeaderAdapter response_adapter(503); | |
297 for (int i = 0; i < 5; ++i) | 290 for (int i = 0; i < 5; ++i) |
298 entry_->UpdateWithResponse(std::string(), &response_adapter); | 291 entry_->UpdateWithResponse(503); |
299 | 292 |
300 TimeTicks release_after_failures = entry_->GetExponentialBackoffReleaseTime(); | 293 TimeTicks release_after_failures = entry_->GetExponentialBackoffReleaseTime(); |
301 | 294 |
302 // Inform the entry that a response body was malformed, which is supposed to | 295 // Inform the entry that a response body was malformed, which is supposed to |
303 // increase the back-off time. Note that we also submit a successful | 296 // increase the back-off time. Note that we also submit a successful |
304 // UpdateWithResponse to pair with ReceivedContentWasMalformed() since that | 297 // UpdateWithResponse to pair with ReceivedContentWasMalformed() since that |
305 // is what happens in practice (if a body is received, then a non-500 | 298 // is what happens in practice (if a body is received, then a non-500 |
306 // response must also have been received). | 299 // response must also have been received). |
307 entry_->ReceivedContentWasMalformed(200); | 300 entry_->ReceivedContentWasMalformed(200); |
308 MockURLRequestThrottlerHeaderAdapter success_adapter(200); | 301 entry_->UpdateWithResponse(200); |
309 entry_->UpdateWithResponse(std::string(), &success_adapter); | |
310 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), release_after_failures); | 302 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), release_after_failures); |
311 } | 303 } |
312 | 304 |
313 TEST_F(URLRequestThrottlerEntryTest, SlidingWindow) { | 305 TEST_F(URLRequestThrottlerEntryTest, SlidingWindow) { |
314 int max_send = URLRequestThrottlerEntry::kDefaultMaxSendThreshold; | 306 int max_send = URLRequestThrottlerEntry::kDefaultMaxSendThreshold; |
315 int sliding_window = | 307 int sliding_window = |
316 URLRequestThrottlerEntry::kDefaultSlidingWindowPeriodMs; | 308 URLRequestThrottlerEntry::kDefaultSlidingWindowPeriodMs; |
317 | 309 |
318 TimeTicks time_1 = entry_->ImplGetTimeNow() + | 310 TimeTicks time_1 = entry_->ImplGetTimeNow() + |
319 TimeDelta::FromMilliseconds(sliding_window / 3); | 311 TimeDelta::FromMilliseconds(sliding_window / 3); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 : request_(context_.CreateRequest(GURL(), DEFAULT_PRIORITY, NULL)) {} | 346 : request_(context_.CreateRequest(GURL(), DEFAULT_PRIORITY, NULL)) {} |
355 | 347 |
356 void SetUp() override { request_->SetLoadFlags(0); } | 348 void SetUp() override { request_->SetLoadFlags(0); } |
357 | 349 |
358 void ExpectEntryAllowsAllOnErrorIfOptedOut( | 350 void ExpectEntryAllowsAllOnErrorIfOptedOut( |
359 URLRequestThrottlerEntryInterface* entry, | 351 URLRequestThrottlerEntryInterface* entry, |
360 bool opted_out, | 352 bool opted_out, |
361 const URLRequest& request) { | 353 const URLRequest& request) { |
362 EXPECT_FALSE(entry->ShouldRejectRequest(request, | 354 EXPECT_FALSE(entry->ShouldRejectRequest(request, |
363 context_.network_delegate())); | 355 context_.network_delegate())); |
364 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); | |
365 for (int i = 0; i < 10; ++i) { | 356 for (int i = 0; i < 10; ++i) { |
366 // Host doesn't really matter in this scenario so we skip it. | 357 entry->UpdateWithResponse(503); |
367 entry->UpdateWithResponse(std::string(), &failure_adapter); | |
368 } | 358 } |
369 EXPECT_NE(opted_out, entry->ShouldRejectRequest( | 359 EXPECT_NE(opted_out, entry->ShouldRejectRequest( |
370 request, context_.network_delegate())); | 360 request, context_.network_delegate())); |
371 | 361 |
372 if (opted_out) { | 362 if (opted_out) { |
373 // We're not mocking out GetTimeNow() in this scenario | 363 // We're not mocking out GetTimeNow() in this scenario |
374 // so add a 100 ms buffer to avoid flakiness (that should always | 364 // so add a 100 ms buffer to avoid flakiness (that should always |
375 // give enough time to get from the TimeTicks::Now() call here | 365 // give enough time to get from the TimeTicks::Now() call here |
376 // to the TimeTicks::Now() call in the entry class). | 366 // to the TimeTicks::Now() call in the entry class). |
377 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), | 367 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 | 435 |
446 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 436 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
447 manager.RegisterRequestUrl(GURL("http://www.google.com/")); | 437 manager.RegisterRequestUrl(GURL("http://www.google.com/")); |
448 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0")); | 438 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0")); |
449 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1")); | 439 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1")); |
450 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure")); | 440 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure")); |
451 | 441 |
452 EXPECT_EQ(3, manager.GetNumberOfEntries()); | 442 EXPECT_EQ(3, manager.GetNumberOfEntries()); |
453 } | 443 } |
454 | 444 |
455 TEST_F(URLRequestThrottlerManagerTest, OptOutHeader) { | 445 TEST_F(URLRequestThrottlerManagerTest, LocalHostOptedOut) { |
456 MockURLRequestThrottlerManager manager; | 446 MockURLRequestThrottlerManager manager; |
457 scoped_refptr<URLRequestThrottlerEntryInterface> entry = | |
458 manager.RegisterRequestUrl(GURL("http://www.google.com/yodude")); | |
459 | |
460 // Fake a response with the opt-out header. | |
461 MockURLRequestThrottlerHeaderAdapter response_adapter( | |
462 std::string(), | |
463 MockURLRequestThrottlerEntry::kExponentialThrottlingDisableValue, | |
464 200); | |
465 entry->UpdateWithResponse("www.google.com", &response_adapter); | |
466 | |
467 // Ensure that the same entry on error always allows everything. | |
468 ExpectEntryAllowsAllOnErrorIfOptedOut(entry.get(), true, *request_); | |
469 | |
470 // Ensure that a freshly created entry (for a different URL on an | |
471 // already opted-out host) also gets "always allow" behavior. | |
472 scoped_refptr<URLRequestThrottlerEntryInterface> other_entry = | |
473 manager.RegisterRequestUrl(GURL("http://www.google.com/bingobob")); | |
474 ExpectEntryAllowsAllOnErrorIfOptedOut(other_entry.get(), true, *request_); | |
475 | |
476 // Fake a response with the opt-out header incorrectly specified. | |
477 scoped_refptr<URLRequestThrottlerEntryInterface> no_opt_out_entry = | |
478 manager.RegisterRequestUrl(GURL("http://www.nike.com/justdoit")); | |
479 MockURLRequestThrottlerHeaderAdapter wrong_adapter( | |
480 std::string(), "yesplease", 200); | |
481 no_opt_out_entry->UpdateWithResponse("www.nike.com", &wrong_adapter); | |
482 ExpectEntryAllowsAllOnErrorIfOptedOut( | |
483 no_opt_out_entry.get(), false, *request_); | |
484 | |
485 // A localhost entry should always be opted out. | 447 // A localhost entry should always be opted out. |
486 scoped_refptr<URLRequestThrottlerEntryInterface> localhost_entry = | 448 scoped_refptr<URLRequestThrottlerEntryInterface> localhost_entry = |
487 manager.RegisterRequestUrl(GURL("http://localhost/hello")); | 449 manager.RegisterRequestUrl(GURL("http://localhost/hello")); |
488 ExpectEntryAllowsAllOnErrorIfOptedOut(localhost_entry.get(), true, *request_); | 450 EXPECT_FALSE(localhost_entry->ShouldRejectRequest( |
| 451 (*request_), context_.network_delegate())); |
| 452 for (int i = 0; i < 10; ++i) { |
| 453 localhost_entry->UpdateWithResponse(503); |
| 454 } |
| 455 EXPECT_FALSE(localhost_entry->ShouldRejectRequest( |
| 456 (*request_), context_.network_delegate())); |
| 457 |
| 458 // We're not mocking out GetTimeNow() in this scenario |
| 459 // so add a 100 ms buffer to avoid flakiness (that should always |
| 460 // give enough time to get from the TimeTicks::Now() call here |
| 461 // to the TimeTicks::Now() call in the entry class). |
| 462 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), |
| 463 localhost_entry->GetExponentialBackoffReleaseTime()); |
489 } | 464 } |
490 | 465 |
491 TEST_F(URLRequestThrottlerManagerTest, ClearOnNetworkChange) { | 466 TEST_F(URLRequestThrottlerManagerTest, ClearOnNetworkChange) { |
492 for (int i = 0; i < 3; ++i) { | 467 for (int i = 0; i < 3; ++i) { |
493 MockURLRequestThrottlerManager manager; | 468 MockURLRequestThrottlerManager manager; |
494 scoped_refptr<URLRequestThrottlerEntryInterface> entry_before = | 469 scoped_refptr<URLRequestThrottlerEntryInterface> entry_before = |
495 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 470 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
496 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); | |
497 for (int j = 0; j < 10; ++j) { | 471 for (int j = 0; j < 10; ++j) { |
498 // Host doesn't really matter in this scenario so we skip it. | 472 entry_before->UpdateWithResponse(503); |
499 entry_before->UpdateWithResponse(std::string(), &failure_adapter); | |
500 } | 473 } |
501 EXPECT_TRUE(entry_before->ShouldRejectRequest(*request_, | 474 EXPECT_TRUE(entry_before->ShouldRejectRequest(*request_, |
502 context_.network_delegate())); | 475 context_.network_delegate())); |
503 | 476 |
504 switch (i) { | 477 switch (i) { |
505 case 0: | 478 case 0: |
506 manager.OnIPAddressChanged(); | 479 manager.OnIPAddressChanged(); |
507 break; | 480 break; |
508 case 1: | 481 case 1: |
509 manager.OnConnectionTypeChanged( | 482 manager.OnConnectionTypeChanged( |
510 NetworkChangeNotifier::CONNECTION_UNKNOWN); | 483 NetworkChangeNotifier::CONNECTION_UNKNOWN); |
511 break; | 484 break; |
512 case 2: | 485 case 2: |
513 manager.OnConnectionTypeChanged(NetworkChangeNotifier::CONNECTION_NONE); | 486 manager.OnConnectionTypeChanged(NetworkChangeNotifier::CONNECTION_NONE); |
514 break; | 487 break; |
515 default: | 488 default: |
516 FAIL(); | 489 FAIL(); |
517 } | 490 } |
518 | 491 |
519 scoped_refptr<URLRequestThrottlerEntryInterface> entry_after = | 492 scoped_refptr<URLRequestThrottlerEntryInterface> entry_after = |
520 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 493 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
521 EXPECT_FALSE(entry_after->ShouldRejectRequest( | 494 EXPECT_FALSE(entry_after->ShouldRejectRequest( |
522 *request_, context_.network_delegate())); | 495 *request_, context_.network_delegate())); |
523 } | 496 } |
524 } | 497 } |
525 | 498 |
526 } // namespace net | 499 } // namespace net |
OLD | NEW |