| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/pickle.h" | 5 #include "base/pickle.h" |
| 6 #include "base/scoped_ptr.h" | 6 #include "base/scoped_ptr.h" |
| 7 #include "base/stringprintf.h" | 7 #include "base/stringprintf.h" |
| 8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "base/time.h" | 9 #include "base/time.h" |
| 10 #include "net/base/test_completion_callback.h" | 10 #include "net/base/test_completion_callback.h" |
| 11 #include "net/url_request/url_request_context.h" | 11 #include "net/url_request/url_request_context.h" |
| 12 #include "net/url_request/url_request_throttler_header_interface.h" | 12 #include "net/url_request/url_request_throttler_header_interface.h" |
| 13 #include "net/url_request/url_request_throttler_manager.h" | 13 #include "net/url_request/url_request_throttler_manager.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 using base::TimeDelta; | 16 using base::TimeDelta; |
| 17 using base::TimeTicks; | 17 using base::TimeTicks; |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 class MockURLRequestThrottlerManager; | 20 class MockURLRequestThrottlerManager; |
| 21 | 21 |
| 22 class MockBackoffEntry : public net::BackoffEntry { |
| 23 public: |
| 24 MockBackoffEntry(const net::BackoffEntry::Policy* policy) |
| 25 : net::BackoffEntry(policy), fake_now_(TimeTicks()) { |
| 26 } |
| 27 |
| 28 TimeTicks GetTimeNow() const { |
| 29 return fake_now_; |
| 30 } |
| 31 |
| 32 void SetFakeNow(TimeTicks now) { |
| 33 fake_now_ = now; |
| 34 } |
| 35 |
| 36 private: |
| 37 TimeTicks fake_now_; |
| 38 }; |
| 39 |
| 22 class MockURLRequestThrottlerEntry : public net::URLRequestThrottlerEntry { | 40 class MockURLRequestThrottlerEntry : public net::URLRequestThrottlerEntry { |
| 23 public : | 41 public : |
| 24 MockURLRequestThrottlerEntry() {} | 42 MockURLRequestThrottlerEntry() : mock_backoff_entry_(&backoff_policy_) {} |
| 25 MockURLRequestThrottlerEntry( | 43 MockURLRequestThrottlerEntry( |
| 26 const TimeTicks& exponential_backoff_release_time, | 44 const TimeTicks& exponential_backoff_release_time, |
| 27 const TimeTicks& sliding_window_release_time, | 45 const TimeTicks& sliding_window_release_time, |
| 28 const TimeTicks& fake_now) | 46 const TimeTicks& fake_now) |
| 29 : fake_time_now_(fake_now) { | 47 : fake_time_now_(fake_now), |
| 48 mock_backoff_entry_(&backoff_policy_) { |
| 49 mock_backoff_entry_.SetFakeNow(fake_now); |
| 30 set_exponential_backoff_release_time(exponential_backoff_release_time); | 50 set_exponential_backoff_release_time(exponential_backoff_release_time); |
| 31 set_sliding_window_release_time(sliding_window_release_time); | 51 set_sliding_window_release_time(sliding_window_release_time); |
| 32 } | 52 } |
| 33 virtual ~MockURLRequestThrottlerEntry() {} | 53 virtual ~MockURLRequestThrottlerEntry() {} |
| 34 | 54 |
| 55 const net::BackoffEntry* GetBackoffEntry() const { |
| 56 return &mock_backoff_entry_; |
| 57 } |
| 58 |
| 59 net::BackoffEntry* GetBackoffEntry() { |
| 60 return &mock_backoff_entry_; |
| 61 } |
| 62 |
| 35 void ResetToBlank(const TimeTicks& time_now) { | 63 void ResetToBlank(const TimeTicks& time_now) { |
| 36 fake_time_now_ = time_now; | 64 fake_time_now_ = time_now; |
| 37 set_exponential_backoff_release_time(time_now); | 65 mock_backoff_entry_.SetFakeNow(time_now); |
| 38 set_failure_count(0); | 66 |
| 67 GetBackoffEntry()->SetCustomReleaseTime(time_now); |
| 68 GetBackoffEntry()->InformOfRequest(true); // Sets failure count to 0. |
| 39 set_sliding_window_release_time(time_now); | 69 set_sliding_window_release_time(time_now); |
| 40 } | 70 } |
| 41 | 71 |
| 42 // Overridden for tests. | 72 // Overridden for tests. |
| 43 virtual TimeTicks GetTimeNow() const { return fake_time_now_; } | 73 virtual TimeTicks GetTimeNow() const { return fake_time_now_; } |
| 44 | 74 |
| 45 void set_exponential_backoff_release_time( | 75 void set_exponential_backoff_release_time( |
| 46 const base::TimeTicks& release_time) { | 76 const base::TimeTicks& release_time) { |
| 47 net::URLRequestThrottlerEntry::set_exponential_backoff_release_time( | 77 GetBackoffEntry()->SetCustomReleaseTime(release_time); |
| 48 release_time); | |
| 49 } | 78 } |
| 50 | 79 |
| 51 base::TimeTicks sliding_window_release_time() const { | 80 base::TimeTicks sliding_window_release_time() const { |
| 52 return net::URLRequestThrottlerEntry::sliding_window_release_time(); | 81 return net::URLRequestThrottlerEntry::sliding_window_release_time(); |
| 53 } | 82 } |
| 54 | 83 |
| 55 void set_sliding_window_release_time( | 84 void set_sliding_window_release_time( |
| 56 const base::TimeTicks& release_time) { | 85 const base::TimeTicks& release_time) { |
| 57 net::URLRequestThrottlerEntry::set_sliding_window_release_time( | 86 net::URLRequestThrottlerEntry::set_sliding_window_release_time( |
| 58 release_time); | 87 release_time); |
| 59 } | 88 } |
| 60 | 89 |
| 61 TimeTicks fake_time_now_; | 90 TimeTicks fake_time_now_; |
| 91 MockBackoffEntry mock_backoff_entry_; |
| 62 }; | 92 }; |
| 63 | 93 |
| 64 class MockURLRequestThrottlerHeaderAdapter | 94 class MockURLRequestThrottlerHeaderAdapter |
| 65 : public net::URLRequestThrottlerHeaderInterface { | 95 : public net::URLRequestThrottlerHeaderInterface { |
| 66 public: | 96 public: |
| 67 MockURLRequestThrottlerHeaderAdapter() | 97 MockURLRequestThrottlerHeaderAdapter() |
| 68 : fake_retry_value_("0.0"), | 98 : fake_retry_value_("0.0"), |
| 69 fake_response_code_(0) { | 99 fake_response_code_(0) { |
| 70 } | 100 } |
| 71 | 101 |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 | 249 |
| 220 TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) { | 250 TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) { |
| 221 TimeDelta lifetime = TimeDelta::FromMilliseconds( | 251 TimeDelta lifetime = TimeDelta::FromMilliseconds( |
| 222 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs); | 252 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs); |
| 223 const TimeDelta kFiveMs = TimeDelta::FromMilliseconds(5); | 253 const TimeDelta kFiveMs = TimeDelta::FromMilliseconds(5); |
| 224 | 254 |
| 225 TimeAndBool test_values[] = { | 255 TimeAndBool test_values[] = { |
| 226 TimeAndBool(now_, false, __LINE__), | 256 TimeAndBool(now_, false, __LINE__), |
| 227 TimeAndBool(now_ - kFiveMs, false, __LINE__), | 257 TimeAndBool(now_ - kFiveMs, false, __LINE__), |
| 228 TimeAndBool(now_ + kFiveMs, false, __LINE__), | 258 TimeAndBool(now_ + kFiveMs, false, __LINE__), |
| 229 TimeAndBool(now_ - lifetime, false, __LINE__), | 259 TimeAndBool(now_ - (lifetime - kFiveMs), false, __LINE__), |
| 260 TimeAndBool(now_ - lifetime, true, __LINE__), |
| 230 TimeAndBool(now_ - (lifetime + kFiveMs), true, __LINE__)}; | 261 TimeAndBool(now_ - (lifetime + kFiveMs), true, __LINE__)}; |
| 231 | 262 |
| 232 for (unsigned int i = 0; i < arraysize(test_values); ++i) { | 263 for (unsigned int i = 0; i < arraysize(test_values); ++i) { |
| 233 entry_->set_exponential_backoff_release_time(test_values[i].time); | 264 entry_->set_exponential_backoff_release_time(test_values[i].time); |
| 234 EXPECT_EQ(entry_->IsEntryOutdated(), test_values[i].result) << | 265 EXPECT_EQ(entry_->IsEntryOutdated(), test_values[i].result) << |
| 235 "Test case #" << i << " line " << test_values[i].line << " failed"; | 266 "Test case #" << i << " line " << test_values[i].line << " failed"; |
| 236 } | 267 } |
| 237 } | 268 } |
| 238 | 269 |
| 239 TEST_F(URLRequestThrottlerEntryTest, MaxAllowedBackoff) { | 270 TEST_F(URLRequestThrottlerEntryTest, MaxAllowedBackoff) { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 for (int i = 0; i < (max_send + 1) / 2; ++i) | 318 for (int i = 0; i < (max_send + 1) / 2; ++i) |
| 288 EXPECT_EQ(0, entry_->ReserveSendingTimeForNextRequest(TimeTicks())); | 319 EXPECT_EQ(0, entry_->ReserveSendingTimeForNextRequest(TimeTicks())); |
| 289 | 320 |
| 290 EXPECT_EQ(time_4, entry_->sliding_window_release_time()); | 321 EXPECT_EQ(time_4, entry_->sliding_window_release_time()); |
| 291 } | 322 } |
| 292 | 323 |
| 293 TEST(URLRequestThrottlerManager, IsUrlStandardised) { | 324 TEST(URLRequestThrottlerManager, IsUrlStandardised) { |
| 294 MockURLRequestThrottlerManager manager; | 325 MockURLRequestThrottlerManager manager; |
| 295 GurlAndString test_values[] = { | 326 GurlAndString test_values[] = { |
| 296 GurlAndString(GURL("http://www.example.com"), | 327 GurlAndString(GURL("http://www.example.com"), |
| 297 std::string("GOOGYhttp://www.example.com/MONSTA"), | 328 std::string("http://www.example.com/"), |
| 298 __LINE__), | 329 __LINE__), |
| 299 GurlAndString(GURL("http://www.Example.com"), | 330 GurlAndString(GURL("http://www.Example.com"), |
| 300 std::string("GOOGYhttp://www.example.com/MONSTA"), | 331 std::string("http://www.example.com/"), |
| 301 __LINE__), | 332 __LINE__), |
| 302 GurlAndString(GURL("http://www.ex4mple.com/Pr4c71c41"), | 333 GurlAndString(GURL("http://www.ex4mple.com/Pr4c71c41"), |
| 303 std::string("GOOGYhttp://www.ex4mple.com/pr4c71c41MONSTA"), | 334 std::string("http://www.ex4mple.com/pr4c71c41"), |
| 304 __LINE__), | 335 __LINE__), |
| 305 GurlAndString( | 336 GurlAndString(GURL("http://www.example.com/0/token/false"), |
| 306 GURL("http://www.example.com/0/token/false"), | 337 std::string("http://www.example.com/0/token/false"), |
| 307 std::string("GOOGYhttp://www.example.com/0/token/falseMONSTA"), | 338 __LINE__), |
| 308 __LINE__), | |
| 309 GurlAndString(GURL("http://www.example.com/index.php?code=javascript"), | 339 GurlAndString(GURL("http://www.example.com/index.php?code=javascript"), |
| 310 std::string("GOOGYhttp://www.example.com/index.phpMONSTA"), | 340 std::string("http://www.example.com/index.php"), |
| 311 __LINE__), | 341 __LINE__), |
| 312 GurlAndString(GURL("http://www.example.com/index.php?code=1#superEntry"), | 342 GurlAndString(GURL("http://www.example.com/index.php?code=1#superEntry"), |
| 313 std::string("GOOGYhttp://www.example.com/index.phpMONSTA"), | 343 std::string("http://www.example.com/index.php"), |
| 314 __LINE__), | 344 __LINE__), |
| 315 GurlAndString(GURL("http://www.example.com:1234/"), | 345 GurlAndString(GURL("http://www.example.com:1234/"), |
| 316 std::string("GOOGYhttp://www.example.com:1234/MONSTA"), | 346 std::string("http://www.example.com:1234/"), |
| 317 __LINE__)}; | 347 __LINE__)}; |
| 318 | 348 |
| 319 for (unsigned int i = 0; i < arraysize(test_values); ++i) { | 349 for (unsigned int i = 0; i < arraysize(test_values); ++i) { |
| 320 std::string temp = manager.DoGetUrlIdFromUrl(test_values[i].url); | 350 std::string temp = manager.DoGetUrlIdFromUrl(test_values[i].url); |
| 321 EXPECT_EQ(temp, test_values[i].result) << | 351 EXPECT_EQ(temp, test_values[i].result) << |
| 322 "Test case #" << i << " line " << test_values[i].line << " failed"; | 352 "Test case #" << i << " line " << test_values[i].line << " failed"; |
| 323 } | 353 } |
| 324 } | 354 } |
| 325 | 355 |
| 326 TEST(URLRequestThrottlerManager, AreEntriesBeingCollected) { | 356 TEST(URLRequestThrottlerManager, AreEntriesBeingCollected) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 344 MockURLRequestThrottlerManager manager; | 374 MockURLRequestThrottlerManager manager; |
| 345 | 375 |
| 346 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 376 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
| 347 manager.RegisterRequestUrl(GURL("http://www.google.com/")); | 377 manager.RegisterRequestUrl(GURL("http://www.google.com/")); |
| 348 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0")); | 378 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0")); |
| 349 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1")); | 379 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1")); |
| 350 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure")); | 380 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure")); |
| 351 | 381 |
| 352 EXPECT_EQ(3, manager.GetNumberOfEntries()); | 382 EXPECT_EQ(3, manager.GetNumberOfEntries()); |
| 353 } | 383 } |
| 354 | |
| 355 class DummyResponseHeaders : public net::URLRequestThrottlerHeaderInterface { | |
| 356 public: | |
| 357 DummyResponseHeaders(int response_code) : response_code_(response_code) {} | |
| 358 | |
| 359 std::string GetNormalizedValue(const std::string& key) const { | |
| 360 return ""; | |
| 361 } | |
| 362 | |
| 363 // Returns the HTTP response code associated with the request. | |
| 364 int GetResponseCode() const { | |
| 365 return response_code_; | |
| 366 } | |
| 367 | |
| 368 private: | |
| 369 int response_code_; | |
| 370 }; | |
| 371 | |
| 372 TEST(URLRequestThrottlerManager, StressTest) { | |
| 373 MockURLRequestThrottlerManager manager; | |
| 374 | |
| 375 for (int i = 0; i < 10000; ++i) { | |
| 376 // Make some entries duplicates or triplicates. | |
| 377 int entry_number = i + 2; | |
| 378 if (i % 17 == 0) { | |
| 379 entry_number = i - 1; | |
| 380 } else if ((i - 1) % 17 == 0) { | |
| 381 entry_number = i - 2; | |
| 382 } else if (i % 13 == 0) { | |
| 383 entry_number = i - 1; | |
| 384 } | |
| 385 | |
| 386 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry = | |
| 387 manager.RegisterRequestUrl(GURL(StringPrintf( | |
| 388 "http://bingourl.com/%d", entry_number))); | |
| 389 entry->IsDuringExponentialBackoff(); | |
| 390 entry->GetExponentialBackoffReleaseTime(); | |
| 391 | |
| 392 DummyResponseHeaders headers(i % 7 == 0 ? 500 : 200); | |
| 393 entry->UpdateWithResponse(&headers); | |
| 394 entry->SetEntryLifetimeMsForTest(1); | |
| 395 | |
| 396 entry->IsDuringExponentialBackoff(); | |
| 397 entry->GetExponentialBackoffReleaseTime(); | |
| 398 entry->ReserveSendingTimeForNextRequest(base::TimeTicks::Now()); | |
| 399 | |
| 400 if (i % 11 == 0) { | |
| 401 manager.DoGarbageCollectEntries(); | |
| 402 } | |
| 403 } | |
| 404 } | |
| 405 | |
| 406 // TODO(joi): Remove the debug-only condition after M11 branch point. | |
| 407 #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG) | |
| 408 TEST(URLRequestThrottlerManager, NullHandlingTest) { | |
| 409 MockURLRequestThrottlerManager manager; | |
| 410 manager.OverrideEntryForTests(GURL("http://www.foo.com/"), NULL); | |
| 411 ASSERT_DEATH({ | |
| 412 manager.DoGarbageCollectEntries(); | |
| 413 }, ""); | |
| 414 } | |
| 415 #endif // defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG) | |
| OLD | NEW |