Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(780)

Side by Side Diff: net/url_request/url_request_throttler_unittest.cc

Issue 1148603003: Remove X-Chrome-Exponential-Throttling header (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/url_request/url_request_throttler_test_support.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 // Host doesn't really matter in this scenario so we skip it.
367 entry->UpdateWithResponse(std::string(), &failure_adapter); 358 entry->UpdateWithResponse(503);
368 } 359 }
369 EXPECT_NE(opted_out, entry->ShouldRejectRequest( 360 EXPECT_NE(opted_out, entry->ShouldRejectRequest(
370 request, context_.network_delegate())); 361 request, context_.network_delegate()));
371 362
372 if (opted_out) { 363 if (opted_out) {
373 // We're not mocking out GetTimeNow() in this scenario 364 // We're not mocking out GetTimeNow() in this scenario
374 // so add a 100 ms buffer to avoid flakiness (that should always 365 // so add a 100 ms buffer to avoid flakiness (that should always
375 // give enough time to get from the TimeTicks::Now() call here 366 // give enough time to get from the TimeTicks::Now() call here
376 // to the TimeTicks::Now() call in the entry class). 367 // to the TimeTicks::Now() call in the entry class).
377 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), 368 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100),
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 436
446 manager.RegisterRequestUrl(GURL("http://www.example.com/")); 437 manager.RegisterRequestUrl(GURL("http://www.example.com/"));
447 manager.RegisterRequestUrl(GURL("http://www.google.com/")); 438 manager.RegisterRequestUrl(GURL("http://www.google.com/"));
448 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0")); 439 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0"));
449 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1")); 440 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1"));
450 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure")); 441 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure"));
451 442
452 EXPECT_EQ(3, manager.GetNumberOfEntries()); 443 EXPECT_EQ(3, manager.GetNumberOfEntries());
453 } 444 }
454 445
455 TEST_F(URLRequestThrottlerManagerTest, OptOutHeader) { 446 TEST_F(URLRequestThrottlerManagerTest, LocalHostOptedOut) {
456 MockURLRequestThrottlerManager manager; 447 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. 448 // A localhost entry should always be opted out.
486 scoped_refptr<URLRequestThrottlerEntryInterface> localhost_entry = 449 scoped_refptr<URLRequestThrottlerEntryInterface> localhost_entry =
487 manager.RegisterRequestUrl(GURL("http://localhost/hello")); 450 manager.RegisterRequestUrl(GURL("http://localhost/hello"));
488 ExpectEntryAllowsAllOnErrorIfOptedOut(localhost_entry.get(), true, *request_); 451 EXPECT_FALSE(localhost_entry->ShouldRejectRequest(
452 (*request_), context_.network_delegate()));
453 for (int i = 0; i < 10; ++i) {
454 // Host doesn't really matter in this scenario so we skip it.
mmenke 2015/05/19 20:31:24 Obsolete comment.
xunjieli 2015/05/19 20:54:16 Done.
455 localhost_entry->UpdateWithResponse(503);
456 }
457 EXPECT_FALSE(localhost_entry->ShouldRejectRequest(
458 (*request_), context_.network_delegate()));
459
460 // We're not mocking out GetTimeNow() in this scenario
461 // so add a 100 ms buffer to avoid flakiness (that should always
462 // give enough time to get from the TimeTicks::Now() call here
463 // to the TimeTicks::Now() call in the entry class).
464 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100),
465 localhost_entry->GetExponentialBackoffReleaseTime());
489 } 466 }
490 467
491 TEST_F(URLRequestThrottlerManagerTest, ClearOnNetworkChange) { 468 TEST_F(URLRequestThrottlerManagerTest, ClearOnNetworkChange) {
492 for (int i = 0; i < 3; ++i) { 469 for (int i = 0; i < 3; ++i) {
493 MockURLRequestThrottlerManager manager; 470 MockURLRequestThrottlerManager manager;
494 scoped_refptr<URLRequestThrottlerEntryInterface> entry_before = 471 scoped_refptr<URLRequestThrottlerEntryInterface> entry_before =
495 manager.RegisterRequestUrl(GURL("http://www.example.com/")); 472 manager.RegisterRequestUrl(GURL("http://www.example.com/"));
496 MockURLRequestThrottlerHeaderAdapter failure_adapter(503);
497 for (int j = 0; j < 10; ++j) { 473 for (int j = 0; j < 10; ++j) {
498 // Host doesn't really matter in this scenario so we skip it. 474 // Host doesn't really matter in this scenario so we skip it.
mmenke 2015/05/19 20:31:24 Obsolete comment.
xunjieli 2015/05/19 20:54:16 Done.
499 entry_before->UpdateWithResponse(std::string(), &failure_adapter); 475 entry_before->UpdateWithResponse(503);
500 } 476 }
501 EXPECT_TRUE(entry_before->ShouldRejectRequest(*request_, 477 EXPECT_TRUE(entry_before->ShouldRejectRequest(*request_,
502 context_.network_delegate())); 478 context_.network_delegate()));
503 479
504 switch (i) { 480 switch (i) {
505 case 0: 481 case 0:
506 manager.OnIPAddressChanged(); 482 manager.OnIPAddressChanged();
507 break; 483 break;
508 case 1: 484 case 1:
509 manager.OnConnectionTypeChanged( 485 manager.OnConnectionTypeChanged(
510 NetworkChangeNotifier::CONNECTION_UNKNOWN); 486 NetworkChangeNotifier::CONNECTION_UNKNOWN);
511 break; 487 break;
512 case 2: 488 case 2:
513 manager.OnConnectionTypeChanged(NetworkChangeNotifier::CONNECTION_NONE); 489 manager.OnConnectionTypeChanged(NetworkChangeNotifier::CONNECTION_NONE);
514 break; 490 break;
515 default: 491 default:
516 FAIL(); 492 FAIL();
517 } 493 }
518 494
519 scoped_refptr<URLRequestThrottlerEntryInterface> entry_after = 495 scoped_refptr<URLRequestThrottlerEntryInterface> entry_after =
520 manager.RegisterRequestUrl(GURL("http://www.example.com/")); 496 manager.RegisterRequestUrl(GURL("http://www.example.com/"));
521 EXPECT_FALSE(entry_after->ShouldRejectRequest( 497 EXPECT_FALSE(entry_after->ShouldRejectRequest(
522 *request_, context_.network_delegate())); 498 *request_, context_.network_delegate()));
523 } 499 }
524 } 500 }
525 501
526 } // namespace net 502 } // namespace net
OLDNEW
« no previous file with comments | « net/url_request/url_request_throttler_test_support.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698