OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/url_request/url_request_throttler_manager.h" | 5 #include "net/url_request/url_request_throttler_manager.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/metrics/histogram_samples.h" | 8 #include "base/metrics/histogram_samples.h" |
9 #include "base/pickle.h" | 9 #include "base/pickle.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 | 88 |
89 void set_exponential_backoff_release_time( | 89 void set_exponential_backoff_release_time( |
90 const base::TimeTicks& release_time) { | 90 const base::TimeTicks& release_time) { |
91 GetBackoffEntry()->SetCustomReleaseTime(release_time); | 91 GetBackoffEntry()->SetCustomReleaseTime(release_time); |
92 } | 92 } |
93 | 93 |
94 base::TimeTicks sliding_window_release_time() const { | 94 base::TimeTicks sliding_window_release_time() const { |
95 return URLRequestThrottlerEntry::sliding_window_release_time(); | 95 return URLRequestThrottlerEntry::sliding_window_release_time(); |
96 } | 96 } |
97 | 97 |
98 void set_sliding_window_release_time( | 98 void set_sliding_window_release_time(const base::TimeTicks& release_time) { |
99 const base::TimeTicks& release_time) { | 99 URLRequestThrottlerEntry::set_sliding_window_release_time(release_time); |
100 URLRequestThrottlerEntry::set_sliding_window_release_time( | |
101 release_time); | |
102 } | 100 } |
103 | 101 |
104 TimeTicks fake_time_now_; | 102 TimeTicks fake_time_now_; |
105 MockBackoffEntry mock_backoff_entry_; | 103 MockBackoffEntry mock_backoff_entry_; |
106 | 104 |
107 protected: | 105 protected: |
108 virtual ~MockURLRequestThrottlerEntry() {} | 106 virtual ~MockURLRequestThrottlerEntry() {} |
109 }; | 107 }; |
110 | 108 |
111 class MockURLRequestThrottlerManager : public URLRequestThrottlerManager { | 109 class MockURLRequestThrottlerManager : public URLRequestThrottlerManager { |
(...skipping 12 matching lines...) Expand all Loading... |
124 | 122 |
125 void CreateEntry(bool is_outdated) { | 123 void CreateEntry(bool is_outdated) { |
126 TimeTicks time = TimeTicks::Now(); | 124 TimeTicks time = TimeTicks::Now(); |
127 if (is_outdated) { | 125 if (is_outdated) { |
128 time -= TimeDelta::FromMilliseconds( | 126 time -= TimeDelta::FromMilliseconds( |
129 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs + 1000); | 127 MockURLRequestThrottlerEntry::kDefaultEntryLifetimeMs + 1000); |
130 } | 128 } |
131 std::string fake_url_string("http://www.fakeurl.com/"); | 129 std::string fake_url_string("http://www.fakeurl.com/"); |
132 fake_url_string.append(base::IntToString(create_entry_index_++)); | 130 fake_url_string.append(base::IntToString(create_entry_index_++)); |
133 GURL fake_url(fake_url_string); | 131 GURL fake_url(fake_url_string); |
134 OverrideEntryForTests( | 132 OverrideEntryForTests(fake_url, |
135 fake_url, | 133 new MockURLRequestThrottlerEntry( |
136 new MockURLRequestThrottlerEntry(this, time, TimeTicks::Now(), | 134 this, time, TimeTicks::Now(), TimeTicks::Now())); |
137 TimeTicks::Now())); | |
138 } | 135 } |
139 | 136 |
140 private: | 137 private: |
141 int create_entry_index_; | 138 int create_entry_index_; |
142 }; | 139 }; |
143 | 140 |
144 struct TimeAndBool { | 141 struct TimeAndBool { |
145 TimeAndBool(const TimeTicks& time_value, bool expected, int line_num) { | 142 TimeAndBool(const TimeTicks& time_value, bool expected, int line_num) { |
146 time = time_value; | 143 time = time_value; |
147 result = expected; | 144 result = expected; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 entry_->ResetToBlank(now_); | 187 entry_->ResetToBlank(now_); |
191 } | 188 } |
192 | 189 |
193 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { | 190 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { |
194 return out << time.ToInternalValue(); | 191 return out << time.ToInternalValue(); |
195 } | 192 } |
196 | 193 |
197 TEST_F(URLRequestThrottlerEntryTest, CanThrottleRequest) { | 194 TEST_F(URLRequestThrottlerEntryTest, CanThrottleRequest) { |
198 TestNetworkDelegate d; | 195 TestNetworkDelegate d; |
199 context_.set_network_delegate(&d); | 196 context_.set_network_delegate(&d); |
200 entry_->set_exponential_backoff_release_time( | 197 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_ + |
201 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); | 198 TimeDelta::FromMilliseconds(1)); |
202 | 199 |
203 d.set_can_throttle_requests(false); | 200 d.set_can_throttle_requests(false); |
204 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 201 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
205 d.set_can_throttle_requests(true); | 202 d.set_can_throttle_requests(true); |
206 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); | 203 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); |
207 } | 204 } |
208 | 205 |
209 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { | 206 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { |
210 base::StatisticsDeltaReader statistics_delta_reader; | 207 base::StatisticsDeltaReader statistics_delta_reader; |
211 entry_->set_exponential_backoff_release_time( | 208 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_ + |
212 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); | 209 TimeDelta::FromMilliseconds(1)); |
213 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); | 210 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); |
214 | 211 |
215 // Also end-to-end test the load flags exceptions. | 212 // Also end-to-end test the load flags exceptions. |
216 request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE); | 213 request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE); |
217 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 214 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
218 | 215 |
219 scoped_ptr<base::HistogramSamples> samples( | 216 scoped_ptr<base::HistogramSamples> samples( |
220 statistics_delta_reader.GetHistogramSamplesSinceCreation( | 217 statistics_delta_reader.GetHistogramSamplesSinceCreation( |
221 kRequestThrottledHistogramName)); | 218 kRequestThrottledHistogramName)); |
222 ASSERT_EQ(1, samples->GetCount(0)); | 219 ASSERT_EQ(1, samples->GetCount(0)); |
223 ASSERT_EQ(1, samples->GetCount(1)); | 220 ASSERT_EQ(1, samples->GetCount(1)); |
224 } | 221 } |
225 | 222 |
226 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { | 223 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { |
227 base::StatisticsDeltaReader statistics_delta_reader; | 224 base::StatisticsDeltaReader statistics_delta_reader; |
228 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); | 225 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); |
229 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 226 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
230 entry_->set_exponential_backoff_release_time( | 227 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_ - |
231 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); | 228 TimeDelta::FromMilliseconds(1)); |
232 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 229 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
233 | 230 |
234 scoped_ptr<base::HistogramSamples> samples( | 231 scoped_ptr<base::HistogramSamples> samples( |
235 statistics_delta_reader.GetHistogramSamplesSinceCreation( | 232 statistics_delta_reader.GetHistogramSamplesSinceCreation( |
236 kRequestThrottledHistogramName)); | 233 kRequestThrottledHistogramName)); |
237 ASSERT_EQ(2, samples->GetCount(0)); | 234 ASSERT_EQ(2, samples->GetCount(0)); |
238 ASSERT_EQ(0, samples->GetCount(1)); | 235 ASSERT_EQ(0, samples->GetCount(1)); |
239 } | 236 } |
240 | 237 |
241 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { | 238 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { |
(...skipping 28 matching lines...) Expand all Loading... |
270 TimeAndBool test_values[] = { | 267 TimeAndBool test_values[] = { |
271 TimeAndBool(now_, false, __LINE__), | 268 TimeAndBool(now_, false, __LINE__), |
272 TimeAndBool(now_ - kFiveMs, false, __LINE__), | 269 TimeAndBool(now_ - kFiveMs, false, __LINE__), |
273 TimeAndBool(now_ + kFiveMs, false, __LINE__), | 270 TimeAndBool(now_ + kFiveMs, false, __LINE__), |
274 TimeAndBool(now_ - (lifetime - kFiveMs), false, __LINE__), | 271 TimeAndBool(now_ - (lifetime - kFiveMs), false, __LINE__), |
275 TimeAndBool(now_ - lifetime, true, __LINE__), | 272 TimeAndBool(now_ - lifetime, true, __LINE__), |
276 TimeAndBool(now_ - (lifetime + kFiveMs), true, __LINE__)}; | 273 TimeAndBool(now_ - (lifetime + kFiveMs), true, __LINE__)}; |
277 | 274 |
278 for (unsigned int i = 0; i < arraysize(test_values); ++i) { | 275 for (unsigned int i = 0; i < arraysize(test_values); ++i) { |
279 entry_->set_exponential_backoff_release_time(test_values[i].time); | 276 entry_->set_exponential_backoff_release_time(test_values[i].time); |
280 EXPECT_EQ(entry_->IsEntryOutdated(), test_values[i].result) << | 277 EXPECT_EQ(entry_->IsEntryOutdated(), test_values[i].result) |
281 "Test case #" << i << " line " << test_values[i].line << " failed"; | 278 << "Test case #" << i << " line " << test_values[i].line << " failed"; |
282 } | 279 } |
283 } | 280 } |
284 | 281 |
285 TEST_F(URLRequestThrottlerEntryTest, MaxAllowedBackoff) { | 282 TEST_F(URLRequestThrottlerEntryTest, MaxAllowedBackoff) { |
286 for (int i = 0; i < 30; ++i) { | 283 for (int i = 0; i < 30; ++i) { |
287 MockURLRequestThrottlerHeaderAdapter response_adapter(503); | 284 MockURLRequestThrottlerHeaderAdapter response_adapter(503); |
288 entry_->UpdateWithResponse(std::string(), &response_adapter); | 285 entry_->UpdateWithResponse(std::string(), &response_adapter); |
289 } | 286 } |
290 | 287 |
291 TimeDelta delay = entry_->GetExponentialBackoffReleaseTime() - now_; | 288 TimeDelta delay = entry_->GetExponentialBackoffReleaseTime() - now_; |
(...skipping 14 matching lines...) Expand all Loading... |
306 // is what happens in practice (if a body is received, then a non-500 | 303 // is what happens in practice (if a body is received, then a non-500 |
307 // response must also have been received). | 304 // response must also have been received). |
308 entry_->ReceivedContentWasMalformed(200); | 305 entry_->ReceivedContentWasMalformed(200); |
309 MockURLRequestThrottlerHeaderAdapter success_adapter(200); | 306 MockURLRequestThrottlerHeaderAdapter success_adapter(200); |
310 entry_->UpdateWithResponse(std::string(), &success_adapter); | 307 entry_->UpdateWithResponse(std::string(), &success_adapter); |
311 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), release_after_failures); | 308 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), release_after_failures); |
312 } | 309 } |
313 | 310 |
314 TEST_F(URLRequestThrottlerEntryTest, SlidingWindow) { | 311 TEST_F(URLRequestThrottlerEntryTest, SlidingWindow) { |
315 int max_send = URLRequestThrottlerEntry::kDefaultMaxSendThreshold; | 312 int max_send = URLRequestThrottlerEntry::kDefaultMaxSendThreshold; |
316 int sliding_window = | 313 int sliding_window = URLRequestThrottlerEntry::kDefaultSlidingWindowPeriodMs; |
317 URLRequestThrottlerEntry::kDefaultSlidingWindowPeriodMs; | |
318 | 314 |
319 TimeTicks time_1 = entry_->fake_time_now_ + | 315 TimeTicks time_1 = |
320 TimeDelta::FromMilliseconds(sliding_window / 3); | 316 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(sliding_window / 3); |
321 TimeTicks time_2 = entry_->fake_time_now_ + | 317 TimeTicks time_2 = entry_->fake_time_now_ + |
322 TimeDelta::FromMilliseconds(2 * sliding_window / 3); | 318 TimeDelta::FromMilliseconds(2 * sliding_window / 3); |
323 TimeTicks time_3 = entry_->fake_time_now_ + | 319 TimeTicks time_3 = |
324 TimeDelta::FromMilliseconds(sliding_window); | 320 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(sliding_window); |
325 TimeTicks time_4 = entry_->fake_time_now_ + | 321 TimeTicks time_4 = |
| 322 entry_->fake_time_now_ + |
326 TimeDelta::FromMilliseconds(sliding_window + 2 * sliding_window / 3); | 323 TimeDelta::FromMilliseconds(sliding_window + 2 * sliding_window / 3); |
327 | 324 |
328 entry_->set_exponential_backoff_release_time(time_1); | 325 entry_->set_exponential_backoff_release_time(time_1); |
329 | 326 |
330 for (int i = 0; i < max_send / 2; ++i) { | 327 for (int i = 0; i < max_send / 2; ++i) { |
331 EXPECT_EQ(2 * sliding_window / 3, | 328 EXPECT_EQ(2 * sliding_window / 3, |
332 entry_->ReserveSendingTimeForNextRequest(time_2)); | 329 entry_->ReserveSendingTimeForNextRequest(time_2)); |
333 } | 330 } |
334 EXPECT_EQ(time_2, entry_->sliding_window_release_time()); | 331 EXPECT_EQ(time_2, entry_->sliding_window_release_time()); |
335 | 332 |
(...skipping 11 matching lines...) Expand all Loading... |
347 LOAD_MAYBE_USER_GESTURE)); | 344 LOAD_MAYBE_USER_GESTURE)); |
348 ASSERT_FALSE(MockURLRequestThrottlerEntry::ExplicitUserRequest( | 345 ASSERT_FALSE(MockURLRequestThrottlerEntry::ExplicitUserRequest( |
349 ~LOAD_MAYBE_USER_GESTURE)); | 346 ~LOAD_MAYBE_USER_GESTURE)); |
350 } | 347 } |
351 | 348 |
352 class URLRequestThrottlerManagerTest : public testing::Test { | 349 class URLRequestThrottlerManagerTest : public testing::Test { |
353 protected: | 350 protected: |
354 URLRequestThrottlerManagerTest() | 351 URLRequestThrottlerManagerTest() |
355 : request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {} | 352 : request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {} |
356 | 353 |
357 virtual void SetUp() { | 354 virtual void SetUp() { request_.SetLoadFlags(0); } |
358 request_.SetLoadFlags(0); | |
359 } | |
360 | 355 |
361 // context_ must be declared before request_. | 356 // context_ must be declared before request_. |
362 TestURLRequestContext context_; | 357 TestURLRequestContext context_; |
363 TestURLRequest request_; | 358 TestURLRequest request_; |
364 }; | 359 }; |
365 | 360 |
366 TEST_F(URLRequestThrottlerManagerTest, IsUrlStandardised) { | 361 TEST_F(URLRequestThrottlerManagerTest, IsUrlStandardised) { |
367 MockURLRequestThrottlerManager manager; | 362 MockURLRequestThrottlerManager manager; |
368 GurlAndString test_values[] = { | 363 GurlAndString test_values[] = { |
369 GurlAndString(GURL("http://www.example.com"), | 364 GurlAndString(GURL("http://www.example.com"), |
(...skipping 16 matching lines...) Expand all Loading... |
386 __LINE__), | 381 __LINE__), |
387 GurlAndString(GURL("http://www.example.com/index.php#superEntry"), | 382 GurlAndString(GURL("http://www.example.com/index.php#superEntry"), |
388 std::string("http://www.example.com/index.php"), | 383 std::string("http://www.example.com/index.php"), |
389 __LINE__), | 384 __LINE__), |
390 GurlAndString(GURL("http://www.example.com:1234/"), | 385 GurlAndString(GURL("http://www.example.com:1234/"), |
391 std::string("http://www.example.com:1234/"), | 386 std::string("http://www.example.com:1234/"), |
392 __LINE__)}; | 387 __LINE__)}; |
393 | 388 |
394 for (unsigned int i = 0; i < arraysize(test_values); ++i) { | 389 for (unsigned int i = 0; i < arraysize(test_values); ++i) { |
395 std::string temp = manager.DoGetUrlIdFromUrl(test_values[i].url); | 390 std::string temp = manager.DoGetUrlIdFromUrl(test_values[i].url); |
396 EXPECT_EQ(temp, test_values[i].result) << | 391 EXPECT_EQ(temp, test_values[i].result) << "Test case #" << i << " line " |
397 "Test case #" << i << " line " << test_values[i].line << " failed"; | 392 << test_values[i].line << " failed"; |
398 } | 393 } |
399 } | 394 } |
400 | 395 |
401 TEST_F(URLRequestThrottlerManagerTest, AreEntriesBeingCollected) { | 396 TEST_F(URLRequestThrottlerManagerTest, AreEntriesBeingCollected) { |
402 MockURLRequestThrottlerManager manager; | 397 MockURLRequestThrottlerManager manager; |
403 | 398 |
404 manager.CreateEntry(true); // true = Entry is outdated. | 399 manager.CreateEntry(true); // true = Entry is outdated. |
405 manager.CreateEntry(true); | 400 manager.CreateEntry(true); |
406 manager.CreateEntry(true); | 401 manager.CreateEntry(true); |
407 manager.DoGarbageCollectEntries(); | 402 manager.DoGarbageCollectEntries(); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 FAIL(); | 512 FAIL(); |
518 } | 513 } |
519 | 514 |
520 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = | 515 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = |
521 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 516 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
522 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); | 517 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); |
523 } | 518 } |
524 } | 519 } |
525 | 520 |
526 } // namespace net | 521 } // namespace net |
OLD | NEW |