| 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 "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/metrics/histogram.h" |
| 6 #include "base/pickle.h" | 7 #include "base/pickle.h" |
| 7 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 8 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 9 #include "base/time.h" | 10 #include "base/time.h" |
| 10 #include "net/base/test_completion_callback.h" | 11 #include "net/base/test_completion_callback.h" |
| 11 #include "net/url_request/url_request_context.h" | 12 #include "net/url_request/url_request_context.h" |
| 12 #include "net/url_request/url_request_throttler_header_interface.h" | 13 #include "net/url_request/url_request_throttler_header_interface.h" |
| 13 #include "net/url_request/url_request_throttler_manager.h" | 14 #include "net/url_request/url_request_throttler_manager.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 using base::TimeDelta; | 17 using base::TimeDelta; |
| 17 using base::TimeTicks; | 18 using base::TimeTicks; |
| 18 | 19 |
| 19 namespace net { | 20 namespace net { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 23 |
| 24 using base::Histogram; |
| 25 using base::StatisticsRecorder; |
| 26 |
| 22 class MockURLRequestThrottlerManager; | 27 class MockURLRequestThrottlerManager; |
| 23 | 28 |
| 24 class MockBackoffEntry : public BackoffEntry { | 29 class MockBackoffEntry : public BackoffEntry { |
| 25 public: | 30 public: |
| 26 explicit MockBackoffEntry(const BackoffEntry::Policy* const policy) | 31 explicit MockBackoffEntry(const BackoffEntry::Policy* const policy) |
| 27 : BackoffEntry(policy), fake_now_(TimeTicks()) { | 32 : BackoffEntry(policy), fake_now_(TimeTicks()) { |
| 28 } | 33 } |
| 29 | 34 |
| 30 virtual ~MockBackoffEntry() {} | 35 virtual ~MockBackoffEntry() {} |
| 31 | 36 |
| 32 TimeTicks GetTimeNow() const { | 37 virtual TimeTicks ImplGetTimeNow() const OVERRIDE { |
| 33 return fake_now_; | 38 return fake_now_; |
| 34 } | 39 } |
| 35 | 40 |
| 36 void SetFakeNow(const TimeTicks& now) { | 41 void SetFakeNow(const TimeTicks& now) { |
| 37 fake_now_ = now; | 42 fake_now_ = now; |
| 38 } | 43 } |
| 39 | 44 |
| 40 private: | 45 private: |
| 41 TimeTicks fake_now_; | 46 TimeTicks fake_now_; |
| 42 }; | 47 }; |
| 43 | 48 |
| 44 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { | 49 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { |
| 45 public : | 50 public : |
| 46 explicit MockURLRequestThrottlerEntry( | 51 explicit MockURLRequestThrottlerEntry( |
| 47 net::URLRequestThrottlerManager* manager) | 52 net::URLRequestThrottlerManager* manager) |
| 48 : net::URLRequestThrottlerEntry(manager), | 53 : net::URLRequestThrottlerEntry(manager, ""), |
| 49 mock_backoff_entry_(&backoff_policy_) { | 54 mock_backoff_entry_(&backoff_policy_) { |
| 50 InitPolicy(); | 55 InitPolicy(); |
| 51 } | 56 } |
| 52 MockURLRequestThrottlerEntry( | 57 MockURLRequestThrottlerEntry( |
| 53 net::URLRequestThrottlerManager* manager, | 58 net::URLRequestThrottlerManager* manager, |
| 54 const TimeTicks& exponential_backoff_release_time, | 59 const TimeTicks& exponential_backoff_release_time, |
| 55 const TimeTicks& sliding_window_release_time, | 60 const TimeTicks& sliding_window_release_time, |
| 56 const TimeTicks& fake_now) | 61 const TimeTicks& fake_now) |
| 57 : net::URLRequestThrottlerEntry(manager), | 62 : net::URLRequestThrottlerEntry(manager, ""), |
| 58 fake_time_now_(fake_now), | 63 fake_time_now_(fake_now), |
| 59 mock_backoff_entry_(&backoff_policy_) { | 64 mock_backoff_entry_(&backoff_policy_) { |
| 60 InitPolicy(); | 65 InitPolicy(); |
| 61 | 66 |
| 62 mock_backoff_entry_.SetFakeNow(fake_now); | 67 mock_backoff_entry_.SetFakeNow(fake_now); |
| 63 set_exponential_backoff_release_time(exponential_backoff_release_time); | 68 set_exponential_backoff_release_time(exponential_backoff_release_time); |
| 64 set_sliding_window_release_time(sliding_window_release_time); | 69 set_sliding_window_release_time(sliding_window_release_time); |
| 65 } | 70 } |
| 66 virtual ~MockURLRequestThrottlerEntry() {} | 71 virtual ~MockURLRequestThrottlerEntry() {} |
| 67 | 72 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 79 } | 84 } |
| 80 | 85 |
| 81 BackoffEntry* GetBackoffEntry() { | 86 BackoffEntry* GetBackoffEntry() { |
| 82 return &mock_backoff_entry_; | 87 return &mock_backoff_entry_; |
| 83 } | 88 } |
| 84 | 89 |
| 85 void ResetToBlank(const TimeTicks& time_now) { | 90 void ResetToBlank(const TimeTicks& time_now) { |
| 86 fake_time_now_ = time_now; | 91 fake_time_now_ = time_now; |
| 87 mock_backoff_entry_.SetFakeNow(time_now); | 92 mock_backoff_entry_.SetFakeNow(time_now); |
| 88 | 93 |
| 89 GetBackoffEntry()->InformOfRequest(true); // Sets failure count to 0. | 94 GetBackoffEntry()->Reset(); |
| 90 GetBackoffEntry()->SetCustomReleaseTime(time_now); | 95 GetBackoffEntry()->SetCustomReleaseTime(time_now); |
| 91 set_sliding_window_release_time(time_now); | 96 set_sliding_window_release_time(time_now); |
| 92 } | 97 } |
| 93 | 98 |
| 94 // Overridden for tests. | 99 // Overridden for tests. |
| 95 virtual TimeTicks GetTimeNow() const { return fake_time_now_; } | 100 virtual TimeTicks ImplGetTimeNow() const OVERRIDE { return fake_time_now_; } |
| 96 | 101 |
| 97 void set_exponential_backoff_release_time( | 102 void set_exponential_backoff_release_time( |
| 98 const base::TimeTicks& release_time) { | 103 const base::TimeTicks& release_time) { |
| 99 GetBackoffEntry()->SetCustomReleaseTime(release_time); | 104 GetBackoffEntry()->SetCustomReleaseTime(release_time); |
| 100 } | 105 } |
| 101 | 106 |
| 102 base::TimeTicks sliding_window_release_time() const { | 107 base::TimeTicks sliding_window_release_time() const { |
| 103 return URLRequestThrottlerEntry::sliding_window_release_time(); | 108 return URLRequestThrottlerEntry::sliding_window_release_time(); |
| 104 } | 109 } |
| 105 | 110 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 GURL url; | 217 GURL url; |
| 213 std::string result; | 218 std::string result; |
| 214 int line; | 219 int line; |
| 215 }; | 220 }; |
| 216 | 221 |
| 217 } // namespace | 222 } // namespace |
| 218 | 223 |
| 219 class URLRequestThrottlerEntryTest : public testing::Test { | 224 class URLRequestThrottlerEntryTest : public testing::Test { |
| 220 protected: | 225 protected: |
| 221 virtual void SetUp(); | 226 virtual void SetUp(); |
| 227 |
| 228 // After calling this function, histogram snapshots in |samples_| contain |
| 229 // only the delta caused by the test case currently running. |
| 230 void CalculateHistogramDeltas(); |
| 231 |
| 222 TimeTicks now_; | 232 TimeTicks now_; |
| 223 MockURLRequestThrottlerManager manager_; // Dummy object, not used. | 233 MockURLRequestThrottlerManager manager_; // Dummy object, not used. |
| 224 scoped_refptr<MockURLRequestThrottlerEntry> entry_; | 234 scoped_refptr<MockURLRequestThrottlerEntry> entry_; |
| 235 |
| 236 std::map<std::string, Histogram::SampleSet> original_samples_; |
| 237 std::map<std::string, Histogram::SampleSet> samples_; |
| 238 }; |
| 239 |
| 240 // List of all histograms we care about in these unit tests. |
| 241 const char* kHistogramNames[] = { |
| 242 "Throttling.CustomRetryAfterMs", |
| 243 "Throttling.FailureCountAtSuccess", |
| 244 "Throttling.HttpResponseCode", |
| 245 "Throttling.PerceivedDowntime", |
| 246 "Throttling.RequestThrottled", |
| 247 "Throttling.SiteOptedOut", |
| 225 }; | 248 }; |
| 226 | 249 |
| 227 void URLRequestThrottlerEntryTest::SetUp() { | 250 void URLRequestThrottlerEntryTest::SetUp() { |
| 228 now_ = TimeTicks::Now(); | 251 now_ = TimeTicks::Now(); |
| 229 entry_ = new MockURLRequestThrottlerEntry(&manager_); | 252 entry_ = new MockURLRequestThrottlerEntry(&manager_); |
| 230 entry_->ResetToBlank(now_); | 253 entry_->ResetToBlank(now_); |
| 254 |
| 255 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { |
| 256 // Must retrieve original samples for each histogram for comparison |
| 257 // as other tests may affect them. |
| 258 const char* name = kHistogramNames[i]; |
| 259 Histogram::SampleSet& original = original_samples_[name]; |
| 260 Histogram* histogram; |
| 261 if (StatisticsRecorder::FindHistogram(name, &histogram)) { |
| 262 histogram->SnapshotSample(&original); |
| 263 } |
| 264 } |
| 265 } |
| 266 |
| 267 void URLRequestThrottlerEntryTest::CalculateHistogramDeltas() { |
| 268 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { |
| 269 const char* name = kHistogramNames[i]; |
| 270 Histogram::SampleSet& original = original_samples_[name]; |
| 271 Histogram::SampleSet& sample = samples_[name]; |
| 272 |
| 273 Histogram* histogram; |
| 274 if (StatisticsRecorder::FindHistogram(name, &histogram)) { |
| 275 ASSERT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags()); |
| 276 |
| 277 histogram->SnapshotSample(&sample); |
| 278 // Ensure |original| size is same as |sample|, then subtract original |
| 279 // values. |
| 280 original.Resize(*histogram); |
| 281 sample.Subtract(original); |
| 282 } |
| 283 } |
| 284 |
| 285 // Ensure we don't accidentally use the originals in our tests. |
| 286 original_samples_.clear(); |
| 231 } | 287 } |
| 232 | 288 |
| 233 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { | 289 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { |
| 234 return out << time.ToInternalValue(); | 290 return out << time.ToInternalValue(); |
| 235 } | 291 } |
| 236 | 292 |
| 237 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { | 293 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { |
| 238 entry_->set_exponential_backoff_release_time( | 294 entry_->set_exponential_backoff_release_time( |
| 239 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); | 295 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); |
| 240 EXPECT_TRUE(entry_->IsDuringExponentialBackoff()); | 296 EXPECT_TRUE(entry_->IsDuringExponentialBackoff()); |
| 297 |
| 298 CalculateHistogramDeltas(); |
| 299 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"].counts(0)); |
| 300 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"].counts(1)); |
| 241 } | 301 } |
| 242 | 302 |
| 243 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { | 303 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { |
| 244 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); | 304 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); |
| 245 EXPECT_FALSE(entry_->IsDuringExponentialBackoff()); | 305 EXPECT_FALSE(entry_->IsDuringExponentialBackoff()); |
| 246 entry_->set_exponential_backoff_release_time( | 306 entry_->set_exponential_backoff_release_time( |
| 247 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); | 307 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); |
| 248 EXPECT_FALSE(entry_->IsDuringExponentialBackoff()); | 308 EXPECT_FALSE(entry_->IsDuringExponentialBackoff()); |
| 309 |
| 310 CalculateHistogramDeltas(); |
| 311 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"].counts(0)); |
| 312 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"].counts(1)); |
| 249 } | 313 } |
| 250 | 314 |
| 251 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateRetryAfter) { | 315 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateRetryAfter) { |
| 252 // If the response we received has a retry-after field, | 316 // If the response we received has a retry-after field, |
| 253 // the request should be delayed. | 317 // the request should be delayed. |
| 254 MockURLRequestThrottlerHeaderAdapter header_w_delay_header("5.5", "", 200); | 318 MockURLRequestThrottlerHeaderAdapter header_w_delay_header("5.5", "", 200); |
| 255 entry_->UpdateWithResponse("", &header_w_delay_header); | 319 entry_->UpdateWithResponse("", &header_w_delay_header); |
| 256 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 320 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 257 << "When the server put a positive value in retry-after we should " | 321 << "When the server put a positive value in retry-after we should " |
| 258 "increase release_time"; | 322 "increase release_time"; |
| 259 | 323 |
| 260 entry_->ResetToBlank(now_); | 324 entry_->ResetToBlank(now_); |
| 261 header_w_delay_header.fake_retry_value_ = "-5.5"; | 325 MockURLRequestThrottlerHeaderAdapter header_w_negative_header( |
| 326 "-5.5", "", 200); |
| 327 entry_->UpdateWithResponse("", &header_w_negative_header); |
| 262 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 328 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 263 << "When given a negative value, it should not change the release_time"; | 329 << "When given a negative value, it should not change the release_time"; |
| 330 |
| 331 CalculateHistogramDeltas(); |
| 332 ASSERT_EQ(1, samples_["Throttling.CustomRetryAfterMs"].TotalCount()); |
| 264 } | 333 } |
| 265 | 334 |
| 266 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { | 335 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { |
| 267 MockURLRequestThrottlerHeaderAdapter failure_response(505); | 336 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
| 268 entry_->UpdateWithResponse("", &failure_response); | 337 entry_->UpdateWithResponse("", &failure_response); |
| 269 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 338 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 270 << "A failure should increase the release_time"; | 339 << "A failure should increase the release_time"; |
| 271 } | 340 } |
| 272 | 341 |
| 273 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { | 342 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { |
| 274 MockURLRequestThrottlerHeaderAdapter success_response(200); | 343 MockURLRequestThrottlerHeaderAdapter success_response(200); |
| 275 entry_->UpdateWithResponse("", &success_response); | 344 entry_->UpdateWithResponse("", &success_response); |
| 276 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 345 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 277 << "A success should not add any delay"; | 346 << "A success should not add any delay"; |
| 278 } | 347 } |
| 279 | 348 |
| 280 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccessThenFailure) { | 349 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccessThenFailure) { |
| 281 MockURLRequestThrottlerHeaderAdapter failure_response(500); | 350 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
| 282 MockURLRequestThrottlerHeaderAdapter success_response(200); | 351 MockURLRequestThrottlerHeaderAdapter success_response(200); |
| 283 entry_->UpdateWithResponse("", &success_response); | 352 entry_->UpdateWithResponse("", &success_response); |
| 284 entry_->UpdateWithResponse("", &failure_response); | 353 entry_->UpdateWithResponse("", &failure_response); |
| 285 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 354 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 286 << "This scenario should add delay"; | 355 << "This scenario should add delay"; |
| 356 entry_->UpdateWithResponse("", &success_response); |
| 357 |
| 358 CalculateHistogramDeltas(); |
| 359 ASSERT_EQ(1, samples_["Throttling.HttpResponseCode"].counts(503)); |
| 360 ASSERT_EQ(2, samples_["Throttling.HttpResponseCode"].counts(200)); |
| 361 ASSERT_EQ(1, samples_["Throttling.FailureCountAtSuccess"].counts(1)); |
| 362 ASSERT_EQ(1, samples_["Throttling.PerceivedDowntime"].TotalCount()); |
| 287 } | 363 } |
| 288 | 364 |
| 289 TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) { | 365 TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) { |
| 290 TimeDelta lifetime = TimeDelta::FromMilliseconds( | 366 TimeDelta lifetime = TimeDelta::FromMilliseconds( |
| 291 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs); | 367 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs); |
| 292 const TimeDelta kFiveMs = TimeDelta::FromMilliseconds(5); | 368 const TimeDelta kFiveMs = TimeDelta::FromMilliseconds(5); |
| 293 | 369 |
| 294 TimeAndBool test_values[] = { | 370 TimeAndBool test_values[] = { |
| 295 TimeAndBool(now_, false, __LINE__), | 371 TimeAndBool(now_, false, __LINE__), |
| 296 TimeAndBool(now_ - kFiveMs, false, __LINE__), | 372 TimeAndBool(now_ - kFiveMs, false, __LINE__), |
| 297 TimeAndBool(now_ + kFiveMs, false, __LINE__), | 373 TimeAndBool(now_ + kFiveMs, false, __LINE__), |
| 298 TimeAndBool(now_ - (lifetime - kFiveMs), false, __LINE__), | 374 TimeAndBool(now_ - (lifetime - kFiveMs), false, __LINE__), |
| 299 TimeAndBool(now_ - lifetime, true, __LINE__), | 375 TimeAndBool(now_ - lifetime, true, __LINE__), |
| 300 TimeAndBool(now_ - (lifetime + kFiveMs), true, __LINE__)}; | 376 TimeAndBool(now_ - (lifetime + kFiveMs), true, __LINE__)}; |
| 301 | 377 |
| 302 for (unsigned int i = 0; i < arraysize(test_values); ++i) { | 378 for (unsigned int i = 0; i < arraysize(test_values); ++i) { |
| 303 entry_->set_exponential_backoff_release_time(test_values[i].time); | 379 entry_->set_exponential_backoff_release_time(test_values[i].time); |
| 304 EXPECT_EQ(entry_->IsEntryOutdated(), test_values[i].result) << | 380 EXPECT_EQ(entry_->IsEntryOutdated(), test_values[i].result) << |
| 305 "Test case #" << i << " line " << test_values[i].line << " failed"; | 381 "Test case #" << i << " line " << test_values[i].line << " failed"; |
| 306 } | 382 } |
| 307 } | 383 } |
| 308 | 384 |
| 309 TEST_F(URLRequestThrottlerEntryTest, MaxAllowedBackoff) { | 385 TEST_F(URLRequestThrottlerEntryTest, MaxAllowedBackoff) { |
| 310 for (int i = 0; i < 30; ++i) { | 386 for (int i = 0; i < 30; ++i) { |
| 311 MockURLRequestThrottlerHeaderAdapter response_adapter(505); | 387 MockURLRequestThrottlerHeaderAdapter response_adapter(503); |
| 312 entry_->UpdateWithResponse("", &response_adapter); | 388 entry_->UpdateWithResponse("", &response_adapter); |
| 313 } | 389 } |
| 314 | 390 |
| 315 TimeDelta delay = entry_->GetExponentialBackoffReleaseTime() - now_; | 391 TimeDelta delay = entry_->GetExponentialBackoffReleaseTime() - now_; |
| 316 EXPECT_EQ(delay.InMilliseconds(), | 392 EXPECT_EQ(delay.InMilliseconds(), |
| 317 MockURLRequestThrottlerEntry::kDefaultMaximumBackoffMs); | 393 MockURLRequestThrottlerEntry::kDefaultMaximumBackoffMs); |
| 318 } | 394 } |
| 319 | 395 |
| 320 TEST_F(URLRequestThrottlerEntryTest, MalformedContent) { | 396 TEST_F(URLRequestThrottlerEntryTest, MalformedContent) { |
| 321 MockURLRequestThrottlerHeaderAdapter response_adapter(505); | 397 MockURLRequestThrottlerHeaderAdapter response_adapter(503); |
| 322 for (int i = 0; i < 5; ++i) | 398 for (int i = 0; i < 5; ++i) |
| 323 entry_->UpdateWithResponse("", &response_adapter); | 399 entry_->UpdateWithResponse("", &response_adapter); |
| 324 | 400 |
| 325 TimeTicks release_after_failures = entry_->GetExponentialBackoffReleaseTime(); | 401 TimeTicks release_after_failures = entry_->GetExponentialBackoffReleaseTime(); |
| 326 | 402 |
| 327 // Inform the entry that a response body was malformed, which is supposed to | 403 // Inform the entry that a response body was malformed, which is supposed to |
| 328 // increase the back-off time. Note that we also submit a successful | 404 // increase the back-off time. Note that we also submit a successful |
| 329 // UpdateWithResponse to pair with ReceivedContentWasMalformed() since that | 405 // UpdateWithResponse to pair with ReceivedContentWasMalformed() since that |
| 330 // is what happens in practice (if a body is received, then a non-500 | 406 // is what happens in practice (if a body is received, then a non-500 |
| 331 // response must also have been received). | 407 // response must also have been received). |
| 332 entry_->ReceivedContentWasMalformed(); | 408 entry_->ReceivedContentWasMalformed(200); |
| 333 MockURLRequestThrottlerHeaderAdapter success_adapter(200); | 409 MockURLRequestThrottlerHeaderAdapter success_adapter(200); |
| 334 entry_->UpdateWithResponse("", &success_adapter); | 410 entry_->UpdateWithResponse("", &success_adapter); |
| 335 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), release_after_failures); | 411 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), release_after_failures); |
| 336 } | 412 } |
| 337 | 413 |
| 338 TEST_F(URLRequestThrottlerEntryTest, SlidingWindow) { | 414 TEST_F(URLRequestThrottlerEntryTest, SlidingWindow) { |
| 339 int max_send = URLRequestThrottlerEntry::kDefaultMaxSendThreshold; | 415 int max_send = URLRequestThrottlerEntry::kDefaultMaxSendThreshold; |
| 340 int sliding_window = | 416 int sliding_window = |
| 341 URLRequestThrottlerEntry::kDefaultSlidingWindowPeriodMs; | 417 URLRequestThrottlerEntry::kDefaultSlidingWindowPeriodMs; |
| 342 | 418 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 __LINE__), | 455 __LINE__), |
| 380 GurlAndString(GURL("http://www.example.com/0/token/false"), | 456 GurlAndString(GURL("http://www.example.com/0/token/false"), |
| 381 std::string("http://www.example.com/0/token/false"), | 457 std::string("http://www.example.com/0/token/false"), |
| 382 __LINE__), | 458 __LINE__), |
| 383 GurlAndString(GURL("http://www.example.com/index.php?code=javascript"), | 459 GurlAndString(GURL("http://www.example.com/index.php?code=javascript"), |
| 384 std::string("http://www.example.com/index.php"), | 460 std::string("http://www.example.com/index.php"), |
| 385 __LINE__), | 461 __LINE__), |
| 386 GurlAndString(GURL("http://www.example.com/index.php?code=1#superEntry"), | 462 GurlAndString(GURL("http://www.example.com/index.php?code=1#superEntry"), |
| 387 std::string("http://www.example.com/index.php"), | 463 std::string("http://www.example.com/index.php"), |
| 388 __LINE__), | 464 __LINE__), |
| 465 GurlAndString(GURL("http://www.example.com/index.php#superEntry"), |
| 466 std::string("http://www.example.com/index.php"), |
| 467 __LINE__), |
| 389 GurlAndString(GURL("http://www.example.com:1234/"), | 468 GurlAndString(GURL("http://www.example.com:1234/"), |
| 390 std::string("http://www.example.com:1234/"), | 469 std::string("http://www.example.com:1234/"), |
| 391 __LINE__)}; | 470 __LINE__)}; |
| 392 | 471 |
| 393 for (unsigned int i = 0; i < arraysize(test_values); ++i) { | 472 for (unsigned int i = 0; i < arraysize(test_values); ++i) { |
| 394 std::string temp = manager.DoGetUrlIdFromUrl(test_values[i].url); | 473 std::string temp = manager.DoGetUrlIdFromUrl(test_values[i].url); |
| 395 EXPECT_EQ(temp, test_values[i].result) << | 474 EXPECT_EQ(temp, test_values[i].result) << |
| 396 "Test case #" << i << " line " << test_values[i].line << " failed"; | 475 "Test case #" << i << " line " << test_values[i].line << " failed"; |
| 397 } | 476 } |
| 398 } | 477 } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 MockURLRequestThrottlerHeaderAdapter wrong_adapter("", "yesplease", 200); | 557 MockURLRequestThrottlerHeaderAdapter wrong_adapter("", "yesplease", 200); |
| 479 no_opt_out_entry->UpdateWithResponse("www.nike.com", &wrong_adapter); | 558 no_opt_out_entry->UpdateWithResponse("www.nike.com", &wrong_adapter); |
| 480 ExpectEntryAllowsAllOnErrorIfOptedOut(no_opt_out_entry, false); | 559 ExpectEntryAllowsAllOnErrorIfOptedOut(no_opt_out_entry, false); |
| 481 | 560 |
| 482 // A localhost entry should always be opted out. | 561 // A localhost entry should always be opted out. |
| 483 scoped_refptr<net::URLRequestThrottlerEntryInterface> localhost_entry = | 562 scoped_refptr<net::URLRequestThrottlerEntryInterface> localhost_entry = |
| 484 manager.RegisterRequestUrl(GURL("http://localhost/hello")); | 563 manager.RegisterRequestUrl(GURL("http://localhost/hello")); |
| 485 ExpectEntryAllowsAllOnErrorIfOptedOut(localhost_entry, true); | 564 ExpectEntryAllowsAllOnErrorIfOptedOut(localhost_entry, true); |
| 486 } | 565 } |
| 487 | 566 |
| 567 TEST(URLRequestThrottlerManager, ClearOnNetworkChange) { |
| 568 for (int i = 0; i < 3; ++i) { |
| 569 MockURLRequestThrottlerManager manager; |
| 570 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_before = |
| 571 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
| 572 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); |
| 573 for (int j = 0; j < 10; ++j) { |
| 574 // Host doesn't really matter in this scenario so we skip it. |
| 575 entry_before->UpdateWithResponse("", &failure_adapter); |
| 576 } |
| 577 EXPECT_TRUE(entry_before->IsDuringExponentialBackoff()); |
| 578 |
| 579 switch (i) { |
| 580 case 0: |
| 581 manager.OnIPAddressChanged(); |
| 582 break; |
| 583 case 1: |
| 584 manager.OnOnlineStateChanged(true); |
| 585 break; |
| 586 case 2: |
| 587 manager.OnOnlineStateChanged(false); |
| 588 break; |
| 589 default: |
| 590 FAIL(); |
| 591 } |
| 592 |
| 593 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = |
| 594 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
| 595 EXPECT_FALSE(entry_after->IsDuringExponentialBackoff()); |
| 596 } |
| 597 } |
| 598 |
| 488 } // namespace net | 599 } // namespace net |
| OLD | NEW |