Chromium Code Reviews| 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 TimeTicks ImplGetTimeNow() const OVERRIDE { |
|
yzshen1
2011/05/25 03:49:06
It might be good to add 'virtual'.
Jói
2011/05/25 14:30:35
Done.
| |
| 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"; | |
| 262 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 325 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 263 << "When given a negative value, it should not change the release_time"; | 326 << "When given a negative value, it should not change the release_time"; |
|
yzshen1
2011/05/25 03:49:06
Please also update the string.
Jói
2011/05/25 14:30:35
This was incorrect, I've fixed it now (the call to
| |
| 327 | |
| 328 CalculateHistogramDeltas(); | |
| 329 ASSERT_EQ(1, samples_["Throttling.CustomRetryAfterMs"].TotalCount()); | |
| 264 } | 330 } |
| 265 | 331 |
| 266 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { | 332 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { |
| 267 MockURLRequestThrottlerHeaderAdapter failure_response(505); | 333 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
| 268 entry_->UpdateWithResponse("", &failure_response); | 334 entry_->UpdateWithResponse("", &failure_response); |
| 269 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 335 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 270 << "A failure should increase the release_time"; | 336 << "A failure should increase the release_time"; |
| 271 } | 337 } |
| 272 | 338 |
| 273 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { | 339 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { |
| 274 MockURLRequestThrottlerHeaderAdapter success_response(200); | 340 MockURLRequestThrottlerHeaderAdapter success_response(200); |
| 275 entry_->UpdateWithResponse("", &success_response); | 341 entry_->UpdateWithResponse("", &success_response); |
| 276 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 342 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 277 << "A success should not add any delay"; | 343 << "A success should not add any delay"; |
| 278 } | 344 } |
| 279 | 345 |
| 280 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccessThenFailure) { | 346 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccessThenFailure) { |
| 281 MockURLRequestThrottlerHeaderAdapter failure_response(500); | 347 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
| 282 MockURLRequestThrottlerHeaderAdapter success_response(200); | 348 MockURLRequestThrottlerHeaderAdapter success_response(200); |
| 283 entry_->UpdateWithResponse("", &success_response); | 349 entry_->UpdateWithResponse("", &success_response); |
| 284 entry_->UpdateWithResponse("", &failure_response); | 350 entry_->UpdateWithResponse("", &failure_response); |
| 285 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 351 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 286 << "This scenario should add delay"; | 352 << "This scenario should add delay"; |
| 353 entry_->UpdateWithResponse("", &success_response); | |
| 354 | |
| 355 CalculateHistogramDeltas(); | |
| 356 ASSERT_EQ(1, samples_["Throttling.HttpResponseCode"].counts(503)); | |
| 357 ASSERT_EQ(2, samples_["Throttling.HttpResponseCode"].counts(200)); | |
| 358 ASSERT_EQ(1, samples_["Throttling.FailureCountAtSuccess"].counts(1)); | |
| 359 ASSERT_EQ(1, samples_["Throttling.PerceivedDowntime"].TotalCount()); | |
| 287 } | 360 } |
| 288 | 361 |
| 289 TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) { | 362 TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) { |
| 290 TimeDelta lifetime = TimeDelta::FromMilliseconds( | 363 TimeDelta lifetime = TimeDelta::FromMilliseconds( |
| 291 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs); | 364 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs); |
| 292 const TimeDelta kFiveMs = TimeDelta::FromMilliseconds(5); | 365 const TimeDelta kFiveMs = TimeDelta::FromMilliseconds(5); |
| 293 | 366 |
| 294 TimeAndBool test_values[] = { | 367 TimeAndBool test_values[] = { |
| 295 TimeAndBool(now_, false, __LINE__), | 368 TimeAndBool(now_, false, __LINE__), |
| 296 TimeAndBool(now_ - kFiveMs, false, __LINE__), | 369 TimeAndBool(now_ - kFiveMs, false, __LINE__), |
| 297 TimeAndBool(now_ + kFiveMs, false, __LINE__), | 370 TimeAndBool(now_ + kFiveMs, false, __LINE__), |
| 298 TimeAndBool(now_ - (lifetime - kFiveMs), false, __LINE__), | 371 TimeAndBool(now_ - (lifetime - kFiveMs), false, __LINE__), |
| 299 TimeAndBool(now_ - lifetime, true, __LINE__), | 372 TimeAndBool(now_ - lifetime, true, __LINE__), |
| 300 TimeAndBool(now_ - (lifetime + kFiveMs), true, __LINE__)}; | 373 TimeAndBool(now_ - (lifetime + kFiveMs), true, __LINE__)}; |
| 301 | 374 |
| 302 for (unsigned int i = 0; i < arraysize(test_values); ++i) { | 375 for (unsigned int i = 0; i < arraysize(test_values); ++i) { |
| 303 entry_->set_exponential_backoff_release_time(test_values[i].time); | 376 entry_->set_exponential_backoff_release_time(test_values[i].time); |
| 304 EXPECT_EQ(entry_->IsEntryOutdated(), test_values[i].result) << | 377 EXPECT_EQ(entry_->IsEntryOutdated(), test_values[i].result) << |
| 305 "Test case #" << i << " line " << test_values[i].line << " failed"; | 378 "Test case #" << i << " line " << test_values[i].line << " failed"; |
| 306 } | 379 } |
| 307 } | 380 } |
| 308 | 381 |
| 309 TEST_F(URLRequestThrottlerEntryTest, MaxAllowedBackoff) { | 382 TEST_F(URLRequestThrottlerEntryTest, MaxAllowedBackoff) { |
| 310 for (int i = 0; i < 30; ++i) { | 383 for (int i = 0; i < 30; ++i) { |
| 311 MockURLRequestThrottlerHeaderAdapter response_adapter(505); | 384 MockURLRequestThrottlerHeaderAdapter response_adapter(503); |
| 312 entry_->UpdateWithResponse("", &response_adapter); | 385 entry_->UpdateWithResponse("", &response_adapter); |
| 313 } | 386 } |
| 314 | 387 |
| 315 TimeDelta delay = entry_->GetExponentialBackoffReleaseTime() - now_; | 388 TimeDelta delay = entry_->GetExponentialBackoffReleaseTime() - now_; |
| 316 EXPECT_EQ(delay.InMilliseconds(), | 389 EXPECT_EQ(delay.InMilliseconds(), |
| 317 MockURLRequestThrottlerEntry::kDefaultMaximumBackoffMs); | 390 MockURLRequestThrottlerEntry::kDefaultMaximumBackoffMs); |
| 318 } | 391 } |
| 319 | 392 |
| 320 TEST_F(URLRequestThrottlerEntryTest, MalformedContent) { | 393 TEST_F(URLRequestThrottlerEntryTest, MalformedContent) { |
| 321 MockURLRequestThrottlerHeaderAdapter response_adapter(505); | 394 MockURLRequestThrottlerHeaderAdapter response_adapter(503); |
| 322 for (int i = 0; i < 5; ++i) | 395 for (int i = 0; i < 5; ++i) |
| 323 entry_->UpdateWithResponse("", &response_adapter); | 396 entry_->UpdateWithResponse("", &response_adapter); |
| 324 | 397 |
| 325 TimeTicks release_after_failures = entry_->GetExponentialBackoffReleaseTime(); | 398 TimeTicks release_after_failures = entry_->GetExponentialBackoffReleaseTime(); |
| 326 | 399 |
| 327 // Inform the entry that a response body was malformed, which is supposed to | 400 // 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 | 401 // increase the back-off time. Note that we also submit a successful |
| 329 // UpdateWithResponse to pair with ReceivedContentWasMalformed() since that | 402 // UpdateWithResponse to pair with ReceivedContentWasMalformed() since that |
| 330 // is what happens in practice (if a body is received, then a non-500 | 403 // is what happens in practice (if a body is received, then a non-500 |
| 331 // response must also have been received). | 404 // response must also have been received). |
| 332 entry_->ReceivedContentWasMalformed(); | 405 entry_->ReceivedContentWasMalformed(200); |
| 333 MockURLRequestThrottlerHeaderAdapter success_adapter(200); | 406 MockURLRequestThrottlerHeaderAdapter success_adapter(200); |
| 334 entry_->UpdateWithResponse("", &success_adapter); | 407 entry_->UpdateWithResponse("", &success_adapter); |
| 335 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), release_after_failures); | 408 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), release_after_failures); |
| 336 } | 409 } |
| 337 | 410 |
| 338 TEST_F(URLRequestThrottlerEntryTest, SlidingWindow) { | 411 TEST_F(URLRequestThrottlerEntryTest, SlidingWindow) { |
| 339 int max_send = URLRequestThrottlerEntry::kDefaultMaxSendThreshold; | 412 int max_send = URLRequestThrottlerEntry::kDefaultMaxSendThreshold; |
| 340 int sliding_window = | 413 int sliding_window = |
| 341 URLRequestThrottlerEntry::kDefaultSlidingWindowPeriodMs; | 414 URLRequestThrottlerEntry::kDefaultSlidingWindowPeriodMs; |
| 342 | 415 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 379 __LINE__), | 452 __LINE__), |
| 380 GurlAndString(GURL("http://www.example.com/0/token/false"), | 453 GurlAndString(GURL("http://www.example.com/0/token/false"), |
| 381 std::string("http://www.example.com/0/token/false"), | 454 std::string("http://www.example.com/0/token/false"), |
| 382 __LINE__), | 455 __LINE__), |
| 383 GurlAndString(GURL("http://www.example.com/index.php?code=javascript"), | 456 GurlAndString(GURL("http://www.example.com/index.php?code=javascript"), |
| 384 std::string("http://www.example.com/index.php"), | 457 std::string("http://www.example.com/index.php"), |
| 385 __LINE__), | 458 __LINE__), |
| 386 GurlAndString(GURL("http://www.example.com/index.php?code=1#superEntry"), | 459 GurlAndString(GURL("http://www.example.com/index.php?code=1#superEntry"), |
| 387 std::string("http://www.example.com/index.php"), | 460 std::string("http://www.example.com/index.php"), |
| 388 __LINE__), | 461 __LINE__), |
| 462 GurlAndString(GURL("http://www.example.com/index.php#superEntry"), | |
| 463 std::string("http://www.example.com/index.php"), | |
| 464 __LINE__), | |
| 389 GurlAndString(GURL("http://www.example.com:1234/"), | 465 GurlAndString(GURL("http://www.example.com:1234/"), |
| 390 std::string("http://www.example.com:1234/"), | 466 std::string("http://www.example.com:1234/"), |
| 391 __LINE__)}; | 467 __LINE__)}; |
| 392 | 468 |
| 393 for (unsigned int i = 0; i < arraysize(test_values); ++i) { | 469 for (unsigned int i = 0; i < arraysize(test_values); ++i) { |
| 394 std::string temp = manager.DoGetUrlIdFromUrl(test_values[i].url); | 470 std::string temp = manager.DoGetUrlIdFromUrl(test_values[i].url); |
| 395 EXPECT_EQ(temp, test_values[i].result) << | 471 EXPECT_EQ(temp, test_values[i].result) << |
| 396 "Test case #" << i << " line " << test_values[i].line << " failed"; | 472 "Test case #" << i << " line " << test_values[i].line << " failed"; |
| 397 } | 473 } |
| 398 } | 474 } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 MockURLRequestThrottlerHeaderAdapter wrong_adapter("", "yesplease", 200); | 554 MockURLRequestThrottlerHeaderAdapter wrong_adapter("", "yesplease", 200); |
| 479 no_opt_out_entry->UpdateWithResponse("www.nike.com", &wrong_adapter); | 555 no_opt_out_entry->UpdateWithResponse("www.nike.com", &wrong_adapter); |
| 480 ExpectEntryAllowsAllOnErrorIfOptedOut(no_opt_out_entry, false); | 556 ExpectEntryAllowsAllOnErrorIfOptedOut(no_opt_out_entry, false); |
| 481 | 557 |
| 482 // A localhost entry should always be opted out. | 558 // A localhost entry should always be opted out. |
| 483 scoped_refptr<net::URLRequestThrottlerEntryInterface> localhost_entry = | 559 scoped_refptr<net::URLRequestThrottlerEntryInterface> localhost_entry = |
| 484 manager.RegisterRequestUrl(GURL("http://localhost/hello")); | 560 manager.RegisterRequestUrl(GURL("http://localhost/hello")); |
| 485 ExpectEntryAllowsAllOnErrorIfOptedOut(localhost_entry, true); | 561 ExpectEntryAllowsAllOnErrorIfOptedOut(localhost_entry, true); |
| 486 } | 562 } |
| 487 | 563 |
| 564 TEST(URLRequestThrottlerManager, ClearOnNetworkChange) { | |
| 565 for (int i = 0; i < 3; ++i) { | |
| 566 MockURLRequestThrottlerManager manager; | |
| 567 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_before = | |
| 568 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | |
| 569 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); | |
| 570 for (int j = 0; j < 10; ++j) { | |
| 571 // Host doesn't really matter in this scenario so we skip it. | |
| 572 entry_before->UpdateWithResponse("", &failure_adapter); | |
| 573 } | |
| 574 EXPECT_TRUE(entry_before->IsDuringExponentialBackoff()); | |
| 575 | |
| 576 switch (i) { | |
| 577 case 0: | |
| 578 manager.OnIPAddressChanged(); | |
| 579 break; | |
| 580 case 1: | |
| 581 manager.OnOnlineStateChanged(true); | |
| 582 break; | |
| 583 case 2: | |
| 584 manager.OnOnlineStateChanged(false); | |
| 585 break; | |
| 586 default: | |
| 587 FAIL(); | |
| 588 } | |
| 589 | |
| 590 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = | |
| 591 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | |
| 592 EXPECT_FALSE(entry_after->IsDuringExponentialBackoff()); | |
| 593 } | |
| 594 } | |
| 595 | |
| 488 } // namespace net | 596 } // namespace net |
| OLD | NEW |