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