| OLD | NEW |
| 1 // Copyright (c) 2010 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/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" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 34 void SetFakeNow(const TimeTicks& now) { | 34 void SetFakeNow(const TimeTicks& now) { |
| 35 fake_now_ = now; | 35 fake_now_ = now; |
| 36 } | 36 } |
| 37 | 37 |
| 38 private: | 38 private: |
| 39 TimeTicks fake_now_; | 39 TimeTicks fake_now_; |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 class MockURLRequestThrottlerEntry : public net::URLRequestThrottlerEntry { | 42 class MockURLRequestThrottlerEntry : public net::URLRequestThrottlerEntry { |
| 43 public : | 43 public : |
| 44 MockURLRequestThrottlerEntry() : mock_backoff_entry_(&backoff_policy_) { | 44 explicit MockURLRequestThrottlerEntry( |
| 45 // Some tests become flaky if we have jitter. | 45 net::URLRequestThrottlerManager* manager) |
| 46 backoff_policy_.jitter_factor = 0.0; | 46 : net::URLRequestThrottlerEntry(manager), |
| 47 mock_backoff_entry_(&backoff_policy_) { |
| 48 InitPolicy(); |
| 47 } | 49 } |
| 48 MockURLRequestThrottlerEntry( | 50 MockURLRequestThrottlerEntry( |
| 51 net::URLRequestThrottlerManager* manager, |
| 49 const TimeTicks& exponential_backoff_release_time, | 52 const TimeTicks& exponential_backoff_release_time, |
| 50 const TimeTicks& sliding_window_release_time, | 53 const TimeTicks& sliding_window_release_time, |
| 51 const TimeTicks& fake_now) | 54 const TimeTicks& fake_now) |
| 52 : fake_time_now_(fake_now), | 55 : net::URLRequestThrottlerEntry(manager), |
| 56 fake_time_now_(fake_now), |
| 53 mock_backoff_entry_(&backoff_policy_) { | 57 mock_backoff_entry_(&backoff_policy_) { |
| 54 // Some tests become flaky if we have jitter. | 58 InitPolicy(); |
| 55 backoff_policy_.jitter_factor = 0.0; | |
| 56 | 59 |
| 57 mock_backoff_entry_.SetFakeNow(fake_now); | 60 mock_backoff_entry_.SetFakeNow(fake_now); |
| 58 set_exponential_backoff_release_time(exponential_backoff_release_time); | 61 set_exponential_backoff_release_time(exponential_backoff_release_time); |
| 59 set_sliding_window_release_time(sliding_window_release_time); | 62 set_sliding_window_release_time(sliding_window_release_time); |
| 60 } | 63 } |
| 61 virtual ~MockURLRequestThrottlerEntry() {} | 64 virtual ~MockURLRequestThrottlerEntry() {} |
| 62 | 65 |
| 66 void InitPolicy() { |
| 67 // Some tests become flaky if we have jitter. |
| 68 backoff_policy_.jitter_factor = 0.0; |
| 69 |
| 70 // This lets us avoid having to make multiple failures initially (this |
| 71 // logic is already tested in the BackoffEntry unit tests). |
| 72 backoff_policy_.num_errors_to_ignore = 0; |
| 73 } |
| 74 |
| 63 const net::BackoffEntry* GetBackoffEntry() const { | 75 const net::BackoffEntry* GetBackoffEntry() const { |
| 64 return &mock_backoff_entry_; | 76 return &mock_backoff_entry_; |
| 65 } | 77 } |
| 66 | 78 |
| 67 net::BackoffEntry* GetBackoffEntry() { | 79 net::BackoffEntry* GetBackoffEntry() { |
| 68 return &mock_backoff_entry_; | 80 return &mock_backoff_entry_; |
| 69 } | 81 } |
| 70 | 82 |
| 71 void ResetToBlank(const TimeTicks& time_now) { | 83 void ResetToBlank(const TimeTicks& time_now) { |
| 72 fake_time_now_ = time_now; | 84 fake_time_now_ = time_now; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 96 } | 108 } |
| 97 | 109 |
| 98 TimeTicks fake_time_now_; | 110 TimeTicks fake_time_now_; |
| 99 MockBackoffEntry mock_backoff_entry_; | 111 MockBackoffEntry mock_backoff_entry_; |
| 100 }; | 112 }; |
| 101 | 113 |
| 102 class MockURLRequestThrottlerHeaderAdapter | 114 class MockURLRequestThrottlerHeaderAdapter |
| 103 : public net::URLRequestThrottlerHeaderInterface { | 115 : public net::URLRequestThrottlerHeaderInterface { |
| 104 public: | 116 public: |
| 105 MockURLRequestThrottlerHeaderAdapter() | 117 MockURLRequestThrottlerHeaderAdapter() |
| 106 : fake_retry_value_("0.0"), | 118 : fake_retry_value_(""), |
| 119 fake_opt_out_value_(""), |
| 107 fake_response_code_(0) { | 120 fake_response_code_(0) { |
| 108 } | 121 } |
| 109 | 122 |
| 123 explicit MockURLRequestThrottlerHeaderAdapter(int response_code) |
| 124 : fake_retry_value_(""), |
| 125 fake_opt_out_value_(""), |
| 126 fake_response_code_(response_code) { |
| 127 } |
| 128 |
| 110 MockURLRequestThrottlerHeaderAdapter(const std::string& retry_value, | 129 MockURLRequestThrottlerHeaderAdapter(const std::string& retry_value, |
| 130 const std::string& opt_out_value, |
| 111 int response_code) | 131 int response_code) |
| 112 : fake_retry_value_(retry_value), | 132 : fake_retry_value_(retry_value), |
| 133 fake_opt_out_value_(opt_out_value), |
| 113 fake_response_code_(response_code) { | 134 fake_response_code_(response_code) { |
| 114 } | 135 } |
| 115 | 136 |
| 116 virtual ~MockURLRequestThrottlerHeaderAdapter() {} | 137 virtual ~MockURLRequestThrottlerHeaderAdapter() {} |
| 117 | 138 |
| 118 virtual std::string GetNormalizedValue(const std::string& key) const { | 139 virtual std::string GetNormalizedValue(const std::string& key) const { |
| 119 if (key == MockURLRequestThrottlerEntry::kRetryHeaderName) | 140 if (key == MockURLRequestThrottlerEntry::kRetryHeaderName && |
| 141 !fake_retry_value_.empty()) { |
| 120 return fake_retry_value_; | 142 return fake_retry_value_; |
| 143 } else if (key == |
| 144 MockURLRequestThrottlerEntry::kExponentialThrottlingHeader && |
| 145 !fake_opt_out_value_.empty()) { |
| 146 return fake_opt_out_value_; |
| 147 } |
| 121 return ""; | 148 return ""; |
| 122 } | 149 } |
| 123 | 150 |
| 124 virtual int GetResponseCode() const { return fake_response_code_; } | 151 virtual int GetResponseCode() const { return fake_response_code_; } |
| 125 | 152 |
| 126 std::string fake_retry_value_; | 153 std::string fake_retry_value_; |
| 154 std::string fake_opt_out_value_; |
| 127 int fake_response_code_; | 155 int fake_response_code_; |
| 128 }; | 156 }; |
| 129 | 157 |
| 130 class MockURLRequestThrottlerManager : public net::URLRequestThrottlerManager { | 158 class MockURLRequestThrottlerManager : public net::URLRequestThrottlerManager { |
| 131 public: | 159 public: |
| 132 MockURLRequestThrottlerManager() : create_entry_index_(0) {} | 160 MockURLRequestThrottlerManager() : create_entry_index_(0) {} |
| 133 | 161 |
| 134 // Method to process the URL using URLRequestThrottlerManager protected | 162 // Method to process the URL using URLRequestThrottlerManager protected |
| 135 // method. | 163 // method. |
| 136 std::string DoGetUrlIdFromUrl(const GURL& url) { return GetIdFromUrl(url); } | 164 std::string DoGetUrlIdFromUrl(const GURL& url) { return GetIdFromUrl(url); } |
| 137 | 165 |
| 138 // Method to use the garbage collecting method of URLRequestThrottlerManager. | 166 // Method to use the garbage collecting method of URLRequestThrottlerManager. |
| 139 void DoGarbageCollectEntries() { GarbageCollectEntries(); } | 167 void DoGarbageCollectEntries() { GarbageCollectEntries(); } |
| 140 | 168 |
| 141 // Returns the number of entries in the map. | 169 // Returns the number of entries in the map. |
| 142 int GetNumberOfEntries() const { return GetNumberOfEntriesForTests(); } | 170 int GetNumberOfEntries() const { return GetNumberOfEntriesForTests(); } |
| 143 | 171 |
| 144 void CreateEntry(bool is_outdated) { | 172 void CreateEntry(bool is_outdated) { |
| 145 TimeTicks time = TimeTicks::Now(); | 173 TimeTicks time = TimeTicks::Now(); |
| 146 if (is_outdated) { | 174 if (is_outdated) { |
| 147 time -= TimeDelta::FromMilliseconds( | 175 time -= TimeDelta::FromMilliseconds( |
| 148 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs + 1000); | 176 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs + 1000); |
| 149 } | 177 } |
| 150 std::string fake_url_string("http://www.fakeurl.com/"); | 178 std::string fake_url_string("http://www.fakeurl.com/"); |
| 151 fake_url_string.append(base::IntToString(create_entry_index_++)); | 179 fake_url_string.append(base::IntToString(create_entry_index_++)); |
| 152 GURL fake_url(fake_url_string); | 180 GURL fake_url(fake_url_string); |
| 153 OverrideEntryForTests( | 181 OverrideEntryForTests( |
| 154 fake_url, | 182 fake_url, |
| 155 new MockURLRequestThrottlerEntry(time, TimeTicks::Now(), | 183 new MockURLRequestThrottlerEntry(this, time, TimeTicks::Now(), |
| 156 TimeTicks::Now())); | 184 TimeTicks::Now())); |
| 157 } | 185 } |
| 158 | 186 |
| 159 private: | 187 private: |
| 160 int create_entry_index_; | 188 int create_entry_index_; |
| 161 }; | 189 }; |
| 162 | 190 |
| 163 struct TimeAndBool { | 191 struct TimeAndBool { |
| 164 TimeAndBool(const TimeTicks& time_value, bool expected, int line_num) { | 192 TimeAndBool(const TimeTicks& time_value, bool expected, int line_num) { |
| 165 time = time_value; | 193 time = time_value; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 183 std::string result; | 211 std::string result; |
| 184 int line; | 212 int line; |
| 185 }; | 213 }; |
| 186 | 214 |
| 187 } // namespace | 215 } // namespace |
| 188 | 216 |
| 189 class URLRequestThrottlerEntryTest : public testing::Test { | 217 class URLRequestThrottlerEntryTest : public testing::Test { |
| 190 protected: | 218 protected: |
| 191 virtual void SetUp(); | 219 virtual void SetUp(); |
| 192 TimeTicks now_; | 220 TimeTicks now_; |
| 221 MockURLRequestThrottlerManager manager_; // Dummy object, not used. |
| 193 scoped_refptr<MockURLRequestThrottlerEntry> entry_; | 222 scoped_refptr<MockURLRequestThrottlerEntry> entry_; |
| 194 }; | 223 }; |
| 195 | 224 |
| 196 void URLRequestThrottlerEntryTest::SetUp() { | 225 void URLRequestThrottlerEntryTest::SetUp() { |
| 197 now_ = TimeTicks::Now(); | 226 now_ = TimeTicks::Now(); |
| 198 entry_ = new MockURLRequestThrottlerEntry(); | 227 entry_ = new MockURLRequestThrottlerEntry(&manager_); |
| 199 entry_->ResetToBlank(now_); | 228 entry_->ResetToBlank(now_); |
| 200 } | 229 } |
| 201 | 230 |
| 202 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { | 231 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { |
| 203 return out << time.ToInternalValue(); | 232 return out << time.ToInternalValue(); |
| 204 } | 233 } |
| 205 | 234 |
| 206 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { | 235 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { |
| 207 entry_->set_exponential_backoff_release_time( | 236 entry_->set_exponential_backoff_release_time( |
| 208 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); | 237 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); |
| 209 EXPECT_TRUE(entry_->IsDuringExponentialBackoff()); | 238 EXPECT_TRUE(entry_->IsDuringExponentialBackoff()); |
| 210 } | 239 } |
| 211 | 240 |
| 212 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { | 241 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { |
| 213 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); | 242 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); |
| 214 EXPECT_FALSE(entry_->IsDuringExponentialBackoff()); | 243 EXPECT_FALSE(entry_->IsDuringExponentialBackoff()); |
| 215 entry_->set_exponential_backoff_release_time( | 244 entry_->set_exponential_backoff_release_time( |
| 216 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); | 245 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); |
| 217 EXPECT_FALSE(entry_->IsDuringExponentialBackoff()); | 246 EXPECT_FALSE(entry_->IsDuringExponentialBackoff()); |
| 218 } | 247 } |
| 219 | 248 |
| 220 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateRetryAfter) { | 249 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateRetryAfter) { |
| 221 // If the response we received has a retry-after field, | 250 // If the response we received has a retry-after field, |
| 222 // the request should be delayed. | 251 // the request should be delayed. |
| 223 MockURLRequestThrottlerHeaderAdapter header_w_delay_header("5.5", 200); | 252 MockURLRequestThrottlerHeaderAdapter header_w_delay_header("5.5", "", 200); |
| 224 entry_->UpdateWithResponse(&header_w_delay_header); | 253 entry_->UpdateWithResponse("", &header_w_delay_header); |
| 225 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 254 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 226 << "When the server put a positive value in retry-after we should " | 255 << "When the server put a positive value in retry-after we should " |
| 227 "increase release_time"; | 256 "increase release_time"; |
| 228 | 257 |
| 229 entry_->ResetToBlank(now_); | 258 entry_->ResetToBlank(now_); |
| 230 header_w_delay_header.fake_retry_value_ = "-5.5"; | 259 header_w_delay_header.fake_retry_value_ = "-5.5"; |
| 231 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 260 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 232 << "When given a negative value, it should not change the release_time"; | 261 << "When given a negative value, it should not change the release_time"; |
| 233 } | 262 } |
| 234 | 263 |
| 235 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { | 264 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { |
| 236 MockURLRequestThrottlerHeaderAdapter failure_response("0", 505); | 265 MockURLRequestThrottlerHeaderAdapter failure_response(505); |
| 237 entry_->UpdateWithResponse(&failure_response); | 266 entry_->UpdateWithResponse("", &failure_response); |
| 238 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 267 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 239 << "A failure should increase the release_time"; | 268 << "A failure should increase the release_time"; |
| 240 } | 269 } |
| 241 | 270 |
| 242 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { | 271 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { |
| 243 MockURLRequestThrottlerHeaderAdapter success_response("0", 200); | 272 MockURLRequestThrottlerHeaderAdapter success_response(200); |
| 244 entry_->UpdateWithResponse(&success_response); | 273 entry_->UpdateWithResponse("", &success_response); |
| 245 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 274 EXPECT_EQ(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 246 << "A success should not add any delay"; | 275 << "A success should not add any delay"; |
| 247 } | 276 } |
| 248 | 277 |
| 249 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccessThenFailure) { | 278 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccessThenFailure) { |
| 250 MockURLRequestThrottlerHeaderAdapter failure_response("0", 500); | 279 MockURLRequestThrottlerHeaderAdapter failure_response(500); |
| 251 MockURLRequestThrottlerHeaderAdapter success_response("0", 200); | 280 MockURLRequestThrottlerHeaderAdapter success_response(200); |
| 252 entry_->UpdateWithResponse(&success_response); | 281 entry_->UpdateWithResponse("", &success_response); |
| 253 entry_->UpdateWithResponse(&failure_response); | 282 entry_->UpdateWithResponse("", &failure_response); |
| 254 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 283 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 255 << "This scenario should add delay"; | 284 << "This scenario should add delay"; |
| 256 } | 285 } |
| 257 | 286 |
| 258 TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) { | 287 TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) { |
| 259 TimeDelta lifetime = TimeDelta::FromMilliseconds( | 288 TimeDelta lifetime = TimeDelta::FromMilliseconds( |
| 260 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs); | 289 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs); |
| 261 const TimeDelta kFiveMs = TimeDelta::FromMilliseconds(5); | 290 const TimeDelta kFiveMs = TimeDelta::FromMilliseconds(5); |
| 262 | 291 |
| 263 TimeAndBool test_values[] = { | 292 TimeAndBool test_values[] = { |
| 264 TimeAndBool(now_, false, __LINE__), | 293 TimeAndBool(now_, false, __LINE__), |
| 265 TimeAndBool(now_ - kFiveMs, false, __LINE__), | 294 TimeAndBool(now_ - kFiveMs, false, __LINE__), |
| 266 TimeAndBool(now_ + kFiveMs, false, __LINE__), | 295 TimeAndBool(now_ + kFiveMs, false, __LINE__), |
| 267 TimeAndBool(now_ - (lifetime - kFiveMs), false, __LINE__), | 296 TimeAndBool(now_ - (lifetime - kFiveMs), false, __LINE__), |
| 268 TimeAndBool(now_ - lifetime, true, __LINE__), | 297 TimeAndBool(now_ - lifetime, true, __LINE__), |
| 269 TimeAndBool(now_ - (lifetime + kFiveMs), true, __LINE__)}; | 298 TimeAndBool(now_ - (lifetime + kFiveMs), true, __LINE__)}; |
| 270 | 299 |
| 271 for (unsigned int i = 0; i < arraysize(test_values); ++i) { | 300 for (unsigned int i = 0; i < arraysize(test_values); ++i) { |
| 272 entry_->set_exponential_backoff_release_time(test_values[i].time); | 301 entry_->set_exponential_backoff_release_time(test_values[i].time); |
| 273 EXPECT_EQ(entry_->IsEntryOutdated(), test_values[i].result) << | 302 EXPECT_EQ(entry_->IsEntryOutdated(), test_values[i].result) << |
| 274 "Test case #" << i << " line " << test_values[i].line << " failed"; | 303 "Test case #" << i << " line " << test_values[i].line << " failed"; |
| 275 } | 304 } |
| 276 } | 305 } |
| 277 | 306 |
| 278 TEST_F(URLRequestThrottlerEntryTest, MaxAllowedBackoff) { | 307 TEST_F(URLRequestThrottlerEntryTest, MaxAllowedBackoff) { |
| 279 for (int i = 0; i < 30; ++i) { | 308 for (int i = 0; i < 30; ++i) { |
| 280 MockURLRequestThrottlerHeaderAdapter response_adapter("0.0", 505); | 309 MockURLRequestThrottlerHeaderAdapter response_adapter(505); |
| 281 entry_->UpdateWithResponse(&response_adapter); | 310 entry_->UpdateWithResponse("", &response_adapter); |
| 282 } | 311 } |
| 283 | 312 |
| 284 TimeDelta delay = entry_->GetExponentialBackoffReleaseTime() - now_; | 313 TimeDelta delay = entry_->GetExponentialBackoffReleaseTime() - now_; |
| 285 EXPECT_EQ(delay.InMilliseconds(), | 314 EXPECT_EQ(delay.InMilliseconds(), |
| 286 MockURLRequestThrottlerEntry::kDefaultMaximumBackoffMs); | 315 MockURLRequestThrottlerEntry::kDefaultMaximumBackoffMs); |
| 287 } | 316 } |
| 288 | 317 |
| 289 TEST_F(URLRequestThrottlerEntryTest, MalformedContent) { | 318 TEST_F(URLRequestThrottlerEntryTest, MalformedContent) { |
| 290 MockURLRequestThrottlerHeaderAdapter response_adapter("0.0", 505); | 319 MockURLRequestThrottlerHeaderAdapter response_adapter(505); |
| 291 for (int i = 0; i < 5; ++i) | 320 for (int i = 0; i < 5; ++i) |
| 292 entry_->UpdateWithResponse(&response_adapter); | 321 entry_->UpdateWithResponse("", &response_adapter); |
| 293 | 322 |
| 294 TimeTicks release_after_failures = entry_->GetExponentialBackoffReleaseTime(); | 323 TimeTicks release_after_failures = entry_->GetExponentialBackoffReleaseTime(); |
| 295 | 324 |
| 296 // Inform the entry that a response body was malformed, which is supposed to | 325 // Inform the entry that a response body was malformed, which is supposed to |
| 297 // increase the back-off time. | 326 // increase the back-off time. |
| 298 entry_->ReceivedContentWasMalformed(); | 327 entry_->ReceivedContentWasMalformed(); |
| 299 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), release_after_failures); | 328 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), release_after_failures); |
| 300 } | 329 } |
| 301 | 330 |
| 302 TEST_F(URLRequestThrottlerEntryTest, SlidingWindow) { | 331 TEST_F(URLRequestThrottlerEntryTest, SlidingWindow) { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 MockURLRequestThrottlerManager manager; | 411 MockURLRequestThrottlerManager manager; |
| 383 | 412 |
| 384 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 413 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
| 385 manager.RegisterRequestUrl(GURL("http://www.google.com/")); | 414 manager.RegisterRequestUrl(GURL("http://www.google.com/")); |
| 386 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0")); | 415 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0")); |
| 387 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1")); | 416 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1")); |
| 388 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure")); | 417 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure")); |
| 389 | 418 |
| 390 EXPECT_EQ(3, manager.GetNumberOfEntries()); | 419 EXPECT_EQ(3, manager.GetNumberOfEntries()); |
| 391 } | 420 } |
| 421 |
| 422 void ExpectEntryAllowsAllOnErrorIfOptedOut( |
| 423 net::URLRequestThrottlerEntryInterface* entry, |
| 424 bool opted_out) { |
| 425 EXPECT_FALSE(entry->IsDuringExponentialBackoff()); |
| 426 MockURLRequestThrottlerHeaderAdapter failure_adapter(503); |
| 427 for (int i = 0; i < 10; ++i) { |
| 428 // Host doesn't really matter in this scenario so we skip it. |
| 429 entry->UpdateWithResponse("", &failure_adapter); |
| 430 } |
| 431 EXPECT_NE(opted_out, entry->IsDuringExponentialBackoff()); |
| 432 |
| 433 if (opted_out) { |
| 434 // We're not mocking out GetTimeNow() in this scenario |
| 435 // so add a 100 ms buffer to avoid flakiness (that should always |
| 436 // give enough time to get from the TimeTicks::Now() call here |
| 437 // to the TimeTicks::Now() call in the entry class). |
| 438 EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), |
| 439 entry->GetExponentialBackoffReleaseTime()); |
| 440 } else { |
| 441 // As above, add 100 ms. |
| 442 EXPECT_LT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100), |
| 443 entry->GetExponentialBackoffReleaseTime()); |
| 444 } |
| 445 } |
| 446 |
| 447 TEST(URLRequestThrottlerManager, OptOutHeader) { |
| 448 MockURLRequestThrottlerManager manager; |
| 449 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry = |
| 450 manager.RegisterRequestUrl(GURL("http://www.google.com/yodude")); |
| 451 |
| 452 // Fake a response with the opt-out header. |
| 453 MockURLRequestThrottlerHeaderAdapter response_adapter( |
| 454 "", |
| 455 MockURLRequestThrottlerEntry::kExponentialThrottlingDisableValue, |
| 456 200); |
| 457 entry->UpdateWithResponse("www.google.com", &response_adapter); |
| 458 |
| 459 // Ensure that the same entry on error always allows everything. |
| 460 ExpectEntryAllowsAllOnErrorIfOptedOut(entry, true); |
| 461 |
| 462 // Ensure that a freshly created entry (for a different URL on an |
| 463 // already opted-out host) also gets "always allow" behavior. |
| 464 scoped_refptr<net::URLRequestThrottlerEntryInterface> other_entry = |
| 465 manager.RegisterRequestUrl(GURL("http://www.google.com/bingobob")); |
| 466 ExpectEntryAllowsAllOnErrorIfOptedOut(other_entry, true); |
| 467 |
| 468 // Fake a response with the opt-out header incorrectly specified. |
| 469 scoped_refptr<net::URLRequestThrottlerEntryInterface> no_opt_out_entry = |
| 470 manager.RegisterRequestUrl(GURL("http://www.nike.com/justdoit")); |
| 471 MockURLRequestThrottlerHeaderAdapter wrong_adapter("", "yesplease", 200); |
| 472 no_opt_out_entry->UpdateWithResponse("www.nike.com", &wrong_adapter); |
| 473 ExpectEntryAllowsAllOnErrorIfOptedOut(no_opt_out_entry, false); |
| 474 |
| 475 // A localhost entry should always be opted out. |
| 476 scoped_refptr<net::URLRequestThrottlerEntryInterface> localhost_entry = |
| 477 manager.RegisterRequestUrl(GURL("http://localhost/hello")); |
| 478 ExpectEntryAllowsAllOnErrorIfOptedOut(localhost_entry, true); |
| 479 } |
| OLD | NEW |