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